00001 //---------------------------------------------------------------------------- 00002 /** @file BenzeneException.hpp 00003 */ 00004 //---------------------------------------------------------------------------- 00005 00006 #ifndef BENZENEEXCEPTION_H 00007 #define BENZENEEXCEPTION_H 00008 00009 #include <string> 00010 #include <sstream> 00011 #include <exception> 00012 #include "Benzene.hpp" 00013 00014 _BEGIN_BENZENE_NAMESPACE_ 00015 00016 //---------------------------------------------------------------------------- 00017 00018 /** Base class for exceptions. 00019 Usage examples: 00020 @verbatim 00021 BenzeneException("Message"); 00022 BenzeneException() << "Message" << data << "more message."; 00023 @endverbatim 00024 */ 00025 class BenzeneException 00026 : public std::exception 00027 { 00028 public: 00029 /** Constructs an exception with no message. */ 00030 BenzeneException(); 00031 00032 /** Construct an exception with the given message. */ 00033 BenzeneException(const std::string& message); 00034 00035 /** Needed for operator<<. */ 00036 BenzeneException(const BenzeneException& other); 00037 00038 /** Destructor. */ 00039 virtual ~BenzeneException() throw(); 00040 00041 /** Returns the error message. */ 00042 const char* what() const throw(); 00043 00044 std::string Response() const; 00045 00046 std::ostream& Stream(); 00047 00048 private: 00049 std::ostringstream m_stream; 00050 00051 mutable std::string m_what; 00052 }; 00053 00054 inline std::ostream& BenzeneException::Stream() 00055 { 00056 return m_stream; 00057 } 00058 00059 inline std::string BenzeneException::Response() const 00060 { 00061 return m_stream.str(); 00062 } 00063 00064 //---------------------------------------------------------------------------- 00065 00066 /** @relates BenzeneException 00067 @note Returns a new object, see @ref BenzeneException 00068 */ 00069 template<typename TYPE> 00070 BenzeneException operator<<(const BenzeneException& except, const TYPE& type) 00071 { 00072 BenzeneException result(except); 00073 result.Stream() << type; 00074 return result; 00075 } 00076 00077 /** @relates BenzeneException 00078 @note Returns a new object, see @ref BenzeneException 00079 */ 00080 template<typename TYPE> 00081 BenzeneException operator<<(const BenzeneException& except, TYPE& type) 00082 { 00083 BenzeneException result(except); 00084 result.Stream() << type; 00085 return result; 00086 } 00087 00088 //---------------------------------------------------------------------------- 00089 00090 _END_BENZENE_NAMESPACE_ 00091 00092 #endif // BENZENEEXCEPTION_H