FirteX-高性能全文索引和检索平台API Documentation |
00001 #ifndef _COMPTR_H_ 00002 #define _COMPTR_H_ 00003 00004 #include "Com.h" 00005 #include "comerror.h" 00006 #include <assert.h> 00007 #include <stdexcept> 00008 00009 namespace firtex 00010 { 00011 namespace com 00012 { 00013 00015 // CComPtr 00016 00017 template <class T> 00018 class CComPtr 00019 { 00020 public: 00021 CComPtr() 00022 { 00023 ptr = 0; 00024 }; 00025 00026 CComPtr( T* rhs ) 00027 { 00028 ptr = rhs; if (ptr) ptr->AddRef(); 00029 }; 00030 CComPtr( CComPtr<T> const& rhs ) 00031 { 00032 ptr = rhs.ptr; if (ptr) ptr->AddRef(); 00033 }; 00034 ~CComPtr() 00035 { 00036 if ( ptr ) 00037 ptr->Release(); 00038 }; 00039 00040 operator T*() const 00041 { 00042 return ptr; 00043 }; 00044 T** operator&() 00045 { 00046 return &ptr; 00047 }; 00048 T* operator->() 00049 { 00050 return ptr; 00051 }; 00052 CComPtr<T>& operator=( T* rhs ) 00053 { 00054 if ( ptr ) 00055 ptr->Release(); 00056 ptr=rhs; 00057 if (ptr) 00058 ptr->AddRef(); 00059 return *this; 00060 }; 00061 CComPtr<T>& operator=( CComPtr<T> const& rhs ) 00062 { 00063 if ( ptr ) 00064 ptr->Release(); 00065 ptr=rhs.ptr; 00066 if (ptr) 00067 ptr->AddRef(); 00068 return *this; 00069 }; 00070 00071 void attach( T* rhs ) 00072 { 00073 if ( ptr ) 00074 ptr->Relese(); 00075 ptr = rhs; 00076 }; 00077 T* detach() 00078 { 00079 T* ret = ptr; 00080 ptr = 0; 00081 return ret; 00082 }; 00083 T* get() 00084 { 00085 return ptr; 00086 }; 00087 00088 private: 00089 T* ptr; 00090 }; 00091 00093 // CComStr 00094 00095 class CComStr 00096 { 00097 public: 00098 CComStr() 00099 { 00100 m_str = NULL; 00101 } 00102 /*explicit*/ CComStr( int nSize ) 00103 { 00104 m_str = FX_SysAllocStringLen(NULL, nSize); 00105 } 00106 /*explicit*/ CComStr(int nSize, wchar_t const* sz) 00107 { 00108 m_str = FX_SysAllocStringLen(sz, nSize); 00109 } 00110 /*explicit*/ CComStr(wchar_t const* pSrc) 00111 { 00112 m_str = FX_SysAllocString(pSrc); 00113 } 00114 /*explicit*/ CComStr(const CComStr& src) 00115 { 00116 m_str = src.copy(); 00117 } 00118 /*explicit*/ CComStr( FX_REFGUID src ) 00119 { 00120 wchar_t guid[64]; 00121 FX_StringFromGUID2( src, guid, sizeof(guid)/sizeof(*guid) ); 00122 m_str = FX_SysAllocString(guid); 00123 } 00124 00125 CComStr& operator=(const CComStr& src) 00126 { 00127 if (m_str != src.m_str) { 00128 BSTR tmp = src.copy(); 00129 if ( !tmp ) throw std::bad_alloc(); 00130 FX_SysFreeString(m_str); 00131 m_str = tmp; 00132 } 00133 return *this; 00134 } 00135 CComStr& operator=(wchar_t const* pSrc) 00136 { 00137 BSTR tmp = FX_SysAllocString(pSrc); 00138 if ( !tmp ) throw std::bad_alloc(); 00139 FX_SysFreeString(m_str); 00140 m_str = tmp; 00141 return *this; 00142 } 00143 ~CComStr() 00144 { 00145 FX_SysFreeString(m_str); 00146 } 00147 00148 uint16_t getLength() const 00149 { 00150 return (m_str == NULL)? 0 : FX_SysStringLen(m_str); 00151 } 00152 operator BSTR() const 00153 { 00154 return m_str; 00155 } 00156 BSTR* operator&() 00157 { 00158 return &m_str; 00159 } 00160 BSTR copy() const 00161 { 00162 return FX_SysAllocStringLen(m_str, FX_SysStringLen(m_str)); 00163 } 00164 FX_HRESULT copyTo(BSTR* pbstr) 00165 { 00166 assert(pbstr != NULL); 00167 if (pbstr == NULL) return FX_E_POINTER; 00168 *pbstr = FX_SysAllocStringLen(m_str, FX_SysStringLen(m_str)); 00169 if (*pbstr == NULL) return FX_E_OUTOFMEMORY; 00170 return FX_S_OK; 00171 } 00172 void attach(BSTR src) 00173 { 00174 assert(m_str == NULL); 00175 m_str = src; 00176 } 00177 BSTR detach() 00178 { 00179 BSTR s = m_str; 00180 m_str = NULL; 00181 return s; 00182 } 00183 BSTR get() 00184 { 00185 return m_str; 00186 } 00187 void empty() 00188 { 00189 FX_SysFreeString(m_str); 00190 m_str = NULL; 00191 } 00192 bool operator!() const 00193 { 00194 return (m_str == NULL); 00195 } 00196 FX_HRESULT append(const CComStr& bstrSrc) 00197 { 00198 return append(bstrSrc.m_str, FX_SysStringLen(bstrSrc.m_str)); 00199 } 00200 FX_HRESULT append(wchar_t const* lpsz) 00201 { 00202 return append(lpsz, (int)wcslen(lpsz)); 00203 } 00204 // a BSTR is just a LPCOLESTR so we need a special version to signify 00205 // that we are appending a BSTR 00206 FX_HRESULT appendBSTR(BSTR p) 00207 { 00208 return append(p, FX_SysStringLen(p)); 00209 } 00210 FX_HRESULT append(wchar_t const* lpsz, int nLen) 00211 { 00212 int n1 = getLength(); 00213 BSTR b; 00214 b = FX_SysAllocStringLen(NULL, n1+nLen); 00215 if (b == NULL) 00216 return FX_E_OUTOFMEMORY; 00217 memcpy(const_cast<wchar_t*>(b), m_str, n1*sizeof(wchar_t)); 00218 memcpy(const_cast<wchar_t*>(b+n1), lpsz, nLen*sizeof(wchar_t)); 00219 assert( b[n1+nLen] == L'\0' ); 00220 FX_SysFreeString(m_str); 00221 m_str = b; 00222 return FX_S_OK; 00223 } 00224 00225 CComStr& operator+=(const CComStr& bstrSrc) 00226 { 00227 appendBSTR(bstrSrc.m_str); 00228 return *this; 00229 } 00230 bool operator<(BSTR bstrSrc) const 00231 { 00232 if (bstrSrc == NULL && m_str == NULL) 00233 return false; 00234 if (bstrSrc != NULL && m_str != NULL) 00235 return wcscmp(m_str, bstrSrc) < 0; 00236 return m_str == NULL; 00237 } 00238 bool operator==(BSTR bstrSrc) const 00239 { 00240 if (bstrSrc == NULL && m_str == NULL) 00241 return true; 00242 if (bstrSrc != NULL && m_str != NULL) 00243 return wcscmp(m_str, bstrSrc) == 0; 00244 return false; 00245 } 00246 bool operator<(char const* pszSrc) const 00247 { 00248 if (pszSrc == NULL && m_str == NULL) 00249 return false; 00250 CComStr tmp( pszSrc ); 00251 if (tmp.m_str != NULL && m_str != NULL) 00252 return wcscmp(m_str, tmp.m_str) < 0; 00253 return m_str == NULL; 00254 } 00255 bool operator==(char const* pszSrc) const 00256 { 00257 if (pszSrc == NULL && m_str == NULL) 00258 return true; 00259 CComStr tmp( pszSrc ); 00260 if ( !tmp ) throw std::bad_alloc(); 00261 if (tmp.m_str != NULL && m_str != NULL) 00262 return wcscmp(m_str, tmp.m_str) == 0; 00263 return false; 00264 } 00265 00266 /* explicit */ CComStr(char const* pSrc) 00267 { 00268 m_str = FX_SysAllocStringA( pSrc ); 00269 } 00270 00271 FX_HRESULT append( char const* pSrc) 00272 { 00273 CComStr tmp( pSrc ); 00274 if ( !tmp ) return FX_E_OUTOFMEMORY; 00275 return append( tmp ); 00276 } 00277 00278 CComStr& operator=(char const* pSrc) 00279 { 00280 CComStr tmp( pSrc ); 00281 if ( !tmp ) throw std::bad_alloc(); 00282 FX_SysFreeString(m_str); 00283 m_str = tmp.detach(); 00284 return *this; 00285 } 00286 private: 00287 BSTR m_str; 00288 }; // class CComStr 00289 }// namespace com 00290 }; // namespace firtex 00291 #endif // _COMPTR_H_
http://www.firtex.org http://www.sourceforge.net/projects/firtex