VSM C++ SDK
Vehicle Specific Modules SDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
exception.h
Go to the documentation of this file.
1 // Copyright (c) 2018, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
11 #ifndef _UGCS_VSM_EXCEPTION_H_
12 #define _UGCS_VSM_EXCEPTION_H_
13 
14 #include <ugcs/vsm/defs.h>
15 #include <stdarg.h>
16 #include <string>
17 
18 namespace ugcs {
19 namespace vsm {
20 
22 class Exception: public std::exception {
23 public:
31  // Workaround for http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60336
32  char dummy;
33  };
34 
39  // Workaround for http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60336
40  char dummy;
41  };
42 
44  Exception(): msg("<no message>") {}
45 
50  Exception(Va_args_overload, const char *msg, ...) __FORMAT(printf, 3, 4);
51 
57  Exception(Va_list_overload, const char *msg, va_list args) __FORMAT(printf, 3, 0);
58 
59  virtual
60  ~Exception() noexcept
61  {}
62 
64  virtual const char *
65  what() const noexcept override
66  {
67  return msg.c_str();
68  }
69 
70 protected:
72  std::string msg;
73 
75  void
76  Create_msg(const char *msg, va_list args);
77 };
78 
85 template <typename Dummy = void, typename TParam = int>
86 class Param_exception: public Exception {
87 public:
89  TParam param;
90 
91  // @{
95  Param_exception(Va_args_overload, const char *msg, ...) __FORMAT(printf, 3, 4)
96  {
97  va_list args;
98  va_start(args, msg);
99  Create_msg(msg, args);
100  va_end(args);
101  }
102 
103  Param_exception(Va_list_overload, const char *msg, va_list args) __FORMAT(printf, 3, 0)
104  {
105  Create_msg(msg, args);
106  }
107 
108  Param_exception(Va_args_overload, const TParam &param, const char *msg, ...)
109  __FORMAT(printf, 4, 5):
110  param(param)
111  {
112  va_list args;
113  va_start(args, msg);
114  Create_msg(msg, args);
115  va_end(args);
116  }
117 
118  Param_exception(Va_list_overload, const TParam &param, const char *msg, va_list args)
119  __FORMAT(printf, 4, 0):
120  param(param)
121  {
122  Create_msg(msg, args);
123  }
124  // @}
125 };
126 
132 template <class Base_exception, typename Dummy = void>
133 class Derived_exception: public Base_exception {
134 public:
136  template <typename... Args>
137  Derived_exception(Args &&... args):
138  Base_exception(std::forward<Args>(args)...)
139  {}
140 };
141 
142 #ifdef DEBUG
143 
149 #define VSM_EXCEPTION(__exc_class, __msg, ...) \
150  throw __exc_class(__exc_class::Va_args_overload(), "[%s:%d] " __msg, __FILE__, __LINE__, ## __VA_ARGS__)
151 
158 #define VSM_PARAM_EXCEPTION(__exc_class, __param, __msg, ...) \
159  throw __exc_class(__exc_class::Va_args_overload(), __param, "[%s:%d] " __msg, __FILE__, __LINE__, ## __VA_ARGS__)
160 
161 #else /* DEBUG */
162 
168 #define VSM_EXCEPTION(__exc_class, __msg, ...) \
169  /* Dummy format argument is added to avoid GCC 4.8.1 bug related to printf attribute. */ \
170  throw __exc_class(__exc_class::Va_args_overload(), "%s" __msg, "", ## __VA_ARGS__)
171 
178 #define VSM_PARAM_EXCEPTION(__exc_class, __param, __msg, ...) \
179  /* Dummy format argument is added to avoid GCC 4.8.1 bug related to printf attribute. */ \
180  throw __exc_class(__exc_class::Va_args_overload(), __param, "%s" __msg, "", ## __VA_ARGS__)
181 
182 #endif /* DEBUG */
183 
190 #define VSM_SYS_EXCEPTION(__msg, ...) do { \
191  std::string sys_msg = ugcs::vsm::Log::Get_system_error(); \
192  VSM_EXCEPTION(ugcs::vsm::System_exception, __msg ": %s", ## __VA_ARGS__, sys_msg.c_str()); \
193 } while (false)
194 
200 #define VSM_DEFINE_EXCEPTION(__exc_class, ...) \
201  struct __exc_class ## _dummy_struct {}; \
202  typedef ugcs::vsm::Param_exception<__exc_class ## _dummy_struct, ## __VA_ARGS__> __exc_class
203 
208 #define VSM_DEFINE_DERIVED_EXCEPTION(__base_class, __exc_class) \
209  \
210  struct __exc_class ## _dummy_struct {}; \
211  \
212  typedef ugcs::vsm::Derived_exception<__base_class, __exc_class ## _dummy_struct> __exc_class
213 
214 
215 /* Some common exceptions below. */
216 
218 VSM_DEFINE_EXCEPTION(Debug_assert_exception);
220 VSM_DEFINE_EXCEPTION(Nullptr_exception);
222 VSM_DEFINE_EXCEPTION(Invalid_param_exception);
224 VSM_DEFINE_EXCEPTION(Invalid_op_exception);
226 VSM_DEFINE_EXCEPTION(Internal_error_exception);
228 VSM_DEFINE_EXCEPTION(System_exception);
229 
230 } /* namespace vsm */
231 } /* namespace ugcs */
232 
233 #endif /* _UGCS_VSM_EXCEPTION_H_ */
Exception class with one parameter.
Definition: exception.h:86
Derived_exception(Args &&...args)
Forwarding constructor.
Definition: exception.h:137
Exception()
Default constructor should not be used often.
Definition: exception.h:44
void Create_msg(const char *msg, va_list args)
Create message string (data member "msg") from format parameters.
Common preprocessor definitions.
#define __FORMAT(type, fmt_idx, arg_idx)
Specify that a function has format arguments (like printf or scanf).
Definition: defs.h:24
Param_exception(Va_args_overload, const char *msg,...)
Different constructors for optionally taking the parameter value and formatted message.
Definition: exception.h:95
TParam param
User-defined parameter.
Definition: exception.h:89
std::string msg
Exception message.
Definition: exception.h:72
virtual const char * what() const noexceptoverride
Get readable exception description.
Definition: exception.h:65
VSM_DEFINE_EXCEPTION(Debug_assert_exception)
Exception to throw when debugging assertion fires.
Dummy structure to explicitly indicate the constructor overload for variable arguments (i...
Definition: exception.h:38
Dummy structure to explicitly indicate the constructor overload for va_list type argument.
Definition: exception.h:30
Helper class for defining derived exceptions.
Definition: exception.h:133
Base class for all VSM exceptions.
Definition: exception.h:22