collection_traits.hpp 9.3 KB

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