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