00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00027
00028
00030 #ifndef __EXCEPTION_H__
00031 #define __EXCEPTION_H__
00032
00033 #include <string>
00034 #include <iostream>
00035 #include "H3DApi.h"
00036 using namespace std;
00037
00038 namespace H3D {
00039 namespace Exception {
00040
00042 class H3DAPI_API H3DException {
00043 public:
00044
00048 H3DException( const string &_message = "" ) : message( _message ) {}
00049
00050 virtual ~H3DException() {};
00054 virtual void print( ostream &os ) const {
00055 os << "H3DException: " << message;
00056 }
00057
00059 string message;
00060 };
00061
00073 class H3DAPI_API H3DAPIException : public H3DException {
00074 public:
00090 H3DAPIException( const string &_message = "",
00091 const string &_function = "",
00092 const string &_filename = "",
00093 const int &_linenumber = -1 ) :
00094 H3DException( _message ),
00095 function(_function), filename(_filename), linenumber(_linenumber ) {}
00096
00097
00103 virtual string className() const {
00104 return "H3DAPIException";
00105 }
00106
00110 virtual void print( ostream &os ) const;
00111
00113 string function;
00114
00116 string filename;
00117
00119 int linenumber;
00120 };
00121
00136 template < class ValueType >
00137 class ValueException : public H3DAPIException {
00138 public:
00150 ValueException( ValueType _value,
00151 const string &_message = "",
00152 const string &_function = "",
00153 const string &_filename = "",
00154 const int &_linenumber = -1 ) :
00155 H3DAPIException( _message, _function, _filename, _linenumber ),
00156 value( _value ) {}
00157
00158
00164 virtual string className() const {
00165 return "ValueException";
00166 }
00167
00171 virtual void print( ostream &os ) const {
00172 os << className() << ": " << value << ".";
00173 if( message.length() ) {
00174 os << " " << message;
00175 }
00176 if( function.length() || filename.length() || linenumber != -1 ) {
00177 os << " (";
00178 if( function.length() ) os << "" << function;
00179 if( filename.length() ) os << " in " << filename;
00180 if( linenumber != -1 ) os << " line " << linenumber;
00181 os << ")";
00182 }
00183 }
00184
00186 ValueType value;
00187 };
00188
00192 class QuitAPI: public H3DException {};
00193
00199 inline ostream &operator<<( ostream& os, const H3DException &e ) {
00200 e.print( os );
00201 return os;
00202 }
00203
00204 }
00205
00210 #define H3D_FULL_LOCATION __FUNCTION__, __FILE__, __LINE__
00211
00216 #define H3D_FILE_LOCATION "", __FILE__, __LINE__
00217
00222 #define H3D_FUNCTION_LOCATION __FUNCTION__
00223
00228 #define H3D_VALUE_EXCEPTION( value_type, name ) \
00229 class name : public H3D::Exception::ValueException< value_type > { \
00230 public: \
00231 name( value_type _value, \
00232 const string &_message = "", \
00233 const string &_function = "", \
00234 const string &_filename = "", \
00235 const int &_linenumber = -1 ) : \
00236 H3D::Exception::ValueException< value_type >( _value, _message, \
00237 _function, \
00238 _filename, _linenumber) {} \
00239 protected: \
00240 string className() const { return #name; } \
00241 }
00242
00246 #define H3D_API_EXCEPTION( name ) \
00247 class name : public H3D::Exception::H3DAPIException { \
00248 public: \
00249 name( const string &_message = "", \
00250 const string &_function = "", \
00251 const string &_filename = "", \
00252 const int &_linenumber = -1 ) : \
00253 H3D::Exception::H3DAPIException( _message, _function, \
00254 _filename, _linenumber) {} \
00255 protected: \
00256 string className() const { return #name; } \
00257 }
00258
00259 }
00260
00261 #endif