00001 #ifndef __J2K__JErrorController_HPP__ 00002 #define __J2K__JErrorController_HPP__ 00003 00004 ////////////////////////////////////////////////////////////////////// 00005 // // 00006 // Notice: // 00007 // ------- // 00008 // // 00009 // We can't use JString or any other J2K classes, // 00010 // since we are not sure, where the error occured // 00011 // and so, we can only rely on ourself ! // 00012 // Hoping that this class works well... =) // 00013 // // 00014 ////////////////////////////////////////////////////////////////////// 00015 00016 #include <j2k/Fred/Standard.hpp> 00017 #include <errno.h> 00018 #include <j2k/Fred/Error/JError.hpp> 00019 #include <j2k/Fred/Error/JErrorHandler.hpp> 00020 00021 00022 ////////////////////////////////////////////////////////////////////// 00023 // Define commonly used macros // 00024 ////////////////////////////////////////////////////////////////////// 00025 00026 #ifndef EOK 00027 #define EOK 0 00028 #endif 00029 00030 00031 #define MC_OnError( err, level, errMsg ) \ 00032 if ( err != EOK ) { \ 00033 pgmErrorController.call( (JErrorLevel)(level), (int)(err), \ 00034 (const char*)(errMsg), (__LINE__), \ 00035 (const char*)(__FILE__) ); \ 00036 } 00037 00038 00039 // This will gives us something betting [0,10] + offset 00040 // EPERM 1 => 0 00041 // ESRCH 3 => 1 00042 // 00043 // EDEADLK 45 => 2 00044 // ENOTSUP 48 => 3 00045 // 00046 // EAGAIN 11 => 4 00047 // ENOMEM 12 => 5 00048 // EFAULT 14 => 6 00049 // EBUSY 16 => 7 00050 // EINVAL 22 => 8 00051 // 00052 // ETIMEDOUT 145 => 9 00053 00054 #if 0 00055 /* NOT USED ANY MORE ! 00056 #define MC_PThreadError( errNo, level, errMsg, offset ) \ 00057 if ( errNo != EOK ) { \ 00058 pgmErrorController.call( level, errNo, errMsg, __LINE__, __FILE__ ); \ 00059 register int PTerror = errNo; \ 00060 if ( EGAIN == 35 && PTerror == 35 ) { PTerror = 11; } \ 00061 PTerror %= 40; \ 00062 PTerror >>= 1; \ 00063 PTerror &= 15; \ 00064 if ( PTerror > 3 ) { PTerror--; } \ 00065 if ( PTerror > 9 ) { PTerror -= 2; } \ 00066 char* pMsg = (char*)JErrorMsg[ (size_t)PTerror + (size_t)offset ]; \ 00067 fprintf( stderr, "%s", pMsg ); \ 00068 } 00069 */ 00070 #endif 00071 00072 class JErrorController; 00073 extern JErrorController pgmErrorController; 00074 00075 #define MC_MainErrorController JErrorController pgmErrorController; 00076 00077 ////////////////////////////////////////////////////////////////////// 00078 // We use a variable array since the error number is known, // 00079 // so a link-list is not needed, it's also faster. // 00080 ////////////////////////////////////////////////////////////////////// 00081 00082 class JErrorController { 00083 public: 00084 JErrorController(); 00085 00086 virtual ~JErrorController(); 00087 00088 void call( JErrorLevel level, int no, const char* errmsg, 00089 int line, const char* file ); 00090 00091 00092 ////////////////////////////////////////////////////////////////////// 00093 // Someone needs some specific handler for an error number // 00094 // from now until it's removed. // 00095 // The new error handler is added to the JErrorNo // 00096 // top of single Linked-List for that error number. // 00097 // The second argument is the static error handler // 00098 // that will be call if that error happen. // 00099 ////////////////////////////////////////////////////////////////////// 00100 void add( JErrorNo no, void (*fn)( MC_JErrorHandlerArg ), short id = 0 ); 00101 00102 ////////////////////////////////////////////////////////////////////// 00103 // Someone doesn't need anymore // 00104 // some specific handler for an error number. // 00105 // The new error handler is now removed from the // 00106 // top of single Linked-List for that error number. // 00107 ////////////////////////////////////////////////////////////////////// 00108 void remove( JErrorNo no ); 00109 00110 short getID( JErrorNo no ); 00111 00112 private: 00113 JErrorHandler* defaultHandler; 00114 JErrorHandler** array; 00115 }; 00116 00117 #endif