freExceptions.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   F.R.E.E. - flexible registration evaluation engine
00004   Version:   v.1.0.0
00005   Date:      $Date: 2006/09/01 12:00:00 $
00006   Module:    $RCSfile: freExceptions.h,v $
00007   Language:  C++
00008 
00009 
00010 
00011   Copyright (c) 2007 Ralf o Floca (Department of Medical Informatics,
00012   Institute for Medical Biometry and Informatics, University of Heidelberg,
00013   Germany). All rights reserved.
00014   See FREECopyright.txt or http://www.mi.med.uni-hd.de/free/copyright.htm
00015   for details.
00016 
00017      This software is distributed WITHOUT ANY WARRANTY; without even 
00018      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00019      PURPOSE.  See the above copyright notices for more information.
00020 
00021 =========================================================================*/
00022 #ifndef __freExceptions_h
00023 #define __freExceptions_h
00024 
00025 #include <exception>
00026 
00027 #include "freLogFileController.h"
00028 #include "freElementals.h"
00029 
00030 #include "itkExceptionObject.h"
00031 
00032 /* Only include CExceptions on systems which provide the MFC.
00033  * On all other systems their is no need (and possibility) to
00034  * take care of these exception classes by the CatchRestNPassMacro*/
00035 #if defined(_MFC_VER)
00036   #include "afx.h"
00037 #endif
00038 
00039 namespace FREE
00040 {
00041 
00044 extern LogFileController logException;
00045 
00063 class ITKCommon_EXPORT ExceptionBase : public itk::ExceptionObject
00064 {
00065 public:  
00066         
00067         itkTypeMacro(ExceptionBase, itk::ExceptionObject);
00068 
00069 
00073         virtual std::string GetFullLocation() const;
00074 
00079   const char* GetErrorCode() const 
00080   { 
00081                 return m_ErrorCode.c_str();
00082   };
00083 
00091   const std::string  GetExceptionTrack() const; 
00092 
00095   void AddTrack(const std::string&  sLocation); 
00096  
00101   virtual void Print(std::ostream& os, bool brief = false) const;
00102 
00104   ExceptionBase( const ExceptionBase& rExcBase );
00105 
00112         ExceptionBase(const std::string& sOriginCode,
00113           const std::string& sError = "unspecified error reason",
00114           const std::string& sLocation = "unknown location",
00115           const std::string&  sFile = "unspecified file",
00116           const unsigned int iLine = -1);
00117 
00118   virtual ~ExceptionBase() throw();
00119 
00120 protected:
00125   STLStringVector m_TrackedLocations;
00126         
00127         std::string m_ErrorCode;
00128 };
00129 
00130 
00138 class ITKCommon_EXPORT ControllerException : public ExceptionBase
00139 {
00140 public:  
00141         itkTypeMacro(ControllerException, ExceptionBase);
00142 
00147   virtual void Print(std::ostream& os, bool brief = false) const;
00148 
00150   ControllerException( const ControllerException& rExcBase );
00151 
00159   ControllerException(const std::string&  sOriginCode,
00160                                                     const std::string&  sControllerName = "unknown controller",
00161                                                     const std::string&  sError = "unspecified error reason",
00162                                                     const std::string&  sLocation = "unknown location",
00163                                                     const std::string&  sFile = "unspecified file",
00164                                                                                         const unsigned int iLine = -1);
00165 
00166   
00167   virtual ~ControllerException() throw();
00168 
00169 protected:
00170         std::string m_ControllerName;
00171 };
00172 
00173 
00182 class ITKCommon_EXPORT ItkException : public ExceptionBase
00183 {
00184 public:  
00185         itkTypeMacro(ItkException, ExceptionBase);
00186 
00190   const std::string  GetItkClassName() const { return m_ErrorCode; };
00191 
00193   ItkException(const ItkException& rExcBase);
00194 
00195         ItkException(const itk::ExceptionObject& rItkException, const std::string& sWrappingLocation = "");
00196 
00197   virtual ~ItkException() throw();
00198 };
00199 
00200 
00201 //----------------------------------------------------------------------------
00202 //----------------------------------------------------------------------------
00205 //----------------------------------------------------------------------------
00208 static void LogException( const std::string&  sOriginCode, const std::string&  sError = "unspecified error reason",
00209                                                   const std::string&  sMethod = "unspecified method", const std::string&  sLocation = "unspecified location")
00210 {
00211   std::string  sEntry = sOriginCode+" @ "+sMethod+" ("+sLocation+")"+": "+sError;
00212   logException.AddDirectEntry(sEntry);
00213 };
00214 
00215 //----------------------------------------------------------------------------
00218 static void LogControllerException( const std::string&  sOriginCode,
00219                                                                                                                                           const std::string&  sControllerName = "unknown controller",
00220                                                                                                                                                 const std::string&  sError = "unspecified error reason",
00221                                                                                                                                                 const std::string&  sMethod = "unspecified method",
00222                                                                                                                                                 const std::string&  sLocation = "unspecified location")
00223 {
00224   std::string  sEntry = sOriginCode+" @ "+sMethod+" ("+sLocation+")"+" : "+sControllerName+" : "+sError;
00225   logException.AddDirectEntry(sEntry);
00226 };
00227 
00228 //----------------------------------------------------------------------------
00229 //----------------------------------------------------------------------------
00232 //----------------------------------------------------------------------------
00234 #ifdef __FUNCSIG__
00235   #define FunctionNameMacro __FUNCSIG__
00236 #else
00237   #ifdef __PRETTY_FUNCTION__
00238     #define FunctionNameMacro __PRETTY_FUNCTION__
00239   #else
00240     #ifdef __func__
00241       #define FunctionNameMacro __func__
00242     #else
00243       #define FunctionNameMacro "unkown"
00244     #endif
00245   #endif
00246 #endif
00247 
00248 //----------------------------------------------------------------------------
00250         #define GetClassLocationMacro( sLocation ) \
00251     ::itk::OStringStream location; \
00252     location << this->GetNameOfClass() << "::" << FunctionNameMacro; \
00253                 sLocation = location.str();
00254 
00255         #define GetStaticLocationMacro( sLocation ) \
00256     ::itk::OStringStream location; \
00257     location << "static " << FunctionNameMacro; \
00258                 sLocation = location.str();
00259 
00260 //----------------------------------------------------------------------------
00262 #define catchItkNPassMacro() \
00263 catch (itk::ExceptionObject& e) \
00264 { \
00265         std::string sLocation; \
00266   GetClassLocationMacro( sLocation ); \
00267         FREE::LogException(e.GetNameOfClass(),e.what(),FunctionNameMacro,sLocation); \
00268   throw FREE::ItkException(e,sLocation); \
00269 }
00270 
00271 #define catchItkNPassStaticMacro() \
00272 catch (itk::ExceptionObject& e) \
00273 { \
00274         std::string sLocation; \
00275   GetStaticLocationMacro( sLocation ); \
00276         FREE::LogException(e.GetNameOfClass(),e.what(),FunctionNameMacro, sLocation); \
00277   throw FREE::ItkException(e,sLocation); \
00278 }
00279 //----------------------------------------------------------------------------
00281 #define catchFreeNPassMacro() \
00282 catch (FREE::ExceptionBase& e) \
00283 { \
00284         std::string sLocation; \
00285   GetClassLocationMacro( sLocation ); \
00286   e.AddTrack(sLocation); \
00287   FREE::logException.AddDirectEntry("Catched @ "+sLocation+" and rethrown"); \
00288   throw; \
00289 }
00290 
00291 #define catchFreeNPassStaticMacro() \
00292 catch (FREE::ExceptionBase& e) \
00293 { \
00294         std::string sLocation; \
00295   GetStaticLocationMacro( sLocation ); \
00296   e.AddTrack(sLocation); \
00297   FREE::logException.AddDirectEntry("Catched @ "+sLocation+" and rethrown"); \
00298   throw; \
00299 }
00300 //----------------------------------------------------------------------------
00302 #ifdef _MFC_VER //Catch CException only if MFC present
00303   #define catchRestNPassMacro( sError ) \
00304   catch (CException* e) \
00305   /*Rest of CException derivates*/ \
00306   { \
00307     TCHAR   szCause[512]; \
00308     std::string  sMfcError; \
00309     \
00310     e->GetErrorMessage(szCause,512); \
00311     sMfcError = szCause; \
00312     e->Delete(); \
00313     \
00314                 std::string sLocation; \
00315                 GetClassLocationMacro( sLocation ); \
00316                 \
00317     FREE::LogException("CException", sMfcError, sLocation); \
00318                 throw FREE::ExceptionBase( "CException", sMfcError, sLocation, __FILE__, __LINE__); \
00319   } \
00320   /*Char based exceptions */ \
00321   catch( char * str ) \
00322   { \
00323     std::string sE = str; \
00324                 std::string sLocation; \
00325                 GetClassLocationMacro( sLocation ); \
00326     FREE::LogException("Char based exception", sE, sLocation); \
00327                 throw FREE::ExceptionBase( "Char based exception", "Error: "+sE, sLocation, __FILE__, __LINE__); \
00328   } \
00329   /*Everything else */ \
00330   catch (...) \
00331   { \
00332                 std::string sLocation; \
00333                 GetClassLocationMacro( sLocation ); \
00334           ::itk::OStringStream message; \
00335                 message << sError; \
00336     FREE::LogException("Unknown exception", message.str(), sLocation); \
00337     throw FREE::ExceptionBase( "Unknown exception", message.str(), sLocation, __FILE__, __LINE__); \
00338   }
00339 #else //MFC-free version of the macro
00340   #define catchRestNPassMacro( sError ) \
00341   /*Char based exceptions */ \
00342   catch( char * str ) \
00343   { \
00344     std::string sE = "Error: "; \
00345                 sE = sE + str; \
00346                 std::string sLocation; \
00347                 GetClassLocationMacro( sLocation ); \
00348     FREE::LogException("Char based exception", sE, sLocation); \
00349                 throw FREE::ExceptionBase( "Char based exception", sE, sLocation, __FILE__, __LINE__); \
00350   } \
00351   /*Everything else */ \
00352   catch (...) \
00353   { \
00354                 std::string sLocation; \
00355                 GetClassLocationMacro( sLocation ); \
00356           ::itk::OStringStream message; \
00357                 message << sError; \
00358     FREE::LogException("Unknown exception", message.str(), sLocation); \
00359     throw FREE::ExceptionBase( "Unknown exception", message.str(), sLocation, __FILE__, __LINE__); \
00360   }
00361 #endif
00362 
00363 #ifdef _MFC_VER //Catch CException only if MFC present
00364   #define catchRestNPassStaticMacro( sError ) \
00365   catch (CException* e) \
00366   /*Rest of CException derivates*/ \
00367   { \
00368     TCHAR   szCause[512]; \
00369     std::string  sMfcError; \
00370     \
00371     e->GetErrorMessage(szCause,512); \
00372     sMfcError = szCause; \
00373     e->Delete(); \
00374     \
00375                 std::string sLocation; \
00376                 GetStaticLocationMacro( sLocation ); \
00377                 \
00378     FREE::LogException("CException", sMfcError, sLocation); \
00379                 throw FREE::ExceptionBase( "CException", sMfcError, sLocation, __FILE__, __LINE__); \
00380   } \
00381   /*Char based exceptions */ \
00382   catch( char * str ) \
00383   { \
00384     std::string sE = str; \
00385                 std::string sLocation; \
00386                 GetStaticLocationMacro( sLocation ); \
00387     FREE::LogException("Char based exception", sE, sLocation); \
00388                 throw FREE::ExceptionBase( "Char based exception", "Error: "+sE, sLocation, __FILE__, __LINE__); \
00389   } \
00390   /*Everything else */ \
00391   catch (...) \
00392   { \
00393                 std::string sLocation; \
00394                 GetStaticLocationMacro( sLocation ); \
00395           ::itk::OStringStream message; \
00396                 message << sError; \
00397     FREE::LogException("Unknown exception", message.str(), sLocation); \
00398     throw FREE::ExceptionBase( "Unknown exception", message.str(), sLocation, __FILE__, __LINE__); \
00399   }
00400 #else //MFC-free version of the macro
00401   #define catchRestNPassStaticMacro( sError ) \
00402   /*Char based exceptions */ \
00403   catch( char * str ) \
00404   { \
00405     std::string sE = "Error: "; \
00406                 sE = sE + str; \
00407                 std::string sLocation; \
00408                 GetStaticLocationMacro( sLocation ); \
00409     FREE::LogException("Char based exception", sE, sLocation); \
00410                 throw FREE::ExceptionBase( "Char based exception", sE, sLocation, __FILE__, __LINE__); \
00411   } \
00412   /*Everything else */ \
00413   catch (...) \
00414   { \
00415                 std::string sLocation; \
00416                 GetStaticLocationMacro( sLocation ); \
00417           ::itk::OStringStream message; \
00418                 message << sError; \
00419     FREE::LogException("Unknown exception", message.str(), sLocation); \
00420     throw FREE::ExceptionBase( "Unknown exception", message.str(), sLocation, __FILE__, __LINE__); \
00421   }
00422 #endif
00423 
00424 //----------------------------------------------------------------------------
00425 #define catchAllNPassMacro( sError ) \
00426 catchFreeNPassMacro() \
00427 catchItkNPassMacro() \
00428 catchRestNPassMacro(sError)
00429 
00430 #define catchAllNPassStaticMacro( sError ) \
00431 catchFreeNPassStaticMacro() \
00432 catchItkNPassStaticMacro() \
00433 catchRestNPassStaticMacro(sError)
00434 
00435 //----------------------------------------------------------------------------
00437 #define throwExceptionMacro( x ) \
00438 { \
00439         std::string sLocation; \
00440         GetClassLocationMacro( sLocation ); \
00441   ::itk::OStringStream message; \
00442   message << x; \
00443         FREE::LogException("FREE Exception", message.str(), sLocation); \
00444         throw FREE::ExceptionBase("FREE Exception", message.str(), sLocation, __FILE__, __LINE__); \
00445 } 
00446 
00447 #define throwStaticExceptionMacro( x ) \
00448 { \
00449         std::string sLocation; \
00450         GetStaticLocationMacro( sLocation ); \
00451   ::itk::OStringStream message; \
00452   message << x; \
00453         FREE::LogException("FREE Exception", message.str(), sLocation); \
00454         throw FREE::ExceptionBase("FREE Exception", message.str(), sLocation, __FILE__, __LINE__); \
00455 } 
00456 
00457 //----------------------------------------------------------------------------
00459 #define throwCtrlExceptionMacro(sOrigin, sError) \
00460 { \
00461         std::string sLocation; \
00462         GetClassLocationMacro( sLocation ); \
00463   ::itk::OStringStream message; \
00464   message << sError; \
00465         FREE::LogControllerException( sOrigin, this->ControllerID(), message.str(), sLocation); \
00466         throw FREE::ControllerException( sOrigin, this->ControllerID(), message.str(), sLocation, __FILE__, __LINE__); \
00467 }
00468 
00469 } //end of namespace free
00470 #endif

Generated at Sat Oct 13 15:54:41 2007 for f.r.e.e. - Flexible Registration and Evaluation Engine by doxygen 1.5.3 written by Dimitri van Heesch, © 1997-2000