collection_traits.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Boost string_algo library collection_traits.hpp header file -------------//
  2. // Copyright Pavol Droba 2002-2003. Use, modification and
  3. // distribution is subject to the Boost Software License, Version
  4. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // (C) Copyright Thorsten Ottosen 2002-2003. Use, modification and
  7. // distribution is subject to the Boost Software License, Version
  8. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. // (C) Copyright Jeremy Siek 2001. Use, modification and
  11. // distribution is subject to the Boost Software License, Version
  12. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. // Original idea of container traits was proposed by Jeremy Siek and
  15. // Thorsten Ottosen. This implementation is lightweighted version
  16. // of container_traits adapter for usage with string_algo library
  17. #ifndef BOOST_RANGE_STRING_COLLECTION_TRAITS_HPP
  18. #define BOOST_RANGE_STRING_COLLECTION_TRAITS_HPP
  19. #include <boost/type_traits/is_array.hpp>
  20. #include <boost/type_traits/is_pointer.hpp>
  21. #include <boost/mpl/eval_if.hpp>
  22. // Implementation
  23. #include <boost/range/detail/collection_traits_detail.hpp>
  24. /*! \file
  25. Defines collection_traits class and related free-standing functions.
  26. This facility is used to unify the access to different types of collections.
  27. It allows the algorithms in the library to work with STL collections, c-style
  28. array, null-terminated c-strings (and more) using the same interface.
  29. */
  30. namespace boost {
  31. namespace algorithm {
  32. // collection_traits template class -----------------------------------------//
  33. //! collection_traits class
  34. /*!
  35. Collection traits provide uniform access to different types of
  36. collections. This functionality allows to write generic algorithms
  37. which work with several different kinds of collections.
  38. Currently following collection types are supported:
  39. - containers with STL compatible container interface ( see ContainerConcept )
  40. ( i.e. \c std::vector<>, \c std::list<>, \c std::string<> ... )
  41. - c-style array
  42. ( \c char[10], \c int[15] ... )
  43. - null-terminated c-strings
  44. ( \c char*, \c wchar_T* )
  45. - std::pair of iterators
  46. ( i.e \c std::pair<vector<int>::iterator,vector<int>::iterator> )
  47. Collection traits provide an external collection interface operations.
  48. All are accessible using free-standing functions.
  49. The following operations are supported:
  50. - \c size()
  51. - \c empty()
  52. - \c begin()
  53. - \c end()
  54. Container traits have somewhat limited functionality on compilers not
  55. supporting partial template specialization and partial template ordering.
  56. */
  57. template< typename T >
  58. struct collection_traits
  59. {
  60. private:
  61. typedef typename ::boost::mpl::eval_if<
  62. ::boost::algorithm::detail::is_pair<T>,
  63. detail::pair_container_traits_selector<T>,
  64. typename ::boost::mpl::eval_if<
  65. ::boost::is_array<T>,
  66. detail::array_container_traits_selector<T>,
  67. typename ::boost::mpl::eval_if<
  68. ::boost::is_pointer<T>,
  69. detail::pointer_container_traits_selector<T>,
  70. detail::default_container_traits_selector<T>
  71. >
  72. >
  73. >::type container_helper_type;
  74. public:
  75. //! Function type
  76. typedef container_helper_type function_type;
  77. //! Value type
  78. typedef typename
  79. container_helper_type::value_type value_type;
  80. //! Size type
  81. typedef typename
  82. container_helper_type::size_type size_type;
  83. //! Iterator type
  84. typedef typename
  85. container_helper_type::iterator iterator;
  86. //! Const iterator type
  87. typedef typename
  88. container_helper_type::const_iterator const_iterator;
  89. //! Result iterator type ( iterator of const_iterator, depending on the constness of the container )
  90. typedef typename
  91. container_helper_type::result_iterator result_iterator;
  92. //! Difference type
  93. typedef typename
  94. container_helper_type::difference_type difference_type;
  95. }; // 'collection_traits'
  96. // collection_traits metafunctions -----------------------------------------//
  97. //! Container value_type trait
  98. /*!
  99. Extract the type of elements contained in a container
  100. */
  101. template< typename C >
  102. struct value_type_of
  103. {
  104. typedef typename collection_traits<C>::value_type type;
  105. };
  106. //! Container difference trait
  107. /*!
  108. Extract the container's difference type
  109. */
  110. template< typename C >
  111. struct difference_type_of
  112. {
  113. typedef typename collection_traits<C>::difference_type type;
  114. };
  115. //! Container iterator trait
  116. /*!
  117. Extract the container's iterator type
  118. */
  119. template< typename C >
  120. struct iterator_of
  121. {
  122. typedef typename collection_traits<C>::iterator type;
  123. };
  124. //! Container const_iterator trait
  125. /*!
  126. Extract the container's const_iterator type
  127. */
  128. template< typename C >
  129. struct const_iterator_of
  130. {
  131. typedef typename collection_traits<C>::const_iterator type;
  132. };
  133. //! Container result_iterator
  134. /*!
  135. Extract the container's result_iterator type. This type maps to \c C::iterator
  136. for mutable container and \c C::const_iterator for const containers.
  137. */
  138. template< typename C >
  139. struct result_iterator_of
  140. {
  141. typedef typename collection_traits<C>::result_iterator type;
  142. };
  143. // collection_traits related functions -----------------------------------------//
  144. //! Free-standing size() function
  145. /*!
  146. Get the size of the container. Uses collection_traits.
  147. */
  148. template< typename C >
  149. inline typename collection_traits<C>::size_type
  150. size( const C& c )
  151. {
  152. return collection_traits<C>::function_type::size( c );
  153. }
  154. //! Free-standing empty() function
  155. /*!
  156. Check whether the container is empty. Uses container traits.
  157. */
  158. template< typename C >
  159. inline bool empty( const C& c )
  160. {
  161. return collection_traits<C>::function_type::empty( c );
  162. }
  163. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  164. //! Free-standing begin() function
  165. /*!
  166. Get the begin iterator of the container. Uses collection_traits.
  167. */
  168. template< typename C >
  169. inline typename collection_traits<C>::iterator
  170. begin( C& c )
  171. {
  172. return collection_traits<C>::function_type::begin( c );
  173. }
  174. //! Free-standing begin() function
  175. /*!
  176. \overload
  177. */
  178. template< typename C >
  179. inline typename collection_traits<C>::const_iterator
  180. begin( const C& c )
  181. {
  182. return collection_traits<C>::function_type::begin( c );
  183. }
  184. //! Free-standing end() function
  185. /*!
  186. Get the begin iterator of the container. Uses collection_traits.
  187. */
  188. template< typename C >
  189. inline typename collection_traits<C>::iterator
  190. end( C& c )
  191. {
  192. return collection_traits<C>::function_type::end( c );
  193. }
  194. //! Free-standing end() function
  195. /*!
  196. \overload
  197. */
  198. template< typename C >
  199. inline typename collection_traits<C>::const_iterator
  200. end( const C& c )
  201. {
  202. return collection_traits<C>::function_type::end( c );
  203. }
  204. #else // BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  205. //! Free-standing begin() function
  206. /*!
  207. \overload
  208. */
  209. template< typename C >
  210. inline typename collection_traits<C>::result_iterator
  211. begin( C& c )
  212. {
  213. return collection_traits<C>::function_type::begin( c );
  214. }
  215. //! Free-standing end() function
  216. /*!
  217. \overload
  218. */
  219. template< typename C >
  220. inline typename collection_traits<C>::result_iterator
  221. end( C& c )
  222. {
  223. return collection_traits<C>::function_type::end( c );
  224. }
  225. #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  226. } // namespace algorithm
  227. } // namespace boost
  228. #endif // BOOST_STRING_COLLECTION_TRAITS_HPP