Elements  6.2
A C++ base framework for the Euclid Software.
Exception.h
Go to the documentation of this file.
1 
26 #ifndef ELEMENTSKERNEL_ELEMENTSKERNEL_EXCEPTION_H_
27 #define ELEMENTSKERNEL_ELEMENTSKERNEL_EXCEPTION_H_
28 
29 #include <cstddef> // for size_t
30 #include <cstdio>
31 #include <exception>
32 #include <sstream>
33 #include <string>
34 #include <type_traits>
35 #include <utility>
36 
37 #include "ElementsKernel/Exit.h"
38 #include "ElementsKernel/Export.h" // for ELEMENTS_API
39 
40 namespace Elements {
41 
48 public:
54  explicit Exception(ExitCode e = ExitCode::NOT_OK) : m_exit_code{e} {}
55 
64  explicit Exception(const char* message, ExitCode e = ExitCode::NOT_OK) : m_error_msg(message), m_exit_code{e} {}
65 
71  explicit Exception(const std::string& message, ExitCode e = ExitCode::NOT_OK)
72  : m_error_msg(message), m_exit_code{e} {}
73 
80  template <typename... Args>
81  explicit Exception(const char* stringFormat, Args&&... args) : m_exit_code{ExitCodeHelper<Args...>{args...}.code} {
82  std::size_t len = snprintf(nullptr, 0, stringFormat, std::forward<Args>(args)...) + 1;
83  char* message = new char[len];
84  snprintf(message, len, stringFormat, std::forward<Args>(args)...);
85  m_error_msg = std::string(message);
86  delete[] message;
87  }
88 
91  virtual ~Exception() noexcept = default;
92 
98  const char* what() const noexcept override {
99  return m_error_msg.c_str();
100  }
101 
106  ExitCode exitCode() const noexcept {
107  return m_exit_code;
108  }
109 
117  template <typename T>
118  void appendMessage(const T& message) {
119  std::stringstream new_message;
120  new_message << m_error_msg << message;
121  m_error_msg = new_message.str();
122  }
123 
124 protected:
127  std::string m_error_msg{};
128  const ExitCode m_exit_code{ExitCode::NOT_OK};
129 
130 private:
134  template <typename... Args>
135  struct ExitCodeHelper {};
136 
137  // Specialisation which handles the last argument
138  template <typename Last>
139  struct ExitCodeHelper<Last> {
140  explicit ExitCodeHelper(const Last& last) : code{getCode(last)} {}
142 
143  private:
144  // This method is used if the T is an ExitCode object
145  template <typename T, typename std::enable_if<std::is_same<T, ExitCode>::value>::type* = nullptr>
146  ExitCode getCode(const T& t) {
147  return t;
148  }
149  // This method is used when the T is not an ExitCode object
150  template <typename T, typename std::enable_if<not std::is_same<T, ExitCode>::value>::type* = nullptr>
151  ExitCode getCode(const T&) {
152  return ExitCode::NOT_OK;
153  }
154  };
155 
156  // Specialization which handles two or more arguments
157  template <typename First, typename... Rest>
158  struct ExitCodeHelper<First, Rest...> : ExitCodeHelper<Rest...> {
159  ExitCodeHelper(const First&, const Rest&... rest) : ExitCodeHelper<Rest...>(rest...) {}
160  };
161 };
162 
163 template <typename Ex, typename T,
164  typename = typename std::enable_if<
166 auto operator<<(Ex&& ex, const T& message) -> decltype(std::forward<Ex>(ex)) {
167  ex.appendMessage(message);
168  return std::forward<Ex>(ex);
169 }
170 
171 } // namespace Elements
172 
173 #endif // ELEMENTSKERNEL_ELEMENTSKERNEL_EXCEPTION_H_
174 
Elements::Exception::ExitCodeHelper< Last >::getCode
ExitCode getCode(const T &)
Definition: Exception.h:151
Elements::Exception::Exception
Exception(const char *stringFormat, Args &&... args)
Constructs a new Exception with a message using format specifiers.
Definition: Exception.h:81
Export.h
defines the macros to be used for explicit export of the symbols
std::string
STL class.
std::exception
STL class.
Elements::Exception::ExitCodeHelper< Last >::getCode
ExitCode getCode(const T &t)
Definition: Exception.h:146
Elements::Exception::exitCode
ExitCode exitCode() const noexcept
Definition: Exception.h:106
Elements::Exception::ExitCodeHelper< First, Rest... >::ExitCodeHelper
ExitCodeHelper(const First &, const Rest &... rest)
Definition: Exception.h:159
std::stringstream
STL class.
Exit.h
define a list of standard exit codes for executables
Elements::Exception::ExitCodeHelper< Last >::ExitCodeHelper
ExitCodeHelper(const Last &last)
Definition: Exception.h:140
Elements::ExitCode
ExitCode
Strongly typed exit numbers.
Definition: Exit.h:97
Elements::Exception::appendMessage
void appendMessage(const T &message)
Appends in the end of the exception message the parameter.
Definition: Exception.h:118
Elements::ExitCode::NOT_OK
@ NOT_OK
Generic unknown failure.
Elements::operator<<
ELEMENTS_API std::ostream & operator<<(std::ostream &, const Environment::Variable &)
Definition: Environment.cpp:295
Elements::Exception::ExitCodeHelper
Definition: Exception.h:135
ELEMENTS_API
#define ELEMENTS_API
Dummy definitions for the backward compatibility mode.
Definition: Export.h:74
std::enable_if
Elements::Exception::Exception
Exception(ExitCode e=ExitCode::NOT_OK)
Definition: Exception.h:54
Elements::Exception
Elements base exception class.
Definition: Exception.h:47
Elements::Exception::Exception
Exception(const std::string &message, ExitCode e=ExitCode::NOT_OK)
Definition: Exception.h:71
Elements::Exception::Exception
Exception(const char *message, ExitCode e=ExitCode::NOT_OK)
Definition: Exception.h:64
std::stringstream::str
T str(T... args)
std::size_t
Elements::Exception::ExitCodeHelper< Last >::code
ExitCode code
Definition: Exception.h:141
Elements::Kernel::Units::e
constexpr double e
The base of the natural logarithm .
Definition: MathConstants.h:51
std::is_base_of
Elements
Definition: callBackExample.h:35