Exception.h

Go to the documentation of this file.
00001 
00002 //    Copyright 2004, SenseGraphics AB
00003 //
00004 //    This file is part of H3D API.
00005 //
00006 //    H3D API is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    H3D API is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with H3D API; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 //
00020 //    A commercial license is also available. Please contact us at 
00021 //    www.sensegraphics.com for more information.
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

Generated on Thu Aug 24 12:38:32 2006 for H3D API by  doxygen 1.4.5