00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef Utils_h
00023 #define Utils_h
00024
00025 #pragma warning( disable : 4786 )
00026
00027 #include "StdAfx.h"
00028 #include <string>
00029 #include <vector>
00030 #include <set>
00031 #include "Exceptions.h"
00032
00033 namespace Util
00034 {
00035 template< class T > class ComPtr;
00036 class GenRefCounted;
00037 class GenRefCounter;
00038 class Variant;
00039
00040
00041
00042
00043
00044
00045
00046 CComBSTR Copy( const std::string& str );
00047 std::string Copy( const CComBSTR& bstr );
00048 std::wstring CopyW( const CComBSTR& bstr );
00049
00050
00051
00052
00053
00054
00055
00056 enum InfoOption
00057 {
00058 IO_None = 0x00,
00059 IO_Identifiers = 0x01,
00060 IO_ID = 0x02,
00061 IO_Meta = 0x04,
00062 IO_Path = 0x08,
00063 IO_Specific = 0x10,
00064 IO_NewLine = 0x20,
00065 IO_All = 0xFF
00066 };
00067
00068 typedef unsigned short InfoOptions;
00069
00070
00071
00072
00073
00074
00075
00076 template< class T >
00077 class ComPtr
00078 {
00079
00080 public :
00081 T* p;
00082
00083
00084 public :
00085 ComPtr()
00086 {
00087 p = NULL;
00088 }
00089
00090 ComPtr( T* q )
00091 {
00092 if ( ( p = q ) != NULL )
00093 q->AddRef();
00094 }
00095
00096 ComPtr( const ComPtr<T>& q )
00097 {
00098 if ( ( p = q.p ) != NULL )
00099 p->AddRef();
00100 }
00101
00102 ComPtr( const CComPtr<T>& q )
00103 {
00104 if ( ( p = q.p ) != NULL )
00105 p->AddRef();
00106 }
00107
00108 ~ComPtr()
00109 {
00110 Release();
00111 }
00112
00113 void Release()
00114 {
00115 if ( p ) {
00116 p->Release();
00117 p = NULL;
00118 }
00119 }
00120
00121
00122 public :
00123 operator CComPtr<T> () const
00124 {
00125 return CComPtr<T>( p );
00126 }
00127
00128 operator T* () const
00129 {
00130 return p;
00131 }
00132
00133 operator bool () const
00134 {
00135 return p != NULL;
00136 }
00137
00138 bool operator ! () const
00139 {
00140 return p == NULL;
00141 }
00142
00143 T& operator * () const
00144 {
00145 ASSERT( p != NULL );
00146 return *p;
00147 }
00148
00149 T* operator -> () const
00150 {
00151 ASSERT( p != NULL );
00152 return p;
00153 }
00154
00155 T* operator = ( T* q )
00156 {
00157 if ( q )
00158 q->AddRef();
00159 if ( p )
00160 p->Release();
00161 return p = q;
00162 }
00163
00164 T* operator = ( const ComPtr<T>& q )
00165 {
00166 return operator=( q.p );
00167 }
00168
00169 T* operator = ( const CComPtr<T>& q )
00170 {
00171 return operator=( q.p );
00172 }
00173
00174 bool operator == ( T* q ) const
00175 {
00176 return p == q;
00177 }
00178
00179 bool operator != ( T* q ) const
00180 {
00181 return p != q;
00182 }
00183
00184 bool operator < ( T* q ) const
00185 {
00186 return p < q;
00187 }
00188
00189
00190 public :
00191 T** Addr()
00192 {
00193
00194 return &p;
00195 }
00196
00197 T* Detach()
00198 {
00199 T* r = p;
00200 p = NULL;
00201 return r;
00202 }
00203
00204 void Attach( T* q )
00205 {
00206 if ( p )
00207 p->Release();
00208 p = q;
00209 }
00210
00211
00212 public :
00213 template<class Q> HRESULT QueryInterface( ComPtr<Q> &q ) const
00214 {
00215 ASSERT( p != NULL && q == NULL );
00216 return p->QueryInterface( __uuidof( Q ), (void**) &q.p );
00217 }
00218
00219 HRESULT CoCreateInstance( REFCLSID rclsid, LPUNKNOWN pUnkOuter = NULL, DWORD dwClsContext = CLSCTX_ALL )
00220 {
00221 ASSERT( p == NULL );
00222 return ::CoCreateInstance( rclsid, pUnkOuter, dwClsContext, __uuidof( T ), (void**) &p );
00223 }
00224
00225 HRESULT CoCreateInstance( LPCOLESTR szProgID, LPUNKNOWN pUnkOuter = NULL, DWORD dwClsContext = CLSCTX_ALL )
00226 {
00227 CLSID clsid;
00228 HRESULT hr = CLSIDFromProgID( szProgID, &clsid );
00229 ASSERT( p == NULL );
00230 if ( SUCCEEDED( hr ) )
00231 hr = ::CoCreateInstance( clsid, pUnkOuter, dwClsContext, __uuidof( T ), (void**) &p );
00232 return hr;
00233 }
00234 };
00235
00236 template<class T>
00237 inline void COMCHECK2(const ComPtr<T>& p, const HRESULT hr) {
00238 COMCHECK2(p.p, hr);
00239 }
00240
00241
00242
00243
00244 template < class t >
00245 class SmartMultiPtr {
00246 CComPtr< t > *m_ptr;
00247 public:
00248 SmartMultiPtr(CComPtr< t > *x) : m_ptr(x) { ; }
00249 ~SmartMultiPtr() { delete[] m_ptr; }
00250 operator CComPtr< t > *() { return m_ptr; }
00251 t **operator &() { return &(m_ptr[0]); }
00252 };
00253
00254 #define MGACOLL_ITERATE(iftype, collifptr) \
00255 { \
00256 ASSERT( collifptr != NULL ); \
00257 long iter_count = 0; \
00258 COMTHROW( collifptr->get_Count(&iter_count) ); \
00259 ASSERT( iter_count >= 0 ); \
00260 CComPtr<iftype> *arrptr, *arrend, *array = new CComPtr<iftype>[iter_count]; \
00261 if(iter_count > 0) \
00262 COMTHROW( collifptr->GetAll(iter_count, &(*array)) ); \
00263 arrend = array+iter_count; \
00264 for(arrptr = array; arrptr != arrend; arrptr++)
00265
00266 #define MGACOLL_ITER (*arrptr)
00267
00268 #define MGACOLL_AT_END (arrptr == arrend)
00269
00270 #define MGACOLL_ITERATE_END \
00271 delete[] array; \
00272 }
00273
00274
00275
00276
00277
00278
00279
00280 class Variant
00281 {
00282
00283 public :
00284 typedef enum {
00285 VT_Boolean = 0,
00286 VT_Integer,
00287 VT_Double,
00288 VT_String
00289 } Type;
00290
00291 typedef union {
00292 bool bVal;
00293 long lVal;
00294 double dVal;
00295 std::string* pstrVal;
00296 } Value;
00297
00298
00299 private :
00300 bool m_bUndefined;
00301 Type m_eType;
00302 Value m_uValue;
00303
00304
00305 public :
00306 Variant();
00307 Variant( bool bValue );
00308 Variant( long lValue );
00309 Variant( double dValue );
00310 Variant( const std::string& strValue );
00311 Variant( const Variant& vValue );
00312 ~Variant();
00313
00314
00315 public :
00316 Type type() const;
00317 bool isUndefined() const;
00318
00319
00320 public :
00321 Variant& operator=( const Variant& vValue );
00322 operator bool () const;
00323 operator long () const;
00324 operator double () const;
00325 operator std::string () const;
00326 };
00327
00328
00329
00330
00331
00332
00333
00334 class GenRefCounted
00335 {
00336
00337 private :
00338 unsigned long m_ulRefCount;
00339 unsigned long m_ulAggrRefCount;
00340 std::set<GenRefCounted*> m_setAggregators;
00341 bool m_bDisposable;
00342 bool m_bDeleted;
00343
00344
00345 protected :
00346 GenRefCounted( bool bDisposable = true, GenRefCounted* pAggregator = NULL )
00347 : m_ulRefCount( 0 ), m_ulAggrRefCount( 0 ), m_bDisposable( bDisposable ), m_bDeleted( false )
00348 {
00349 if ( pAggregator )
00350 m_setAggregators.insert( pAggregator );
00351 }
00352
00353 GenRefCounted( bool bDisposable, GenRefCounted* pAggregator1, GenRefCounted* pAggregator2 )
00354 : m_ulRefCount( 0 ), m_ulAggrRefCount( 0 ), m_bDisposable( bDisposable ), m_bDeleted( false )
00355 {
00356 if ( pAggregator1 )
00357 m_setAggregators.insert( pAggregator1 );
00358 if ( pAggregator2 )
00359 m_setAggregators.insert( pAggregator2 );
00360 }
00361
00362 public :
00363 virtual ~GenRefCounted()
00364 {
00365 m_setAggregators.clear();
00366 }
00367
00368
00369 private :
00370 void incRef()
00371 {
00372 m_ulRefCount++;
00373 if ( m_ulRefCount == 1 )
00374 for ( std::set<GenRefCounted*>::iterator it = m_setAggregators.begin() ; it != m_setAggregators.end() ; it++ )
00375 (*it)->incAggrRef();
00376 }
00377
00378 bool decRef()
00379 {
00380 m_ulRefCount--;
00381 return doDispose();
00382 }
00383
00384 void incAggrRef()
00385 {
00386 m_ulAggrRefCount++;
00387 }
00388
00389 void decAggrRef()
00390 {
00391 m_ulAggrRefCount--;
00392 if (doDispose()) {
00393
00394
00395
00396 delete this;
00397 }
00398 }
00399
00400 bool doDispose()
00401 {
00402 if ( m_ulRefCount == 0 && m_ulAggrRefCount == 0 ) {
00403 for ( std::set<GenRefCounted*>::iterator it = m_setAggregators.begin() ; it != m_setAggregators.end() ; it++ )
00404 (*it)->decAggrRef();
00405 return m_bDeleted || m_bDisposable;
00406 }
00407 return false;
00408 }
00409
00410
00411 protected :
00412 void setDisposable( bool bDisposable )
00413 {
00414 m_bDisposable = bDisposable;
00415 }
00416
00417 void setAggregators( const std::set<GenRefCounted*>& setAggregators )
00418 {
00419 m_setAggregators = setAggregators;
00420 }
00421
00422 public :
00423 bool isDeleted() const
00424 {
00425 return m_bDeleted;
00426 }
00427
00428 virtual bool setDeleted()
00429 {
00430 if ( m_bDeleted )
00431 return true;
00432 m_bDeleted = true;
00433 return false;
00434 }
00435
00436 bool isDisposable() const
00437 {
00438 return m_bDisposable;
00439 }
00440
00441
00442 friend GenRefCounter;
00443 };
00444
00445
00446
00447
00448
00449
00450
00451 class GenRefCounter
00452 {
00453
00454 private :
00455 GenRefCounted* m_refCounted;
00456
00457
00458 protected :
00459 GenRefCounter()
00460 : m_refCounted( NULL )
00461 {
00462 }
00463
00464 GenRefCounter( const GenRefCounter& grc )
00465 : m_refCounted( grc.m_refCounted )
00466 {
00467 incRef();
00468 }
00469
00470 GenRefCounter( GenRefCounted* pgrc )
00471 : m_refCounted( pgrc )
00472 {
00473 incRef();
00474 }
00475
00476 GenRefCounter& operator = ( const GenRefCounter& grc )
00477 {
00478 if ( this != &grc )
00479 setCounted( grc.m_refCounted );
00480 return *this;
00481 }
00482
00483 GenRefCounter& operator = ( GenRefCounted* pgrc )
00484 {
00485 setCounted( pgrc );
00486 return *this;
00487 }
00488
00489 public :
00490 virtual ~GenRefCounter()
00491 {
00492 decRef();
00493 }
00494
00495
00496 public :
00497 operator bool () const
00498 {
00499 return m_refCounted != NULL;
00500 }
00501
00502 bool operator ! () const
00503 {
00504 return m_refCounted == NULL;
00505 }
00506
00507 bool operator == ( const GenRefCounter& grc ) const
00508 {
00509 return m_refCounted == grc.m_refCounted;
00510 }
00511
00512 bool operator != ( const GenRefCounter& grc ) const
00513 {
00514 return m_refCounted != grc.m_refCounted;
00515 }
00516
00517 bool operator < ( const GenRefCounter& grc ) const
00518 {
00519 return m_refCounted < grc.m_refCounted;
00520 }
00521
00522
00523 public :
00524 GenRefCounted* getCounted( bool bThrow = true ) const
00525 {
00526 if ( ! m_refCounted && bThrow )
00527 ASSERTTHROW( Exception( "Object is null" ) );
00528 return m_refCounted;
00529 }
00530
00531 protected :
00532 void setCounted( GenRefCounted* pgrc )
00533 {
00534 if ( pgrc != m_refCounted ) {
00535 decRef();
00536 m_refCounted = pgrc;
00537 incRef();
00538 }
00539 }
00540
00541
00542 private :
00543 void incRef()
00544 {
00545 if ( m_refCounted )
00546 m_refCounted->incRef();
00547 }
00548
00549 void decRef()
00550 {
00551 if ( m_refCounted ) {
00552 if ( m_refCounted->decRef() ) {
00553 delete m_refCounted;
00554 }
00555 m_refCounted = NULL;
00556 }
00557 }
00558 };
00559
00560 };
00561
00562 Util::InfoOption& operator ++( Util::InfoOption& eOption, int );
00563 Util::InfoOption& operator ++( Util::InfoOption& eOption );
00564 bool isAll( const std::set<Util::InfoOption>& setOptions );
00565 bool isNone( const std::set<Util::InfoOption>& setOptions );
00566 Util::InfoOptions copy( const std::set<Util::InfoOption>& setOptions );
00567 std::set<Util::InfoOption> copy( Util::InfoOptions usOptions );
00568
00569 #endif // Utils_h