coral
A C++ library for distributed co-simulation
error.hpp
Go to the documentation of this file.
1 
10 #ifndef CORAL_ERROR_HPP
11 #define CORAL_ERROR_HPP
12 
13 #include <sstream>
14 #include <stdexcept>
15 #include <string>
16 #include <system_error>
17 #include "coral/config.h"
18 
19 
20 namespace coral
21 {
22 
24 namespace error
25 {
26 
27 
30 {
31 public:
32  explicit ProtocolViolationException(const std::string& whatArg)
33  : std::runtime_error(whatArg) { }
34 
35  explicit ProtocolViolationException(const char* whatArg)
36  : std::runtime_error(whatArg) { }
37 };
38 
39 
42 {
43 public:
44  explicit ProtocolNotSupported(const std::string& whatArg)
45  : std::runtime_error(whatArg) { }
46 
47  explicit ProtocolNotSupported(const char* whatArg)
48  : std::runtime_error(whatArg) { }
49 };
50 
51 
103 #define CORAL_INPUT_CHECK(test) \
104  do { \
105  if (!(test)) { \
106  coral::error::detail::Throw<std::invalid_argument> \
107  (__FUNCTION__, -1, "Input requirement not satisfied", #test); \
108  } \
109  } while(false)
110 
111 
123 {
124 public:
125  explicit PreconditionViolation(const std::string& whatArg)
126  : std::logic_error(whatArg) { }
127 };
128 
129 
168 #define CORAL_PRECONDITION_CHECK(test) \
169  do { \
170  if (!(test)) { \
171  coral::error::detail::Throw<coral::error::PreconditionViolation> \
172  (__FUNCTION__, -1, "Precondition not satisfied", #test); \
173  } \
174  } while(false)
175 
176 
177 namespace detail
178 {
179  // Internal helper function.
180  // This function is only designed for use by the macros in this header,
181  // and it is subject to change without warning at any time.
182  template<class ExceptionT>
183  inline void Throw(
184  const char* location, int lineNo, const char* msg, const char* detail)
185  {
187  s << location;
188  if (lineNo >= 0) s << '(' << lineNo << ')';
189  s << ": " << msg;
190  if (detail) s << ": " << detail;
191  throw ExceptionT(s.str());
192  }
193 }
194 
195 
207 std::string ErrnoMessage(const std::string& msg, int errnoValue) CORAL_NOEXCEPT;
208 
209 
216 enum class generic_error
217 {
219  aborted = 1,
220 
222  canceled,
223 
226 
228  fatal,
229 };
230 
232 const std::error_category& generic_category() CORAL_NOEXCEPT;
233 
235 enum class sim_error
236 {
239 
241  data_timeout,
242 };
243 
245 const std::error_category& sim_category() CORAL_NOEXCEPT;
246 
247 
248 // Standard functions to make std::error_code and std::error_condition from
249 // generic_error and sim_error.
250 std::error_code make_error_code(generic_error e) CORAL_NOEXCEPT;
251 std::error_code make_error_code(sim_error e) CORAL_NOEXCEPT;
252 std::error_condition make_error_condition(generic_error e) CORAL_NOEXCEPT;
253 std::error_condition make_error_condition(sim_error e) CORAL_NOEXCEPT;
254 
255 
256 }} // namespace
257 
258 
259 namespace std
260 {
261  // Register sim_error for implicit conversion to std::error_code
262  // TODO: Should this be error_condition?
263  template<>
264  struct is_error_code_enum<coral::error::sim_error>
265  : public true_type
266  { };
267 }
268 #endif // header guard
generic_error
Generic errors.
Definition: error.hpp:216
STL namespace.
STL class.
sim_error
Simulation errors.
Definition: error.hpp:235
const std::error_category & generic_category() CORAL_NOEXCEPT
Error category for generic errors.
std::string ErrnoMessage(const std::string &msg, int errnoValue) CORAL_NOEXCEPT
Constructs an error message by combining a user-defined message and a standard system error message...
STL class.
Exception thrown when communication fails due to a protocol violation.
Definition: error.hpp:29
const std::error_category & sim_category() CORAL_NOEXCEPT
Error category for simulation errors.
A pending operation was canceled before it was started.
Exception thrown on an attempt to use an unsupported protocol.
Definition: error.hpp:41
Definition: variable_io.hpp:28
T str(T...args)
An exception which is used to signal that one or more of a function&#39;s preconditions were not met...
Definition: error.hpp:122
An ongoing operation was aborted.
Slave is unable to perform a time step.
An irrecoverable error happened.
STL class.
Communications timeout between slaves.