Exceptions.hpp

00001 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
00002 //
00003 //        This file is part of E-Cell Simulation Environment package
00004 //
00005 //                Copyright (C) 1996-2002 Keio University
00006 //
00007 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
00008 //
00009 //
00010 // E-Cell is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2 of the License, or (at your option) any later version.
00014 // 
00015 // E-Cell is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00018 // See the GNU General Public License for more details.
00019 // 
00020 // You should have received a copy of the GNU General Public
00021 // License along with E-Cell -- see the file COPYING.
00022 // If not, write to the Free Software Foundation, Inc.,
00023 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00024 // 
00025 //END_HEADER
00026 //
00027 // written by Koichi Takahashi <shafi@e-cell.org>,
00028 // E-Cell Project.
00029 //
00030 
00031 #ifndef __EXCEPTIONS_HPP
00032 #define __EXCEPTIONS_HPP
00033 #include <stdexcept>
00034 
00035 #include "Defs.hpp"
00036 
00037 
00038 namespace libecs
00039 {
00040 
00041   /** @defgroup exception The Exceptions
00042       The exceptions.
00043 
00044       @ingroup libecs
00045       @{
00046   */ 
00047   
00048 
00049   /// A macro to throw an exception, with method name
00050 #define THROW_EXCEPTION( CLASS, MESSAGE )\
00051 throw CLASS( __PRETTY_FUNCTION__, MESSAGE )
00052 
00053 #if defined( DEBUG ) 
00054 #define DEBUG_EXCEPTION( EXPRESSION, CLASS, MESSAGE )\
00055 if( ! ( EXPRESSION ) )\
00056 {\
00057   THROW_EXCEPTION( CLASS, "[ " + String( STR( EXPRESSION )  ) + " ]\n\t"\
00058                    + MESSAGE );\
00059 }
00060 #else
00061 #define DEBUG_EXCEPTION( EXPRESSION, CLASS, MESSAGE ) ;
00062 #endif
00063 
00064 
00065 
00066   /// Base exception class
00067   class Exception 
00068     : 
00069     public std::exception 
00070   {
00071 
00072   public:
00073 
00074     Exception( StringCref method, StringCref message = "" )
00075       : 
00076       theMethod( method ), 
00077       theMessage( message ) 
00078     {
00079       ; // do nothing
00080     }
00081 
00082     ECELL_API virtual ~Exception() throw();
00083 
00084     ECELL_API virtual const String message() const;
00085 
00086     virtual const char* what() const throw() { return message().c_str(); }
00087     virtual const char* const getClassName() const  { return "Exception"; }
00088 
00089   protected:
00090 
00091     const String theMethod;
00092     const String theMessage;
00093   };
00094 
00095   /**
00096 
00097   @internal
00098   */
00099 
00100 #define DEFINE_EXCEPTION( CLASSNAME, BASECLASS )\
00101 class CLASSNAME : public BASECLASS\
00102 {\
00103 public:\
00104   CLASSNAME( StringCref method, StringCref message = "" )\
00105     :  BASECLASS( method, message ) {}\
00106   virtual ~CLASSNAME() throw() {}\
00107   virtual StringLiteral getClassName() const\
00108     { return #CLASSNAME ; }\
00109 };\
00110 
00111 
00112   // system errors
00113   DEFINE_EXCEPTION( UnexpectedError,        Exception );
00114   DEFINE_EXCEPTION( NotFound,               Exception );
00115   DEFINE_EXCEPTION( CantOpen,               Exception ); 
00116   DEFINE_EXCEPTION( NotImplemented,         Exception ); 
00117 
00118 //    DEFINE_EXCEPTION( CallbackFailed,         Exception );
00119   DEFINE_EXCEPTION( AssertionFailed,        Exception );
00120   DEFINE_EXCEPTION( AlreadyExist,           Exception );
00121   DEFINE_EXCEPTION( ValueError,             Exception );
00122   DEFINE_EXCEPTION( TypeError,              Exception );
00123 
00124   // simulation errors
00125   DEFINE_EXCEPTION( SimulationError,        Exception );
00126   DEFINE_EXCEPTION( InitializationFailed,   SimulationError );
00127   DEFINE_EXCEPTION( RangeError,             SimulationError );
00128 
00129   // PropertySlot errors
00130   DEFINE_EXCEPTION( PropertyException,      Exception );
00131   DEFINE_EXCEPTION( NoSlot,                 PropertyException );
00132   DEFINE_EXCEPTION( AttributeError,         PropertyException );
00133 
00134   // FullID errors
00135   DEFINE_EXCEPTION( BadID,                  Exception ); 
00136   DEFINE_EXCEPTION( BadSystemPath,          BadID );
00137   DEFINE_EXCEPTION( InvalidEntityType,      BadID);
00138 
00139 
00140 /**
00141    This macro throws UnexpectedError exception with a method name.
00142 
00143    Use this macro to indicate where must not be reached.
00144 */
00145 
00146 #define NEVER_GET_HERE\
00147       THROW_EXCEPTION( UnexpectedError, \
00148                        "never get here (" + String( __PRETTY_FUNCTION__ )\
00149                        + ")." )
00150 
00151 
00152   /** @} */ //end of exception module
00153 
00154 } // namespace libecs
00155 
00156 #endif /* __EXCEPTIONS_HPP */
00157 
00158 
00159 /*
00160   Do not modify
00161   $Author: sachiboo $
00162   $Revision: 2619 $
00163   $Date: 2006-11-24 13:13:59 +0100 (Fri, 24 Nov 2006) $
00164   $Locker$
00165 */

Generated on Fri Aug 31 18:42:38 2007 for E-CELL C++ libraries (libecs and libemc) 3.1.105 by  doxygen 1.5.3