exception.hpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) Sergey P. Derevyago, 2008-2009.
00003  *
00004  * Permission to copy, use, modify, sell and distribute this software is granted
00005  * provided this copyright notice appears in all copies.
00006  * This software is provided "as is" without express or implied warranty, and
00007  * with no claim as to its suitability for any purpose.
00008  *
00009  */
00010 
00022 #ifndef __DERS__EXCEPTION_HPP__
00023 #define __DERS__EXCEPTION_HPP__
00024 
00025 #include <ders/config.hpp>
00026 #include <ders/text.hpp>
00027 
00031 #define _FLINE_ ders::FileLine(__FILE__, __LINE__)
00032 
00033 namespace ders {  // ::ders
00034 
00035 class Exception;
00036 typedef sh_ptr<Exception> shException;
00037 
00041 struct FileLine {
00042        const char* file;
00043        int line;
00044 
00045        FileLine(const char* f, int l) : file(f), line(l) {}
00046 };
00047 
00053 sh_text toTextAll(const shException& she);
00054 
00061 shException recatchException(mem_pool& mp, const FileLine& location);
00062 
00066 class Exception : public destroyable {
00067       friend shException newException(const FileLine& loc, const ch_rng& msg,
00068         shException nest);
00069 
00070       Exception(const Exception&);
00071       Exception& operator=(const Exception&);
00072 
00073  protected:
00074       Exception(const FileLine& loc, const ch_rng& msg, shException nest);
00075 
00076  public:
00077       const FileLine location;
00078       const sh_text message;
00079       const shException nested;
00080 
00081       virtual sh_text getClassName() const;
00082       virtual sh_text toText() const;
00083 
00084       template<class E> E* is();
00085       template<class E> const E* is() const;
00086 
00087       virtual void destroy(mem_pool& mp2) { destroy_this(this, mp2); }
00088 };
00089 
00090 shException newException(mem_pool& mp, const FileLine& loc, const ch_rng& msg);
00091 shException newException(const FileLine& loc, const ch_rng& msg, shException
00092   nest);
00093 
00098 class MsgException : public Exception {
00099       MsgException(const MsgException&);
00100       MsgException& operator=(const MsgException&);
00101 
00102  protected:
00103       MsgException(const FileLine& loc, const ch_rng& msg, shException nest);
00104 
00105  public:
00106       virtual sh_text getClassName() const=0;
00107 };
00108 
00112 class ExitMsgException : public MsgException {
00113       friend shException newExitMsgException(const FileLine& loc, const ch_rng&
00114         msg, int ecode, shException nest);
00115 
00116       ExitMsgException(const ExitMsgException&);
00117       ExitMsgException& operator=(const ExitMsgException&);
00118 
00119  protected:
00120       ExitMsgException(const FileLine& loc, const ch_rng& msg, int ecode,
00121         shException nest);
00122 
00123  public:
00124       const int exitCode;
00125 
00126       virtual sh_text getClassName() const;
00127       virtual sh_text toText() const;
00128 
00129       virtual void destroy(mem_pool& mp2) { destroy_this(this, mp2); }
00130 };
00131 
00132 shException newExitMsgException(mem_pool& mp, const FileLine& loc, const ch_rng&
00133   msg, int ecode);
00134 shException newExitMsgException(const FileLine& loc, const ch_rng& msg, int
00135   ecode, shException nest);
00136 
00140 class ExternException : public Exception {
00141       ExternException(const ExternException&);
00142       ExternException& operator=(const ExternException&);
00143 
00144  protected:
00145       ExternException(const FileLine& loc, const ch_rng& msg, const ch_rng&
00146         tname, shException nest);
00147 
00148  public:
00149       const sh_text typeName;
00150 
00151       virtual sh_text getClassName() const=0;
00152       virtual sh_text toText() const;
00153 };
00154 
00158 class StdExternException : public ExternException {
00159       friend shException newStdExternException(const FileLine& loc, const
00160         ch_rng& msg, const ch_rng& tname, shException nest);
00161 
00162       StdExternException(const StdExternException&);
00163       StdExternException& operator=(const StdExternException&);
00164 
00165  protected:
00166       StdExternException(const FileLine& loc, const ch_rng& msg, const ch_rng&
00167         tname, shException nest);
00168 
00169  public:
00170       virtual sh_text getClassName() const;
00171 
00172       virtual void destroy(mem_pool& mp2) { destroy_this(this, mp2); }
00173 };
00174 
00175 shException newStdExternException(mem_pool& mp, const FileLine& loc, const
00176   ch_rng& msg, const ch_rng& tname);
00177 shException newStdExternException(const FileLine& loc, const ch_rng& msg, const
00178   ch_rng& tname, shException nest);
00179 
00183 class UnknExternException : public ExternException {
00184       friend shException newUnknExternException(const FileLine& loc, const
00185         ch_rng& msg, shException nest);
00186 
00187       UnknExternException(const UnknExternException&);
00188       UnknExternException& operator=(const UnknExternException&);
00189 
00190  protected:
00191       UnknExternException(const FileLine& loc, const ch_rng& msg, shException
00192         nest);
00193 
00194  public:
00195       virtual sh_text getClassName() const;
00196 
00197       virtual void destroy(mem_pool& mp2) { destroy_this(this, mp2); }
00198 };
00199 
00200 shException newUnknExternException(mem_pool& mp, const FileLine& loc, const
00201   ch_rng& msg);
00202 shException newUnknExternException(const FileLine& loc, const ch_rng& msg,
00203   shException nest);
00204 
00205 template<class E> inline E* Exception::is()
00206 { return dynamic_cast<E*>(this); }
00207 
00208 template<class E> inline const E* Exception::is() const
00209 { return dynamic_cast<const E*>(this); }
00210 
00211 }  // namespace ::ders
00212 
00213 #endif  // __DERS__EXCEPTION_HPP__
00214 

Generated on Tue Dec 8 11:35:32 2009 for derslib by  doxygen 1.5.5