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.

423 lines
12 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: atlstl/automation/automation_collections.hpp
  3. *
  4. * Purpose: Adaptor classes for creating COM collection instances.
  5. *
  6. * Created: 16th April 1999
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 1999-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file atlstl/automation/automation_collections.hpp
  40. * \brief [C++ only; requires ATL library] Definition of the
  41. * atlstl::generic_automation_collection class template, with which
  42. * COM Collections may be readily defined
  43. * (\ref group__library__com_automation "COM Automation" Library).
  44. */
  45. #ifndef ATLSTL_INCL_ATLSTL_AUTOMATION_HPP_AUTOMATION_COLLECTIONS
  46. #define ATLSTL_INCL_ATLSTL_AUTOMATION_HPP_AUTOMATION_COLLECTIONS
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define ATLSTL_VER_ATLSTL_AUTOMATION_HPP_AUTOMATION_COLLECTIONS_MAJOR 3
  49. # define ATLSTL_VER_ATLSTL_AUTOMATION_HPP_AUTOMATION_COLLECTIONS_MINOR 2
  50. # define ATLSTL_VER_ATLSTL_AUTOMATION_HPP_AUTOMATION_COLLECTIONS_REVISION 2
  51. # define ATLSTL_VER_ATLSTL_AUTOMATION_HPP_AUTOMATION_COLLECTIONS_EDIT 107
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Compatibility
  55. */
  56. /*
  57. [Incompatibilies-start]
  58. STLSOFT_COMPILER_IS_DMC:
  59. [Incompatibilies-end]
  60. */
  61. /* /////////////////////////////////////////////////////////////////////////
  62. * Includes
  63. */
  64. #ifndef ATLSTL_INCL_ATLSTL_HPP_ATLSTL
  65. # include <atlstl/atlstl.hpp>
  66. #endif /* !ATLSTL_INCL_ATLSTL_HPP_ATLSTL */
  67. #ifndef STLSOFT_INCL_SYS_H_ATLCOM
  68. # define STLSOFT_INCL_SYS_H_ATLCOM
  69. # include <atlcom.h>
  70. #endif /* !STLSOFT_INCL_SYS_H_ATLCOM */
  71. /* /////////////////////////////////////////////////////////////////////////
  72. * Namespace
  73. */
  74. #ifndef _ATLSTL_NO_NAMESPACE
  75. # if defined(_STLSOFT_NO_NAMESPACE) || \
  76. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  77. /* There is no stlsoft namespace, so must define ::atlstl */
  78. namespace atlstl
  79. {
  80. # else
  81. /* Define stlsoft::atlstl_project */
  82. namespace stlsoft
  83. {
  84. namespace atlstl_project
  85. {
  86. # endif /* _STLSOFT_NO_NAMESPACE */
  87. #endif /* !_ATLSTL_NO_NAMESPACE */
  88. /* /////////////////////////////////////////////////////////////////////////
  89. * Classes
  90. */
  91. // [[synesis:class:collection: atlstl::generic_collection_base<T<D>, T<ThreadModel>, T<I>, int>]]
  92. template< ss_typename_param_k D
  93. , ss_typename_param_k ThreadModel
  94. , ss_typename_param_k I
  95. , int DispidCount
  96. >
  97. class generic_collection_base
  98. : public I
  99. , public CComObjectRootEx<ThreadModel>
  100. {
  101. /// \name Member Types
  102. /// @{
  103. public:
  104. typedef D derived_class_type;
  105. // typedef generic_collection_base<D, pfn, ThreadModel> class_type;
  106. typedef generic_collection_base<D, ThreadModel, I, DispidCount> class_type;
  107. /// @}
  108. /// \name Construction
  109. /// @{
  110. protected:
  111. generic_collection_base()
  112. {}
  113. /// @}
  114. /// \name Interface map
  115. /// @{
  116. protected:
  117. BEGIN_COM_MAP(generic_collection_base)
  118. COM_INTERFACE_ENTRY(IDispatch) // This is a constraint for I to be IDispatch, or a dual interface
  119. END_COM_MAP()
  120. /// @}
  121. /// \name IDispatch members
  122. /// @{
  123. protected:
  124. STDMETHOD(GetTypeInfoCount)(UINT* pctinfo)
  125. {
  126. *pctinfo = 0;
  127. return S_OK;
  128. }
  129. STDMETHOD(GetTypeInfo)(UINT iTInfo, LCID /* lcid */, ITypeInfo** ppTInfo)
  130. {
  131. if(0 != iTInfo)
  132. {
  133. return DISP_E_BADINDEX;
  134. }
  135. else if(NULL == ppTInfo)
  136. {
  137. return E_POINTER;
  138. }
  139. else
  140. {
  141. *ppTInfo = NULL;
  142. return S_FALSE;
  143. }
  144. }
  145. STDMETHOD(GetIDsOfNames)( REFIID /* riid */
  146. , LPOLESTR* rgszNames
  147. , UINT cNames
  148. , LCID /* lcid */
  149. , DISPID* rgDispId)
  150. {
  151. if(1 == cNames)
  152. {
  153. if(0 == ::wcscmp(rgszNames[0], L"Count"))
  154. {
  155. derived_class_type* pThis = static_cast<derived_class_type*>(this);
  156. HRESULT hr = pThis->SupportsCount();
  157. if(S_OK == hr)
  158. {
  159. rgDispId[0] = DispidCount;
  160. return hr;
  161. }
  162. }
  163. }
  164. return DISP_E_UNKNOWNNAME;
  165. }
  166. STDMETHOD(Invoke)( DISPID dispidMember
  167. , REFIID /* riid */
  168. , LCID /* lcid */
  169. , WORD /* wFlags */
  170. , DISPPARAMS* pDispParams
  171. , VARIANT* pVarResult
  172. , EXCEPINFO* /* pExcepInfo */
  173. , UINT* /* puArgErr */)
  174. {
  175. if(DISPID_NEWENUM == dispidMember)
  176. {
  177. derived_class_type* pThis = static_cast<derived_class_type*>(this);
  178. if(0 != pDispParams->cArgs)
  179. {
  180. return DISP_E_BADPARAMCOUNT;
  181. }
  182. else
  183. {
  184. LPUNKNOWN punkEnumerator;
  185. HRESULT hr;
  186. ::VariantInit(pVarResult);
  187. // hr = (pThis->*pfn)(&punkEnumerator);
  188. hr = pThis->get__NewEnum(&punkEnumerator);
  189. if(SUCCEEDED(hr))
  190. {
  191. pVarResult->vt = VT_UNKNOWN;
  192. pVarResult->punkVal = punkEnumerator;
  193. }
  194. return hr;
  195. }
  196. }
  197. else if(DispidCount == dispidMember)
  198. {
  199. derived_class_type* pThis = static_cast<derived_class_type*>(this);
  200. HRESULT hr;
  201. ::VariantInit(pVarResult);
  202. hr = pThis->get_Count(&pVarResult->lVal);
  203. if(SUCCEEDED(hr))
  204. {
  205. pVarResult->vt = VT_I4;
  206. }
  207. return hr;
  208. }
  209. else
  210. {
  211. return DISP_E_MEMBERNOTFOUND;
  212. }
  213. }
  214. /// @}
  215. };
  216. template< ss_typename_param_k I1
  217. , ss_typename_param_k I2
  218. , ss_typename_param_k I3
  219. >
  220. inline LPUNKNOWN get_clone(I1* instance, HRESULT (STDAPICALLTYPE I3::*pfn)(I2**), HRESULT &hr)
  221. {
  222. I2* clone;
  223. hr = (instance->*pfn)(&clone);
  224. if(FAILED(hr))
  225. {
  226. clone = NULL;
  227. }
  228. return clone;
  229. }
  230. /** Class for defining COM Automation collections
  231. */
  232. // [[synesis:class:collection: atlstl::generic_automation_collection<T<E>, T<ThreadModel>, T<I>, int>]]
  233. template< ss_typename_param_k E
  234. , ss_typename_param_k ThreadModel = CComObjectThreadModel
  235. , ss_typename_param_k I = IDispatch
  236. , int DispidCount = 20001
  237. > // Enumerator type
  238. class generic_automation_collection
  239. : public generic_collection_base<generic_automation_collection<E, ThreadModel, I, DispidCount>, ThreadModel, I, DispidCount>
  240. {
  241. public:
  242. typedef E enumerator_type;
  243. typedef generic_automation_collection<E, ThreadModel, I, DispidCount> class_type;
  244. public:
  245. generic_automation_collection()
  246. : m_enumerator(NULL)
  247. , m_count(static_cast<as_size_t>(~0))
  248. {}
  249. void SetEnumerator(enumerator_type* enumerator, as_bool_t bAddRef)
  250. {
  251. ATLSTL_ASSERT(NULL != enumerator);
  252. ATLSTL_ASSERT(NULL == m_enumerator);
  253. m_enumerator = enumerator;
  254. if(bAddRef)
  255. {
  256. m_enumerator->AddRef();
  257. }
  258. }
  259. void SetCount(as_size_t count)
  260. {
  261. m_count = count;
  262. }
  263. ~generic_automation_collection()
  264. {
  265. m_enumerator->Release();
  266. }
  267. HRESULT SupportsCount() const
  268. {
  269. return (static_cast<as_size_t>(~0) == m_count) ? S_FALSE : S_OK;
  270. }
  271. public:
  272. template< ss_typename_param_k ITER
  273. , ss_typename_param_k ITF
  274. , ss_typename_param_k N
  275. >
  276. HRESULT Init(ITER begin, ITER end, ITF* owner, N flags)
  277. {
  278. ATLSTL_ASSERT(NULL != m_enumerator);
  279. return m_enumerator->Init(begin, end, owner, flags);
  280. }
  281. template< ss_typename_param_k ITER
  282. , ss_typename_param_k ITF
  283. >
  284. HRESULT Init(ITER begin, ITER end, ITF* owner)
  285. {
  286. ATLSTL_ASSERT(NULL != m_enumerator);
  287. return m_enumerator->Init(begin, end, owner);
  288. }
  289. public:
  290. HRESULT get__NewEnum(LPUNKNOWN* punk)
  291. {
  292. ATLSTL_ASSERT(NULL != m_enumerator);
  293. HRESULT hr;
  294. *punk = get_clone(m_enumerator, &enumerator_type::Clone, hr);
  295. #if 0
  296. typedef HRESULT (STDAPICALLTYPE enumerator_type::_CComEnumBase::*pfn_t)(LPUNKNOWN*);
  297. typedef HRESULT (STDAPICALLTYPE enumerator_type::_CComEnumBase::*pfnv_t)(void*);
  298. union
  299. {
  300. pfn_t pfn;
  301. pfnv_t pfnv;
  302. } u;
  303. u.pfnv = (pfnv_t)(&enumerator_type::Clone);
  304. return (m_enumerator->*u.pfn)(punk);
  305. #else /* ? 0 */
  306. return hr;
  307. #endif /* 0 */
  308. }
  309. HRESULT get_Count(long* pVal)
  310. {
  311. ATLSTL_ASSERT(NULL != pVal);
  312. if(static_cast<as_size_t>(~0) == m_count)
  313. {
  314. return E_UNEXPECTED;
  315. }
  316. else
  317. {
  318. *pVal = static_cast<long>(m_count);
  319. return S_OK;
  320. }
  321. }
  322. /// \name Member Variables
  323. /// @{
  324. private:
  325. enumerator_type* m_enumerator;
  326. as_size_t m_count;
  327. /// @}
  328. private:
  329. generic_automation_collection(class_type const&);
  330. class_type& operator =(class_type const&);
  331. };
  332. #if 0
  333. template< ss_typename_param_k C //!< Collection interface
  334. , ss_typename_param_k E //!< Enumerator interface
  335. , ss_typename_param_k T //!< Element type
  336. , ss_typename_param_k XXXXXXXXX
  337. >
  338. class simple_automation_collection
  339. {
  340. };
  341. #endif /* 0 */
  342. ////////////////////////////////////////////////////////////////////////////
  343. // Unit-testing
  344. #ifdef STLSOFT_UNITTEST
  345. # include "./unittest/automation_collections_unittest_.h"
  346. #endif /* STLSOFT_UNITTEST */
  347. /* ////////////////////////////////////////////////////////////////////// */
  348. #ifndef _ATLSTL_NO_NAMESPACE
  349. # if defined(_STLSOFT_NO_NAMESPACE) || \
  350. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  351. } // namespace atlstl
  352. # else
  353. } // namespace atlstl_project
  354. } // namespace stlsoft
  355. # endif /* _STLSOFT_NO_NAMESPACE */
  356. #endif /* !_ATLSTL_NO_NAMESPACE */
  357. /* ////////////////////////////////////////////////////////////////////// */
  358. #endif /* !ATLSTL_INCL_ATLSTL_AUTOMATION_HPP_AUTOMATION_COLLECTIONS */
  359. /* ///////////////////////////// end of file //////////////////////////// */