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 #ifndef BOOST_TYPE_ERASURE_DOXYGEN
32 template<class Concept, class T>
35 template<class Concept>
48 template<class From, class To>
49 struct placeholder_conversion : boost::mpl::false_ {};
51 struct placeholder_conversion<T, T> : boost::mpl::true_ {};
53 struct placeholder_conversion<T, T&> : boost::mpl::true_ {};
55 struct placeholder_conversion<T, const T&> : boost::mpl::true_ {};
57 struct placeholder_conversion<const T, T> : boost::mpl::true_ {};
59 struct placeholder_conversion<const T, const T&> : boost::mpl::true_ {};
61 struct placeholder_conversion<T&, T> : boost::mpl::true_ {};
63 struct placeholder_conversion<T&, T&> : boost::mpl::true_ {};
65 struct placeholder_conversion<T&, const T&> : boost::mpl::true_ {};
67 struct placeholder_conversion<const T&, T> : boost::mpl::true_ {};
69 struct placeholder_conversion<const T&, const T&> : boost::mpl::true_ {};
71 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
73 struct placeholder_conversion<T&&, T> : boost::mpl::true_ {};
75 struct placeholder_conversion<T&&, const T&> : boost::mpl::true_ {};
77 struct placeholder_conversion<T&&, T&&> : boost::mpl::true_ {};
83 * \brief A wrapper to help with overload resolution for functions
84 * operating on an @ref any.
86 * The template arguments are interpreted in
87 * the same way as @ref any.
89 * A parameter of type @ref param can be initialized
90 * with an @ref any that has the same @c Concept
91 * and base placeholder when there exists a corresponding
92 * standard conversion for the placeholder.
93 * A conversion sequence from @ref any "any<C, P>" to @ref param "param<C, P1>" is
94 * a better conversion sequence than @ref any "any<C, P>" to @ref param "param<C, P2>"
95 * iff the corresponding placeholder standard conversion
96 * sequence from P to P1 is a better conversion sequence than
99 * \note Overloading based on cv-qualifiers and rvalue-ness is
100 * only supported in C++11. In C++03, all conversion sequences
101 * from @ref any to @ref param have the same rank.
106 * void f(param<C, _a&>);
107 * void f(param<C, const _a&>);
108 * void g(param<C, const _a&>);
109 * void g(param<C, _a&&>);
112 * f(any<C, _a>()); // calls void f(param<C, const _a&>);
113 * f(a); // calls void f(param<C, _a&>); (ambiguous in C++03)
114 * g(any<C, _a>()); // calls void g(param<C, _a&&>); (ambiguous in C++03)
115 * g(a); // calls void g(param<C, const _a&>);
119 template<class Concept, class T>
123 friend struct boost::type_erasure::detail::access;
126 typedef void _boost_type_erasure_is_any;
128 typedef param _boost_type_erasure_derived_type;
131 param(any<Concept, U>& a
132 #ifndef BOOST_TYPE_ERASURE_DOXYGEN
133 , typename boost::enable_if<
134 ::boost::type_erasure::detail::placeholder_conversion<U, T>
141 param(const any<Concept, U>& a
142 #ifndef BOOST_TYPE_ERASURE_DOXYGEN
143 , typename boost::enable_if<
144 ::boost::type_erasure::detail::placeholder_conversion<
145 typename ::boost::add_const<U>::type,
153 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
155 param(any<Concept, U>&& a
156 #ifndef BOOST_TYPE_ERASURE_DOXYGEN
157 , typename boost::enable_if<
158 ::boost::type_erasure::detail::placeholder_conversion<
165 : _impl(std::move(a))
170 param(const ::boost::type_erasure::detail::storage& data,
171 const ::boost::type_erasure::binding<Concept>& table)
175 /** Returns the stored @ref any. */
176 any<Concept, T> get() const { return _impl; }
178 any<Concept, T> _impl;
181 #if !defined(BOOST_NO_CXX11_REF_QUALIFIERS) && !defined(BOOST_TYPE_ERASURE_DOXYGEN)
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; }
203 _impl_t(const ::boost::type_erasure::detail::storage& data_,
204 const ::boost::type_erasure::binding<Concept>& table_)
205 : table(table_), data(data_)
207 _impl_t(const any<Concept, T>& u)
208 : table(::boost::type_erasure::detail::access::table(u)),
209 data(::boost::type_erasure::detail::access::data(u))
211 // It's safe to capture the table by reference, because
212 // the user's argument should out-live us. storage is
213 // just a void*, so we don't need to add indirection.
214 const ::boost::type_erasure::binding<Concept>& table;
215 ::boost::type_erasure::detail::storage data;
219 template<class Concept, class T>
220 class param<Concept, T&> : public param<Concept, const T&> {
223 friend struct boost::type_erasure::detail::access;
226 typedef void _boost_type_erasure_is_any;
228 typedef param _boost_type_erasure_derived_type;
230 param(const ::boost::type_erasure::detail::storage& data,
231 const ::boost::type_erasure::binding<Concept>& table)
232 : param<Concept, const T&>(data, table)
234 any<Concept, T&> get() const
236 return any<Concept, T&>(
237 ::boost::type_erasure::detail::access::data(this->_impl),
238 ::boost::type_erasure::detail::access::table(this->_impl));
242 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
244 template<class Concept, class T>
245 class param<Concept, T&&> : public param<Concept, const T&> {
248 friend struct boost::type_erasure::detail::access;
251 typedef void _boost_type_erasure_is_any;
253 typedef param _boost_type_erasure_derived_type;
255 param(const ::boost::type_erasure::detail::storage& data,
256 const ::boost::type_erasure::binding<Concept>& table)
257 : param<Concept, const T&>(data, table)
259 any<Concept, T&&> get() const
261 return any<Concept, T&&>(
262 ::boost::type_erasure::detail::access::data(this->_impl),
263 ::boost::type_erasure::detail::access::table(this->_impl));
272 * \brief Metafunction that creates a @ref param.
274 * If @c T is a (cv/reference qualified) placeholder,
275 * returns @ref param<@ref concept_of "concept_of<Any>::type", T>,
276 * otherwise, returns T. This metafunction is intended
277 * to be used for function arguments in specializations of
278 * @ref concept_interface.
280 * \see derived, rebind_any
282 template<class Any, class T>
284 #ifdef BOOST_TYPE_ERASURE_DOXYGEN
285 typedef detail::unspecified type;
287 typedef typename ::boost::mpl::if_<
288 ::boost::type_erasure::is_placeholder<
289 typename ::boost::remove_cv<
290 typename ::boost::remove_reference<T>::type>::type>,
291 param<typename ::boost::type_erasure::concept_of<Any>::type, T>,
297 #ifndef BOOST_NO_CXX11_TEMPLATE_ALIASES
299 template<class Any, class T>
300 using as_param_t = typename ::boost::type_erasure::as_param<Any, T>::type;