]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/range/detail/collection_traits.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / range / detail / collection_traits.hpp
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/type_traits/is_array.hpp>
26 #include <boost/type_traits/is_pointer.hpp>
27 #include <boost/mpl/eval_if.hpp>
28
29 // Implementation
30 #include <boost/range/detail/collection_traits_detail.hpp>
31
32 /*! \file
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.
37 */
38
39 namespace boost {
40 namespace algorithm {
41
42 // collection_traits template class -----------------------------------------//
43
44 //! collection_traits class
45 /*!
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.
49
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<> ... )
53 - c-style array
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> )
59
60 Collection traits provide an external collection interface operations.
61 All are accessible using free-standing functions.
62
63 The following operations are supported:
64 - \c size()
65 - \c empty()
66 - \c begin()
67 - \c end()
68
69 Container traits have somewhat limited functionality on compilers not
70 supporting partial template specialization and partial template ordering.
71 */
72 template< typename T >
73 struct collection_traits
74 {
75 private:
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<
80 ::boost::is_array<T>,
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>
86 >
87 >
88 >::type container_helper_type;
89 public:
90 //! Function type
91 typedef container_helper_type function_type;
92 //! Value type
93 typedef typename
94 container_helper_type::value_type value_type;
95 //! Size type
96 typedef typename
97 container_helper_type::size_type size_type;
98 //! Iterator type
99 typedef typename
100 container_helper_type::iterator iterator;
101 //! Const iterator type
102 typedef typename
103 container_helper_type::const_iterator const_iterator;
104 //! Result iterator type ( iterator of const_iterator, depending on the constness of the container )
105 typedef typename
106 container_helper_type::result_iterator result_iterator;
107 //! Difference type
108 typedef typename
109 container_helper_type::difference_type difference_type;
110
111 }; // 'collection_traits'
112
113 // collection_traits metafunctions -----------------------------------------//
114
115 //! Container value_type trait
116 /*!
117 Extract the type of elements contained in a container
118 */
119 template< typename C >
120 struct value_type_of
121 {
122 typedef typename collection_traits<C>::value_type type;
123 };
124
125 //! Container difference trait
126 /*!
127 Extract the container's difference type
128 */
129 template< typename C >
130 struct difference_type_of
131 {
132 typedef typename collection_traits<C>::difference_type type;
133 };
134
135 //! Container iterator trait
136 /*!
137 Extract the container's iterator type
138 */
139 template< typename C >
140 struct iterator_of
141 {
142 typedef typename collection_traits<C>::iterator type;
143 };
144
145 //! Container const_iterator trait
146 /*!
147 Extract the container's const_iterator type
148 */
149 template< typename C >
150 struct const_iterator_of
151 {
152 typedef typename collection_traits<C>::const_iterator type;
153 };
154
155
156 //! Container result_iterator
157 /*!
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.
160 */
161 template< typename C >
162 struct result_iterator_of
163 {
164 typedef typename collection_traits<C>::result_iterator type;
165 };
166
167 // collection_traits related functions -----------------------------------------//
168
169 //! Free-standing size() function
170 /*!
171 Get the size of the container. Uses collection_traits.
172 */
173 template< typename C >
174 inline typename collection_traits<C>::size_type
175 size( const C& c )
176 {
177 return collection_traits<C>::function_type::size( c );
178 }
179
180 //! Free-standing empty() function
181 /*!
182 Check whether the container is empty. Uses container traits.
183 */
184 template< typename C >
185 inline bool empty( const C& c )
186 {
187 return collection_traits<C>::function_type::empty( c );
188 }
189
190 //! Free-standing begin() function
191 /*!
192 Get the begin iterator of the container. Uses collection_traits.
193 */
194 template< typename C >
195 inline typename collection_traits<C>::iterator
196 begin( C& c )
197 {
198 return collection_traits<C>::function_type::begin( c );
199 }
200
201 //! Free-standing begin() function
202 /*!
203 \overload
204 */
205 template< typename C >
206 inline typename collection_traits<C>::const_iterator
207 begin( const C& c )
208 {
209 return collection_traits<C>::function_type::begin( c );
210 }
211
212 //! Free-standing end() function
213 /*!
214 Get the begin iterator of the container. Uses collection_traits.
215 */
216 template< typename C >
217 inline typename collection_traits<C>::iterator
218 end( C& c )
219 {
220 return collection_traits<C>::function_type::end( c );
221 }
222
223 //! Free-standing end() function
224 /*!
225 \overload
226 */
227 template< typename C >
228 inline typename collection_traits<C>::const_iterator
229 end( const C& c )
230 {
231 return collection_traits<C>::function_type::end( c );
232 }
233
234 } // namespace algorithm
235 } // namespace boost
236
237 #endif // BOOST_STRING_COLLECTION_TRAITS_HPP