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