00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00015 #ifndef __CEXCEPTION_HPP__
00016 #define __CEXCEPTION_HPP__
00017
00018 #include <string>
00019 #include "config.hpp"
00020 #include "sh_ptr.hpp"
00021
00031 class CException {
00032 public:
00036 struct FileLine {
00038 const char* fname;
00040 int line;
00041
00045 FileLine(const char* f, int l) : fname(f), line(l) {}
00046 };
00047
00048 #ifdef DERS_RETHROW_BUG
00049
00050 static sh_ptr<CException> current;
00051 #endif
00052
00053 private:
00055 CException(const CException&);
00057 CException& operator=(const CException&);
00058
00059 protected:
00064 CException(const FileLine& loc, const std::string& msg,
00065 sh_ptr<CException> nest);
00066
00067 public:
00069 const FileLine location;
00071 const std::string message;
00073 const sh_ptr<CException> nested;
00074
00081 friend sh_ptr<CException> newCException(const FileLine& loc,
00082 const std::string& msg, sh_ptr<CException> nest=sh_ptr<CException>());
00083
00087 virtual ~CException();
00088
00093 virtual std::string getClassName() const;
00094
00100 virtual std::string toString() const;
00101
00107 std::string toStringAll() const;
00108
00114 template<class E>
00115 E* is() { return dynamic_cast<E*>(this); }
00116
00122 template<class E>
00123 const E* is() const { return dynamic_cast<const E*>(this); }
00124
00129 void* operator new(size_t size)
00130 {
00131 return sized_alloc::alloc(size);
00132 }
00133
00138 void operator delete(void* ptr, size_t size)
00139 {
00140 sized_alloc::free(ptr, size);
00141 }
00142 };
00143
00148 class ExternalCException : public CException {
00149 protected:
00154 ExternalCException(const FileLine& loc, const std::string& msg,
00155 const std::string& tname, sh_ptr<CException> nest);
00156
00157 public:
00162 const std::string typeName;
00163
00170 friend sh_ptr<CException> newExternalCException(const FileLine& loc,
00171 const std::string& msg, const std::string& tname,
00172 sh_ptr<CException> nest=sh_ptr<CException>());
00173
00177 virtual ~ExternalCException();
00178
00183 virtual std::string getClassName() const;
00184
00190 virtual std::string toString() const;
00191 };
00192
00196 class STDExternalCException : public ExternalCException {
00197 protected:
00202 STDExternalCException(const FileLine& loc, const std::string& msg,
00203 const std::string& tname, sh_ptr<CException> nest);
00204
00205 public:
00212 friend sh_ptr<CException> newSTDExternalCException(const FileLine& loc,
00213 const std::string& msg, const std::string& tname,
00214 sh_ptr<CException> nest=sh_ptr<CException>());
00215
00219 virtual ~STDExternalCException();
00220
00225 virtual std::string getClassName() const;
00226 };
00227
00232 class UnknownExternalCException : public ExternalCException {
00233 protected:
00238 UnknownExternalCException(const FileLine& loc, const std::string& msg,
00239 const std::string& tname, sh_ptr<CException> nest);
00240
00241 public:
00248 friend sh_ptr<CException> newUnknownExternalCException(
00249 const FileLine& loc, const std::string& msg, const std::string& tname,
00250 sh_ptr<CException> nest=sh_ptr<CException>());
00251
00255 virtual ~UnknownExternalCException();
00256
00261 virtual std::string getClassName() const;
00262 };
00263
00273 sh_ptr<CException> toCException(const CException::FileLine& location);
00274
00278 #define _FLINE_ CException::FileLine(__FILE__, __LINE__)
00279
00280 #ifndef DERS_RETHROW_BUG
00281 inline sh_ptr<CException> newCException(const CException::FileLine& loc,
00282 const std::string& msg, sh_ptr<CException> nest)
00283 {
00284 return sh_ptr<CException>(new CException(loc, msg, nest));
00285 }
00286
00287 inline sh_ptr<CException> newExternalCException(const CException::FileLine& loc,
00288 const std::string& msg, const std::string& tname, sh_ptr<CException> nest)
00289 {
00290 return sh_ptr<CException>(new ExternalCException(loc, msg, tname, nest));
00291 }
00292
00293 inline sh_ptr<CException> newSTDExternalCException(
00294 const CException::FileLine& loc, const std::string& msg,
00295 const std::string& tname, sh_ptr<CException> nest)
00296 {
00297 return sh_ptr<CException>(new STDExternalCException(loc, msg, tname, nest));
00298 }
00299
00300 inline sh_ptr<CException> newUnknownExternalCException(
00301 const CException::FileLine& loc, const std::string& msg,
00302 const std::string& tname, sh_ptr<CException> nest)
00303 {
00304 return sh_ptr<CException>(new UnknownExternalCException(loc, msg, tname,
00305 nest));
00306 }
00307 #endif
00308
00309 #endif