src/zserio/CppRuntimeException.h
Line | Count | Source |
1 | | #ifndef ZSERIO_CPP_RUNTIME_EXCEPTION_H_INC |
2 | | #define ZSERIO_CPP_RUNTIME_EXCEPTION_H_INC |
3 | | |
4 | | #include <array> |
5 | | #include <exception> |
6 | | #include <string> |
7 | | #include <type_traits> |
8 | | #include <vector> |
9 | | |
10 | | #include "zserio/Span.h" |
11 | | #include "zserio/StringConvertUtil.h" |
12 | | #include "zserio/Traits.h" |
13 | | #include "zserio/Types.h" |
14 | | |
15 | | namespace zserio |
16 | | { |
17 | | |
18 | | /** |
19 | | * Exception thrown when an error within the Zserio C++ runtime library occurs. |
20 | | */ |
21 | | class CppRuntimeException : public std::exception |
22 | | { |
23 | | public: |
24 | | /** |
25 | | * Constructor. |
26 | | * |
27 | | * \param message Description of the error. |
28 | | */ |
29 | | explicit CppRuntimeException(const char* message = ""); |
30 | | |
31 | | /** |
32 | | * Method generated by default. |
33 | | * \{ |
34 | | */ |
35 | 34.3k | ~CppRuntimeException() override = default; |
36 | | |
37 | | CppRuntimeException(const CppRuntimeException& other) = default; |
38 | 113 | CppRuntimeException& operator=(const CppRuntimeException& other) = default; |
39 | | |
40 | 16.8k | CppRuntimeException(CppRuntimeException&& other) = default; |
41 | 2 | CppRuntimeException& operator=(CppRuntimeException&& other) = default; |
42 | | /** |
43 | | * \} |
44 | | */ |
45 | | |
46 | | const char* what() const noexcept override; |
47 | | |
48 | | /** |
49 | | * Appends a message to the description. |
50 | | * |
51 | | * \param message Description of the error to append. |
52 | | */ |
53 | | void append(const char* message); |
54 | | |
55 | | /** |
56 | | * Appends a message of a known length to the description. |
57 | | * |
58 | | * \param message Description of the error to append. |
59 | | * \param messageLen Length of the message. |
60 | | */ |
61 | | void append(const char* message, size_t messageLen); |
62 | | |
63 | | private: |
64 | | void appendImpl(Span<const char> message); |
65 | | |
66 | | std::array<char, 512> m_buffer; // note fixed sized array is deeply copied on copy operations and it's OK |
67 | | size_t m_len = 0; |
68 | | }; |
69 | | |
70 | | /** |
71 | | * Appends a message to the exception's description. |
72 | | * |
73 | | * \param exception Exception to modify. |
74 | | * \param message Description of the error to append. |
75 | | * |
76 | | * \return Reference to the exception to allow operator chaining. |
77 | | */ |
78 | | CppRuntimeException& operator<<(CppRuntimeException& exception, const char* message); |
79 | | |
80 | | /** |
81 | | * Appends a bool value to the exception's description. |
82 | | * |
83 | | * \param exception Exception to modify. |
84 | | * \param value Bool value to append. |
85 | | * |
86 | | * \return Reference to the exception to allow operator chaining. |
87 | | */ |
88 | | CppRuntimeException& operator<<(CppRuntimeException& exception, bool value); |
89 | | |
90 | | /** |
91 | | * Appends a float value to the exception's description. |
92 | | * |
93 | | * \param exception Exception to modify. |
94 | | * \param value Float value to append. |
95 | | * |
96 | | * \return Reference to the exception to allow operator chaining. |
97 | | */ |
98 | | CppRuntimeException& operator<<(CppRuntimeException& exception, float value); |
99 | | |
100 | | /** |
101 | | * Appends a double value to the exception's description. |
102 | | * |
103 | | * \param exception Exception to modify. |
104 | | * \param value Double value to append. |
105 | | * |
106 | | * \return Reference to the exception to allow operator chaining. |
107 | | */ |
108 | | CppRuntimeException& operator<<(CppRuntimeException& exception, double value); |
109 | | |
110 | | /** |
111 | | * Appends an integral value to the exception's description. |
112 | | * |
113 | | * \param exception Exception to modify. |
114 | | * \param value Integral value to append. |
115 | | * |
116 | | * \return Reference to the exception to allow operator chaining. |
117 | | */ |
118 | | template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0> |
119 | | CppRuntimeException& operator<<(CppRuntimeException& exception, T value) |
120 | 1.93k | { |
121 | 1.93k | std::array<char, 24> buffer = {}; |
122 | 1.93k | const char* stringValue = convertIntToString(buffer, value); |
123 | 1.93k | return exception << stringValue; |
124 | 1.93k | } |
125 | | |
126 | | /** |
127 | | * Appends any object which implement getValue() method (e.g. bitmask) to the exception's description. |
128 | | * |
129 | | * \param exception Exception to modify. |
130 | | * \param value Object with getValue() method to append. |
131 | | * |
132 | | * \return Reference to the exception to allow operator chaining. |
133 | | */ |
134 | | template <typename T, typename std::enable_if<is_bitmask<T>::value, int>::type = 0> |
135 | | CppRuntimeException& operator<<(CppRuntimeException& exception, T value) |
136 | 1 | { |
137 | 1 | exception << value.getValue(); |
138 | 1 | return exception; |
139 | 1 | } |
140 | | |
141 | | /** |
142 | | * Appends a string value to the exception's description. |
143 | | * |
144 | | * \param exception Exception to modify. |
145 | | * \param value String value to append. |
146 | | * |
147 | | * \return Reference to the exception to allow operator chaining. |
148 | | */ |
149 | | template <typename ALLOC> |
150 | | CppRuntimeException& operator<<( |
151 | | CppRuntimeException& exception, const std::basic_string<char, std::char_traits<char>, ALLOC>& value) |
152 | 5 | { |
153 | 5 | exception.append(value.c_str(), value.size()); |
154 | 5 | return exception; |
155 | 5 | } |
156 | | |
157 | | /** |
158 | | * Appends a vector value to the exception's description. |
159 | | * |
160 | | * \param exception Exception to modify. |
161 | | * \param value Vector value to append. |
162 | | * |
163 | | * \return Reference to the exception to allow operator chaining. |
164 | | */ |
165 | | template <typename T, typename ALLOC> |
166 | | CppRuntimeException& operator<<(CppRuntimeException& exception, const std::vector<T, ALLOC>& value) |
167 | 1 | { |
168 | 1 | return exception << "vector([...], " << value.size() << ")"; |
169 | 1 | } |
170 | | |
171 | | namespace detail |
172 | | { |
173 | | |
174 | | // inspired by C++ ostreams - see https://cplusplus.github.io/LWG/issue1203 |
175 | | // note that e.g. in gcc implementation of ostreams there are two constraints, but the second one: |
176 | | // typename = decltype(std::declval<EXCEPTION&>() << std::declval<const VALUE&>()) |
177 | | // is probably unnecessary and since it caused a compilation error in MSVC 2017 Conformance Mode, |
178 | | // we intentionally skipped it (even though it was probably a compiler bug) |
179 | | template <typename EXCEPTION, typename VALUE, |
180 | | typename = typename std::enable_if<std::is_base_of<CppRuntimeException, EXCEPTION>::value, int>::type> |
181 | | using CppRuntimeExceptionRValueInsertion = EXCEPTION&&; |
182 | | |
183 | | } // namespace detail |
184 | | |
185 | | /** |
186 | | * Appends any value for which operator<< is implemented to the exception's description. |
187 | | * |
188 | | * Overload for rvalue to enable operator<< on a temporary object, .e.g. CppRuntimeException() << "value". |
189 | | * Moreover note that this overload preserves the concrete type of the exception! |
190 | | * |
191 | | * \param exception Exception to modify. |
192 | | * \param value Value to append. |
193 | | * |
194 | | * \return R-value reference to the original exception to allow operator chaining. |
195 | | */ |
196 | | template <typename CPP_RUNTIME_EXCEPTION, typename T> |
197 | | detail::CppRuntimeExceptionRValueInsertion<CPP_RUNTIME_EXCEPTION, T> operator<<( |
198 | | CPP_RUNTIME_EXCEPTION&& exception, const T& value) |
199 | 35.1k | { |
200 | 35.1k | exception << value; |
201 | 35.1k | return std::forward<CPP_RUNTIME_EXCEPTION>(exception); |
202 | 35.1k | } |
203 | | |
204 | | } // namespace zserio |
205 | | |
206 | | #endif // ifndef ZSERIO_CPP_RUNTIME_EXCEPTION_H_INC |