Embedded Multicore Building Blocks V1.0.0
exceptions.h
1 /*
2  * Copyright (c) 2014-2017, Siemens AG. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef EMBB_BASE_EXCEPTIONS_H_
28 #define EMBB_BASE_EXCEPTIONS_H_
29 
30 #ifdef EMBB_PLATFORM_COMPILER_MSVC
31 #pragma warning(push)
32 // Disable warning that exceptions are disabled but try/catch is used.
33 #pragma warning(disable : 4530)
34 #endif // EMBB_PLATFORM_COMPILER_MSVC
35 
36 #include <string>
37 #include <exception>
38 
39 #ifdef EMBB_PLATFORM_COMPILER_MSVC
40 #pragma warning(pop)
41 #endif
42 
43 #include <embb/base/internal/cmake_config.h>
44 #include <embb/base/c/errors.h>
45 
75 #ifdef EMBB_USE_EXCEPTIONS
76 #define EMBB_TRY try
77 #define EMBB_THROW(Type, Message) throw Type(Message);
78 #define EMBB_CATCH(Statement) catch(Statement)
79 #else /* EMBB_USE_EXCEPTIONS */
80 #include <stdio.h>
81 #include <stdlib.h>
82 #include <embb/base/c/internal/unused.h>
83 #define EMBB_TRY
84 
87 #define EMBB_CATCH_VAR_CAT2(X, Y) X##Y
88 
91 #define EMBB_CATCH_VAR_CAT1(X, Y) EMBB_CATCH_VAR_CAT2(X, Y)
92 
97 #define EMBB_CATCH_VAR EMBB_CATCH_VAR_CAT1(embb_catch_var_, __LINE__)
98 
107 #define EMBB_CATCH(Statement) \
108  int EMBB_CATCH_VAR = false; \
109  EMBB_UNUSED(EMBB_CATCH_VAR); \
110  if (EMBB_CATCH_VAR)
111 
115 #define EMBB_THROW(Type, Message) \
116  { \
117  Type e(Message); \
118  fprintf(stderr, \
119  "Exit program due to (not thrown) " #Type ": %s\n", e.what()); \
120  exit(e.Code()); \
121  }
122 #endif /* else EMBB_USE_EXCEPTIONS */
123 
124 namespace embb {
125 namespace base {
126 
132 class Exception : public std::exception {
133  public:
137  explicit Exception(
138  const char* message
140  ) : message_(message) {}
141 
145  virtual ~Exception() throw() {}
146 
151  const Exception& e
153  ) : message_(e.message_) {}
154 
161  const Exception& e
163  ) {
164  message_ = e.message_;
165  return *this;
166  }
167 
173  virtual const char* What() const throw() {
174  return message_;
175  }
176 
182  virtual int Code() const = 0;
183 
184  private:
188  const char* message_;
189 };
190 
196 class NoMemoryException : public Exception {
197  public:
202  const char* message
204  ) : Exception(message) {}
205  virtual int Code() const { return EMBB_NOMEM; }
206 };
207 
214  public:
219  const char* message
221  ) : Exception(message) {}
222  virtual int Code() const { return EMBB_BUSY; }
223 };
224 
231  public:
236  const char* message
238  ) : Exception(message) {}
239  virtual int Code() const { return EMBB_UNDERFLOW; }
240 };
241 
247 class OverflowException : public Exception {
248  public:
253  const char* message
255  ) : Exception(message) {}
256  virtual int Code() const { return EMBB_OVERFLOW; }
257 };
258 
264 class ErrorException : public Exception {
265  public:
269  explicit ErrorException(
270  const char* message
272  ) : Exception(message) {}
273  virtual int Code() const { return EMBB_ERROR; }
274 };
275 
276 } // namespace base
277 } // namespace embb
278 
279 #endif // EMBB_BASE_EXCEPTIONS_H_
Definition: lock_free_mpmc_queue.h:40
Indicates lack of memory necessary to allocate a resource.
Definition: exceptions.h:196
virtual int Code() const =0
Returns an integer code representing the exception.
ResourceBusyException(const char *message)
Constructs an exception with the specified message.
Definition: exceptions.h:218
Exception(const Exception &e)
Constructs an exception by copying from an existing one.
Definition: exceptions.h:150
Indicates a general error.
Definition: exceptions.h:264
virtual int Code() const
Returns an integer code representing the exception.
Definition: exceptions.h:256
virtual const char * What() const
Returns the error message.
Definition: exceptions.h:173
virtual ~Exception()
Destructs the exception.
Definition: exceptions.h:145
NoMemoryException(const char *message)
Constructs an exception with the specified message.
Definition: exceptions.h:201
Error, not enough memory.
Definition: errors.h:48
virtual int Code() const
Returns an integer code representing the exception.
Definition: exceptions.h:273
Abstract base class for exceptions.
Definition: exceptions.h:132
virtual int Code() const
Returns an integer code representing the exception.
Definition: exceptions.h:205
UnderflowException(const char *message)
Constructs an exception with the specified message.
Definition: exceptions.h:235
Indicates a numeric underflow.
Definition: exceptions.h:230
virtual int Code() const
Returns an integer code representing the exception.
Definition: exceptions.h:239
Resource busy.
Definition: errors.h:50
Indicates business (unavailability) of a required resource.
Definition: exceptions.h:213
ErrorException(const char *message)
Constructs an exception with the specified message.
Definition: exceptions.h:269
Error, numeric underflow.
Definition: errors.h:52
Exception(const char *message)
Constructs an exception with a custom message.
Definition: exceptions.h:137
Indicates a numeric overflow.
Definition: exceptions.h:247
virtual int Code() const
Returns an integer code representing the exception.
Definition: exceptions.h:222
OverflowException(const char *message)
Constructs an exception with the specified message.
Definition: exceptions.h:252
Error, not further specified.
Definition: errors.h:53
Exception & operator=(const Exception &e)
Assigns an existing exception.
Definition: exceptions.h:160
Error, numeric overflow.
Definition: errors.h:51