00001 //--------------------------------------------------------------------------- 00002 #ifndef worm_exceptionsH 00003 #define worm_exceptionsH 00004 //--------------------------------------------------------------------------- 00005 #include <stdio.h> 00006 // microsoft pragma to avoid warnings from STL 00007 #pragma warning(disable:4786) 00008 #include <string> 00009 #include <string.h> 00010 #include <exception> 00011 00012 //--------------------------------------------------------------------------- 00013 class worm_exception : public std::exception 00014 { 00015 protected: 00016 std::string What; 00017 public: 00018 worm_exception( const char * p_message ) 00019 { 00020 if ( p_message == NULL ) 00021 { 00022 What = ""; 00023 } 00024 else 00025 { 00026 What.reserve( strlen(p_message) ); 00027 What = p_message; 00028 } 00029 } 00030 worm_exception( const std::string & p_message ) 00031 { 00032 What = p_message; 00033 } 00034 worm_exception operator= ( worm_exception & p_other ) 00035 { 00036 What = p_other.what(); 00037 return *this; 00038 } 00039 worm_exception operator= ( worm_exception * p_other ) 00040 { 00041 What = p_other->what(); 00042 return *this; 00043 } 00044 worm_exception operator+= ( const int p_value ) 00045 { 00046 char _i[15]; 00047 sprintf( _i , "%d" , p_value ); 00048 What.reserve( What.size() + strlen(_i) ); 00049 What += _i; 00050 return *this; 00051 } 00052 worm_exception operator+= ( const long p_value ) 00053 { 00054 char _i[18]; 00055 sprintf( _i , "%d" , p_value ); 00056 What.reserve( What.size() + strlen(_i) ); 00057 What += _i; 00058 return *this; 00059 } 00060 worm_exception operator+= ( const double p_value ) 00061 { 00062 char _f[15]; 00063 sprintf( _f , "%f" , p_value ); 00064 What.reserve( What.size() + strlen(_f) ); 00065 What += _f; 00066 return *this; 00067 } 00068 worm_exception operator+= ( const char * p_text ) 00069 { 00070 if ( p_text != NULL ) 00071 { 00072 What.reserve( What.size() + strlen(p_text) ); 00073 What += p_text; 00074 } 00075 return *this; 00076 } 00077 worm_exception operator+= ( const std::string p_text ) 00078 { 00079 What += p_text; 00080 return *this; 00081 } 00082 const char * what() const { return What.c_str(); } 00083 std::string what_string() const { return What; } 00084 }; 00085 //--------------------------------------------------------------------------- 00086 // 00087 // the main purpose for the following classes is to allow applications 00088 // to distinguish among and handle different conditions based on 00089 // exception class. 00090 // 00091 //--------------------------------------------------------------------------- 00092 class timeout_exception : public worm_exception 00093 { 00094 public: 00095 timeout_exception() : worm_exception( "timed out" ) { } 00096 timeout_exception( char * p_what ) : worm_exception( p_what ) { } 00097 timeout_exception( std::string & p_what ) : worm_exception( p_what ) { } 00098 }; 00099 00100 //--------------------------------------------------------------------------- 00101 class length_exception : public worm_exception 00102 { 00103 public: 00104 length_exception() : worm_exception( "length error" ) { } 00105 length_exception( char * p_what ) : worm_exception( p_what ) { } 00106 length_exception( std::string & p_what ) : worm_exception( p_what ) { } 00107 }; 00108 00109 //--------------------------------------------------------------------------- 00110 class cancel_exception : public worm_exception 00111 { 00112 public: 00113 cancel_exception() : worm_exception( "cancelled" ) { } 00114 cancel_exception( char * p_what ) : worm_exception( p_what ) { } 00115 cancel_exception( std::string & p_what ) : worm_exception( p_what ) { } 00116 }; 00117 00118 //--------------------------------------------------------------------------- 00119 00120 #endif