00001 #ifndef EXCEPTION_H
00002 #define EXCEPTION_H
00003
00026 #include <exception>
00027 #include <string>
00028 #include <tagcoll/stringf.h>
00029
00030 namespace Tagcoll {
00031
00042 void DefaultUnexpected();
00043
00050 class InstallUnexpected
00051 {
00052 protected:
00053 void (*old)();
00054 public:
00055 InstallUnexpected(void (*func)() = DefaultUnexpected);
00056 ~InstallUnexpected();
00057 };
00058
00111 class Exception : public std::exception
00112 {
00113 public:
00114 Exception() throw () {}
00115 virtual ~Exception() throw () {}
00116
00118 virtual const char* type() const throw () { return "Exception"; }
00119
00121 virtual std::string desc() const throw () { return type(); }
00122
00124 virtual const char* what() const throw () { return desc().c_str(); }
00125 };
00126
00128 class ContextException : public Exception
00129 {
00130 protected:
00131 std::string _context;
00132
00133 public:
00138 ContextException(const std::string& context) throw () : _context(context) {};
00139 ~ContextException() throw () {}
00140
00141 virtual const char* type() const throw () { return "ContextException"; }
00142
00143 virtual std::string desc() const throw () { return _context; }
00144
00145 virtual std::string context() const throw () { return _context; }
00146 };
00147
00157 class InterruptedException : public ContextException
00158 {
00159 public:
00160 InterruptedException(const std::string& context) throw () :
00161 ContextException(context) {}
00162
00163 virtual const char* type() const throw ()
00164 {
00165 return "InterruptedException";
00166 }
00167 };
00168
00178 class WaitInterruptedException : public InterruptedException
00179 {
00180 public:
00181 WaitInterruptedException(const std::string& context) throw () :
00182 InterruptedException(context) {}
00183
00184 virtual const char* type() const throw ()
00185 {
00186 return "WaitInterruptedException";
00187 }
00188 };
00189
00195 class ConsistencyCheckException : public ContextException
00196 {
00197 public:
00198 ConsistencyCheckException(const std::string& context) throw () :
00199 ContextException(context) {}
00200
00201 virtual const char* type() const throw ()
00202 {
00203 return "ConsistencyCheckException";
00204 }
00205 };
00206
00207 class OutOfRangeException : public ConsistencyCheckException
00208 {
00209 protected:
00210 std::string _var_desc;
00211
00212 public:
00213 OutOfRangeException(const std::string& context, const std::string& var_desc) throw ()
00214 : ConsistencyCheckException(context), _var_desc(var_desc) {}
00215 ~OutOfRangeException() throw () {}
00216
00217 virtual const char* type() const throw ()
00218 {
00219 return "ConsistencyCheckException";
00220 }
00221
00223 virtual std::string var_desc() const throw () { return _var_desc; }
00224
00225 virtual std::string desc() const throw ()
00226 {
00227 return _var_desc + " out of range " + _context;
00228 }
00229 };
00230
00249 template <class C>
00250 class ValOutOfRangeException : public OutOfRangeException
00251 {
00252 protected:
00253 C _val;
00254 C _inf;
00255 C _sup;
00256
00257 public:
00261 ValOutOfRangeException(const std::string& context, const std::string& var_desc,
00262 C val, C inf, C sup) throw ()
00263 : OutOfRangeException(context, var_desc),
00264 _val(val), _inf(inf), _sup(sup) {}
00265
00267
00268
00269 virtual C val() const throw () { return _val; }
00271 virtual C inf() const throw () { return _inf; }
00273 virtual C sup() const throw () { return _sup; }
00275
00276 virtual const char* type() const throw ()
00277 {
00278 return "ValOutOfRangeException<>";
00279 }
00280
00281 virtual std::string desc() const throw ()
00282 {
00283 return _var_desc + "(" + stringf::fmt(_val) + ") out of range (" +
00284 stringf::fmt(_inf) + "-" + stringf::fmt(_sup) + ") " + _context;
00285 }
00286 };
00287
00307 class SystemException : public ContextException
00308 {
00309 protected:
00310 int _code;
00311
00312 public:
00313 SystemException(int code, const std::string& context) throw () :
00314 ContextException(context), _code(code) {}
00315
00316 virtual const char* type() const throw () { return "SystemException"; }
00317
00319 virtual int code() const throw () { return _code; }
00320
00322 virtual std::string system_desc() const throw ();
00323
00324 virtual std::string desc() const throw ()
00325 {
00326 return system_desc() + " " + _context;
00327 }
00328 };
00329
00337 class FileException : public SystemException
00338 {
00339 public:
00340 FileException(int code, const std::string& context) throw () :
00341 SystemException(code, context) {}
00342
00343 virtual const char* type() const throw () { return "FileException"; }
00344 };
00345
00346 };
00347
00348
00349 #endif