1 // Boost string_algo library collection_traits.hpp header file -------------//
3 // Copyright Pavol Droba 2002-2003. Use, modification and
4 // distribution is subject to the Boost Software License, Version
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // (C) Copyright Thorsten Ottosen 2002-2003. Use, modification and
9 // distribution is subject to the Boost Software License, Version
10 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
13 // (C) Copyright Jeremy Siek 2001. Use, modification and
14 // distribution is subject to the Boost Software License, Version
15 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
16 // http://www.boost.org/LICENSE_1_0.txt)
18 // Original idea of container traits was proposed by Jeremy Siek and
19 // Thorsten Ottosen. This implementation is lightweighted version
20 // of container_traits adapter for usage with string_algo library
22 #ifndef BOOST_RANGE_STRING_COLLECTION_TRAITS_HPP
23 #define BOOST_RANGE_STRING_COLLECTION_TRAITS_HPP
25 #include <boost/type_traits/is_array.hpp>
26 #include <boost/type_traits/is_pointer.hpp>
27 #include <boost/mpl/eval_if.hpp>
30 #include <boost/range/detail/collection_traits_detail.hpp>
33 Defines collection_traits class and related free-standing functions.
34 This facility is used to unify the access to different types of collections.
35 It allows the algorithms in the library to work with STL collections, c-style
36 array, null-terminated c-strings (and more) using the same interface.
42 // collection_traits template class -----------------------------------------//
44 //! collection_traits class
46 Collection traits provide uniform access to different types of
47 collections. This functionality allows to write generic algorithms
48 which work with several different kinds of collections.
50 Currently following collection types are supported:
51 - containers with STL compatible container interface ( see ContainerConcept )
52 ( i.e. \c std::vector<>, \c std::list<>, \c std::string<> ... )
54 ( \c char[10], \c int[15] ... )
55 - null-terminated c-strings
56 ( \c char*, \c wchar_T* )
57 - std::pair of iterators
58 ( i.e \c std::pair<vector<int>::iterator,vector<int>::iterator> )
60 Collection traits provide an external collection interface operations.
61 All are accessible using free-standing functions.
63 The following operations are supported:
69 Container traits have somewhat limited functionality on compilers not
70 supporting partial template specialization and partial template ordering.
72 template< typename T >
73 struct collection_traits
76 typedef typename ::boost::mpl::eval_if<
77 ::boost::algorithm::detail::is_pair<T>,
78 detail::pair_container_traits_selector<T>,
79 typename ::boost::mpl::eval_if<
81 detail::array_container_traits_selector<T>,
82 typename ::boost::mpl::eval_if<
83 ::boost::is_pointer<T>,
84 detail::pointer_container_traits_selector<T>,
85 detail::default_container_traits_selector<T>
88 >::type container_helper_type;
91 typedef container_helper_type function_type;
94 container_helper_type::value_type value_type;
97 container_helper_type::size_type size_type;
100 container_helper_type::iterator iterator;
101 //! Const iterator type
103 container_helper_type::const_iterator const_iterator;
104 //! Result iterator type ( iterator of const_iterator, depending on the constness of the container )
106 container_helper_type::result_iterator result_iterator;
109 container_helper_type::difference_type difference_type;
111 }; // 'collection_traits'
113 // collection_traits metafunctions -----------------------------------------//
115 //! Container value_type trait
117 Extract the type of elements contained in a container
119 template< typename C >
122 typedef typename collection_traits<C>::value_type type;
125 //! Container difference trait
127 Extract the container's difference type
129 template< typename C >
130 struct difference_type_of
132 typedef typename collection_traits<C>::difference_type type;
135 //! Container iterator trait
137 Extract the container's iterator type
139 template< typename C >
142 typedef typename collection_traits<C>::iterator type;
145 //! Container const_iterator trait
147 Extract the container's const_iterator type
149 template< typename C >
150 struct const_iterator_of
152 typedef typename collection_traits<C>::const_iterator type;
156 //! Container result_iterator
158 Extract the container's result_iterator type. This type maps to \c C::iterator
159 for mutable container and \c C::const_iterator for const containers.
161 template< typename C >
162 struct result_iterator_of
164 typedef typename collection_traits<C>::result_iterator type;
167 // collection_traits related functions -----------------------------------------//
169 //! Free-standing size() function
171 Get the size of the container. Uses collection_traits.
173 template< typename C >
174 inline typename collection_traits<C>::size_type
177 return collection_traits<C>::function_type::size( c );
180 //! Free-standing empty() function
182 Check whether the container is empty. Uses container traits.
184 template< typename C >
185 inline bool empty( const C& c )
187 return collection_traits<C>::function_type::empty( c );
190 //! Free-standing begin() function
192 Get the begin iterator of the container. Uses collection_traits.
194 template< typename C >
195 inline typename collection_traits<C>::iterator
198 return collection_traits<C>::function_type::begin( c );
201 //! Free-standing begin() function
205 template< typename C >
206 inline typename collection_traits<C>::const_iterator
209 return collection_traits<C>::function_type::begin( c );
212 //! Free-standing end() function
214 Get the begin iterator of the container. Uses collection_traits.
216 template< typename C >
217 inline typename collection_traits<C>::iterator
220 return collection_traits<C>::function_type::end( c );
223 //! Free-standing end() function
227 template< typename C >
228 inline typename collection_traits<C>::const_iterator
231 return collection_traits<C>::function_type::end( c );
234 } // namespace algorithm
237 #endif // BOOST_STRING_COLLECTION_TRAITS_HPP