LibCT 2.0
|
00001 // ------------------------------------------------------------------ 00012 #ifndef _LIBCT_EXCEPTION_H 00013 #define _LIBCT_EXCEPTION_H 00014 00015 #include "FormatString.h" 00016 #include "Defines.h" 00017 #include "Debug.h" 00018 00019 #include <exception> 00020 #include <string> 00021 00022 namespace LibCT 00023 { 00025 enum ExceptionCode 00026 { 00027 ExceptionCodeIO, 00028 ExceptionCodeVariable, 00029 ExceptionCodeProject, 00030 }; 00031 00033 class Exception : public std::exception 00034 { 00035 public: 00037 Exception(int errCode, 00038 const std::string& desc, 00039 const std::string& src, 00040 const char* pFile, 00041 long lineNum 00042 ) 00043 : m_ErrCode(errCode) 00044 , m_Desc(desc) 00045 , m_Src(src) 00046 , m_pFile(pFile) 00047 , m_LineNum(lineNum) 00048 , m_FullDesc("") 00049 { 00050 // Build the full description 00051 m_FullDesc = FormatString<1024>("Exception %d - %s: %s occured in %s at line %d in file %s\n", errCode, 00052 GetErrorCodeString(), m_Desc.c_str(), src.c_str(), lineNum, pFile); 00053 } 00054 00056 ~Exception() throw() {} 00057 00060 const char* what() const throw() { return m_FullDesc.c_str(); } 00061 00064 virtual const char* GetErrorCodeString() const { return "Unknown"; } 00065 00066 00067 protected: 00068 int m_ErrCode; 00069 std::string m_Desc; 00070 std::string m_Src; 00071 const char* m_pFile; 00072 long m_LineNum; 00073 std::string m_FullDesc; 00074 }; 00075 00077 00082 template <int num> 00083 struct ExceptionCodeType 00084 { 00085 enum { number = num }; 00086 }; 00087 00089 00091 class IOException : public Exception 00092 { 00093 public: 00095 IOException( 00096 int errCode, 00097 const std::string& desc, 00098 const std::string& src, 00099 const char* pFile, 00100 long lineNum 00101 ) 00102 : Exception(errCode, desc, src, pFile, lineNum) 00103 { 00104 } 00105 00108 const char* GetErrorCodeString() const { return "IO Exception"; } 00109 }; 00110 00112 class VariableException : public Exception 00113 { 00114 public: 00116 VariableException( 00117 int errCode, 00118 const std::string& desc, 00119 const std::string& src, 00120 const char* pFile, 00121 long lineNum 00122 ) 00123 : Exception(errCode, desc, src, pFile, lineNum) 00124 { 00125 } 00126 00129 const char* GetErrorCodeString() const { return "Variable Exception"; } 00130 }; 00131 00133 00137 class ExceptionFactory 00138 { 00139 private: 00141 ExceptionFactory() {} 00142 public: 00145 static IOException Create( 00146 ExceptionCodeType<ExceptionCodeIO> code, 00147 const std::string& desc, 00148 const std::string& src, 00149 const char* pFile, 00150 long lineNum 00151 ) 00152 { 00153 (void)code; 00154 return IOException(code.number, desc, src, pFile, lineNum); 00155 } 00156 00159 static VariableException Create( 00160 ExceptionCodeType<ExceptionCodeVariable> code, 00161 const std::string& desc, 00162 const std::string& src, 00163 const char* pFile, 00164 long lineNum 00165 ) 00166 { 00167 (void)code; 00168 return VariableException(code.number, desc, src, pFile, lineNum); 00169 } 00170 }; 00171 } 00172 00173 #endif // _LIBCT_EXCEPTION_H