00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "stdafx.h"
00023 #include "Exceptions.h"
00024
00025 #include "stdarg.h"
00026
00027
00028 #define ADDPARAMETERS() \
00029 va_list vList; \
00030 va_start( vList, strFormat ); \
00031 for ( int i = 0 ; i < strFormat.size() ; i++ ) { \
00032 switch( strFormat[ i ] ) { \
00033 case Exception::PM_CHAR : { \
00034 addParameter( "" + va_arg( vList, char ) ); \
00035 break; \
00036 } \
00037 case Exception::PM_STRING : { \
00038 addParameter( std::string( va_arg( vList, char* ) ) ); \
00039 break; \
00040 } \
00041 case Exception::PM_INTEGER : { \
00042 char chBuffer[ 100 ]; \
00043 sprintf( chBuffer, "%d", va_arg( vList, int ) ); \
00044 addParameter( std::string( chBuffer ) ); \
00045 break; \
00046 } \
00047 case Exception::PM_LONG : { \
00048 char chBuffer[ 100 ]; \
00049 sprintf( chBuffer, "%d", va_arg( vList, long ) ); \
00050 addParameter( std::string( chBuffer ) ); \
00051 break; \
00052 } \
00053 case Exception::PM_FLOAT : { \
00054 char chBuffer[ 100 ]; \
00055 sprintf( chBuffer, "%f", va_arg( vList, float ) ); \
00056 addParameter( std::string( chBuffer ) ); \
00057 break; \
00058 } \
00059 case Exception::PM_DOUBLE : { \
00060 char chBuffer[ 100 ]; \
00061 sprintf( chBuffer, "%f", va_arg( vList, double ) ); \
00062 addParameter( std::string( chBuffer ) ); \
00063 break; \
00064 } \
00065 default : { \
00066 addParameter( "..." ); \
00067 } \
00068 } \
00069 } \
00070 va_end( vList ); \
00071
00072
00073 namespace Util
00074 {
00075 Exception& Exception::operator << ( char pch)
00076 {
00077 addParameter( std::string(1, pch));
00078 return *this;
00079 }
00080
00081 Exception& Exception::operator << ( int pint)
00082 {
00083 char chBuffer[ 100 ];
00084 sprintf_s( chBuffer, "%d", pint);
00085 addParameter( std::string( chBuffer ) );
00086 return *this;
00087 }
00088
00089 Exception& Exception::operator << ( long plong)
00090 {
00091 char chBuffer[ 100 ];
00092 sprintf_s( chBuffer, "%d", plong);
00093 addParameter( std::string( chBuffer ) );
00094 return *this;
00095 }
00096
00097 Exception& Exception::operator << ( const std::string& pstr)
00098 {
00099 addParameter( pstr );
00100 return *this;
00101 }
00102
00103 Exception& Exception::operator << ( float pfloat)
00104 {
00105 char chBuffer[ 100 ];
00106 sprintf_s( chBuffer, "%f", pfloat );
00107 addParameter( std::string( chBuffer ) );
00108 return *this;
00109 }
00110
00111 Exception& Exception::operator << ( double pdoub)
00112 {
00113 char chBuffer[ 100 ];
00114 sprintf_s( chBuffer, "%f", pdoub );
00115 addParameter( std::string( chBuffer ) );
00116 return *this;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125 const char Exception::PM_CHAR = 'c';
00126 const char Exception::PM_STRING = 's';
00127 const char Exception::PM_INTEGER = 'i';
00128 const char Exception::PM_LONG = 'l';
00129 const char Exception::PM_FLOAT = 'f';
00130 const char Exception::PM_DOUBLE = 'd';
00131
00132 Exception::Exception()
00133 {
00134 }
00135
00136
00137
00138
00139
00140
00141
00142 Exception::Exception( std::string strMessage, const StringVector& vecParameters )
00143 : m_strMessage( strMessage ), m_vecParameters( vecParameters )
00144 {
00145 }
00146
00147 Exception::Exception( const Exception& ex )
00148 : m_strMessage( ex.m_strMessage ), m_vecParameters( ex.m_vecParameters )
00149 {
00150 }
00151
00152 std::string Exception::getErrorMessage( bool bSubstitute ) const
00153 {
00154 if ( ! bSubstitute )
00155 return m_strMessage;
00156 else {
00157 std::string strMessage = m_strMessage;
00158 int iCnt = 0;
00159 size_t iPos = m_strMessage.find( '?' );
00160 while ( iPos != std::string::npos && iCnt < m_vecParameters.size() ) {
00161 strMessage = strMessage.substr( 0, iPos ) + m_vecParameters[ iCnt++ ] + strMessage.substr( iPos + 1, strMessage.length() - iPos - 1 );
00162 iPos = strMessage.find( '?' );
00163 }
00164 return strMessage;
00165 }
00166 }
00167
00168 std::string Exception::getParameter( int iPos ) const
00169 {
00170 return ( iPos < 0 || iPos >= m_vecParameters.size() ) ? "" : m_vecParameters[ iPos ];
00171 }
00172
00173 size_t Exception::getParameterCount() const
00174 {
00175 return m_vecParameters.size();
00176 }
00177
00178 void Exception::addParameter( const std::string& strParameter )
00179 {
00180 m_vecParameters.push_back( strParameter );
00181 }
00182
00183 std::string Exception::getKind() const
00184 {
00185 return "Util::Exception";
00186 }
00187
00188 };
00189
00190 namespace BON
00191 {
00192
00193
00194
00195
00196
00197
00198
00199 Exception::Exception( HRESULT hResult )
00200 : Util::Exception(), m_hResult( hResult )
00201 {
00202 }
00203
00204
00205
00206
00207
00208
00209
00210 Exception::Exception( HRESULT hResult, std::string strMessage, const StringVector& vecParameters )
00211 : Util::Exception( strMessage, vecParameters ), m_hResult( hResult )
00212 {
00213 }
00214
00215
00216
00217
00218
00219
00220
00221 Exception::Exception( std::string strMessage, const StringVector& vecParameters )
00222 : Util::Exception( strMessage, vecParameters ), m_hResult( E_FAIL )
00223 {
00224 }
00225
00226 Exception::Exception( const Exception& ex )
00227 : Util::Exception( ex ), m_hResult( ex.m_hResult )
00228 {
00229 }
00230
00231 HRESULT Exception::getHResult() const
00232 {
00233 return m_hResult;
00234 }
00235
00236 std::string Exception::getKind() const
00237 {
00238 return "BON::Exception";
00239 }
00240
00241 };
00242
00243 namespace MON
00244 {
00245
00246
00247
00248
00249
00250
00251
00252 Exception::Exception( HRESULT hResult )
00253 : Util::Exception(), m_hResult( hResult )
00254 {
00255 }
00256
00257
00258
00259
00260
00261
00262
00263 Exception::Exception( HRESULT hResult, std::string strMessage, const StringVector& vecParameters )
00264 : Util::Exception( strMessage, vecParameters ), m_hResult( hResult )
00265 {
00266 }
00267
00268
00269
00270
00271
00272
00273
00274 Exception::Exception( std::string strMessage, const StringVector& vecParameters )
00275 : Util::Exception( strMessage, vecParameters ), m_hResult( E_FAIL )
00276 {
00277 }
00278
00279 Exception::Exception( const Exception& ex )
00280 : Util::Exception( ex ), m_hResult( ex.m_hResult )
00281 {
00282 }
00283
00284 HRESULT Exception::getHResult() const
00285 {
00286 return m_hResult;
00287 }
00288
00289 std::string Exception::getKind() const
00290 {
00291 return "MON::Exception";
00292 }
00293
00294 };