Elements 6.1.2
A C++ base framework for the Euclid Software.
Loading...
Searching...
No Matches
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
40namespace Elements {
41
48public:
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
124protected:
127 std::string m_error_msg{};
128 const ExitCode m_exit_code{ExitCode::NOT_OK};
129
130private:
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
163template <typename Ex, typename T,
164 typename = typename std::enable_if<
166auto 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
define a list of standard exit codes for executables
defines the macros to be used for explicit export of the symbols
Elements base exception class.
Definition: Exception.h:47
ExitCode exitCode() const noexcept
Definition: Exception.h:106
void appendMessage(const T &message)
Appends in the end of the exception message the parameter.
Definition: Exception.h:118
Exception(const std::string &message, ExitCode e=ExitCode::NOT_OK)
Definition: Exception.h:71
Exception(ExitCode e=ExitCode::NOT_OK)
Definition: Exception.h:54
virtual ~Exception() noexcept=default
Exception(const char *stringFormat, Args &&... args)
Constructs a new Exception with a message using format specifiers.
Definition: Exception.h:81
Exception(const char *message, ExitCode e=ExitCode::NOT_OK)
Definition: Exception.h:64
ExitCode
Strongly typed exit numbers.
Definition: Exit.h:97
#define ELEMENTS_API
Dummy definitions for the backward compatibility mode.
Definition: Export.h:74
@ NOT_OK
Generic unknown failure.
ELEMENTS_API std::ostream & operator<<(std::ostream &, const Environment::Variable &)
T str(T... args)
ExitCodeHelper(const First &, const Rest &... rest)
Definition: Exception.h:159