exception.H

00001 /*
00002  * This file is part of the "Archon" framework.
00003  * (http://files3d.sourceforge.net)
00004  *
00005  * Copyright © 2002 by Kristian Spangsege and Brian Kristiansen.
00006  *
00007  * Permission to use, copy, modify, and distribute this software and
00008  * its documentation under the terms of the GNU General Public License is
00009  * hereby granted. No representations are made about the suitability of
00010  * this software for any purpose. It is provided "as is" without express
00011  * or implied warranty. See the GNU General Public License
00012  * (http://www.gnu.org/copyleft/gpl.html) for more details.
00013  *
00014  * The characters in this file are ISO8859-1 encoded.
00015  *
00016  * The documentation in this file is in "Doxygen" style
00017  * (http://www.doxygen.org).
00018  */
00019 
00020 #ifndef ARCHON_UTILITIES_EXCEPTION_H
00021 #define ARCHON_UTILITIES_EXCEPTION_H
00022 
00023 #include <typeinfo>
00024 #include <exception>
00025 #include <string>
00026 #include <iostream>
00027 
00028 #define ARCHON_THROW(e) \
00029 throw e(string(__FILE__)+":"+Archon::Utilities::Exception::line(__LINE__))
00030 
00031 #define ARCHON_THROW1(e, v) \
00032 throw e(string(__FILE__)+":"+Archon::Utilities::Exception::line(__LINE__), (v))
00033 
00034 #define ARCHON_THROW2(e, v, w) \
00035 throw e(string(__FILE__)+":"+Archon::Utilities::Exception::line(__LINE__), (v), (w))
00036 
00037 #define ARCHON_THROW3(e, v, w, x) \
00038 throw e(string(__FILE__)+":"+Archon::Utilities::Exception::line(__LINE__), (v), (w), (x))
00039 
00040 #define ARCHON_THROW4(e, v, w, x, y) \
00041 throw e(string(__FILE__)+":"+Archon::Utilities::Exception::line(__LINE__), (v), (w), (x), (y))
00042 
00043 #define ARCHON_THROW5(e, v, w, x, y, z) \
00044 throw e(string(__FILE__)+":"+Archon::Utilities::Exception::line(__LINE__), (v), (w), (x), (y), (z))
00045 
00052 namespace Archon
00053 {
00057   namespace Utilities
00058   {
00059     using namespace std;
00060 
00109     struct Exception: exception
00110     {
00111       virtual ~Exception() throw() {}
00112 
00113       string getLocation() { return location; }
00114 
00115       virtual string getMessage() const throw()
00116       {
00117         return message;
00118       }
00119 
00120       typedef string (*Formatter)
00121         (const type_info &type, string location, string message);
00122 
00126       typedef bool (*InfoCatcher)(Formatter, string &);
00127 
00172       template<InfoCatcher C> static void terminal()
00173       {
00174         string message;
00175         if(!(*C)(&Exception::explainLong, message))
00176           message = Exception::explainLongUnknown();
00177         cerr << message;
00178         abort();
00179       }
00180 
00189       static string explainLong(const type_info &,
00190                                 string location, string message);
00191 
00200       static string explainLongUnknown();
00201 
00209       static string explainShort(const type_info &,
00210                                  string location, string message);
00211 
00216       static string explainShortUnknown();
00217 
00218 
00222       static string line(long);
00223 
00224     protected:
00225       Exception() {}
00226       Exception(string location): location(location) {}
00227       Exception(string location, string message):
00228         location(location), message(message) {}
00229 
00230     private:
00231       string location;
00232       string message;
00233       string w;
00234 
00235       const char *what() const throw()
00236       {
00237         const_cast<Exception *>(this)->w = getMessage();
00238         return w.c_str();
00239       }
00240     };
00241 
00306     bool exceptionCatchInfo(Exception::Formatter format, string &result);
00307 
00340     struct UnexpectedException: Exception
00341     {
00342       UnexpectedException(string location, string message):
00343         Exception(location, message) {}
00344 
00345     protected:
00346       UnexpectedException() {}
00347       UnexpectedException(string location): Exception(location) {}
00348     };
00349 
00353     struct InternalException: UnexpectedException
00354     {
00355       InternalException(string location, string message):
00356         UnexpectedException(location, message) {}
00357     };
00358 
00365     struct ArgumentException: UnexpectedException
00366     {
00367       ArgumentException(string location, string message):
00368         UnexpectedException(location, message) {}
00369     };
00370 
00378     struct StateException: UnexpectedException
00379     {
00380       StateException(string location, string message):
00381         UnexpectedException(location, message) {}
00382     };
00383 
00390     struct ResourceException: UnexpectedException
00391     {
00392       ResourceException(string location, string message):
00393         UnexpectedException(location, message) {}
00394     };
00395 
00396 
00404     struct IOException: Exception
00405     {
00406       IOException(string location, string message):
00407         Exception(location, message) {}
00408 
00409     protected:
00410       IOException() {}
00411     };
00412 
00419     struct ParseException: virtual Exception
00420     {
00421       ParseException(string location, string message):
00422         Exception(location, message) {}
00423 
00424     protected:
00425       ParseException() {}
00426     };
00427   }
00428 }
00429 
00430 #endif // ARCHON_UTILITIES_EXCEPTION_H

Generated on Sun Jul 30 22:55:41 2006 for Archon by  doxygen 1.4.4