/****************************************************************************** Copyright (c) 1999 Microsoft Corporation Module Name: MPC_main.h Abstract: This file includes and defines things common to all modules. Revision History: Davide Massarenti (Dmassare) 05/08/99 created ******************************************************************************/ #if !defined(__INCLUDED___MPC___MAIN_H___) #define __INCLUDED___MPC___MAIN_H___ #include #include #include #include #include #include #include #include #include #include #include #include #include #define SAFEBSTR( bstr ) (bstr ? bstr : L"") #define SAFEASTR( str ) (str ? str : "") #define SAFEWSTR( str ) (str ? str : L"") #define ARRAYSIZE( a ) (sizeof(a)/sizeof(*a)) #define MAXSTRLEN( a ) (ARRAYSIZE(a)-1) #define SANITIZEASTR( str ) if(str == NULL) str = "" #define SANITIZEWSTR( str ) if(str == NULL) str = L"" #define STRINGISPRESENT( str ) (str && str[0]) ///////////////////////////////////////////////////////////////////////// namespace MPC { // // Non-reference counting version of std::basic_string, that is MT-safe. // typedef std::basic_stringNR , std::allocator > string; typedef std::basic_stringNR, std::allocator > wstring; //////////////////// // // Auto-uppercase string classes, useful for quick maps and sets of strings (the conversion is done once). // class stringUC { MPC::string m_data; void Convert() { if(m_data.length()) ::CharUpperBuffA( (LPSTR)m_data.c_str(), m_data.size() ); } public: stringUC() { } stringUC( const MPC::string& src ) { m_data = src; Convert(); } stringUC( LPCSTR src ) { m_data = src; Convert(); } stringUC& operator=( const MPC::string& src ) { m_data = src; Convert(); return *this; } stringUC& operator=( LPCSTR src ) { m_data = src; Convert(); return *this; } bool operator<( const stringUC& right ) const { return m_data < right.m_data; } bool operator==( const string& right ) const { return m_data == right; } operator string&() { return m_data; } operator const string&() const { return m_data; } LPCSTR c_str() const { return m_data.c_str(); } }; class wstringUC { MPC::wstring m_data; void Convert() { if(m_data.length()) ::CharUpperBuffW( (LPWSTR)m_data.c_str(), m_data.size() ); } public: wstringUC() { } wstringUC( const MPC::wstring& src ) { m_data = src; Convert(); } wstringUC( LPCWSTR src ) { m_data = src; Convert(); } wstringUC& operator=( const MPC::wstring& src ) { m_data = src; Convert(); return *this; } wstringUC& operator=( LPCWSTR src ) { m_data = src; Convert(); return *this; } bool operator<( const wstringUC& right ) const { return m_data < right.m_data; } bool operator==( const wstring& right ) const { return m_data == right; } operator wstring&() { return m_data; } operator const wstring&() const { return m_data; } LPCWSTR c_str() const { return m_data.c_str(); } }; //////////////////// typedef std::list StringList; typedef StringList::iterator StringIter; typedef StringList::const_iterator StringIterConst; typedef std::list WStringList; typedef WStringList::iterator WStringIter; typedef WStringList::const_iterator WStringIterConst; typedef std::list StringUCList; typedef StringUCList::iterator StringUCIter; typedef StringUCList::const_iterator StringUCIterConst; typedef std::list WStringUCList; typedef WStringUCList::iterator WStringUCIter; typedef WStringUCList::const_iterator WStringUCIterConst; //////////////////// class NocaseLess { public: bool operator()( /*[in]*/ const MPC::string& , /*[in]*/ const MPC::string& ) const; bool operator()( /*[in]*/ const MPC::wstring&, /*[in]*/ const MPC::wstring& ) const; bool operator()( /*[in]*/ const BSTR , /*[in]*/ const BSTR ) const; }; class NocaseCompare { public: bool operator()( /*[in]*/ const MPC::string& , /*[in]*/ const MPC::string& ) const; bool operator()( /*[in]*/ const MPC::wstring&, /*[in]*/ const MPC::wstring& ) const; bool operator()( /*[in]*/ const BSTR , /*[in]*/ const BSTR ) const; }; //////////////////// typedef std::vector StringVector; typedef StringVector::iterator StringVectorIter; typedef StringVector::const_iterator StringVectorIterConst; typedef std::vector WStringVector; typedef WStringVector::iterator WStringVectorIter; typedef WStringVector::const_iterator WStringVectorIterConst; //////////////////// typedef std::set StringSet; typedef StringSet::iterator StringSetIter; typedef StringSet::const_iterator StringSetIterConst; typedef std::set WStringSet; typedef WStringSet::iterator WStringSetIter; typedef WStringSet::const_iterator WStringSetIterConst; typedef std::set StringUCSet; typedef StringUCSet::iterator StringUCSetIter; typedef StringUCSet::const_iterator StringUCSetIterConst; typedef std::set WStringUCSet; typedef WStringUCSet::iterator WStringUCSetIter; typedef WStringUCSet::const_iterator WStringUCSetIterConst; typedef std::set StringNocaseSet; typedef StringNocaseSet::iterator StringNocaseSetIter; typedef StringNocaseSet::const_iterator StringNocaseSetIterConst; typedef std::set WStringNocaseSet; typedef WStringNocaseSet::iterator WStringNocaseSetIter; typedef WStringNocaseSet::const_iterator WStringNocaseSetIterConst; //////////////////// typedef std::map StringLookup; typedef StringLookup::iterator StringLookupIter; typedef StringLookup::const_iterator StringLookupIterConst; typedef std::map WStringLookup; typedef WStringLookup::iterator WStringLookupIter; typedef WStringLookup::const_iterator WStringLookupIterConst; typedef std::map StringUCLookup; typedef StringUCLookup::iterator StringUCLookupIter; typedef StringUCLookup::const_iterator StringUCLookupIterConst; typedef std::map WStringUCLookup; typedef WStringUCLookup::iterator WStringUCLookupIter; typedef WStringUCLookup::const_iterator WStringUCLookupIterConst; //////////////////// typedef std::list< IDispatch* > IDispatchList; typedef IDispatchList::iterator IDispatchIter; typedef IDispatchList::const_iterator IDispatchIterConst; ///////////////////////////////////////////////////////////////////////////// template HRESULT CopyTo( Src* pSrc, Dst* *pVal ) { if(!pVal) return E_POINTER; *pVal = pSrc; if(pSrc) pSrc->AddRef(); return S_OK; } template HRESULT CopyTo2( Src* pSrc, Dst* *pVal ) { if(!pVal) return E_POINTER; *pVal = pSrc; if(pSrc) ((I*)pSrc)->AddRef(); return S_OK; } template void Release( T*& p ) { if(p) { p->Release(); p = NULL; } } template void Release2( T*& p ) { if(p) { ((I*)p)->Release(); p = NULL; } } template void Attach( T*& p, T* src ) { if(src) src->AddRef (); if(p ) p ->Release(); p = src; } template HRESULT CreateInstance( T** pp ) { HRESULT hr; if(pp) { CComObject* p = NULL; *pp = NULL; if(SUCCEEDED(hr = CComObject::CreateInstance( &p ))) { if(p) { *pp = p; p->AddRef(); } else { hr = E_NOINTERFACE; } } } else { hr = E_POINTER; } return hr; } template HRESULT CreateInstanceCached( T** pp ) { HRESULT hr; if(pp) { MPC::CComObjectCached* p = NULL; *pp = NULL; if(SUCCEEDED(hr = MPC::CComObjectCached::CreateInstance( &p ))) { if(p) { *pp = p; p->AddRef(); } else { hr = E_NOINTERFACE; } } } else { hr = E_POINTER; } return hr; } template HRESULT CreateInstanceNoLock( T** pp ) { HRESULT hr; if(pp) { MPC::CComObjectNoLock* p = NULL; *pp = NULL; if(SUCCEEDED(hr = MPC::CComObjectNoLock::CreateInstance( &p ))) { if(p) { *pp = p; p->AddRef(); } else { hr = E_NOINTERFACE; } } } else { hr = E_POINTER; } return hr; } template void ReleaseAll( T& container ) { T::const_iterator it; for(it = container.begin(); it != container.end(); it++) { (*it)->Release(); } container.clear(); } template void ReleaseAllVariant( T& container ) { T::iterator it; for(it = container.begin(); it != container.end(); it++) { ::VariantClear( &(*it) ); } container.clear(); } template void CallDestructorForAll( T& container ) { T::const_iterator it; for(it = container.begin(); it != container.end(); it++) { delete (*it); } container.clear(); } ///////////////////////////////////////////////////////////////////////////// typedef CAdapt CComBSTR_STL; template class CComPtr_STL : public CAdapt< CComPtr< T > > { }; }; // namespace ///////////////////////////////////////////////////////////////////////// #endif // !defined(__INCLUDED___MPC___MAIN_H___)