00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef VRS_ERRORHANDLER_H
00021 #define VRS_ERRORHANDLER_H
00022
00023 #include <vrs/config.h>
00024 #include <vrs/exception.h>
00025
00026 namespace VRS {
00027
00028 class VRS_CORE_API ErrorHandler {
00029 public:
00030 enum KindOfError { PROGRAM_ERROR, ARGUMENT_ERROR, WARNING, NOIMPL };
00031 enum KindOfAction { DO_ABORT, DO_EXIT, DO_CONTINUE, THROW_EXCEPTION };
00032 typedef void (*Function)(int errortype, const char* expr,
00033 const char* file, int line, const char* text);
00034
00035 static Function setHandler(int errortype, Function funct);
00037 static int setAction(int errortype, int action);
00039
00040 static void callHandler(int errortype, const char* expr, const char* file, int line);
00042
00047 static std::ostream& newErrorOutputStringStream();
00054 static std::string getLastError();
00060
00061
00062 static void enableErrorLogging(bool enable);
00063 };
00064
00065 }
00066
00067
00068
00069
00070
00071 #ifdef __GNUC__
00072 # define VRS_WHICH_FUNCTION __PRETTY_FUNCTION__ << std::endl << "\t"
00073 #else // __GNUC__
00074 # define VRS_WHICH_FUNCTION ""
00075 #endif // __GNUC__
00076
00077 #define VRS_CONSTRUCT_ERRORHANDLER(ERROR_TYPE, ERROR_MSG, MSG) \
00078 { \
00079 std::ostream& s = ::VRS::ErrorHandler::newErrorOutputStringStream(); \
00080 s << VRS_WHICH_FUNCTION << MSG; \
00081 ::VRS::ErrorHandler::callHandler(::VRS::ErrorHandler::ERROR_TYPE, #ERROR_MSG, __FILE__, __LINE__); \
00082 }
00083
00084 #ifdef NDEBUG
00085 # define VRS_NO_INTERNAL_WARNING
00086 # define VRS_NO_INTERNAL_ERROR
00087 # define VRS_NO_CHECKARG
00088 #endif // NDEBUG
00089
00090
00091
00092
00093
00094
00095
00096 #ifdef VRS_NO_CHECKARG
00097 # define VRS_CheckArg(EXPR, MSG) ((void)0)
00098 #else // VRS_NO_CHECKARG
00099 # define VRS_CheckArg(EXPR, MSG) if(!(EXPR)) { VRS_CONSTRUCT_ERRORHANDLER(ARGUMENT_ERROR, EXPR, MSG); } ((void)0)
00100 #endif // VRS_NO_CHECKARG
00101
00102
00103
00104
00105
00106 #ifdef VRS_NO_ERROR
00107 # define VRS_Error(EXPR, MSG) ((void)0)
00108 #else // VRS_NO_ERROR
00109 # define VRS_Error(EXPR, MSG) if(!(EXPR)) { VRS_CONSTRUCT_ERRORHANDLER(PROGRAM_ERROR, EXPR, MSG); } ((void)0)
00110 #endif // VRS_NO_ERROR
00111
00112
00113
00114
00115 #ifdef VRS_NO_INTERNAL_ERROR
00116 # define VRS_InternalError(EXPR, MSG) ((void)0)
00117 #else // VRS_NO_INTERNAL_ERROR
00118 # define VRS_InternalError(EXPR, MSG) if(!(EXPR)) { VRS_CONSTRUCT_ERRORHANDLER(PROGRAM_ERROR, EXPR, MSG); } ((void)0)
00119 #endif // VRS_NO_INTERNAL_ERROR
00120
00121
00122
00123
00124 #define VRS_Assertion(EXPR, MSG) VRS_Error(EXPR, MSG)
00125 #define VRS_RangeAssertion(VAL, MINV, MAXV) \
00126 VRS_Assertion(((MINV) <= (VAL)) && ((VAL) < (MAXV)), "range error: [" << (MINV) << ", " << (MAXV) << "), " << (VAL))
00127
00128
00129
00130
00131
00132
00133 #ifdef VRS_NO_WARNING
00134 # define VRS_Warning(EXPR, MSG) ((void)0)
00135 #else // VRS_NO_WARNING
00136 # define VRS_Warning(EXPR, MSG) if(!(EXPR)) { VRS_CONSTRUCT_ERRORHANDLER(WARNING, EXPR, MSG); } ((void)0)
00137 #endif // VRS_NO_WARNING
00138
00139
00140
00141
00142
00143 #ifdef VRS_NO_WARNING
00144 # define VRS_WarnOnce(EXPR, MSG) ((void)0)
00145 #else // VRS_NO_WARNING
00146 # define VRS_WarnOnce(EXPR, MSG) \
00147 { static bool haveWarned = false; \
00148 if (!haveWarned) { \
00149 if(!(EXPR)) { \
00150 VRS_CONSTRUCT_ERRORHANDLER(WARNING, EXPR, MSG); \
00151 haveWarned = true; \
00152 } \
00153 } \
00154 } ((void)0)
00155 #endif // VRS_NO_WARNING
00156
00157
00158
00159
00160
00161 #ifdef VRS_NO_INTERNAL_WARNING
00162 # define VRS_InternalWarning(EXPR, MSG) ((void)0)
00163 #else // VRS_NO_INTERNAL_WARNING
00164 # define VRS_InternalWarning(EXPR, MSG) if(!(EXPR)) { VRS_CONSTRUCT_ERRORHANDLER(WARNING, EXPR, MSG); } ((void)0)
00165 #endif // VRS_NO_INTERNAL_WARNING
00166
00167
00168
00169
00170
00171 #ifdef VRS_NO_INTERNAL_WARNING
00172 # define VRS_InternalWarnOnce(EXPR, MSG) ((void)0)
00173 #else // VRS_NO_INTERNAL_WARNING
00174 # define VRS_InternalWarnOnce(EXPR, MSG) \
00175 { static bool haveWarned = false; \
00176 if (!haveWarned) { \
00177 if(!(EXPR)) { \
00178 VRS_CONSTRUCT_ERRORHANDLER(WARNING, EXPR, MSG); \
00179 haveWarned = true; \
00180 } \
00181 } \
00182 } ((void)0)
00183 #endif // VRS_NO_INTERNAL_WARNING
00184
00185
00186
00187
00188
00189 #ifdef VRS_NO_NOIMPL
00190 # define VRS_NoImpl(MSG) ((void)0)
00191 #else // VRS_NO_NOIMPL
00192 # define VRS_NoImpl(MSG) VRS_CONSTRUCT_ERRORHANDLER(NOIMPL, "Not implemented", MSG); ((void)0)
00193 #endif // VRS_NO_NOIMPL
00194
00195
00196
00197
00198
00199 template<class S, class T>
00200 inline T* VRS_CastImpl(const S* s, T*) {
00201 return dynamic_cast<T*>(const_cast<S*>(s));
00202 }
00203
00204 #ifdef VRS_NO_INTERNAL_ERROR
00205
00206 template<class S, class T>
00207 inline T* VRS_GuardedCastImpl(const S* s, T* dummy) {
00208 return static_cast<T*>(const_cast<S*>(s));
00209 }
00210
00211 #else // VRS_NO_INTERNAL_ERROR
00212
00213 template<class S, class T>
00214 inline T* VRS_GuardedCastImpl(const S* s, T* dummy) {
00215 VRS_CheckArg(s, "tried to cast null pointer of type " << S::ClassNameVRS());
00216 T* t = VRS_CastImpl(s, dummy);
00217 VRS_InternalError(t, "guarded cast failed: type " << S::ClassNameVRS() << " cannot be cast to type " << T::ClassNameVRS());
00218 return t;
00219 }
00220
00221 #endif // VRS_NO_INTERNAL_ERROR
00222
00223 #define VRS_Cast(TYPE, POINTER) VRS_CastImpl(POINTER, (TYPE*) 0)
00224
00227 #define VRS_GuardedCast(TYPE, POINTER) VRS_GuardedCastImpl(POINTER, (TYPE*) 0)
00228
00231 #endif // VRS_ERRORHANDLER_H