00001 #ifndef __VMMLIB__EXCEPTION__HPP__
00002 #define __VMMLIB__EXCEPTION__HPP__
00003
00004 #include <iostream>
00005 #include <stdexcept>
00006 #include <string>
00007 #include <sstream>
00008 #include <cassert>
00009
00010 #include <vmmlib/vmmlib_config.hpp>
00011
00012
00013 #define VMMLIB_HERE ( except_here( __FILE__, __LINE__ ) )
00014
00015 #ifdef VMMLIB_THROW_EXCEPTIONS
00016 #define VMMLIB_ERROR( desc, here ) throw( exception( desc, here ) )
00017 #else
00018 #define VMMLIB_ERROR( desc, here ) error_noexcept( desc, here )
00019 #endif
00020
00021
00022 namespace vmml
00023 {
00024
00025 struct except_here
00026 {
00027 except_here( const char* file_, int line_ ) : file( file_ ), line( line_ ) {}
00028 const char* file;
00029 int line;
00030 };
00031
00032
00033 inline void
00034 error_noexcept( const std::string& desc, const except_here& here )
00035 {
00036 std::cerr
00037 << "vmmlib error at " << here.file << ":" << here.line << "\n"
00038 << desc
00039 << std::endl;
00040 assert( 0 );
00041 }
00042
00043
00044
00045 class exception : public std::exception
00046 {
00047 public:
00048 exception( const std::string& desc, const except_here& here )
00049 : _description( desc )
00050 , _here( here )
00051 {}
00052
00053 virtual ~exception() throw() {}
00054
00055 virtual const char* what() const throw()
00056 {
00057 std::stringstream ss;
00058 ss
00059 << _here.file << "(" << _here.line << "): - "
00060 << _description
00061 << std::endl;
00062 return ss.str().c_str();
00063 }
00064
00065 protected:
00066 std::string _description;
00067 const except_here& _here;
00068
00069 private:
00070
00071 exception() : _here( *new except_here( "", 0 ) ){};
00072
00073 virtual const exception& operator=( const exception& e ){return *this;}
00074
00075
00076 };
00077
00078
00079 }
00080
00081 #endif