Embedded Template Library 1.0
Loading...
Searching...
No Matches
error_handler.h
Go to the documentation of this file.
1
3
4/******************************************************************************
5The MIT License(MIT)
6
7Embedded Template Library.
8https://github.com/ETLCPP/etl
9https://www.etlcpp.com
10
11Copyright(c) 2014 John Wellbelove
12
13Permission is hereby granted, free of charge, to any person obtaining a copy
14of this software and associated documentation files(the "Software"), to deal
15in the Software without restriction, including without limitation the rights
16to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
17copies of the Software, and to permit persons to whom the Software is
18furnished to do so, subject to the following conditions :
19
20The above copyright notice and this permission notice shall be included in all
21copies or substantial portions of the Software.
22
23THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
26AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29SOFTWARE.
30******************************************************************************/
31
32#ifndef ETL_ERROR_HANDLER_INCLUDED
33#define ETL_ERROR_HANDLER_INCLUDED
34
38
39#include "platform.h"
40#include "exception.h"
41#include "function.h"
42#include "nullptr.h"
43
44#include <assert.h>
45
46#if defined(ETL_LOG_ERRORS) || defined(ETL_IN_UNIT_TEST)
47namespace etl
48{
49 //***************************************************************************
52 //***************************************************************************
53 class error_handler
54 {
55 public:
56
57 //*************************************************************************
59 //*************************************************************************
60 struct free_function : public etl::function<void, const etl::exception&>
61 {
62 explicit free_function(void (*p_function_)(const etl::exception&))
63 : etl::function<void, const etl::exception&>(p_function_)
64 {
65 }
66 };
67
68 //*************************************************************************
70 //*************************************************************************
71 template <typename TObject>
72 struct member_function : public etl::function<TObject, const etl::exception&>
73 {
74 member_function(TObject& object_, void(TObject::*p_function_)(const etl::exception&))
75 : etl::function<TObject, const etl::exception&>(object_, p_function_)
76 {
77 }
78 };
79
80 //*****************************************************************************
83 //*****************************************************************************
84 static void set_callback(ifunction<const etl::exception&>& f)
85 {
86 create((void*)(&f), ifunction_stub);
87 }
88
89 //*************************************************************************
91 //*************************************************************************
92 template <void(*Method)(const etl::exception&)>
93 static void set_callback()
94 {
95 create(ETL_NULLPTR, function_stub<Method>);
96 }
97
98 //*************************************************************************
100 //*************************************************************************
101 template <typename T, void(T::* Method)(const etl::exception&)>
102 static void set_callback(T& instance)
103 {
104 create((void*)(&instance), method_stub<T, Method>);
105 }
106
107 //*************************************************************************
109 //*************************************************************************
110 template <typename T, void(T::* Method)(const etl::exception&) const>
111 static void set_callback(const T& instance)
112 {
113 create((void*)(&instance), const_method_stub<T, Method>);
114 }
115
116 //*************************************************************************
118 //*************************************************************************
119 template <typename T, T& Instance, void(T::* Method)(const etl::exception&)>
120 static void set_callback()
121 {
122 create(method_instance_stub<T, Instance, Method>);
123 }
124
125 //*************************************************************************
127 //*************************************************************************
128 template <typename T, T const& Instance, void(T::* Method)(const etl::exception&) const>
129 static void set_callback()
130 {
131 create(const_method_instance_stub<T, Instance, Method>);
132 }
133
134 //*****************************************************************************
137 //*****************************************************************************
138 static void error(const etl::exception& e)
139 {
140 invocation_element& invocation = get_invocation_element();
141
142 if (invocation.stub != ETL_NULLPTR)
143 {
144 (*invocation.stub)(invocation.object, e);
145 }
146 }
147
148 private:
149
150 typedef void(*stub_type)(void* object, const etl::exception&);
151
152 //*************************************************************************
154 //*************************************************************************
155 struct invocation_element
156 {
157 //***********************************************************************
158 invocation_element()
159 : object(ETL_NULLPTR)
160 , stub(ETL_NULLPTR)
161 {
162 }
163
164 //***********************************************************************
165 void* object;
166 stub_type stub;
167 };
168
169 //*************************************************************************
171 //*************************************************************************
172 static invocation_element& get_invocation_element()
173 {
174 static invocation_element invocation;
175
176 return invocation;
177 }
178
179 //*************************************************************************
181 //*************************************************************************
182 static void create(void* object, stub_type stub)
183 {
184 invocation_element& invocation = get_invocation_element();
185
186 invocation.object = object;
187 invocation.stub = stub;
188 }
189
190 //*************************************************************************
192 //*************************************************************************
193 static void create(stub_type stub)
194 {
195 invocation_element& invocation = get_invocation_element();
196
197 invocation.object = ETL_NULLPTR;
198 invocation.stub = stub;
199 }
200
201 //*************************************************************************
203 //*************************************************************************
204 template <typename T, void(T::* Method)(const etl::exception&)>
205 static void method_stub(void* object, const etl::exception& e)
206 {
207 T* p = static_cast<T*>(object);
208 return (p->*Method)(e);
209 }
210
211 //*************************************************************************
213 //*************************************************************************
214 template <typename T, void(T::* Method)(const etl::exception&) const>
215 static void const_method_stub(void* object, const etl::exception& e)
216 {
217 T* const p = static_cast<T*>(object);
218 return (p->*Method)(e);
219 }
220
221 //*************************************************************************
223 //*************************************************************************
224 template <typename T, T& Instance, void(T::* Method)(const etl::exception&)>
225 static void method_instance_stub(void*, const etl::exception& e)
226 {
227 return (Instance.*Method)(e);
228 }
229
230 //*************************************************************************
232 //*************************************************************************
233 template <typename T, const T& Instance, void(T::* Method)(const etl::exception&) const>
234 static void const_method_instance_stub(void*, const etl::exception& e)
235 {
236 (Instance.*Method)(e);
237 }
238
239 //*************************************************************************
241 //*************************************************************************
242 template <void(*Method)(const etl::exception&)>
243 static void function_stub(void*, const etl::exception& e)
244 {
245 (Method)(e);
246 }
247
248 //*************************************************************************
250 //*************************************************************************
251 static void ifunction_stub(void* object, const etl::exception& e)
252 {
254 p->operator()(e);
255 }
256 };
257}
258#endif
259
260//***************************************************************************
269//***************************************************************************
270#if defined(ETL_NO_CHECKS)
271 #define ETL_ASSERT(b, e) ETL_DO_NOTHING // Does nothing.
272 #define ETL_ASSERT_OR_RETURN(b, e) ETL_DO_NOTHING // Does nothing.
273 #define ETL_ASSERT_OR_RETURN_VALUE(b, e, v) ETL_DO_NOTHING // Does nothing.
274
275 #define ETL_ASSERT_FAIL(e) ETL_DO_NOTHING // Does nothing.
276 #define ETL_ASSERT_FAIL_AND_RETURN(e) ETL_DO_NOTHING // Does nothing.
277 #define ETL_ASSERT_FAIL_AND_RETURN_VALUE(e, v) ETL_DO_NOTHING // Does nothing.
278#elif ETL_USING_EXCEPTIONS
279 #if defined(ETL_LOG_ERRORS)
280 #define ETL_ASSERT(b, e) {if (!(b)) {etl::error_handler::error((e)); throw((e));}} // If the condition fails, calls the error handler then throws an exception.
281 #define ETL_ASSERT_OR_RETURN(b, e) {if (!(b)) {etl::error_handler::error((e)); throw((e)); return;}} // If the condition fails, calls the error handler then throws an exception.
282 #define ETL_ASSERT_OR_RETURN_VALUE(b, e, v) {if (!(b)) {etl::error_handler::error((e)); throw((e)); return(v);}} // If the condition fails, calls the error handler then throws an exception.
283
284 #define ETL_ASSERT_FAIL(e) {etl::error_handler::error((e)); throw((e));} // Calls the error handler then throws an exception.
285 #define ETL_ASSERT_FAIL_AND_RETURN(e) {etl::error_handler::error((e)); throw((e)); return;} // Calls the error handler then throws an exception.
286 #define ETL_ASSERT_FAIL_AND_RETURN_VALUE(e, v) {etl::error_handler::error((e)); throw((e)); return(v);} // Calls the error handler then throws an exception.
287 #else
288 #define ETL_ASSERT(b, e) {if (!(b)) {throw((e));}} // If the condition fails, throws an exception.
289 #define ETL_ASSERT_OR_RETURN(b, e) {if (!(b)) {throw((e));}} // If the condition fails, throws an exception.
290 #define ETL_ASSERT_OR_RETURN_VALUE(b, e, v) {if (!(b)) {throw((e));}} // If the condition fails, throws an exception.
291
292 #define ETL_ASSERT_FAIL(e) {throw((e));} // Throws an exception.
293 #define ETL_ASSERT_FAIL_AND_RETURN(e) {throw((e));} // Throws an exception.
294 #define ETL_ASSERT_FAIL_AND_RETURN_VALUE(e, v) {throw((e));} // Throws an exception.
295
296 #endif
297#else
298 #if defined(ETL_LOG_ERRORS)
299 #define ETL_ASSERT(b, e) {if(!(b)) {etl::error_handler::error((e));}} // If the condition fails, calls the error handler
300 #define ETL_ASSERT_OR_RETURN(b, e) {if(!(b)) {etl::error_handler::error((e)); return;}} // If the condition fails, calls the error handler and return
301 #define ETL_ASSERT_OR_RETURN_VALUE(b, e, v) {if(!(b)) {etl::error_handler::error((e)); return (v);}} // If the condition fails, calls the error handler and return a value
302
303 #define ETL_ASSERT_FAIL(e) {etl::error_handler::error((e));} // Calls the error handler
304 #define ETL_ASSERT_FAIL_AND_RETURN(e) {etl::error_handler::error((e)); return;} // Calls the error handler and return
305 #define ETL_ASSERT_FAIL_AND_RETURN_VALUE(e, v) {etl::error_handler::error((e)); return (v);} // Calls the error handler and return a value
306 #else
307 #if ETL_IS_DEBUG_BUILD
308 #define ETL_ASSERT(b, e) assert((b)) // If the condition fails, asserts.
309 #define ETL_ASSERT_OR_RETURN(b, e) {if (!(b)) {assert(false); return;}} // If the condition fails, asserts and return.
310 #define ETL_ASSERT_OR_RETURN_VALUE(b, e, v) {if (!(b)) {assert(false); return(v);}} // If the condition fails, asserts and return a value.
311
312 #define ETL_ASSERT_FAIL(e) assert(false) // Asserts.
313 #define ETL_ASSERT_FAIL_AND_RETURN(e) {assert(false); return;} // Asserts.
314 #define ETL_ASSERT_FAIL_AND_RETURN_VALUE(e, v) {assert(false); return(v);} // Asserts.
315 #else
316 #define ETL_ASSERT(b, e) // Does nothing.
317 #define ETL_ASSERT_OR_RETURN(b, e) {if (!(b)) return;} // Returns.
318 #define ETL_ASSERT_OR_RETURN_VALUE(b, e, v) {if (!(b)) return(v);} // Returns a value.
319
320 #define ETL_ASSERT_FAIL(e) // Does nothing.
321 #define ETL_ASSERT_FAIL_AND_RETURN(e) {return;} // Returns.
322 #define ETL_ASSERT_FAIL_AND_RETURN_VALUE(e, v) {return(v);} // Returns a value.
323 #endif
324 #endif
325#endif
326
327#if defined(ETL_VERBOSE_ERRORS)
328 #define ETL_ERROR(e) (e(__FILE__, __LINE__)) // Make an exception with the file name and line number.
329 #define ETL_ERROR_WITH_VALUE(e, v) (e(__FILE__, __LINE__, (v))) // Make an exception with the file name, line number and value.
330#else
331 #define ETL_ERROR(e) (e("", __LINE__)) // Make an exception with the line number.
332 #define ETL_ERROR_WITH_VALUE(e, v) (e("", __LINE__, (v))) // Make an exception with the file name, line number and value.
333#endif
334
335#if defined(ETL_VERBOSE_ERRORS)
336 #define ETL_ERROR_TEXT(verbose_text, terse_text) (verbose_text) // Use the verbose text.
337#else
338 #define ETL_ERROR_TEXT(verbose_text, terse_text) (terse_text) // Use the terse text.
339#endif
340
341#endif
342
Definition exception.h:47
Definition function.h:94
bitset_ext
Definition absolute.h:38
T * create(Args &&... args)
Creates the object from a type. Variadic parameter constructor.
Definition variant_pool_generator.h:348
pair holds two objects of arbitrary type
Definition utility.h:164