00001 /*************************************************************************** 00002 * * 00003 * (c) Art Tevs, MPI Informatik Saarbruecken * 00004 * mailto: <tevs@mpi-sb.mpg.de> * 00005 * * 00006 * This program is free software; you can redistribute it and/or modify * 00007 * it under the terms of the GNU General Public License as published by * 00008 * the Free Software Foundation; either version 2 of the License, or * 00009 * (at your option) any later version. * 00010 * * 00011 ***************************************************************************/ 00012 00013 #ifndef __NR_EXCEPTION_C_H_ 00014 #define __NR_EXCEPTION_C_H_ 00015 00016 00017 //---------------------------------------------------------------------------------- 00018 // Includes 00019 //---------------------------------------------------------------------------------- 00020 #include "Prerequisities.h" 00021 00022 00023 /** 00024 * This is a macro which throw an exception object 00025 * \ingroup error 00026 **/ 00027 #define NR_EXCEPT( num, desc, src ) throw( ::nrEngine::Exception( num, desc, src, __FILE__, __LINE__ ) ) 00028 00029 /** 00030 * Assertion definition used in the engine. This assertion will produce more readable text. 00031 * \ingroup error 00032 **/ 00033 #define NR_ASSERT( exp ) ::nrEngine::Exception::Assert(exp, #exp, __FILE__, "" , __LINE__ ) 00034 00035 00036 namespace nrEngine{ 00037 00038 //! Exception thrown by the engine if any non returnable error occurs 00039 /** 00040 * \par 00041 * nrEngine does use return codes to indicate that an error occurs. This is done because 00042 * of performance and code styling reasons. So the user does not have to encapsulate 00043 * each function call of the engine into try{}catch(){} block. Instead of this just 00044 * only check the return value of the function.<br> 00045 * But sometimtes we need to throw an error instead of return back an error code. 00046 * This is done for example in constructors and destructors, where you are not able 00047 * to return any value.<br> 00048 * For such cases you have to catch up the error thrown by the engine. Each exception 00049 * object is of this class. 00050 * 00051 * \par 00052 * Probably in next releases of the engine we will go away from the return codes to 00053 * the exception system. 00054 * 00055 * \ingroup error 00056 **/ 00057 class _NRExport Exception 00058 { 00059 public: 00060 /** 00061 * Default constructor. 00062 * \param number Error code. Can be combined of more than one error code 00063 * \param description Description of the esception 00064 * \param source Where was exception thrown (function name) 00065 */ 00066 Exception( Result number, const std::string& description, const std::string& source ); 00067 00068 /** 00069 * Advanced constructor. 00070 * \param number Error code. Can be combined of more than one error code 00071 * \param description Description of the esception 00072 * \param source Where was exception thrown (function name) 00073 * \param file In which file was the exception 00074 * \param line Where was the exception thrown 00075 */ 00076 Exception( Result number, const std::string& description, const std::string& source, char* file, long line ); 00077 00078 /** 00079 * Copy constructor. 00080 */ 00081 Exception(const Exception& rhs); 00082 00083 00084 /** 00085 * Assert function declared as static to allow using without initialisation 00086 * of the object itself. This function will generate a formatted 00087 * error message and will log it. 00088 * @param exp Boolean expression to be checked (false = fail) 00089 * @param szExp Error message 00090 * @param szFilename Filename of the file where error happens 00091 * @param szFuncName Name of the function where assertion is checked 00092 * @param iLineNum Line number where assert fails 00093 **/ 00094 static void Assert(bool exp, const char *szExp, const char *szFilename, const char* szFuncName, int iLineNum); 00095 00096 private: 00097 long lineNumber; 00098 std::string description; 00099 std::string source; 00100 std::string file; 00101 Result number; 00102 00103 00104 /** 00105 * Generate the formatted assert error message. 00106 * We use two functions to allow compiler to optimize the assertion 00107 * calls. 00108 **/ 00109 static void _assertMsg(const char *szExp, const char *szFilename, const char* szFuncName, int iLineNum); 00110 00111 /** 00112 * Give some message to log files 00113 **/ 00114 static void _log(const char *szExp, const char *szFilename, const char* szFuncName, int iLineNum, const char* msg); 00115 }; 00116 00117 }; // end namespace 00118 00119 #endif