dlrCommon/exception.h

Go to the documentation of this file.
00001 
00015 #ifndef _DLR_COMMON_EXCEPTION_H_
00016 #define _DLR_COMMON_EXCEPTION_H_
00017 
00018 #include <cstring>
00019 #include <exception>
00020 
00021 // We use preprocessor macros to throw exceptions so that __LINE__
00022 // and __FILE__ will report the correct line number and filename when
00023 // the exception is thrown.
00024 
00025 // Define _DLRCOMMON_VOCAL_EXCEPTIONS_ for debugging purposes if you'd
00026 // like to print a message to stderr each time one of the DLR_THROW
00027 // macros is called.
00028 #ifdef _DLRCOMMON_VOCAL_EXCEPTIONS_
00029 
00030 #include <iostream>
00031 
00049 #define DLR_THROW(exception, functionName, message) { \
00050   exception exceptionInstance(message, functionName, __FILE__, __LINE__); \
00051   std::cerr << "DLR_THROW: " << exceptionInstance.what() << std::endl; \
00052   throw exceptionInstance; \
00053   }
00054 
00055 
00070 #define DLR_THROW2(exception, message) { \
00071   exception exceptionInstance(message, __FILE__, __LINE__); \
00072   std::cerr << "DLR_THROW2: " << exceptionInstance.what() << std::endl; \
00073   throw exceptionInstance; \
00074   }
00075 
00076 
00077 
00094 #define DLR_THROW3(exception, functionName, message) { \
00095   exception exceptionInstance(message, functionName, __FILE__, __LINE__); \
00096   std::cerr << "DLR_THROW3: " << exceptionInstance.what() << std::endl; \
00097   throw exceptionInstance; \
00098   }
00099 
00100 
00101 #else // #ifdef _DLRCOMMON_VOCAL_EXCEPTIONS_
00102 
00103 
00121 #define DLR_THROW(exception, functionName, message) { \
00122   throw exception(message, functionName, __FILE__, __LINE__); \
00123   }
00124 
00125 
00140 #define DLR_THROW2(exception, message) { \
00141   throw exception(message, __FILE__, __LINE__); \
00142   }
00143 
00144 
00161 #define DLR_THROW3(exception, functionName, message) { \
00162   throw exception(message, functionName, __FILE__, __LINE__); \
00163   }
00164 
00165 
00166 #endif // #ifdef _DLRCOMMON_VOCAL_EXCEPTIONS_
00167 
00168 
00174 #define DLR_EXCEPTION_MESSAGE_LENGTH 512
00175 
00176 
00189 #ifndef DLR_EXCEPTION_TRACE_MESSAGE_LENGTH
00190 #define DLR_EXCEPTION_TRACE_MESSAGE_LENGTH 512
00191 #endif
00192 
00204 #ifndef DLR_EXCEPTION_TRACE_REQUIRED_STACK_LEVELS
00205 #define DLR_EXCEPTION_TRACE_REQUIRED_STACK_LEVELS 16
00206 #endif
00207 
00208 
00235 #define DLR_DECLARE_EXCEPTION_TYPE(ExceptionName, ParentName) \
00236   class ExceptionName : public ParentName { \
00237   public: \
00238     ExceptionName() throw() \
00239       : ParentName("", #ExceptionName) {} \
00240     \
00241     ExceptionName(const char* message) throw() \
00242       : ParentName(message, #ExceptionName) {} \
00243     \
00244     ExceptionName(const char* message, const char* fileName, \
00245                   int lineNumber) throw() \
00246       : ParentName(message, #ExceptionName, 0, fileName, lineNumber) {} \
00247     \
00248     ExceptionName(const char* message, const char* functionName, \
00249                   const char* fileName, int lineNumber) throw() \
00250       : ParentName(message, #ExceptionName, functionName, fileName, \
00251                    lineNumber) {} \
00252     \
00253     ExceptionName(const ExceptionName& source) throw() \
00254       : ParentName(source) {} \
00255     \
00256     virtual ~ExceptionName() throw() {} \
00257     \
00258   protected: \
00259     ExceptionName(const char* message, const char* childClassName) throw() \
00260       : ParentName(message, childClassName) {} \
00261     \
00262     ExceptionName(const char* message, const char* childClassName, \
00263                   const char* functionName, const char* fileName, \
00264                   int lineNumber) throw() \
00265       : ParentName(message, childClassName, functionName, fileName, \
00266                    lineNumber) {} \
00267   }
00268 
00269 
00276 namespace dlr {
00277 
00284   namespace common {
00285     
00316     class Exception : public std::exception {
00317     public:
00318 
00328       Exception(const char* message) throw();
00329 
00330 
00344       Exception(const char* message, const char* fileName,
00345                 int lineNumber) throw();
00346 
00347 
00364       Exception(const char* message, const char* functionName,
00365                 const char* fileName, int lineNumber) throw();
00366 
00367 
00373       Exception(const Exception& source) throw();
00374 
00375 
00379       virtual ~Exception() throw() {}
00380     
00381 
00392       virtual void addTrace(const char* message) throw();
00393 
00394     
00404       virtual const char* trace() const throw() {return m_traceMessage;}
00405 
00406 
00416       virtual const char* what() const throw() {return m_message;}
00417 
00418 
00427       virtual Exception& operator=(const Exception& source) throw();
00428 
00429     protected:
00460       Exception(const char* message, const char* childClassName) throw();
00461 
00462 
00491       Exception(const char* message, const char* childClassName,
00492                 const char* functionName, const char* fileName,
00493                 int lineNumber) throw();
00494 
00495 
00501       char m_message[DLR_EXCEPTION_MESSAGE_LENGTH];
00502 
00503     
00508       char m_traceMessage[DLR_EXCEPTION_TRACE_REQUIRED_STACK_LEVELS
00509                           * DLR_EXCEPTION_TRACE_MESSAGE_LENGTH];
00510 
00511 
00516       size_t m_traceMessageIndex;
00517     
00518     };
00519 
00520   
00526     class IOException : public Exception {
00527     public:
00528       IOException() throw()
00529         : Exception("", "IOException") {}
00530 
00531       IOException(const char* message) throw()
00532         : Exception(message, "IOException") {}
00533 
00534       IOException(const char* message, const char* fileName,
00535                   int lineNumber) throw()
00536         : Exception(message, "IOException", 0, fileName, lineNumber) {}
00537 
00538       IOException(const char* message, const char* functionName,
00539                   const char* fileName, int lineNumber) throw()
00540         : Exception(message, "IOException", functionName, fileName,
00541                     lineNumber) {}
00542 
00543       IOException(const IOException& source) throw()
00544         : Exception(source) {}
00545 
00546       virtual ~IOException() throw() {}
00547     
00548     protected:
00549       IOException(const char* message, const char* childClassName) throw()
00550         : Exception(message, childClassName) {}
00551     
00552       IOException(const char* message, const char* childClassName,
00553                   const char* functionName, const char* fileName,
00554                   int lineNumber) throw()
00555         : Exception(message, childClassName, functionName, fileName,
00556                     lineNumber) {}
00557     };
00558 
00559   
00564     DLR_DECLARE_EXCEPTION_TYPE(IndexException, Exception);
00565 
00566   
00571     DLR_DECLARE_EXCEPTION_TYPE(LogicException, Exception);
00572 
00573   
00579     DLR_DECLARE_EXCEPTION_TYPE(NotImplementedException, Exception);
00580 
00581   
00587     DLR_DECLARE_EXCEPTION_TYPE(RunTimeException, Exception);
00588 
00589   
00595     DLR_DECLARE_EXCEPTION_TYPE(StateException, Exception);
00596 
00597   
00603     DLR_DECLARE_EXCEPTION_TYPE(ValueException, Exception);
00604 
00605   } // namespace common
00606   
00607 } // namespace dlr
00608 
00609 
00610 /* ======= Declarations to maintain compatibility with legacy code. ======= */
00611 
00612 namespace dlr {
00613 
00614   using common::Exception;
00615   using common::IOException;
00616   using common::IndexException;
00617   using common::LogicException;
00618   using common::NotImplementedException;
00619   using common::RunTimeException;
00620   using common::StateException;
00621   using common::ValueException;
00622   
00623 } // namespace dlr
00624 
00625 #endif // #ifndef _DLR_COMMON_EXCEPTION_H_

Generated on Mon Jul 9 20:34:02 2007 for dlrLibs Utility Libraries by  doxygen 1.5.2