]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Boost.TypeErasure library |
2 | // | |
3 | // Copyright 2011 Steven Watanabe | |
4 | // | |
5 | // Distributed under the Boost Software License Version 1.0. (See | |
6 | // accompanying file LICENSE_1_0.txt or copy at | |
7 | // http://www.boost.org/LICENSE_1_0.txt) | |
8 | // | |
9 | // $Id$ | |
10 | ||
11 | #ifndef BOOST_TYPE_ERASURE_PARAM_HPP_INCLUDED | |
12 | #define BOOST_TYPE_ERASURE_PARAM_HPP_INCLUDED | |
13 | ||
14 | #include <boost/config.hpp> | |
15 | #include <boost/utility/enable_if.hpp> | |
16 | #include <boost/type_traits/is_same.hpp> | |
17 | #include <boost/type_traits/add_const.hpp> | |
18 | #include <boost/type_traits/remove_cv.hpp> | |
19 | #include <boost/type_traits/remove_reference.hpp> | |
20 | #include <boost/mpl/bool.hpp> | |
21 | #include <boost/mpl/if.hpp> | |
22 | #include <boost/type_erasure/detail/access.hpp> | |
23 | #include <boost/type_erasure/detail/storage.hpp> | |
24 | #include <boost/type_erasure/is_placeholder.hpp> | |
25 | #include <boost/type_erasure/concept_of.hpp> | |
26 | ||
27 | namespace boost { | |
28 | namespace type_erasure { | |
29 | ||
30 | template<class Concept, class T> | |
31 | class any; | |
32 | ||
33 | template<class Concept> | |
34 | class binding; | |
35 | ||
36 | namespace detail { | |
37 | ||
38 | struct access; | |
39 | ||
40 | } | |
41 | ||
42 | namespace detail { | |
43 | ||
44 | template<class From, class To> | |
45 | struct placeholder_conversion : boost::mpl::false_ {}; | |
46 | template<class T> | |
47 | struct placeholder_conversion<T, T> : boost::mpl::true_ {}; | |
48 | template<class T> | |
49 | struct placeholder_conversion<T, T&> : boost::mpl::true_ {}; | |
50 | template<class T> | |
51 | struct placeholder_conversion<T, const T&> : boost::mpl::true_ {}; | |
52 | template<class T> | |
53 | struct placeholder_conversion<const T, T> : boost::mpl::true_ {}; | |
54 | template<class T> | |
55 | struct placeholder_conversion<const T, const T&> : boost::mpl::true_ {}; | |
56 | template<class T> | |
57 | struct placeholder_conversion<T&, T> : boost::mpl::true_ {}; | |
58 | template<class T> | |
59 | struct placeholder_conversion<T&, T&> : boost::mpl::true_ {}; | |
60 | template<class T> | |
61 | struct placeholder_conversion<T&, const T&> : boost::mpl::true_ {}; | |
62 | template<class T> | |
63 | struct placeholder_conversion<const T&, T> : boost::mpl::true_ {}; | |
64 | template<class T> | |
65 | struct placeholder_conversion<const T&, const T&> : boost::mpl::true_ {}; | |
66 | ||
67 | #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES | |
68 | template<class T> | |
69 | struct placeholder_conversion<T&&, T> : boost::mpl::true_ {}; | |
70 | template<class T> | |
71 | struct placeholder_conversion<T&&, const T&> : boost::mpl::true_ {}; | |
72 | template<class T> | |
73 | struct placeholder_conversion<T&&, T&&> : boost::mpl::true_ {}; | |
74 | #endif | |
75 | ||
76 | } | |
77 | ||
78 | #ifdef __clang__ | |
79 | #if !__has_feature(cxx_reference_qualified_functions) | |
80 | /** INTERNAL ONLY */ | |
81 | #define BOOST_NO_FUNCTION_REFERENCE_QUALIFIERS | |
82 | #endif | |
83 | #else | |
84 | /** INTERNAL ONLY */ | |
85 | #define BOOST_NO_FUNCTION_REFERENCE_QUALIFIERS | |
86 | #endif | |
87 | ||
88 | /** | |
89 | * \brief A wrapper to help with overload resolution for functions | |
90 | * operating on an @ref any. | |
91 | * | |
92 | * The template arguments are interpreted in | |
93 | * the same way as @ref any. | |
94 | * | |
95 | * A parameter of type @ref param can be initialized | |
96 | * with an @ref any that has the same @c Concept | |
97 | * and base placeholder when there exists a corresponding | |
98 | * standard conversion for the placeholder. | |
99 | * A conversion sequence from @ref any<C, P> to @ref param<C, P1> is | |
100 | * a better conversion sequence than @ref any<C, P> to @ref param<C, P2> | |
101 | * iff the corresponding placeholder standard conversion | |
102 | * sequence from P to P1 is a better conversion sequence than | |
103 | * P to P2. | |
104 | * | |
105 | * \note Overloading based on cv-qualifiers and rvalue-ness is | |
106 | * only supported in C++11. In C++03, all conversion sequences | |
107 | * from @ref any to @ref param have the same rank. | |
108 | * | |
109 | * Example: | |
110 | * | |
111 | * \code | |
112 | * void f(param<C, _a&>); | |
113 | * void f(param<C, const _a&>); | |
114 | * void g(param<C, const _a&>); | |
115 | * void g(param<C, _a&&>); | |
116 | * | |
117 | * any<C, _a> a; | |
118 | * f(any<C, _a>()); // calls void f(param<C, const _a&>); | |
119 | * f(a); // calls void f(param<C, _a&>); (ambiguous in C++03) | |
120 | * g(any<C, _a>()); // calls void g(param<C, _a&&>); (ambiguous in C++03) | |
121 | * g(a); // calls void g(param<C, const _a&>); | |
122 | * \endcode | |
123 | * | |
124 | */ | |
125 | template<class Concept, class T> | |
126 | class param { | |
127 | public: | |
128 | ||
129 | friend struct boost::type_erasure::detail::access; | |
130 | ||
131 | /** INTERNAL ONLY */ | |
132 | typedef void _boost_type_erasure_is_any; | |
133 | /** INTERNAL ONLY */ | |
134 | typedef param _boost_type_erasure_derived_type; | |
135 | ||
136 | template<class U> | |
137 | param(any<Concept, U>& a | |
138 | #ifndef BOOST_TYPE_ERASURE_DOXYGEN | |
139 | , typename boost::enable_if< | |
140 | ::boost::type_erasure::detail::placeholder_conversion<U, T> | |
141 | >::type* = 0 | |
142 | #endif | |
143 | ) | |
144 | : _impl(a) | |
145 | {} | |
146 | template<class U> | |
147 | param(const any<Concept, U>& a | |
148 | #ifndef BOOST_TYPE_ERASURE_DOXYGEN | |
149 | , typename boost::enable_if< | |
150 | ::boost::type_erasure::detail::placeholder_conversion< | |
151 | typename ::boost::add_const<U>::type, | |
152 | T | |
153 | > | |
154 | >::type* = 0 | |
155 | #endif | |
156 | ) | |
157 | : _impl(a) | |
158 | {} | |
159 | #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES | |
160 | template<class U> | |
161 | param(any<Concept, U>&& a | |
162 | #ifndef BOOST_TYPE_ERASURE_DOXYGEN | |
163 | , typename boost::enable_if< | |
164 | ::boost::type_erasure::detail::placeholder_conversion< | |
165 | U&&, | |
166 | T | |
167 | > | |
168 | >::type* = 0 | |
169 | #endif | |
170 | ) | |
171 | : _impl(std::move(a)) | |
172 | {} | |
173 | #endif | |
174 | ||
175 | /** Returns the stored @ref any. */ | |
176 | any<Concept, T> get() const { return _impl; } | |
177 | private: | |
178 | any<Concept, T> _impl; | |
179 | }; | |
180 | ||
181 | #ifndef BOOST_NO_FUNCTION_REFERENCE_QUALIFIERS | |
182 | ||
183 | template<class Concept, class T> | |
184 | class param<Concept, const T&> { | |
185 | public: | |
186 | ||
187 | friend struct boost::type_erasure::detail::access; | |
188 | ||
189 | /** INTERNAL ONLY */ | |
190 | typedef void _boost_type_erasure_is_any; | |
191 | /** INTERNAL ONLY */ | |
192 | typedef param _boost_type_erasure_derived_type; | |
193 | ||
194 | param(const ::boost::type_erasure::detail::storage& data, | |
195 | const ::boost::type_erasure::binding<Concept>& table) | |
196 | : _impl(data, table) | |
197 | {} | |
198 | template<class U> | |
199 | param(U& u, typename boost::enable_if< ::boost::is_same<U, const any<Concept, T> > >::type* = 0) : _impl(u) {} | |
200 | any<Concept, const T&> get() const { return _impl; } | |
201 | protected: | |
202 | any<Concept, const T&> _impl; | |
203 | }; | |
204 | ||
205 | template<class Concept, class T> | |
206 | class param<Concept, T&> : public param<Concept, const T&> { | |
207 | public: | |
208 | ||
209 | friend struct boost::type_erasure::detail::access; | |
210 | ||
211 | /** INTERNAL ONLY */ | |
212 | typedef void _boost_type_erasure_is_any; | |
213 | /** INTERNAL ONLY */ | |
214 | typedef param _boost_type_erasure_derived_type; | |
215 | ||
216 | param(const ::boost::type_erasure::detail::storage& data, | |
217 | const ::boost::type_erasure::binding<Concept>& table) | |
218 | : param<Concept, const T&>(data, table) | |
219 | {} | |
220 | any<Concept, T&> get() const | |
221 | { | |
222 | return any<Concept, T&>( | |
223 | ::boost::type_erasure::detail::access::data(this->_impl), | |
224 | ::boost::type_erasure::detail::access::table(this->_impl)); | |
225 | } | |
226 | }; | |
227 | ||
228 | #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES | |
229 | ||
230 | template<class Concept, class T> | |
231 | class param<Concept, T&&> : public param<Concept, const T&> { | |
232 | public: | |
233 | ||
234 | friend struct boost::type_erasure::detail::access; | |
235 | ||
236 | /** INTERNAL ONLY */ | |
237 | typedef void _boost_type_erasure_is_any; | |
238 | /** INTERNAL ONLY */ | |
239 | typedef param _boost_type_erasure_derived_type; | |
240 | ||
241 | param(const ::boost::type_erasure::detail::storage& data, | |
242 | const ::boost::type_erasure::binding<Concept>& table) | |
243 | : param<Concept, const T&>(data, table) | |
244 | {} | |
245 | any<Concept, T&&> get() const | |
246 | { | |
247 | return any<Concept, T&&>( | |
248 | ::boost::type_erasure::detail::access::data(this->_impl), | |
249 | ::boost::type_erasure::detail::access::table(this->_impl)); | |
250 | } | |
251 | }; | |
252 | ||
253 | #endif | |
254 | ||
255 | #endif | |
256 | ||
257 | /** | |
258 | * \brief Metafunction that creates a @ref param. | |
259 | * | |
260 | * If @c T is a (cv/reference qualifed) placeholder, | |
261 | * returns @ref param<@ref concept_of "concept_of<Any>::type", T>, | |
262 | * otherwise, returns T. This metafunction is intended | |
263 | * to be used for function arguments in specializations of | |
264 | * @ref concept_interface. | |
265 | * | |
266 | * \see derived, rebind_any | |
267 | */ | |
268 | template<class Any, class T> | |
269 | struct as_param { | |
270 | #ifdef BOOST_TYPE_ERASURE_DOXYGEN | |
271 | typedef detail::unspecified type; | |
272 | #else | |
273 | typedef typename ::boost::mpl::if_< | |
274 | ::boost::type_erasure::is_placeholder< | |
275 | typename ::boost::remove_cv< | |
276 | typename ::boost::remove_reference<T>::type>::type>, | |
277 | param<typename ::boost::type_erasure::concept_of<Any>::type, T>, | |
278 | T | |
279 | >::type type; | |
280 | #endif | |
281 | }; | |
282 | ||
283 | } | |
284 | } | |
285 | ||
286 | #endif |