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.

281 lines
11 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/collections/associative_container_veneer.hpp
  3. *
  4. * Purpose: RRID veneer for associative 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/associative_container_veneer.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::associative_container_veneer
  42. * class template
  43. * (\ref group__library__collections "Collections" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_ASSOCIATIVE_CONTAINER_VENEER
  46. #define STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_ASSOCIATIVE_CONTAINER_VENEER
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_COLLECTIONS_HPP_ASSOCIATIVE_CONTAINER_VENEER_MAJOR 4
  49. # define STLSOFT_VER_STLSOFT_COLLECTIONS_HPP_ASSOCIATIVE_CONTAINER_VENEER_MINOR 0
  50. # define STLSOFT_VER_STLSOFT_COLLECTIONS_HPP_ASSOCIATIVE_CONTAINER_VENEER_REVISION 4
  51. # define STLSOFT_VER_STLSOFT_COLLECTIONS_HPP_ASSOCIATIVE_CONTAINER_VENEER_EDIT 47
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Compatibility
  55. */
  56. /*
  57. [Incompatibilies-start]
  58. STLSOFT_COMPILER_IS_BORLAND:
  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_BORLAND)
  69. # error stlsoft/collections/associative_container_veneer.hpp is incompatible with the Borland C/C++ compiler
  70. #endif /* compiler */
  71. #ifndef STLSOFT_INCL_STLSOFT_FUNCTIONAL_HPP_NOOP
  72. # include <stlsoft/functional/noop.hpp>
  73. #endif /* !STLSOFT_INCL_STLSOFT_FUNCTIONAL_HPP_NOOP */
  74. #ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS
  75. # include <stlsoft/collections/util/collections.hpp>
  76. #endif /* !STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS */
  77. #ifndef STLSOFT_INCL_STLSOFT_OBSOLETE_HPP_FUNCTIONALS
  78. # include <stlsoft/obsolete/functionals.hpp>
  79. #endif /* !STLSOFT_INCL_STLSOFT_OBSOLETE_HPP_FUNCTIONALS */
  80. /* /////////////////////////////////////////////////////////////////////////
  81. * Namespace
  82. */
  83. #ifndef _STLSOFT_NO_NAMESPACE
  84. namespace stlsoft
  85. {
  86. #endif /* _STLSOFT_NO_NAMESPACE */
  87. /* /////////////////////////////////////////////////////////////////////////
  88. * Classes
  89. */
  90. /** \brief Applies per-item functions to an associative container's items at its destruction
  91. *
  92. * \ingroup group__library__collections
  93. *
  94. * \param T The sequence container type
  95. * \param FV The function class applied to the values of the container
  96. * \param FK The function class applied to the keys of the container. For translators that support default template arguments this defaults to noop_function<T::key_type>
  97. *
  98. * \ingroup concepts_veneer
  99. */
  100. template< ss_typename_param_k T
  101. , ss_typename_param_k FV
  102. #ifdef STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT
  103. , ss_typename_param_k FK = noop_function<ss_typename_type_def_k T::key_type>
  104. #else /* ? STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
  105. , ss_typename_param_k FK /* = noop_function<T> */
  106. #endif /* STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
  107. >
  108. class associative_container_veneer
  109. : public T
  110. , public stl_collection_tag
  111. {
  112. public:
  113. /// The container type
  114. typedef T container_type;
  115. /// The function class applied to the container's values
  116. typedef FV value_destruction_function_type;
  117. /// The function class applied to the container's keys
  118. typedef FK key_destruction_function_type;
  119. /// The current parameterisation of the type
  120. typedef associative_container_veneer<T, FV, FK> class_type;
  121. /// The parent class type
  122. private:
  123. typedef T parent_class_type;
  124. public:
  125. /// The container's \c key_compare type
  126. typedef ss_typename_type_k parent_class_type::key_compare key_compare;
  127. /// The container's \c allocator_type type
  128. typedef ss_typename_type_k parent_class_type::allocator_type allocator_type;
  129. /// The container's \c size_type type
  130. typedef ss_typename_type_k parent_class_type::size_type size_type;
  131. // Construction
  132. public:
  133. #ifdef STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  134. /// Default constructor
  135. associative_container_veneer()
  136. {}
  137. /// Copy constructor
  138. associative_container_veneer(class_type const& rhs)
  139. : parent_class_type(rhs)
  140. {}
  141. # if !defined(STLSOFT_COMPILER_IS_DMC)
  142. /// Constructs with the given predicate
  143. ss_explicit_k associative_container_veneer(key_compare const& comp)
  144. : parent_class_type(comp)
  145. {}
  146. /// Constructs with the given predicate and allocator
  147. associative_container_veneer(key_compare const& comp, allocator_type const& a)
  148. : parent_class_type(comp, a)
  149. {}
  150. /// Constructs from a range
  151. template <ss_typename_param_k I>
  152. associative_container_veneer(I i1, I i2)
  153. : parent_class_type(i1, i2)
  154. {}
  155. /// Constructs from a range, with the given predicate
  156. template <ss_typename_param_k I>
  157. associative_container_veneer(I i1, I i2, key_compare const& comp)
  158. : parent_class_type(i1, i2, comp)
  159. {}
  160. /// Constructs from a range, with the given predicate and allocator
  161. template <ss_typename_param_k I>
  162. associative_container_veneer(I i1, I i2, key_compare const& comp, allocator_type const& a)
  163. : parent_class_type(i1, i2, comp, a)
  164. {}
  165. # else /* ? compiler */
  166. template <ss_typename_param_k N1>
  167. ss_explicit_k associative_container_veneer(N1 n1)
  168. : parent_class_type(n1)
  169. {}
  170. template< ss_typename_param_k N1
  171. , ss_typename_param_k N2
  172. >
  173. associative_container_veneer(N1 n1, N2 n2)
  174. : parent_class_type(n1, n2)
  175. {}
  176. template< ss_typename_param_k N1
  177. , ss_typename_param_k N2
  178. , ss_typename_param_k N3
  179. >
  180. associative_container_veneer(N1 n1, N2 n2, N3 n3)
  181. : parent_class_type(n1, n2, n3)
  182. {}
  183. template< ss_typename_param_k N1
  184. , ss_typename_param_k N2
  185. , ss_typename_param_k N3
  186. , ss_typename_param_k N4
  187. >
  188. associative_container_veneer(N1 n1, N2 n2, N3 n3, N4 n4)
  189. : parent_class_type(n1, n2, n3, n4)
  190. {}
  191. template< ss_typename_param_k N1
  192. , ss_typename_param_k N2
  193. , ss_typename_param_k N3
  194. , ss_typename_param_k N4
  195. , ss_typename_param_k N5
  196. >
  197. associative_container_veneer(N1 n1, N2 n2, N3 n3, N4 n4, N5 n5)
  198. : parent_class_type(n1, n2, n3, n4, n5)
  199. {}
  200. # endif /* compiler */
  201. #endif /* !STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT */
  202. /// Destructor, within which all remaining entries are subject to the
  203. /// key and value destruction functions
  204. ~associative_container_veneer() stlsoft_throw_0()
  205. {
  206. // Determine the destruction function type, allowing for
  207. // compilers that do not work with member-function templates
  208. typedef select_both < key_destruction_function_type
  209. , value_destruction_function_type
  210. #ifndef STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  211. , ss_typename_type_k container_type::iterator::value_type
  212. #endif // STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
  213. > destruction_function_t;
  214. // Simply iterate through the sequence contents and call
  215. // the destruction functions on each item in turn.
  216. ss_typename_type_k container_type::iterator b = container_type::begin();
  217. ss_typename_type_k container_type::iterator e = container_type::end();
  218. for(destruction_function_t fn; b != e; ++b)
  219. {
  220. fn(*b);
  221. }
  222. }
  223. /// Copy assignment operator
  224. ///
  225. /// This method is provided in case the parameterising class provides the
  226. /// operator. If it does not, then the compiler will ignore it unless a
  227. /// call is made to it, in which case an error would have been reported
  228. /// anyway
  229. class_type& operator =(class_type const& rhs)
  230. {
  231. parent_class_type::operator =(rhs);
  232. return *this;
  233. }
  234. protected:
  235. /// Hidden in accordance with the constraints of the
  236. /// <a href = "http://synesis.com.au/resources/articles/cpp/veneers.pdf">veneer</a> concept
  237. void *operator new(ss_size_t )
  238. {
  239. return 0;
  240. }
  241. /// Hidden in accordance with the constraints of the
  242. /// <a href = "http://synesis.com.au/resources/articles/cpp/veneers.pdf">veneer</a> concept
  243. void operator delete(void*)
  244. {}
  245. };
  246. /* ////////////////////////////////////////////////////////////////////// */
  247. #ifndef _STLSOFT_NO_NAMESPACE
  248. } // namespace stlsoft
  249. #endif /* _STLSOFT_NO_NAMESPACE */
  250. /* ////////////////////////////////////////////////////////////////////// */
  251. #endif /* !STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_ASSOCIATIVE_CONTAINER_VENEER */
  252. /* ///////////////////////////// end of file //////////////////////////// */