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.

272 lines
9.5 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/collections/sequence_container_veneer.hpp
  3. *
  4. * Purpose: RRID veneer for sequence containers
  5. *
  6. * Created: 2nd October 2002
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2002-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 stlsoft/collections/sequence_container_veneer.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::sequence_container_veneer
  42. * class template
  43. * (\ref group__library__collections "Collections" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_SEQUENCE_CONTAINER_VENEER
  46. #define STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_SEQUENCE_CONTAINER_VENEER
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_COLLECTIONS_HPP_SEQUENCE_CONTAINER_VENEER_MAJOR 4
  49. # define STLSOFT_VER_STLSOFT_COLLECTIONS_HPP_SEQUENCE_CONTAINER_VENEER_MINOR 0
  50. # define STLSOFT_VER_STLSOFT_COLLECTIONS_HPP_SEQUENCE_CONTAINER_VENEER_REVISION 3
  51. # define STLSOFT_VER_STLSOFT_COLLECTIONS_HPP_SEQUENCE_CONTAINER_VENEER_EDIT 50
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Compatibility
  55. */
  56. /*
  57. [Incompatibilies-start]
  58. STLSOFT_COMPILER_IS_MSVC: _MSC_VER<1200
  59. STLSOFT_COMPILER_IS_WATCOM:
  60. [Incompatibilies-end]
  61. */
  62. /* /////////////////////////////////////////////////////////////////////////
  63. * Includes
  64. */
  65. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  66. # include <stlsoft/stlsoft.h>
  67. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  68. #if defined(STLSOFT_COMPILER_IS_MSVC) && \
  69. _MSC_VER < 1200
  70. # error stlsoft/collections/sequence_container_veneer.hpp is not compatible with Visual C++ 5.0 or earlier
  71. #endif /* compiler */
  72. #ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS
  73. # include <stlsoft/collections/util/collections.hpp>
  74. #endif /* !STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS */
  75. #ifdef STLSOFT_UNITTEST
  76. # include <algorithm>
  77. # include <list>
  78. # include <numeric>
  79. # include <vector>
  80. #endif /* STLSOFT_UNITTEST */
  81. /* /////////////////////////////////////////////////////////////////////////
  82. * Namespace
  83. */
  84. #ifndef _STLSOFT_NO_NAMESPACE
  85. namespace stlsoft
  86. {
  87. #endif /* _STLSOFT_NO_NAMESPACE */
  88. /* /////////////////////////////////////////////////////////////////////////
  89. * Classes
  90. */
  91. /** \brief Applies a per-item function to a sequence container's items at its destruction
  92. *
  93. * \ingroup group__library__collections
  94. *
  95. * \param T The sequence container type
  96. * \param F The function class
  97. *
  98. * \ingroup concepts_veneer
  99. */
  100. template< ss_typename_param_k T
  101. , ss_typename_param_k F
  102. >
  103. class sequence_container_veneer
  104. : public T
  105. , public stl_collection_tag
  106. {
  107. public:
  108. /// The container type
  109. typedef T container_type;
  110. /// The function class applied to the container's items
  111. typedef F element_destruction_function_type;
  112. /// The current parameterisation of the type
  113. typedef sequence_container_veneer<T, F> class_type;
  114. private:
  115. typedef T parent_class_type;
  116. public:
  117. /// The container's \c allocator_type type
  118. typedef ss_typename_type_k parent_class_type::allocator_type allocator_type;
  119. /// The container's \c size_type type
  120. typedef ss_typename_type_k parent_class_type::size_type size_type;
  121. // Construction
  122. public:
  123. #ifdef STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  124. /// Default constructor
  125. sequence_container_veneer()
  126. {}
  127. /// Copy constructor
  128. sequence_container_veneer(class_type const& rhs)
  129. : parent_class_type(rhs)
  130. {}
  131. # if !defined(STLSOFT_COMPILER_IS_DMC)
  132. /// Constructs with the given allocator
  133. ss_explicit_k sequence_container_veneer(allocator_type const& a)
  134. : parent_class_type(a)
  135. {}
  136. /// Constructs from a range
  137. template <ss_typename_param_k I>
  138. sequence_container_veneer(I i1, I i2)
  139. : parent_class_type(i1, i2)
  140. {}
  141. /// Constructs from a range with the given allocator
  142. template <ss_typename_param_k I>
  143. sequence_container_veneer(I i1, I i2, allocator_type const& a)
  144. : parent_class_type(i1, i2, a)
  145. {}
  146. /// Constructs with the given number of elements (initialised with the given value)
  147. template <ss_typename_param_k V>
  148. sequence_container_veneer(size_type n, V v)
  149. : parent_class_type(n, v)
  150. {}
  151. /// Constructs with the given number of elements (initialised with the given value) with the given allocator
  152. template <ss_typename_param_k V>
  153. sequence_container_veneer(size_type n, V v, allocator_type const& a)
  154. : parent_class_type(n, v, a)
  155. {}
  156. # else
  157. template <ss_typename_param_k N1>
  158. ss_explicit_k sequence_container_veneer(N1 n1)
  159. : parent_class_type(n1)
  160. {}
  161. template< ss_typename_param_k N1
  162. , ss_typename_param_k N2
  163. >
  164. sequence_container_veneer(N1 n1, N2 n2)
  165. : parent_class_type(n1, n2)
  166. {}
  167. template< ss_typename_param_k N1
  168. , ss_typename_param_k N2
  169. , ss_typename_param_k N3
  170. >
  171. sequence_container_veneer(N1 n1, N2 n2, N3 n3)
  172. : parent_class_type(n1, n2, n3)
  173. {}
  174. template< ss_typename_param_k N1
  175. , ss_typename_param_k N2
  176. , ss_typename_param_k N3
  177. , ss_typename_param_k N4
  178. >
  179. sequence_container_veneer(N1 n1, N2 n2, N3 n3, N4 n4)
  180. : parent_class_type(n1, n2, n3, n4)
  181. {}
  182. template< ss_typename_param_k N1
  183. , ss_typename_param_k N2
  184. , ss_typename_param_k N3
  185. , ss_typename_param_k N4
  186. , ss_typename_param_k N5
  187. >
  188. sequence_container_veneer(N1 n1, N2 n2, N3 n3, N4 n4, N5 n5)
  189. : parent_class_type(n1, n2, n3, n4, n5)
  190. {}
  191. # endif /* compiler */
  192. #endif /* !STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT */
  193. /// Destructor, within which all remaining entries are subject to the
  194. /// destruction function
  195. ~sequence_container_veneer() stlsoft_throw_0()
  196. {
  197. // Simply iterate through the sequence contents and call
  198. // the destruction function on each item in turn.
  199. parent_class_type *this_ = this;
  200. ss_typename_type_k container_type::iterator b = this_->begin();
  201. ss_typename_type_k container_type::iterator e = this_->end();
  202. for(; b != e; ++b)
  203. {
  204. element_destruction_function_type()(*b);
  205. }
  206. }
  207. /// Copy assignment operator
  208. ///
  209. /// This method is provided in case the parameterising class provides the
  210. /// operator. If it does not, then the compiler will ignore it unless a
  211. /// call is made to it, in which case an error would have been reported
  212. /// anyway
  213. class_type& operator =(class_type const& rhs)
  214. {
  215. parent_class_type::operator =(rhs);
  216. return *this;
  217. }
  218. protected:
  219. /// Hidden in accordance with the constraints of the
  220. /// <a href = "http://synesis.com.au/resources/articles/cpp/veneers.pdf">veneer</a> concept
  221. void *operator new(ss_size_t )
  222. {
  223. return 0;
  224. }
  225. /// Hidden in accordance with the constraints of the
  226. /// <a href = "http://synesis.com.au/resources/articles/cpp/veneers.pdf">veneer</a> concept
  227. void operator delete(void*)
  228. {}
  229. };
  230. ////////////////////////////////////////////////////////////////////////////
  231. // Unit-testing
  232. #ifdef STLSOFT_UNITTEST
  233. # include "./unittest/sequence_container_veneer_unittest_.h"
  234. #endif /* STLSOFT_UNITTEST */
  235. /* ////////////////////////////////////////////////////////////////////// */
  236. #ifndef _STLSOFT_NO_NAMESPACE
  237. } // namespace stlsoft
  238. #endif /* _STLSOFT_NO_NAMESPACE */
  239. /* ////////////////////////////////////////////////////////////////////// */
  240. #endif /* !STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_SEQUENCE_CONTAINER_VENEER */
  241. /* ///////////////////////////// end of file //////////////////////////// */