VSM C++ SDK
Vehicle Specific Modules SDK
exception.h
Go to the documentation of this file.
1 // Copyright (c) 2014, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
11 #ifndef EXCEPTION_H_
12 #define EXCEPTION_H_
13 
14 #include <string>
15 #include <stdarg.h>
16 
17 #include <ugcs/vsm/defs.h>
18 
19 namespace ugcs {
20 namespace vsm {
21 
23 class Exception: public std::exception {
24 public:
25 
34  char dummy;
35  };
36 
42  char dummy;
43  };
44 
46  Exception(): msg("<no message>") {};
47 
52  Exception(Va_args_overload, const char *msg, ...) __FORMAT(printf, 3, 4);
53 
59  Exception(Va_list_overload, const char *msg, va_list args) __FORMAT(printf, 3, 0);
60 
61  virtual
62  ~Exception() noexcept
63  {}
64 
66  virtual const char *
67  what() const noexcept override
68  {
69  return msg.c_str();
70  }
71 
72 protected:
74  std::string msg;
75 
77  void
78  Create_msg(const char *msg, va_list args);
79 };
80 
87 template <typename Dummy = void, typename TParam = int>
88 class Param_exception: public Exception {
89 public:
91  TParam param;
92 
93  // @{
97  Param_exception(Va_args_overload, const char *msg, ...) __FORMAT(printf, 3, 4)
98  {
99  va_list args;
100  va_start(args, msg);
101  Create_msg(msg, args);
102  va_end(args);
103  }
104 
105  Param_exception(Va_list_overload, const char *msg, va_list args) __FORMAT(printf, 3, 0)
106  {
107  Create_msg(msg, args);
108  }
109 
110  Param_exception(Va_args_overload, const TParam &param, const char *msg, ...)
111  __FORMAT(printf, 4, 5):
112  param(param)
113  {
114  va_list args;
115  va_start(args, msg);
116  Create_msg(msg, args);
117  va_end(args);
118  }
119 
120  Param_exception(Va_list_overload, const TParam &param, const char *msg, va_list args)
121  __FORMAT(printf, 4, 0):
122  param(param)
123  {
124  Create_msg(msg, args);
125  }
126  // @}
127 };
128 
134 template <class Base_exception, typename Dummy = void>
135 class Derived_exception: public Base_exception {
136 public:
138  template <typename... Args>
139  Derived_exception(Args &&... args):
140  Base_exception(std::forward<Args>(args)...)
141  {}
142 };
143 
144 #ifdef DEBUG
145 
151 #define VSM_EXCEPTION(__exc_class, __msg, ...) \
152  throw __exc_class(__exc_class::Va_args_overload(), "[%s:%d] " __msg, __FILE__, __LINE__, ## __VA_ARGS__)
153 
160 #define VSM_PARAM_EXCEPTION(__exc_class, __param, __msg, ...) \
161  throw __exc_class(__exc_class::Va_args_overload(), __param, "[%s:%d] " __msg, __FILE__, __LINE__, ## __VA_ARGS__)
162 
163 #else /* DEBUG */
164 
170 #define VSM_EXCEPTION(__exc_class, __msg, ...) \
171  /* Dummy format argument is added to avoid GCC 4.8.1 bug related to printf attribute. */ \
172  throw __exc_class(__exc_class::Va_args_overload(), "%s" __msg, "", ## __VA_ARGS__)
173 
180 #define VSM_PARAM_EXCEPTION(__exc_class, __param, __msg, ...) \
181  /* Dummy format argument is added to avoid GCC 4.8.1 bug related to printf attribute. */ \
182  throw __exc_class(__exc_class::Va_args_overload(), __param, "%s" __msg, "", ## __VA_ARGS__)
183 
184 #endif /* DEBUG */
185 
192 #define VSM_SYS_EXCEPTION(__msg, ...) do { \
193  std::string sys_msg = ugcs::vsm::Log::Get_system_error(); \
194  VSM_EXCEPTION(ugcs::vsm::System_exception, __msg ": %s", ## __VA_ARGS__, sys_msg.c_str()); \
195 } while (false)
196 
202 #define VSM_DEFINE_EXCEPTION(__exc_class, ...) \
203  struct __exc_class ## _dummy_struct {}; \
204  typedef ugcs::vsm::Param_exception<__exc_class ## _dummy_struct, ## __VA_ARGS__> __exc_class
205 
210 #define VSM_DEFINE_DERIVED_EXCEPTION(__base_class, __exc_class) \
211  \
212  struct __exc_class ## _dummy_struct {}; \
213  \
214  typedef ugcs::vsm::Derived_exception<__base_class, __exc_class ## _dummy_struct> __exc_class
215 
216 
217 /* Some common exceptions below. */
218 
231 
232 } /* namespace vsm */
233 } /* namespace ugcs */
234 
235 #endif /* EXCEPTION_H_ */
UGCS root namespace.
Definition: android-linux/ugcs/vsm/platform_sockets.h:27
Exception class with one parameter.
Definition: exception.h:88
Derived_exception(Args &&...args)
Forwarding constructor.
Definition: exception.h:139
Exception()
Default constructor should not be used often.
Definition: exception.h:46
void Create_msg(const char *msg, va_list args)
Create message string (data member "msg") from format parameters.
STL namespace.
Common preprocessor definitions.
char dummy
Workaround for http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60336.
Definition: exception.h:42
char dummy
Workaround for http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60336.
Definition: exception.h:34
#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:97
TParam param
User-defined parameter.
Definition: exception.h:91
#define VSM_DEFINE_EXCEPTION(__exc_class,...)
Define custom exception type.
Definition: exception.h:202
std::string msg
Exception message.
Definition: exception.h:74
virtual const char * what() const noexceptoverride
Get readable exception description.
Definition: exception.h:67
Dummy structure to explicitly indicate the constructor overload for variable arguments (i...
Definition: exception.h:40
Dummy structure to explicitly indicate the constructor overload for va_list type argument.
Definition: exception.h:32
Helper class for defining derived exceptions.
Definition: exception.h:135
Base class for all VSM exceptions.
Definition: exception.h:23