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.

458 lines
17 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: comstl/collections/collection_sequence.hpp (originally MOEnSeq.h, ::SynesisCom)
  3. *
  4. * Purpose: STL sequence for COM collection interfaces.
  5. *
  6. * Created: 17th September 1998
  7. * Updated: 3rd February 2012
  8. *
  9. * Thanks: To Eduardo Bezerra and Vivi Orunitia for reporting
  10. * incompatibilities with Borland's 5.82 (Turbo C++). The awful
  11. * preprocessor hack around retrievalQuanta are the result. ;)
  12. *
  13. * Home: http://stlsoft.org/
  14. *
  15. * Copyright (c) 1998-2012, Matthew Wilson and Synesis Software
  16. * All rights reserved.
  17. *
  18. * Redistribution and use in source and binary forms, with or without
  19. * modification, are permitted provided that the following conditions are met:
  20. *
  21. * - Redistributions of source code must retain the above copyright notice, this
  22. * list of conditions and the following disclaimer.
  23. * - Redistributions in binary form must reproduce the above copyright notice,
  24. * this list of conditions and the following disclaimer in the documentation
  25. * and/or other materials provided with the distribution.
  26. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  27. * any contributors may be used to endorse or promote products derived from
  28. * this software without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  31. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  34. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  35. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  36. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  37. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  38. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  39. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGE.
  41. *
  42. * ////////////////////////////////////////////////////////////////////// */
  43. /** \file comstl/collections/collection_sequence.hpp
  44. *
  45. * \brief [C++ only] Definition of the comstl::collection_sequence
  46. * collection class template
  47. * (\ref group__library__collections "Collections" Library).
  48. */
  49. #ifndef COMSTL_INCL_COMSTL_COLLECTIONS_HPP_COLLECTION_SEQUENCE
  50. #define COMSTL_INCL_COMSTL_COLLECTIONS_HPP_COLLECTION_SEQUENCE
  51. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  52. # define COMSTL_VER_COMSTL_COLLECTIONS_HPP_COLLECTION_SEQUENCE_MAJOR 6
  53. # define COMSTL_VER_COMSTL_COLLECTIONS_HPP_COLLECTION_SEQUENCE_MINOR 1
  54. # define COMSTL_VER_COMSTL_COLLECTIONS_HPP_COLLECTION_SEQUENCE_REVISION 10
  55. # define COMSTL_VER_COMSTL_COLLECTIONS_HPP_COLLECTION_SEQUENCE_EDIT 104
  56. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  57. /* /////////////////////////////////////////////////////////////////////////
  58. * Compatibility
  59. */
  60. /*
  61. [Incompatibilies-start]
  62. STLSOFT_COMPILER_IS_WATCOM:
  63. [Incompatibilies-end]
  64. */
  65. /* /////////////////////////////////////////////////////////////////////////
  66. * Includes
  67. */
  68. #ifndef COMSTL_INCL_COMSTL_H_COMSTL
  69. # include <comstl/comstl.h>
  70. #endif /* !COMSTL_INCL_COMSTL_H_COMSTL */
  71. #ifndef COMSTL_INCL_COMSTL_UTIL_H_REFCOUNT_FUNCTIONS
  72. # include <comstl/util/refcount_functions.h>
  73. #endif /* !COMSTL_INCL_COMSTL_UTIL_H_REFCOUNT_FUNCTIONS */
  74. #ifndef COMSTL_INCL_COMSTL_COLLECTIONS_HPP_ENUMERATION_POLICIES
  75. # include <comstl/collections/enumeration_policies.hpp>
  76. #endif /* !COMSTL_INCL_COMSTL_COLLECTIONS_HPP_ENUMERATION_POLICIES */
  77. #ifndef COMSTL_INCL_COMSTL_UTIL_HPP_INTERFACE_TRAITS
  78. # include <comstl/util/interface_traits.hpp>
  79. #endif /* !COMSTL_INCL_COMSTL_UTIL_HPP_INTERFACE_TRAITS */
  80. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  81. # ifndef COMSTL_INCL_COMSTL_ERROR_HPP_EXCEPTIONS
  82. # include <comstl/error/exceptions.hpp>
  83. # endif /* !COMSTL_INCL_COMSTL_ERROR_HPP_EXCEPTIONS */
  84. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  85. #ifndef COMSTL_INCL_COMSTL_COLLECTIONS_HPP_ENUMERATOR_SEQUENCE
  86. # include <comstl/collections/enumerator_sequence.hpp>
  87. #endif /* !COMSTL_INCL_COMSTL_COLLECTIONS_HPP_ENUMERATOR_SEQUENCE */
  88. #ifndef STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER
  89. # include <stlsoft/util/std/iterator_helper.hpp>
  90. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER */
  91. #ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS
  92. # include <stlsoft/collections/util/collections.hpp>
  93. #endif /* !STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS */
  94. #ifndef STLSOFT_INCL_ALGORITHM
  95. # define STLSOFT_INCL_ALGORITHM
  96. # include <algorithm>
  97. #endif /* !STLSOFT_INCL_ALGORITHM */
  98. #ifdef STLSOFT_UNITTEST
  99. # include <comstl/util/value_policies.hpp>
  100. # if !defined(STLSOFT_COMPILER_IS_DMC)
  101. # include "./unittest/_recls_COM_decl_.h"
  102. # endif /* compiler */
  103. # include <stdio.h>
  104. #endif /* STLSOFT_UNITTEST */
  105. /* /////////////////////////////////////////////////////////////////////////
  106. * Namespace
  107. */
  108. #ifndef _COMSTL_NO_NAMESPACE
  109. # if defined(_STLSOFT_NO_NAMESPACE) || \
  110. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  111. /* There is no stlsoft namespace, so must define ::comstl */
  112. namespace comstl
  113. {
  114. # else
  115. /* Define stlsoft::comstl_project */
  116. namespace stlsoft
  117. {
  118. namespace comstl_project
  119. {
  120. # endif /* _STLSOFT_NO_NAMESPACE */
  121. #endif /* !_COMSTL_NO_NAMESPACE */
  122. /* /////////////////////////////////////////////////////////////////////////
  123. * Classes
  124. */
  125. /** \brief A template for adapting COM collections to STL-compatible sequence iteration.
  126. *
  127. * \ingroup group__library__collections
  128. *
  129. * \param I Interface
  130. * \param V Value type
  131. * \param VP Value policy type
  132. * \param R Reference type. Defaults to <code>V const&</code>
  133. * \param CP Cloning policy type. Defaults to comstl::input_cloning_policy
  134. * \param Q Quanta. Defaults to 8
  135. * \param EAP Enumerate acquisition policy type. Defaults to comstl::new_enum_property_policy
  136. *
  137. * The various parameterising types are used to stipulate the interface and the
  138. * value type, and how they are to be handled.
  139. *
  140. * For example, the following parameterisation defines a sequence operating
  141. * over a notional <b>IGUIDCollection</b> collection instance.
  142. *
  143. \code
  144. typedef collection_sequence<IGUIDCollection
  145. , IEnumGUID
  146. , GUID
  147. , GUID_policy
  148. , GUID const&
  149. , forward_cloning_policy<IEnumGUID>
  150. , 5
  151. > collection_sequence_t;
  152. \endcode
  153. *
  154. * The value type is <b>GUID</b> and it is returned as a reference, as
  155. * the <b>GUID const&</b> in fact.
  156. *
  157. * The \ref group__project__comstl type <b>GUID_policy</b> controls how the <b>GUID</b>
  158. * instances are initialised, copied and destroyed.
  159. *
  160. * The \ref group__project__comstl type forward_cloning_policy allows the sequence to provide
  161. * <a href = "http://sgi.com/tech/stl/ForwardIterator.html">Forward Iterator</a>
  162. * semantics.
  163. *
  164. * And the <b>5</b> indicates that the sequence should grab five values at a time,
  165. * to save round trips to the enumerator.
  166. *
  167. * This would be used as follows:
  168. *
  169. \code
  170. void dump_GUID(GUID const&);
  171. IGUIDCollection *penGUIDs = . . .; // Create an instance from wherever
  172. collection_sequence_t guids(penGUIDs, false); // Eat the reference
  173. std::for_each(guids.begin(), guids.end(), dump_GUID);
  174. \endcode
  175. *
  176. * \note The iterator instances returned by begin() and end() are valid outside
  177. * the lifetime of the collection instance from which they are obtained
  178. *
  179. * \remarks A detailed examination of the design and implementation of this
  180. * class template is described in Chapters 28 and 29 of
  181. * <a href="http://www.extendedstl.com/"><b>Extended STL, volume 1</b></a>
  182. * (published by Addison-Wesley, June 2007).
  183. *
  184. * \sa comstl::enumerator_sequence
  185. */
  186. // [[synesis:class:collection: comstl::collection_sequence<T<CI>, T<EI>, T<V>, T<VP>, T<R>, T<CP>, size_t, T<EAP>>]]
  187. template< ss_typename_param_k CI /* Collection interface */
  188. , ss_typename_param_k EI /* Enumerator interface */
  189. , ss_typename_param_k V /* Value type */
  190. , ss_typename_param_k VP /* Value policy type */
  191. , ss_typename_param_k R = V const& /* Reference type */
  192. , ss_typename_param_k CP = input_cloning_policy<EI> /* Cloning policy type */
  193. , cs_size_t Q = 8 /* Quanta */
  194. , ss_typename_param_k EAP = new_enum_property_policy<CI> /* Policy for acquiring the enumerator from the collection */
  195. >
  196. class collection_sequence
  197. : public stlsoft_ns_qual(stl_collection_tag)
  198. {
  199. /// \name Member Types
  200. /// @{
  201. private:
  202. typedef enumerator_sequence<EI, V, VP, R, CP, Q> enumerator_sequence_type;
  203. public:
  204. /// \brief Collection interface type
  205. typedef CI collection_interface_type;
  206. /// \brief Enumerator interface type
  207. typedef ss_typename_type_k enumerator_sequence_type::interface_type enumerator_interface_type;
  208. /// \brief Value type
  209. typedef ss_typename_type_k enumerator_sequence_type::value_type value_type;
  210. /// \brief Value policy type
  211. typedef ss_typename_type_k enumerator_sequence_type::value_policy_type value_policy_type;
  212. /// \brief Reference type
  213. typedef ss_typename_type_k enumerator_sequence_type::reference reference;
  214. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  215. typedef ss_typename_type_k enumerator_sequence_type::reference_type reference_type; // For backwards compatiblity
  216. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  217. /// \brief The mutating (non-const) pointer type
  218. typedef ss_typename_type_k enumerator_sequence_type::pointer pointer;
  219. /// \brief The non-mutating (const) pointer type
  220. typedef ss_typename_type_k enumerator_sequence_type::const_pointer const_pointer;
  221. /// \brief The mutating (non-const) iterator type
  222. typedef ss_typename_type_k enumerator_sequence_type::iterator iterator;
  223. /// \brief The non-mutating (const) iterator type
  224. typedef ss_typename_type_k enumerator_sequence_type::const_iterator const_iterator;
  225. /// \brief Cloning policy type
  226. typedef ss_typename_type_k enumerator_sequence_type::cloning_policy_type cloning_policy_type;
  227. /// \brief Iterator tag type
  228. typedef ss_typename_type_k enumerator_sequence_type::iterator_tag_type iterator_tag_type;
  229. #ifdef STLSOFT_COMPILER_IS_BORLAND
  230. # define retrievalQuanta Q
  231. #else /* ? compiler */
  232. /// \brief Retrieval quanta
  233. enum { retrievalQuanta = enumerator_sequence_type::retrievalQuanta };
  234. #endif /* compiler */
  235. /// \brief The policy for acquiring the enumerator from the collection
  236. typedef EAP enumerator_acquisition_policy_type;
  237. /// \brief Type of the current parameterisation
  238. typedef collection_sequence<CI, EI, V, VP, R, CP, Q, EAP> class_type;
  239. /// \brief The size type
  240. typedef ss_typename_type_k enumerator_sequence_type::size_type size_type;
  241. /// \brief The difference type
  242. typedef ss_typename_type_k enumerator_sequence_type::difference_type difference_type;
  243. /// @}
  244. public:
  245. /// \brief Conversion constructor
  246. ///
  247. /// \param i The enumeration interface pointer to adapt
  248. /// \param bAddRef Causes a reference to be added if \c true, otherwise the sequence is deemed to <i>sink</i>, or consume, the interface pointer
  249. /// \param quanta The actual quanta required for this instance. Must be <= Q
  250. ///
  251. /// \note This does not throw an exception, so it is safe to be used to "eat" the
  252. /// reference. The only possible exception to this is if COMSTL_ASSERT(), which is
  253. /// used to validate that the given quanta size is within the limit specified in
  254. /// the specialisation, has been redefined to throw an exception. But since
  255. /// precondition violations are no more recoverable than any others (see the article
  256. /// "The Nuclear Reactor and the Deep Space Probe"), this does not represent
  257. /// a concerning contradiction to the no-throw status of the constructor.
  258. collection_sequence(collection_interface_type *i, cs_bool_t bAddRef, size_type quanta = 0)
  259. : m_i(i)
  260. , m_quanta(validate_quanta_(quanta))
  261. {
  262. COMSTL_ASSERT(NULL != i);
  263. COMSTL_MESSAGE_ASSERT("Cannot set a quantum that exceeds the value specified in the template specialisation", quanta <= retrievalQuanta); // Could have named these things better!
  264. if(bAddRef)
  265. {
  266. m_i->AddRef();
  267. }
  268. COMSTL_ASSERT(is_valid());
  269. }
  270. /// \brief Releases the adapted interface pointer
  271. ~collection_sequence() stlsoft_throw_0()
  272. {
  273. COMSTL_ASSERT(is_valid());
  274. m_i->Release();
  275. }
  276. /// \name Iteration
  277. /// @{
  278. public:
  279. /// \brief Begins the iteration
  280. ///
  281. /// \return An iterator representing the start of the sequence
  282. iterator begin() const
  283. {
  284. COMSTL_ASSERT(is_valid());
  285. LPUNKNOWN punkEnum;
  286. HRESULT hr = enumerator_acquisition_policy_type::acquire(m_i, &punkEnum);
  287. if(SUCCEEDED(hr))
  288. {
  289. enumerator_interface_type *ei;
  290. hr = punkEnum->QueryInterface(IID_traits<enumerator_interface_type>::iid(), reinterpret_cast<void**>(&ei));
  291. punkEnum->Release();
  292. if(SUCCEEDED(hr))
  293. {
  294. COMSTL_ASSERT(is_valid());
  295. return enumerator_sequence_type(ei, false, m_quanta).begin();
  296. }
  297. else
  298. {
  299. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  300. COMSTL_ASSERT(is_valid());
  301. STLSOFT_THROW_X(com_exception("the enumerator does not provide the requested interface", hr));
  302. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  303. }
  304. }
  305. else
  306. {
  307. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  308. COMSTL_ASSERT(is_valid());
  309. STLSOFT_THROW_X(com_exception("enumerator could not be elicited from the collection", hr));
  310. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  311. }
  312. COMSTL_ASSERT(is_valid());
  313. return iterator();
  314. }
  315. /// \brief Ends the iteration
  316. ///
  317. /// \return An iterator representing the end of the sequence
  318. iterator end() const
  319. {
  320. COMSTL_ASSERT(is_valid());
  321. return iterator();
  322. }
  323. /// @}
  324. /// \name Capacity
  325. /// @{
  326. public:
  327. /** \brief Returns the number of items in the collection
  328. *
  329. \code
  330. IGUIDCollection *penGUIDs = . . .; // Create an instance from wherever
  331. collection_sequence_t guids(penGUIDs, false); // Eat the reference
  332. size_t numItems = guids.size(); // Evaluate the number of elements in the collection
  333. \endcode
  334. *
  335. *
  336. * \note This method will not compile for collection interfaces
  337. * that do not contain the get_Count method
  338. */
  339. size_type size() const
  340. {
  341. COMSTL_ASSERT(is_valid());
  342. ULONG count;
  343. HRESULT hr = m_i->get_Count(&count);
  344. COMSTL_ASSERT(is_valid());
  345. return SUCCEEDED(hr) ? count : 0;
  346. }
  347. /// @}
  348. /// \name Invariant
  349. /// @{
  350. private:
  351. cs_bool_t is_valid() const
  352. {
  353. if(NULL == m_i)
  354. {
  355. return false;
  356. }
  357. return true;
  358. }
  359. /// @}
  360. // Implementation
  361. private:
  362. static size_type validate_quanta_(size_type quanta)
  363. {
  364. COMSTL_MESSAGE_ASSERT("Cannot set a quantum that exceeds the value specified in the template specialisation", quanta <= retrievalQuanta); // Could have named these things better!
  365. if( 0 == quanta ||
  366. quanta > retrievalQuanta)
  367. {
  368. quanta = retrievalQuanta;
  369. }
  370. return quanta;
  371. }
  372. // Members
  373. private:
  374. collection_interface_type *m_i;
  375. size_type const m_quanta;
  376. // Not to be implemented
  377. private:
  378. collection_sequence(class_type const&);
  379. class_type const& operator =(class_type const&);
  380. };
  381. ////////////////////////////////////////////////////////////////////////////
  382. // Compiler compatibility
  383. #ifdef STLSOFT_COMPILER_IS_BORLAND
  384. # undef retrievalQuanta
  385. #endif /* compiler */
  386. ////////////////////////////////////////////////////////////////////////////
  387. // Unit-testing
  388. #ifdef STLSOFT_UNITTEST
  389. # include "./unittest/collection_sequence_unittest_.h"
  390. #endif /* STLSOFT_UNITTEST */
  391. /* ////////////////////////////////////////////////////////////////////// */
  392. #ifndef _COMSTL_NO_NAMESPACE
  393. # if defined(_STLSOFT_NO_NAMESPACE) || \
  394. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  395. } // namespace comstl
  396. # else
  397. } // namespace stlsoft::comstl_project
  398. } // namespace stlsoft
  399. # endif /* _STLSOFT_NO_NAMESPACE */
  400. #endif /* !_COMSTL_NO_NAMESPACE */
  401. /* ////////////////////////////////////////////////////////////////////// */
  402. #endif /* !COMSTL_INCL_COMSTL_COLLECTIONS_HPP_COLLECTION_SEQUENCE */
  403. /* ///////////////////////////// end of file //////////////////////////// */