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
00153 class NotFoundException : public ContextException
00154 {
00155 public:
00156 NotFoundException(const std::string& context) throw () :
00157 ContextException(context) {}
00158
00159 virtual const char* type() const throw ()
00160 {
00161 return "NotFoundException";
00162 }
00163 };
00164
00174 class InterruptedException : public ContextException
00175 {
00176 public:
00177 InterruptedException(const std::string& context) throw () :
00178 ContextException(context) {}
00179
00180 virtual const char* type() const throw ()
00181 {
00182 return "InterruptedException";
00183 }
00184 };
00185
00195 class WaitInterruptedException : public InterruptedException
00196 {
00197 public:
00198 WaitInterruptedException(const std::string& context) throw () :
00199 InterruptedException(context) {}
00200
00201 virtual const char* type() const throw ()
00202 {
00203 return "WaitInterruptedException";
00204 }
00205 };
00206
00212 class ConsistencyCheckException : public ContextException
00213 {
00214 public:
00215 ConsistencyCheckException(const std::string& context) throw () :
00216 ContextException(context) {}
00217
00218 virtual const char* type() const throw ()
00219 {
00220 return "ConsistencyCheckException";
00221 }
00222 };
00223
00224 class OutOfRangeException : public ConsistencyCheckException
00225 {
00226 protected:
00227 std::string _var_desc;
00228
00229 public:
00230 OutOfRangeException(const std::string& context, const std::string& var_desc) throw ()
00231 : ConsistencyCheckException(context), _var_desc(var_desc) {}
00232 ~OutOfRangeException() throw () {}
00233
00234 virtual const char* type() const throw ()
00235 {
00236 return "ConsistencyCheckException";
00237 }
00238
00240 virtual std::string var_desc() const throw () { return _var_desc; }
00241
00242 virtual std::string desc() const throw ()
00243 {
00244 return _var_desc + " out of range " + _context;
00245 }
00246 };
00247
00266 template <class C>
00267 class ValOutOfRangeException : public OutOfRangeException
00268 {
00269 protected:
00270 C _val;
00271 C _inf;
00272 C _sup;
00273
00274 public:
00278 ValOutOfRangeException(const std::string& context, const std::string& var_desc,
00279 C val, C inf, C sup) throw ()
00280 : OutOfRangeException(context, var_desc),
00281 _val(val), _inf(inf), _sup(sup) {}
00282
00284
00285
00286 virtual C val() const throw () { return _val; }
00288 virtual C inf() const throw () { return _inf; }
00290 virtual C sup() const throw () { return _sup; }
00292
00293 virtual const char* type() const throw ()
00294 {
00295 return "ValOutOfRangeException<>";
00296 }
00297
00298 virtual std::string desc() const throw ()
00299 {
00300 return _var_desc + "(" + stringf::fmt(_val) + ") out of range (" +
00301 stringf::fmt(_inf) + "-" + stringf::fmt(_sup) + ") " + _context;
00302 }
00303 };
00304
00324 class SystemException : public ContextException
00325 {
00326 protected:
00327 int _code;
00328
00329 public:
00330 SystemException(int code, const std::string& context) throw () :
00331 ContextException(context), _code(code) {}
00332
00333 virtual const char* type() const throw () { return "SystemException"; }
00334
00336 virtual int code() const throw () { return _code; }
00337
00339 virtual std::string system_desc() const throw ();
00340
00341 virtual std::string desc() const throw ()
00342 {
00343 return system_desc() + " " + _context;
00344 }
00345 };
00346
00354 class FileException : public SystemException
00355 {
00356 public:
00357 FileException(int code, const std::string& context) throw () :
00358 SystemException(code, context) {}
00359
00360 virtual const char* type() const throw () { return "FileException"; }
00361 };
00362
00363 };
00364
00365
00366 #endif