Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

469 lines
12 KiB

  1. /******************************************************************************
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. MPC_main.h
  5. Abstract:
  6. This file includes and defines things common to all modules.
  7. Revision History:
  8. Davide Massarenti (Dmassare) 05/08/99
  9. created
  10. ******************************************************************************/
  11. #if !defined(__INCLUDED___MPC___MAIN_H___)
  12. #define __INCLUDED___MPC___MAIN_H___
  13. #include <atlbase.h>
  14. #include <Yvals_nodll.h>
  15. #include <iosfwd_nodll>
  16. #include <xstring_noref>
  17. #include <string_noref>
  18. #include <list>
  19. #include <vector>
  20. #include <map>
  21. #include <set>
  22. #include <algorithm>
  23. #include <httpext.h>
  24. #include <wininet.h>
  25. #include <comdef.h>
  26. #define SAFEBSTR( bstr ) (bstr ? bstr : L"")
  27. #define SAFEASTR( str ) (str ? str : "")
  28. #define SAFEWSTR( str ) (str ? str : L"")
  29. #define ARRAYSIZE( a ) (sizeof(a)/sizeof(*a))
  30. #define MAXSTRLEN( a ) (ARRAYSIZE(a)-1)
  31. #define SANITIZEASTR( str ) if(str == NULL) str = ""
  32. #define SANITIZEWSTR( str ) if(str == NULL) str = L""
  33. #define STRINGISPRESENT( str ) (str && str[0])
  34. /////////////////////////////////////////////////////////////////////////
  35. namespace MPC
  36. {
  37. //
  38. // Non-reference counting version of std::basic_string, that is MT-safe.
  39. //
  40. typedef std::basic_stringNR<char , std::char_traits<char> , std::allocator<char> > string;
  41. typedef std::basic_stringNR<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > wstring;
  42. ////////////////////
  43. //
  44. // Auto-uppercase string classes, useful for quick maps and sets of strings (the conversion is done once).
  45. //
  46. class stringUC
  47. {
  48. MPC::string m_data;
  49. void Convert()
  50. {
  51. if(m_data.length()) ::CharUpperBuffA( (LPSTR)m_data.c_str(), m_data.size() );
  52. }
  53. public:
  54. stringUC()
  55. {
  56. }
  57. stringUC( const MPC::string& src )
  58. {
  59. m_data = src; Convert();
  60. }
  61. stringUC( LPCSTR src )
  62. {
  63. m_data = src; Convert();
  64. }
  65. stringUC& operator=( const MPC::string& src )
  66. {
  67. m_data = src; Convert();
  68. return *this;
  69. }
  70. stringUC& operator=( LPCSTR src )
  71. {
  72. m_data = src; Convert();
  73. return *this;
  74. }
  75. bool operator<( const stringUC& right ) const
  76. {
  77. return m_data < right.m_data;
  78. }
  79. bool operator==( const string& right ) const
  80. {
  81. return m_data == right;
  82. }
  83. operator string&()
  84. {
  85. return m_data;
  86. }
  87. operator const string&() const
  88. {
  89. return m_data;
  90. }
  91. LPCSTR c_str() const
  92. {
  93. return m_data.c_str();
  94. }
  95. };
  96. class wstringUC
  97. {
  98. MPC::wstring m_data;
  99. void Convert()
  100. {
  101. if(m_data.length()) ::CharUpperBuffW( (LPWSTR)m_data.c_str(), m_data.size() );
  102. }
  103. public:
  104. wstringUC()
  105. {
  106. }
  107. wstringUC( const MPC::wstring& src )
  108. {
  109. m_data = src; Convert();
  110. }
  111. wstringUC( LPCWSTR src )
  112. {
  113. m_data = src; Convert();
  114. }
  115. wstringUC& operator=( const MPC::wstring& src )
  116. {
  117. m_data = src; Convert();
  118. return *this;
  119. }
  120. wstringUC& operator=( LPCWSTR src )
  121. {
  122. m_data = src; Convert();
  123. return *this;
  124. }
  125. bool operator<( const wstringUC& right ) const
  126. {
  127. return m_data < right.m_data;
  128. }
  129. bool operator==( const wstring& right ) const
  130. {
  131. return m_data == right;
  132. }
  133. operator wstring&()
  134. {
  135. return m_data;
  136. }
  137. operator const wstring&() const
  138. {
  139. return m_data;
  140. }
  141. LPCWSTR c_str() const
  142. {
  143. return m_data.c_str();
  144. }
  145. };
  146. ////////////////////
  147. typedef std::list<MPC::string> StringList;
  148. typedef StringList::iterator StringIter;
  149. typedef StringList::const_iterator StringIterConst;
  150. typedef std::list<MPC::wstring> WStringList;
  151. typedef WStringList::iterator WStringIter;
  152. typedef WStringList::const_iterator WStringIterConst;
  153. typedef std::list<MPC::stringUC> StringUCList;
  154. typedef StringUCList::iterator StringUCIter;
  155. typedef StringUCList::const_iterator StringUCIterConst;
  156. typedef std::list<MPC::wstringUC> WStringUCList;
  157. typedef WStringUCList::iterator WStringUCIter;
  158. typedef WStringUCList::const_iterator WStringUCIterConst;
  159. ////////////////////
  160. class NocaseLess
  161. {
  162. public:
  163. bool operator()( /*[in]*/ const MPC::string& , /*[in]*/ const MPC::string& ) const;
  164. bool operator()( /*[in]*/ const MPC::wstring&, /*[in]*/ const MPC::wstring& ) const;
  165. bool operator()( /*[in]*/ const BSTR , /*[in]*/ const BSTR ) const;
  166. };
  167. class NocaseCompare
  168. {
  169. public:
  170. bool operator()( /*[in]*/ const MPC::string& , /*[in]*/ const MPC::string& ) const;
  171. bool operator()( /*[in]*/ const MPC::wstring&, /*[in]*/ const MPC::wstring& ) const;
  172. bool operator()( /*[in]*/ const BSTR , /*[in]*/ const BSTR ) const;
  173. };
  174. ////////////////////
  175. typedef std::vector<MPC::string> StringVector;
  176. typedef StringVector::iterator StringVectorIter;
  177. typedef StringVector::const_iterator StringVectorIterConst;
  178. typedef std::vector<MPC::wstring> WStringVector;
  179. typedef WStringVector::iterator WStringVectorIter;
  180. typedef WStringVector::const_iterator WStringVectorIterConst;
  181. ////////////////////
  182. typedef std::set<MPC::string> StringSet;
  183. typedef StringSet::iterator StringSetIter;
  184. typedef StringSet::const_iterator StringSetIterConst;
  185. typedef std::set<MPC::wstring> WStringSet;
  186. typedef WStringSet::iterator WStringSetIter;
  187. typedef WStringSet::const_iterator WStringSetIterConst;
  188. typedef std::set<MPC::stringUC> StringUCSet;
  189. typedef StringUCSet::iterator StringUCSetIter;
  190. typedef StringUCSet::const_iterator StringUCSetIterConst;
  191. typedef std::set<MPC::wstringUC> WStringUCSet;
  192. typedef WStringUCSet::iterator WStringUCSetIter;
  193. typedef WStringUCSet::const_iterator WStringUCSetIterConst;
  194. typedef std::set<MPC::string,MPC::NocaseLess> StringNocaseSet;
  195. typedef StringNocaseSet::iterator StringNocaseSetIter;
  196. typedef StringNocaseSet::const_iterator StringNocaseSetIterConst;
  197. typedef std::set<MPC::wstring,MPC::NocaseLess> WStringNocaseSet;
  198. typedef WStringNocaseSet::iterator WStringNocaseSetIter;
  199. typedef WStringNocaseSet::const_iterator WStringNocaseSetIterConst;
  200. ////////////////////
  201. typedef std::map<MPC::string,MPC::string> StringLookup;
  202. typedef StringLookup::iterator StringLookupIter;
  203. typedef StringLookup::const_iterator StringLookupIterConst;
  204. typedef std::map<MPC::wstring,MPC::wstring> WStringLookup;
  205. typedef WStringLookup::iterator WStringLookupIter;
  206. typedef WStringLookup::const_iterator WStringLookupIterConst;
  207. typedef std::map<MPC::stringUC,MPC::string> StringUCLookup;
  208. typedef StringUCLookup::iterator StringUCLookupIter;
  209. typedef StringUCLookup::const_iterator StringUCLookupIterConst;
  210. typedef std::map<MPC::wstringUC,MPC::wstring> WStringUCLookup;
  211. typedef WStringUCLookup::iterator WStringUCLookupIter;
  212. typedef WStringUCLookup::const_iterator WStringUCLookupIterConst;
  213. ////////////////////
  214. typedef std::list< IDispatch* > IDispatchList;
  215. typedef IDispatchList::iterator IDispatchIter;
  216. typedef IDispatchList::const_iterator IDispatchIterConst;
  217. /////////////////////////////////////////////////////////////////////////////
  218. template <class Src, class Dst> HRESULT CopyTo( Src* pSrc, Dst* *pVal )
  219. {
  220. if(!pVal) return E_POINTER;
  221. *pVal = pSrc; if(pSrc) pSrc->AddRef();
  222. return S_OK;
  223. }
  224. template <typename I, class Src, class Dst> HRESULT CopyTo2( Src* pSrc, Dst* *pVal )
  225. {
  226. if(!pVal) return E_POINTER;
  227. *pVal = pSrc; if(pSrc) ((I*)pSrc)->AddRef();
  228. return S_OK;
  229. }
  230. template <class T> void Release( T*& p )
  231. {
  232. if(p)
  233. {
  234. p->Release(); p = NULL;
  235. }
  236. }
  237. template <typename I, class T> void Release2( T*& p )
  238. {
  239. if(p)
  240. {
  241. ((I*)p)->Release(); p = NULL;
  242. }
  243. }
  244. template <class T> void Attach( T*& p, T* src )
  245. {
  246. if(src) src->AddRef ();
  247. if(p ) p ->Release();
  248. p = src;
  249. }
  250. template <class T> HRESULT CreateInstance( T** pp )
  251. {
  252. HRESULT hr;
  253. if(pp)
  254. {
  255. CComObject<T>* p = NULL;
  256. *pp = NULL;
  257. if(SUCCEEDED(hr = CComObject<T>::CreateInstance( &p )))
  258. {
  259. if(p)
  260. {
  261. *pp = p; p->AddRef();
  262. }
  263. else
  264. {
  265. hr = E_NOINTERFACE;
  266. }
  267. }
  268. }
  269. else
  270. {
  271. hr = E_POINTER;
  272. }
  273. return hr;
  274. }
  275. template <class T> HRESULT CreateInstanceCached( T** pp )
  276. {
  277. HRESULT hr;
  278. if(pp)
  279. {
  280. MPC::CComObjectCached<T>* p = NULL;
  281. *pp = NULL;
  282. if(SUCCEEDED(hr = MPC::CComObjectCached<T>::CreateInstance( &p )))
  283. {
  284. if(p)
  285. {
  286. *pp = p; p->AddRef();
  287. }
  288. else
  289. {
  290. hr = E_NOINTERFACE;
  291. }
  292. }
  293. }
  294. else
  295. {
  296. hr = E_POINTER;
  297. }
  298. return hr;
  299. }
  300. template <class T> HRESULT CreateInstanceNoLock( T** pp )
  301. {
  302. HRESULT hr;
  303. if(pp)
  304. {
  305. MPC::CComObjectNoLock<T>* p = NULL;
  306. *pp = NULL;
  307. if(SUCCEEDED(hr = MPC::CComObjectNoLock<T>::CreateInstance( &p )))
  308. {
  309. if(p)
  310. {
  311. *pp = p; p->AddRef();
  312. }
  313. else
  314. {
  315. hr = E_NOINTERFACE;
  316. }
  317. }
  318. }
  319. else
  320. {
  321. hr = E_POINTER;
  322. }
  323. return hr;
  324. }
  325. template <class T> void ReleaseAll( T& container )
  326. {
  327. T::const_iterator it;
  328. for(it = container.begin(); it != container.end(); it++)
  329. {
  330. (*it)->Release();
  331. }
  332. container.clear();
  333. }
  334. template <class T> void ReleaseAllVariant( T& container )
  335. {
  336. T::iterator it;
  337. for(it = container.begin(); it != container.end(); it++)
  338. {
  339. ::VariantClear( &(*it) );
  340. }
  341. container.clear();
  342. }
  343. template <class T> void CallDestructorForAll( T& container )
  344. {
  345. T::const_iterator it;
  346. for(it = container.begin(); it != container.end(); it++)
  347. {
  348. delete (*it);
  349. }
  350. container.clear();
  351. }
  352. /////////////////////////////////////////////////////////////////////////////
  353. typedef CAdapt<CComBSTR> CComBSTR_STL;
  354. template <class T> class CComPtr_STL : public CAdapt< CComPtr< T > >
  355. {
  356. };
  357. }; // namespace
  358. /////////////////////////////////////////////////////////////////////////
  359. #endif // !defined(__INCLUDED___MPC___MAIN_H___)