Stefan Scholz / ETL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers error_handler.h Source File

error_handler.h

Go to the documentation of this file.
00001 
00002 ///\file
00003 
00004 /******************************************************************************
00005 The MIT License(MIT)
00006 
00007 Embedded Template Library.
00008 https://github.com/ETLCPP/etl
00009 http://www.etlcpp.com
00010 
00011 Copyright(c) 2014 jwellbelove
00012 
00013 Permission is hereby granted, free of charge, to any person obtaining a copy
00014 of this software and associated documentation files(the "Software"), to deal
00015 in the Software without restriction, including without limitation the rights
00016 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
00017 copies of the Software, and to permit persons to whom the Software is
00018 furnished to do so, subject to the following conditions :
00019 
00020 The above copyright notice and this permission notice shall be included in all
00021 copies or substantial portions of the Software.
00022 
00023 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00024 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00025 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
00026 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00027 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00028 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00029 SOFTWARE.
00030 ******************************************************************************/
00031 
00032 #ifndef __ETL_ERROR_HANDLER__
00033 #define __ETL_ERROR_HANDLER__
00034 
00035 ///\defgroup error_handler error_handler
00036 /// Error handler for when throwing exceptions is not required.
00037 ///\ingroup utilities
00038 
00039 #include <assert.h>
00040 
00041 #include "platform.h "
00042 #include "exception.h "
00043 #include "function.h "
00044 
00045 namespace etl
00046 {
00047   //***************************************************************************
00048   /// Error handler for when throwing exceptions is not required.
00049   ///\ingroup error_handler
00050   //***************************************************************************
00051   class error_handler
00052   {
00053   public:
00054 
00055     //*************************************************************************
00056     /// Callback class for free handler functions.
00057     //*************************************************************************
00058     struct free_function : public etl::function<void, const etl::exception&>
00059     {
00060       free_function(void (*p_function_)(const etl::exception&))
00061         : etl::function<void, const etl::exception&> (p_function_)
00062       {
00063       }
00064     };
00065 
00066     //*************************************************************************
00067     /// Callback class for member handler functions.
00068     //*************************************************************************
00069     template <typename TObject>
00070     struct member_function : public etl::function<TObject, const etl::exception&>
00071     {
00072       member_function(TObject& object_, void(TObject::*p_function_)(const etl::exception&))
00073         : etl::function<TObject, const etl::exception&> (object_, p_function_)
00074       {
00075       }
00076     };
00077 
00078     static void set_callback(ifunction<const etl::exception&> & f);
00079     static void error(const etl::exception& e);
00080 
00081   private:
00082 
00083     static ifunction<const etl::exception&> * p_ifunction;
00084   };
00085 }
00086 
00087 //***************************************************************************
00088 /// Asserts a condition.
00089 /// Versions of the macro that return a constant value of 'true' will allow the compiler to optimise away
00090 /// any 'if' statements that it is contained within.
00091 /// If ETL_NO_CHECKS is defined then no runtime checks are executed at all.
00092 /// If asserts or exceptions are enabled then the error is thrown if the assert fails. The return value is always 'true'.
00093 /// If ETL_LOG_ERRORS is defined then the error is logged if the assert fails. The return value is the value of the boolean test.
00094 /// Otherwise 'assert' is called. The return value is always 'true'.
00095 ///\ingroup error_handler
00096 //***************************************************************************
00097 #if defined(ETL_NO_CHECKS)
00098   #define ETL_ASSERT(b, e)                                                             // Does nothing.
00099 #elif defined(ETL_THROW_EXCEPTIONS)
00100   #if defined(ETL_LOG_ERRORS)
00101     #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.
00102   #else
00103     #define ETL_ASSERT(b, e) {if (!(b)) {throw((e));}}                                 // If the condition fails, throws an exception.
00104   #endif
00105 #else
00106   #if defined(ETL_LOG_ERRORS)
00107     #if defined(NDEBUG)
00108       #define ETL_ASSERT(b, e) {if(!(b)) {etl::error_handler::error((e));}}              // If the condition fails, calls the error handler
00109     #else
00110       #define ETL_ASSERT(b, e) {if(!(b)) {etl::error_handler::error((e));} assert((b));} // If the condition fails, calls the error handler then asserts.
00111     #endif
00112   #else
00113     #if defined(NDEBUG)
00114       #define ETL_ASSERT(b, e)                                                         // Does nothing.
00115     #else
00116       #define ETL_ASSERT(b, e) assert((b))                                             // If the condition fails, asserts.
00117     #endif
00118   #endif
00119 #endif
00120 
00121 #if defined(ETL_VERBOSE_ERRORS)
00122   #define ETL_ERROR(e) (e(__FILE__, __LINE__)) // Make an exception with the file name and line number.
00123 #else
00124   #define ETL_ERROR(e) (e("", __LINE__))       // Make an exception with the line number.
00125 #endif
00126 
00127 #if defined(ETL_VERBOSE_ERRORS)
00128   #define ETL_ERROR_TEXT(verbose_text, terse_text) (verbose_text) // Use the verbose text.
00129 #else
00130   #define ETL_ERROR_TEXT(verbose_text, terse_text) (terse_text)   // Use the terse text.
00131 #endif
00132 
00133 #endif
00134 
00135