Exception.h

Go to the documentation of this file.
00001 #ifndef EXCEPTION_H
00002 #define EXCEPTION_H
00003 
00004 /*
00005  * Generic base exception hierarchy
00006  *
00007  * Copyright (C) 2003  Enrico Zini <enrico@debian.org>
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  * This library is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with this library; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00022  */
00023 
00024 #include <string>
00025 #include <buffy/stringf.h>
00026 
00074 
00075 
00083 void DefaultUnexpected();
00084 
00087 class InstallUnexpected
00088 {
00089 protected:  
00090     void (*old)();
00091 public:
00092     InstallUnexpected(void (*func)() = DefaultUnexpected);
00093     ~InstallUnexpected();
00094 };
00095 
00097 
00103 class Exception
00104 {
00105 public:
00106     Exception() throw () {}
00107     virtual ~Exception() throw () {}
00108     Exception(const Exception& e) throw () {}
00109 
00111     virtual const char* type() const throw () { return "Exception"; }
00112 
00114     virtual std::string desc() const throw () { return type(); }
00115 };
00116 
00118 class ContextException : public Exception
00119 {
00120 protected:
00121     std::string _context;
00122 
00123 public:
00128     ContextException(const std::string& context) throw () : _context(context) {};
00129     ~ContextException() throw () {}
00130 
00131     virtual const char* type() const throw () { return "ContextException"; }
00132 
00133     virtual std::string desc() const throw () { return _context; }
00134 
00135     virtual std::string context() const throw () { return _context; }
00136 };
00137 
00140 
00146 class InterruptedException : public ContextException
00147 {
00148 public:
00149     InterruptedException(const std::string& context) throw () :
00150         ContextException(context) {}
00151 
00152     virtual const char* type() const throw ()
00153     {
00154         return "InterruptedException";
00155     }
00156 };
00157 
00159 
00166 class WaitInterruptedException : public InterruptedException
00167 {
00168 public:
00169     WaitInterruptedException(const std::string& context) throw () :
00170         InterruptedException(context) {}
00171 
00172     virtual const char* type() const throw ()
00173     {
00174         return "WaitInterruptedException";
00175     }
00176 };
00177 
00179 
00182 class ConsistencyCheckException : public ContextException
00183 {
00184 public:
00185     ConsistencyCheckException(const std::string& context) throw () :
00186         ContextException(context) {}
00187 
00188     virtual const char* type() const throw ()
00189     {
00190         return "ConsistencyCheckException";
00191     }
00192 };
00193 
00194 class OutOfRangeException : public ConsistencyCheckException
00195 {
00196 protected:
00197     std::string _var_desc;
00198 
00199 public:
00200     OutOfRangeException(const std::string& context, const std::string& var_desc) throw ()
00201         : ConsistencyCheckException(context), _var_desc(var_desc) {}
00202     ~OutOfRangeException() throw () {}
00203 
00204     virtual const char* type() const throw ()
00205     {
00206         return "ConsistencyCheckException";
00207     }
00208 
00210     virtual std::string var_desc() const throw () { return _var_desc; }
00211 
00212     virtual std::string desc() const throw ()
00213     {
00214         return _var_desc + " out of range " + _context;
00215     }
00216 };
00217 
00219 
00235 template <class C>
00236 class ValOutOfRangeException : public OutOfRangeException
00237 {
00238 protected:
00239     C _val;
00240     C _inf;
00241     C _sup;
00242 
00243 public:
00247     ValOutOfRangeException(const std::string& context, const std::string& var_desc,
00248                         C val, C inf, C sup) throw ()
00249                     : OutOfRangeException(context, var_desc),
00250                         _val(val), _inf(inf), _sup(sup) {}
00251     
00253 
00254 
00255     virtual C val() const throw () { return _val; }
00257     virtual C inf() const throw () { return _inf; }
00259     virtual C sup() const throw () { return _sup; }
00261 
00262     virtual const char* type() const throw ()
00263     {
00264         return "ValOutOfRangeException<>";
00265     }
00266 
00267     virtual std::string desc() const throw ()
00268     {
00269         return _var_desc + "(" + stringf::fmt(_val) + ") out of range (" +
00270                 stringf::fmt(_inf) + "-" + stringf::fmt(_sup) + ") " + _context;
00271     }
00272 };
00273 
00275 
00292 class SystemException : public ContextException
00293 {
00294 protected:
00295     int _code;
00296 
00297 public:
00298     SystemException(int code, const std::string& context) throw () :
00299         ContextException(context), _code(code) {}
00300 
00301     virtual const char* type() const throw () { return "SystemException"; }
00302 
00304     virtual int code() const throw () { return _code; }
00305 
00307     virtual std::string system_desc() const throw ();
00308 
00309     virtual std::string desc() const throw ()
00310     {
00311         return system_desc() + " " + _context;
00312     }
00313 };
00314 
00316 
00321 class FileException : public SystemException
00322 {
00323 public:
00324     FileException(int code, const std::string& context) throw () :
00325         SystemException(code, context) {}
00326 
00327     virtual const char* type() const throw () { return "FileException"; }
00328 };
00329 
00330 // vim:set ts=4 sw=4:
00331 #endif

Generated on Wed Nov 30 18:18:30 2005 for libbuffy by  doxygen 1.4.5