00001
00002
00003
00004
00005
00012 #ifndef HERROR_H
00013 # define HERROR_H
00014
00015
00016
00025 enum ErrCode
00026 {
00027 #if __DEBUG__
00028 ERR_ASSERT = -1,
00029 #endif
00030
00031 NO_ERROR = 0,
00032
00033 ERR_API_ERROR,
00034
00035 ERR_BAD_PARAMETER,
00036 ERR_BAD_WINDOW,
00037
00038 ERR_BUFFER_OVERFLOW,
00039 ERR_BUFFER_UNDERFLOW,
00040 ERR_BUY_MORE_RAM,
00041
00042 ERR_CRC_FAILURE,
00043 ERR_DATA_INVALID,
00044 ERR_DIV_ZERO,
00045 ERR_DUPLICATE,
00046
00047 ERR_FILE_INVALID,
00048 ERR_FILE_DELETE,
00049 ERR_FILE_OPEN,
00050 ERR_FILE_READ,
00051 ERR_FILE_WRITE,
00052
00053 ERR_MAX_CAPACITY,
00054 ERR_NOT_FOUND,
00055 ERR_NOT_IMPLEMENTED,
00056 ERR_NOT_INITIALIZED,
00057 ERR_NULL_PTR,
00058 ERR_OUT_OF_RANGE,
00059 ERR_STL_EXCEPTION,
00060
00061 ERR_MYSTERY
00062 };
00073 class HAssertion : public HObj,
00074 public std::logic_error
00075 {
00076 typedef HObj base_class;
00077 static StringPtr ClassName(void) { return "HAssertion"; }
00078
00079
00080
00081 protected:
00082 StringPtr m_pstrWhat;
00083 StringPtr m_pstrFile;
00084 uint32 m_uLine;
00085
00086
00087
00088 public:
00089 HAssertion(StringPtr pstrWhat, StringPtr pstrFile, uint32 uLine) : std::logic_error("HAssertion")
00090 { (void) Set(pstrWhat, pstrFile, uLine); }
00091
00092 virtual
00093 ~HAssertion(void) throw()
00094 { }
00095
00096
00097
00101 void
00102 Reset(void)
00103 {
00104 m_pstrWhat = NULL;
00105 m_pstrFile = NULL;
00106 m_uLine = 0;
00107 }
00117 void
00118 Throw(StringPtr pstrWhat, StringPtr pstrFile, uint32 uLine) throw()
00119 { throw Set(pstrWhat, pstrFile, uLine); }
00125 const char *
00126 what(void) const throw()
00127 {
00128 static Text szWhat[MAX_PATH+1];
00129
00130 MEM_ZERO(szWhat, sizeof(szWhat));
00131 STR_SPRINTF(szWhat, MAX_PATH,
00132 "Assert \"%s\" failed at %s(%lu)",
00133 STR_SAFE(m_pstrWhat), STR_SAFE(m_pstrFile), m_uLine);
00134
00135 return szWhat;
00136 }
00137
00138
00139
00146 HAssertion & Set(StringPtr pstrWhat, StringPtr pstrFile, uint32 uLine)
00147 {
00148 m_pstrWhat = pstrWhat;
00149 m_pstrFile = pstrFile;
00150 m_uLine = uLine;
00151
00152 DEBUG_LOG("### ASSERT: \"%s\" ###\n", what());
00153
00154 return *this;
00155 }
00156 };
00166 class HError : public HObj,
00167 public std::runtime_error
00168 {
00169 typedef HObj base_class;
00170 static StringPtr ClassName(void) { return "HError"; }
00171
00172
00173
00174 protected:
00175 static HError theErr;
00176
00177 ErrCode m_ec;
00178 StringPtr m_pstrFile;
00179 uint32 m_uLine;
00180
00181
00182
00183 public:
00184 HError(void) : std::runtime_error("HError")
00185 { Reset(); }
00186
00187 HError(ErrCode ec, StringPtr pstrFile, uint32 uLine) : std::runtime_error("HError")
00188 { (void) SetInfo(ec, pstrFile, uLine, false); }
00189
00190 virtual
00191 ~HError(void) throw()
00192 { }
00193
00194
00195
00200 ErrCode
00201 Reset(void)
00202 {
00203 m_ec = NO_ERROR;
00204 m_pstrFile = kStrEmpty;
00205 m_uLine = 0;
00206 return NO_ERROR;
00207 }
00215 static void
00216 Throw(ErrCode ec, StringPtr pstrFile, uint32 uLine) throw()
00217 {
00218 if (GetLastError().SetInfo(ec, pstrFile, uLine, false) != NO_ERROR)
00219 throw GetLastError();
00220 }
00226 virtual const char *
00227 what(void) const throw()
00228 {
00229 static Text szWhat[MAX_PATH+1];
00230
00231 MEM_ZERO(szWhat, sizeof(szWhat));
00232 (void) STR_SPRINTF( szWhat, MAX_PATH,
00233 "Error #%ld at %s(%lu)",
00234 int32(m_ec), STR_SAFE(m_pstrFile), m_uLine);
00235
00236 return szWhat;
00237 }
00238
00239
00240
00246 static HError & GetLastError(void)
00247 {
00248 static HError me;
00249 return me;
00250 }
00259 static ErrCode Set(ErrCode ec, StringPtr pstrFile, uint32 uLine)
00260 { return GetLastError().SetInfo(ec, pstrFile, uLine, true); }
00269 static ErrCode SetSilent(ErrCode ec, StringPtr pstrFile, uint32 uLine)
00270 { return GetLastError().SetInfo(ec, pstrFile, uLine, false); }
00280 ErrCode SetInfo(ErrCode ec, StringPtr pstrFile, uint32 uLine, bool bNotify)
00281 {
00282 m_ec = ec;
00283 m_pstrFile = pstrFile;
00284 m_uLine = uLine;
00285 if (bNotify && ec != NO_ERROR)
00286 {
00287 DEBUG_LOG("### ERROR: %s ###\n", what());
00288 }
00289
00290 return ec;
00291 }
00292
00293
00294
00297 inline static ErrCode NoError(void)
00298 { return GetLastError().Reset(); }
00301 inline bool IsError(void) const
00302 { return (m_ec != NO_ERROR); }
00303
00304
00305
00306 operator ErrCode(void) const
00307 { return m_ec; }
00308 };
00309
00310 #endif // HERROR_H
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337