00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef Exceptions_h
00023 #define Exceptions_h
00024
00025 #pragma warning( disable : 4786 )
00026 #pragma warning( disable : 4018 )
00027
00028 #include <StdAfx.h>
00029 #include <string>
00030 #include <vector>
00031 #include <comdef.h>
00032
00033
00034 template<class T>
00035 inline void COMCHECK2(const CComPtr<T>& p, const HRESULT hr)
00036 {
00037 if (!SUCCEEDED(hr)) {
00038 const IID piid = __uuidof(T);
00039 CComQIPtr<ISupportErrorInfo> supportErrorInfo = p;
00040 if (supportErrorInfo != NULL && supportErrorInfo->InterfaceSupportsErrorInfo(piid) == S_OK) {
00041 CComQIPtr<IErrorInfo> errorInfo;
00042 GetErrorInfo(0, &errorInfo);
00043
00044 _bstr_t bstr;
00045 errorInfo->GetDescription(bstr.GetAddress());
00046
00047 BON::Exception exception(hr, "?");
00048 exception << static_cast<const TCHAR*>(bstr);
00049 throw exception;
00050 } else {
00051 BON::Exception exception(hr);
00052 throw exception;
00053 }
00054 }
00055 }
00056
00057 template<class T>
00058 inline void COMCHECK2(T* p, const HRESULT hr) {
00059 COMCHECK2(CComPtr<T>(p), hr);
00060 }
00061
00062 #define ASSERTTHROW( exc ) \
00063 { \
00064 ASSERT( ( exc.getErrorMessage().c_str(), false ) ); \
00065 throw exc; \
00066 }
00067
00068 #define BONCOMTHROW( expr ) \
00069 { \
00070 HRESULT hResult = expr; \
00071 if ( FAILED( hResult ) ) \
00072 ASSERTTHROW( BON::Exception( hResult, "COMException" ) ); \
00073 }
00074
00075 namespace Util
00076 {
00077 class Exception;
00078
00079 typedef std::vector<std::string> StringVector;
00080
00081
00082
00083
00084
00085
00086
00087 class Exception
00088
00089 {
00090
00091 public :
00092 static const char PM_CHAR;
00093 static const char PM_STRING;
00094 static const char PM_INTEGER;
00095 static const char PM_LONG;
00096 static const char PM_FLOAT;
00097 static const char PM_DOUBLE;
00098
00099
00100 private :
00101 std::string m_strMessage;
00102 StringVector m_vecParameters;
00103
00104
00105 public :
00106 Exception();
00107
00108
00109 Exception( std::string strMessage, const StringVector& vecParemeters = StringVector() );
00110 Exception( const Exception& ex );
00111 virtual ~Exception() { }
00112
00113
00114 public :
00115 Exception& operator << ( char pchar);
00116 Exception& operator << ( int pint);
00117 Exception& operator << ( long plong);
00118 Exception& operator << ( const std::string& pstr);
00119 Exception& operator << ( float pfloat);
00120 Exception& operator << ( double pdoub);
00121 std::string getErrorMessage( bool bSubstitute = true ) const;
00122 std::string getParameter( int iPos ) const;
00123 size_t getParameterCount() const;
00124 virtual std::string getKind() const;
00125
00126 protected :
00127 void addParameter( const std::string& strParameter );
00128 };
00129
00130 };
00131
00132 namespace BON
00133 {
00134 class Exception;
00135
00136 typedef std::vector<std::string> StringVector;
00137
00138
00139
00140
00141
00142
00143
00144 class Exception
00145 : public Util::Exception
00146 {
00147
00148 private :
00149 HRESULT m_hResult;
00150
00151
00152 public :
00153 Exception( HRESULT hResult );
00154
00155 Exception( HRESULT hResult, std::string strMessage, const StringVector& vecParemeters = StringVector() );
00156
00157 Exception( std::string strMessage, const StringVector& vecParemeters = StringVector() );
00158 Exception( const Exception& ex );
00159 virtual ~Exception() { }
00160
00161
00162 public :
00163 HRESULT getHResult() const;
00164 std::string getKind() const;
00165 };
00166
00167 };
00168
00169 namespace MON
00170 {
00171 class Exception;
00172
00173 typedef std::vector<std::string> StringVector;
00174
00175
00176
00177
00178
00179
00180
00181 class Exception
00182 : public Util::Exception
00183 {
00184
00185 private :
00186 HRESULT m_hResult;
00187
00188
00189 public :
00190 Exception( HRESULT hResult );
00191
00192 Exception( HRESULT hResult, std::string strMessage, const StringVector& vecParemeters = StringVector() );
00193
00194 Exception( std::string strMessage, const StringVector& vecParemeters = StringVector() );
00195 Exception( const Exception& ex );
00196 virtual ~Exception() { }
00197
00198
00199 public :
00200 HRESULT getHResult() const;
00201 std::string getKind() const;
00202 };
00203
00204 };
00205
00206 #endif // Exceptions_h