]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | // See http://www.boost.org for updates, documentation, and revision history. | |
9 | ||
10 | #ifndef BOOST_RANGE_STRING_DETAIL_COLLECTION_TRAITS_HPP | |
11 | #define BOOST_RANGE_STRING_DETAIL_COLLECTION_TRAITS_HPP | |
12 | ||
13 | #include <cstddef> | |
14 | #include <string> | |
20effc67 TL |
15 | #include <utility> |
16 | #include <iterator> | |
7c673cae FG |
17 | #include <boost/type_traits/is_array.hpp> |
18 | #include <boost/type_traits/is_pointer.hpp> | |
19 | #include <boost/type_traits/is_const.hpp> | |
20 | #include <boost/type_traits/is_convertible.hpp> | |
21 | #include <boost/type_traits/remove_pointer.hpp> | |
22 | #include <boost/type_traits/remove_cv.hpp> | |
23 | #include <boost/mpl/eval_if.hpp> | |
24 | #include <boost/mpl/identity.hpp> | |
25 | #include <boost/mpl/vector.hpp> | |
26 | #include <boost/mpl/fold.hpp> | |
7c673cae FG |
27 | |
28 | // Container traits implementation --------------------------------------------------------- | |
29 | ||
30 | namespace boost { | |
31 | namespace algorithm { | |
32 | namespace detail { | |
33 | ||
34 | // Default collection traits ----------------------------------------------------------------- | |
35 | ||
36 | // Default collection helper | |
37 | /* | |
38 | Wraps std::container compliant containers | |
39 | */ | |
20effc67 | 40 | template< typename ContainerT > |
7c673cae FG |
41 | struct default_container_traits |
42 | { | |
43 | typedef typename ContainerT::value_type value_type; | |
44 | typedef typename ContainerT::iterator iterator; | |
45 | typedef typename ContainerT::const_iterator const_iterator; | |
46 | typedef typename | |
47 | ::boost::mpl::if_< ::boost::is_const<ContainerT>, | |
48 | const_iterator, | |
49 | iterator | |
50 | >::type result_iterator; | |
51 | typedef typename ContainerT::difference_type difference_type; | |
52 | typedef typename ContainerT::size_type size_type; | |
20effc67 | 53 | |
7c673cae FG |
54 | // static operations |
55 | template< typename C > | |
56 | static size_type size( const C& c ) | |
57 | { | |
58 | return c.size(); | |
59 | } | |
60 | ||
61 | template< typename C > | |
62 | static bool empty( const C& c ) | |
63 | { | |
64 | return c.empty(); | |
65 | } | |
66 | ||
7c673cae FG |
67 | template< typename C > |
68 | static iterator begin( C& c ) | |
69 | { | |
70 | return c.begin(); | |
71 | } | |
72 | ||
73 | template< typename C > | |
74 | static const_iterator begin( const C& c ) | |
75 | { | |
76 | return c.begin(); | |
77 | } | |
78 | ||
79 | template< typename C > | |
80 | static iterator end( C& c ) | |
81 | { | |
82 | return c.end(); | |
83 | } | |
84 | ||
85 | template< typename C > | |
86 | static const_iterator end( const C& c ) | |
87 | { | |
88 | return c.end(); | |
89 | } | |
90 | ||
7c673cae FG |
91 | }; |
92 | ||
93 | template<typename T> | |
94 | struct default_container_traits_selector | |
95 | { | |
96 | typedef default_container_traits<T> type; | |
97 | }; | |
98 | ||
99 | // Pair container traits --------------------------------------------------------------------- | |
100 | ||
101 | typedef double yes_type; | |
102 | typedef char no_type; | |
103 | ||
104 | // pair selector | |
105 | template< typename T, typename U > | |
106 | yes_type is_pair_impl( const std::pair<T,U>* ); | |
107 | no_type is_pair_impl( ... ); | |
108 | ||
109 | template<typename T> struct is_pair | |
110 | { | |
111 | private: | |
112 | static T* t; | |
113 | public: | |
114 | BOOST_STATIC_CONSTANT( bool, value= | |
115 | sizeof(is_pair_impl(t))==sizeof(yes_type) ); | |
116 | }; | |
117 | ||
118 | // pair helper | |
119 | template< typename PairT > | |
120 | struct pair_container_traits | |
121 | { | |
122 | typedef typename PairT::first_type element_type; | |
123 | ||
20effc67 TL |
124 | typedef typename |
125 | std::iterator_traits<element_type>::value_type value_type; | |
7c673cae | 126 | typedef std::size_t size_type; |
20effc67 TL |
127 | typedef typename |
128 | std::iterator_traits<element_type>::difference_type difference_type; | |
7c673cae FG |
129 | |
130 | typedef element_type iterator; | |
131 | typedef element_type const_iterator; | |
132 | typedef element_type result_iterator; | |
133 | ||
134 | // static operations | |
135 | template< typename P > | |
136 | static size_type size( const P& p ) | |
137 | { | |
138 | difference_type diff = std::distance( p.first, p.second ); | |
139 | if ( diff < 0 ) | |
140 | return 0; | |
141 | else | |
142 | return diff; | |
143 | } | |
144 | ||
145 | template< typename P > | |
146 | static bool empty( const P& p ) | |
147 | { | |
148 | return p.first==p.second; | |
149 | } | |
150 | ||
151 | template< typename P > | |
152 | static const_iterator begin( const P& p ) | |
153 | { | |
154 | return p.first; | |
155 | } | |
156 | ||
157 | template< typename P > | |
158 | static const_iterator end( const P& p ) | |
159 | { | |
160 | return p.second; | |
161 | } | |
162 | }; // 'pair_container_helper' | |
163 | ||
164 | template<typename T> | |
165 | struct pair_container_traits_selector | |
166 | { | |
167 | typedef pair_container_traits<T> type; | |
168 | }; | |
169 | ||
170 | // Array container traits --------------------------------------------------------------- | |
171 | ||
172 | // array traits ( partial specialization ) | |
173 | template< typename T > | |
174 | struct array_traits; | |
175 | ||
176 | template< typename T, std::size_t sz > | |
177 | struct array_traits<T[sz]> | |
178 | { | |
179 | // typedef | |
180 | typedef T* iterator; | |
181 | typedef const T* const_iterator; | |
182 | typedef T value_type; | |
183 | typedef std::size_t size_type; | |
184 | typedef std::ptrdiff_t difference_type; | |
185 | ||
186 | // size of the array ( static ); | |
187 | BOOST_STATIC_CONSTANT( size_type, array_size = sz ); | |
188 | }; | |
189 | ||
20effc67 | 190 | |
7c673cae FG |
191 | // array length resolving |
192 | /* | |
193 | Lenght of string contained in a static array could | |
194 | be different from the size of the array. | |
195 | For string processing we need the length without | |
196 | terminating 0. | |
197 | ||
198 | Therefore, the length is calculated for char and wchar_t | |
199 | using char_traits, rather then simply returning | |
200 | the array size. | |
201 | */ | |
202 | template< typename T > | |
203 | struct array_length_selector | |
204 | { | |
205 | template< typename TraitsT > | |
206 | struct array_length | |
207 | { | |
208 | typedef typename | |
209 | TraitsT::size_type size_type; | |
210 | ||
211 | BOOST_STATIC_CONSTANT( | |
212 | size_type, | |
213 | array_size=TraitsT::array_size ); | |
214 | ||
215 | template< typename A > | |
216 | static size_type length( const A& ) | |
217 | { | |
218 | return array_size; | |
219 | } | |
220 | ||
221 | template< typename A > | |
222 | static bool empty( const A& ) | |
223 | { | |
224 | return array_size==0; | |
225 | } | |
226 | }; | |
227 | }; | |
228 | ||
229 | // specialization for char | |
230 | template<> | |
231 | struct array_length_selector<char> | |
232 | { | |
233 | template< typename TraitsT > | |
234 | struct array_length | |
235 | { | |
236 | typedef typename | |
237 | TraitsT::size_type size_type; | |
238 | ||
239 | template< typename A > | |
240 | static size_type length( const A& a ) | |
241 | { | |
242 | if ( a==0 ) | |
243 | return 0; | |
244 | else | |
245 | return std::char_traits<char>::length(a); | |
246 | } | |
20effc67 | 247 | |
7c673cae FG |
248 | template< typename A > |
249 | static bool empty( const A& a ) | |
250 | { | |
251 | return a==0 || a[0]==0; | |
252 | } | |
253 | }; | |
254 | }; | |
255 | ||
256 | // specialization for wchar_t | |
257 | template<> | |
258 | struct array_length_selector<wchar_t> | |
259 | { | |
260 | template< typename TraitsT > | |
261 | struct array_length | |
262 | { | |
263 | typedef typename | |
264 | TraitsT::size_type size_type; | |
265 | ||
266 | template< typename A > | |
267 | static size_type length( const A& a ) | |
268 | { | |
269 | if ( a==0 ) | |
270 | return 0; | |
271 | else | |
272 | return std::char_traits<wchar_t>::length(a); | |
273 | } | |
274 | ||
275 | template< typename A > | |
276 | static bool empty( const A& a ) | |
277 | { | |
278 | return a==0 || a[0]==0; | |
279 | } | |
280 | }; | |
281 | }; | |
282 | ||
283 | template< typename T > | |
284 | struct array_container_traits | |
285 | { | |
286 | private: | |
287 | // resolve array traits | |
288 | typedef array_traits<T> traits_type; | |
289 | ||
290 | public: | |
291 | typedef typename | |
292 | traits_type::value_type value_type; | |
293 | typedef typename | |
294 | traits_type::iterator iterator; | |
295 | typedef typename | |
296 | traits_type::const_iterator const_iterator; | |
297 | typedef typename | |
298 | traits_type::size_type size_type; | |
299 | typedef typename | |
300 | traits_type::difference_type difference_type; | |
301 | ||
302 | typedef typename | |
303 | ::boost::mpl::if_< ::boost::is_const<T>, | |
304 | const_iterator, | |
305 | iterator | |
306 | >::type result_iterator; | |
20effc67 | 307 | |
7c673cae FG |
308 | private: |
309 | // resolve array size | |
310 | typedef typename | |
311 | ::boost::remove_cv<value_type>::type char_type; | |
312 | typedef typename | |
313 | array_length_selector<char_type>:: | |
314 | BOOST_NESTED_TEMPLATE array_length<traits_type> array_length_type; | |
315 | ||
316 | public: | |
317 | BOOST_STATIC_CONSTANT( size_type, array_size = traits_type::array_size ); | |
318 | ||
319 | // static operations | |
320 | template< typename A > | |
321 | static size_type size( const A& a ) | |
322 | { | |
323 | return array_length_type::length(a); | |
324 | } | |
325 | ||
326 | template< typename A > | |
327 | static bool empty( const A& a ) | |
328 | { | |
329 | return array_length_type::empty(a); | |
330 | } | |
7c673cae | 331 | |
7c673cae FG |
332 | |
333 | template< typename A > | |
334 | static iterator begin( A& a ) | |
335 | { | |
336 | return a; | |
337 | } | |
338 | ||
339 | template< typename A > | |
340 | static const_iterator begin( const A& a ) | |
341 | { | |
342 | return a; | |
343 | } | |
344 | ||
345 | template< typename A > | |
346 | static iterator end( A& a ) | |
347 | { | |
348 | return a+array_length_type::length(a); | |
349 | } | |
350 | ||
351 | template< typename A > | |
352 | static const_iterator end( const A& a ) | |
353 | { | |
354 | return a+array_length_type::length(a); | |
355 | } | |
356 | ||
7c673cae FG |
357 | }; |
358 | ||
359 | template<typename T> | |
360 | struct array_container_traits_selector | |
361 | { | |
362 | typedef array_container_traits<T> type; | |
363 | }; | |
364 | ||
365 | // Pointer container traits --------------------------------------------------------------- | |
366 | ||
367 | template<typename T> | |
368 | struct pointer_container_traits | |
369 | { | |
370 | typedef typename | |
371 | ::boost::remove_pointer<T>::type value_type; | |
372 | ||
373 | typedef typename | |
374 | ::boost::remove_cv<value_type>::type char_type; | |
375 | typedef ::std::char_traits<char_type> char_traits; | |
376 | ||
377 | typedef value_type* iterator; | |
378 | typedef const value_type* const_iterator; | |
379 | typedef std::ptrdiff_t difference_type; | |
380 | typedef std::size_t size_type; | |
381 | ||
382 | typedef typename | |
383 | ::boost::mpl::if_< ::boost::is_const<T>, | |
384 | const_iterator, | |
385 | iterator | |
386 | >::type result_iterator; | |
387 | ||
388 | // static operations | |
389 | template< typename P > | |
390 | static size_type size( const P& p ) | |
391 | { | |
392 | if ( p==0 ) | |
393 | return 0; | |
394 | else | |
395 | return char_traits::length(p); | |
396 | } | |
397 | ||
398 | template< typename P > | |
399 | static bool empty( const P& p ) | |
400 | { | |
401 | return p==0 || p[0]==0; | |
402 | } | |
403 | ||
7c673cae FG |
404 | template< typename P > |
405 | static iterator begin( P& p ) | |
406 | { | |
407 | return p; | |
408 | } | |
409 | ||
410 | template< typename P > | |
411 | static const_iterator begin( const P& p ) | |
412 | { | |
413 | return p; | |
414 | } | |
415 | ||
416 | template< typename P > | |
417 | static iterator end( P& p ) | |
418 | { | |
419 | if ( p==0 ) | |
420 | return p; | |
421 | else | |
422 | return p+char_traits::length(p); | |
423 | } | |
424 | ||
425 | template< typename P > | |
426 | static const_iterator end( const P& p ) | |
427 | { | |
428 | if ( p==0 ) | |
429 | return p; | |
430 | else | |
431 | return p+char_traits::length(p); | |
432 | } | |
433 | ||
7c673cae FG |
434 | }; |
435 | ||
436 | template<typename T> | |
437 | struct pointer_container_traits_selector | |
438 | { | |
439 | typedef pointer_container_traits<T> type; | |
440 | }; | |
441 | ||
442 | } // namespace detail | |
443 | } // namespace algorithm | |
444 | } // namespace boost | |
445 | ||
446 | ||
447 | #endif // BOOST_STRING_DETAIL_COLLECTION_HPP |