Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

Exception.h

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

Generated on Sun Jan 23 02:09:10 2005 for libtagcoll by  doxygen 1.4.0