1 // Boost.TypeErasure library
3 // Copyright 2011 Steven Watanabe
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)
11 #ifndef BOOST_TYPE_ERASURE_PARAM_HPP_INCLUDED
12 #define BOOST_TYPE_ERASURE_PARAM_HPP_INCLUDED
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>
28 namespace type_erasure {
30 template<class Concept, class T>
33 template<class Concept>
44 template<class From, class To>
45 struct placeholder_conversion : boost::mpl::false_ {};
47 struct placeholder_conversion<T, T> : boost::mpl::true_ {};
49 struct placeholder_conversion<T, T&> : boost::mpl::true_ {};
51 struct placeholder_conversion<T, const T&> : boost::mpl::true_ {};
53 struct placeholder_conversion<const T, T> : boost::mpl::true_ {};
55 struct placeholder_conversion<const T, const T&> : boost::mpl::true_ {};
57 struct placeholder_conversion<T&, T> : boost::mpl::true_ {};
59 struct placeholder_conversion<T&, T&> : boost::mpl::true_ {};
61 struct placeholder_conversion<T&, const T&> : boost::mpl::true_ {};
63 struct placeholder_conversion<const T&, T> : boost::mpl::true_ {};
65 struct placeholder_conversion<const T&, const T&> : boost::mpl::true_ {};
67 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
69 struct placeholder_conversion<T&&, T> : boost::mpl::true_ {};
71 struct placeholder_conversion<T&&, const T&> : boost::mpl::true_ {};
73 struct placeholder_conversion<T&&, T&&> : boost::mpl::true_ {};
79 #if !__has_feature(cxx_reference_qualified_functions)
81 #define BOOST_NO_FUNCTION_REFERENCE_QUALIFIERS
85 #define BOOST_NO_FUNCTION_REFERENCE_QUALIFIERS
89 * \brief A wrapper to help with overload resolution for functions
90 * operating on an @ref any.
92 * The template arguments are interpreted in
93 * the same way as @ref any.
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
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.
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&&>);
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&>);
125 template<class Concept, class T>
129 friend struct boost::type_erasure::detail::access;
132 typedef void _boost_type_erasure_is_any;
134 typedef param _boost_type_erasure_derived_type;
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>
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,
159 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
161 param(any<Concept, U>&& a
162 #ifndef BOOST_TYPE_ERASURE_DOXYGEN
163 , typename boost::enable_if<
164 ::boost::type_erasure::detail::placeholder_conversion<
171 : _impl(std::move(a))
175 /** Returns the stored @ref any. */
176 any<Concept, T> get() const { return _impl; }
178 any<Concept, T> _impl;
181 #ifndef BOOST_NO_FUNCTION_REFERENCE_QUALIFIERS
183 template<class Concept, class T>
184 class param<Concept, const T&> {
187 friend struct boost::type_erasure::detail::access;
190 typedef void _boost_type_erasure_is_any;
192 typedef param _boost_type_erasure_derived_type;
194 param(const ::boost::type_erasure::detail::storage& data,
195 const ::boost::type_erasure::binding<Concept>& table)
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; }
202 any<Concept, const T&> _impl;
205 template<class Concept, class T>
206 class param<Concept, T&> : public param<Concept, const T&> {
209 friend struct boost::type_erasure::detail::access;
212 typedef void _boost_type_erasure_is_any;
214 typedef param _boost_type_erasure_derived_type;
216 param(const ::boost::type_erasure::detail::storage& data,
217 const ::boost::type_erasure::binding<Concept>& table)
218 : param<Concept, const T&>(data, table)
220 any<Concept, T&> get() const
222 return any<Concept, T&>(
223 ::boost::type_erasure::detail::access::data(this->_impl),
224 ::boost::type_erasure::detail::access::table(this->_impl));
228 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
230 template<class Concept, class T>
231 class param<Concept, T&&> : public param<Concept, const T&> {
234 friend struct boost::type_erasure::detail::access;
237 typedef void _boost_type_erasure_is_any;
239 typedef param _boost_type_erasure_derived_type;
241 param(const ::boost::type_erasure::detail::storage& data,
242 const ::boost::type_erasure::binding<Concept>& table)
243 : param<Concept, const T&>(data, table)
245 any<Concept, T&&> get() const
247 return any<Concept, T&&>(
248 ::boost::type_erasure::detail::access::data(this->_impl),
249 ::boost::type_erasure::detail::access::table(this->_impl));
258 * \brief Metafunction that creates a @ref param.
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.
266 * \see derived, rebind_any
268 template<class Any, class T>
270 #ifdef BOOST_TYPE_ERASURE_DOXYGEN
271 typedef detail::unspecified type;
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>,