param.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. #ifndef BOOST_TYPE_ERASURE_PARAM_HPP_INCLUDED
  11. #define BOOST_TYPE_ERASURE_PARAM_HPP_INCLUDED
  12. #include <boost/config.hpp>
  13. #include <boost/utility/enable_if.hpp>
  14. #include <boost/type_traits/is_same.hpp>
  15. #include <boost/type_traits/add_const.hpp>
  16. #include <boost/type_traits/remove_cv.hpp>
  17. #include <boost/type_traits/remove_reference.hpp>
  18. #include <boost/mpl/bool.hpp>
  19. #include <boost/mpl/if.hpp>
  20. #include <boost/type_erasure/detail/access.hpp>
  21. #include <boost/type_erasure/detail/storage.hpp>
  22. #include <boost/type_erasure/is_placeholder.hpp>
  23. #include <boost/type_erasure/concept_of.hpp>
  24. namespace boost {
  25. namespace type_erasure {
  26. #ifndef BOOST_TYPE_ERASURE_DOXYGEN
  27. template<class Concept, class T>
  28. class any;
  29. template<class Concept>
  30. class binding;
  31. #endif
  32. namespace detail {
  33. struct access;
  34. }
  35. namespace detail {
  36. template<class From, class To>
  37. struct placeholder_conversion : boost::mpl::false_ {};
  38. template<class T>
  39. struct placeholder_conversion<T, T> : boost::mpl::true_ {};
  40. template<class T>
  41. struct placeholder_conversion<T, T&> : boost::mpl::true_ {};
  42. template<class T>
  43. struct placeholder_conversion<T, const T&> : boost::mpl::true_ {};
  44. template<class T>
  45. struct placeholder_conversion<const T, T> : boost::mpl::true_ {};
  46. template<class T>
  47. struct placeholder_conversion<const T, const 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&, T&> : boost::mpl::true_ {};
  52. template<class T>
  53. struct placeholder_conversion<T&, const T&> : boost::mpl::true_ {};
  54. template<class T>
  55. struct placeholder_conversion<const T&, T> : boost::mpl::true_ {};
  56. template<class T>
  57. struct placeholder_conversion<const T&, const T&> : boost::mpl::true_ {};
  58. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  59. template<class T>
  60. struct placeholder_conversion<T&&, T> : boost::mpl::true_ {};
  61. template<class T>
  62. struct placeholder_conversion<T&&, const T&> : boost::mpl::true_ {};
  63. template<class T>
  64. struct placeholder_conversion<T&&, T&&> : boost::mpl::true_ {};
  65. #endif
  66. }
  67. /**
  68. * \brief A wrapper to help with overload resolution for functions
  69. * operating on an @ref any.
  70. *
  71. * The template arguments are interpreted in
  72. * the same way as @ref any.
  73. *
  74. * A parameter of type @ref param can be initialized
  75. * with an @ref any that has the same @c Concept
  76. * and base placeholder when there exists a corresponding
  77. * standard conversion for the placeholder.
  78. * A conversion sequence from @ref any "any<C, P>" to @ref param "param<C, P1>" is
  79. * a better conversion sequence than @ref any "any<C, P>" to @ref param "param<C, P2>"
  80. * iff the corresponding placeholder standard conversion
  81. * sequence from P to P1 is a better conversion sequence than
  82. * P to P2.
  83. *
  84. * \note Overloading based on cv-qualifiers and rvalue-ness is
  85. * only supported in C++11. In C++03, all conversion sequences
  86. * from @ref any to @ref param have the same rank.
  87. *
  88. * Example:
  89. *
  90. * \code
  91. * void f(param<C, _a&>);
  92. * void f(param<C, const _a&>);
  93. * void g(param<C, const _a&>);
  94. * void g(param<C, _a&&>);
  95. *
  96. * any<C, _a> a;
  97. * f(any<C, _a>()); // calls void f(param<C, const _a&>);
  98. * f(a); // calls void f(param<C, _a&>); (ambiguous in C++03)
  99. * g(any<C, _a>()); // calls void g(param<C, _a&&>); (ambiguous in C++03)
  100. * g(a); // calls void g(param<C, const _a&>);
  101. * \endcode
  102. *
  103. */
  104. template<class Concept, class T>
  105. class param {
  106. public:
  107. friend struct boost::type_erasure::detail::access;
  108. /** INTERNAL ONLY */
  109. typedef void _boost_type_erasure_is_any;
  110. /** INTERNAL ONLY */
  111. typedef param _boost_type_erasure_derived_type;
  112. template<class U>
  113. param(any<Concept, U>& a
  114. #ifndef BOOST_TYPE_ERASURE_DOXYGEN
  115. , typename boost::enable_if<
  116. ::boost::type_erasure::detail::placeholder_conversion<U, T>
  117. >::type* = 0
  118. #endif
  119. )
  120. : _impl(a)
  121. {}
  122. template<class U>
  123. param(const any<Concept, U>& a
  124. #ifndef BOOST_TYPE_ERASURE_DOXYGEN
  125. , typename boost::enable_if<
  126. ::boost::type_erasure::detail::placeholder_conversion<
  127. typename ::boost::add_const<U>::type,
  128. T
  129. >
  130. >::type* = 0
  131. #endif
  132. )
  133. : _impl(a)
  134. {}
  135. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  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<
  141. U&&,
  142. T
  143. >
  144. >::type* = 0
  145. #endif
  146. )
  147. : _impl(std::move(a))
  148. {}
  149. #endif
  150. /** INTERNAL ONLY */
  151. param(const ::boost::type_erasure::detail::storage& data,
  152. const ::boost::type_erasure::binding<Concept>& table)
  153. : _impl(data, table)
  154. {}
  155. /** Returns the stored @ref any. */
  156. any<Concept, T> get() const { return _impl; }
  157. private:
  158. any<Concept, T> _impl;
  159. };
  160. #if !defined(BOOST_NO_CXX11_REF_QUALIFIERS) && !defined(BOOST_TYPE_ERASURE_DOXYGEN)
  161. template<class Concept, class T>
  162. class param<Concept, const T&> {
  163. public:
  164. friend struct boost::type_erasure::detail::access;
  165. /** INTERNAL ONLY */
  166. typedef void _boost_type_erasure_is_any;
  167. /** INTERNAL ONLY */
  168. typedef param _boost_type_erasure_derived_type;
  169. param(const ::boost::type_erasure::detail::storage& data,
  170. const ::boost::type_erasure::binding<Concept>& table)
  171. : _impl(data, table)
  172. {}
  173. template<class U>
  174. param(U& u, typename boost::enable_if< ::boost::is_same<U, const any<Concept, T> > >::type* = 0) : _impl(u) {}
  175. any<Concept, const T&> get() const { return _impl; }
  176. protected:
  177. struct _impl_t {
  178. _impl_t(const ::boost::type_erasure::detail::storage& data_,
  179. const ::boost::type_erasure::binding<Concept>& table_)
  180. : table(table_), data(data_)
  181. {}
  182. _impl_t(const any<Concept, T>& u)
  183. : table(::boost::type_erasure::detail::access::table(u)),
  184. data(::boost::type_erasure::detail::access::data(u))
  185. {}
  186. // It's safe to capture the table by reference, because
  187. // the user's argument should out-live us. storage is
  188. // just a void*, so we don't need to add indirection.
  189. const ::boost::type_erasure::binding<Concept>& table;
  190. ::boost::type_erasure::detail::storage data;
  191. } _impl;
  192. };
  193. template<class Concept, class T>
  194. class param<Concept, T&> : public param<Concept, const T&> {
  195. public:
  196. friend struct boost::type_erasure::detail::access;
  197. /** INTERNAL ONLY */
  198. typedef void _boost_type_erasure_is_any;
  199. /** INTERNAL ONLY */
  200. typedef param _boost_type_erasure_derived_type;
  201. param(const ::boost::type_erasure::detail::storage& data,
  202. const ::boost::type_erasure::binding<Concept>& table)
  203. : param<Concept, const T&>(data, table)
  204. {}
  205. any<Concept, T&> get() const
  206. {
  207. return any<Concept, T&>(
  208. ::boost::type_erasure::detail::access::data(this->_impl),
  209. ::boost::type_erasure::detail::access::table(this->_impl));
  210. }
  211. };
  212. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  213. template<class Concept, class T>
  214. class param<Concept, T&&> : public param<Concept, const T&> {
  215. public:
  216. friend struct boost::type_erasure::detail::access;
  217. /** INTERNAL ONLY */
  218. typedef void _boost_type_erasure_is_any;
  219. /** INTERNAL ONLY */
  220. typedef param _boost_type_erasure_derived_type;
  221. param(const ::boost::type_erasure::detail::storage& data,
  222. const ::boost::type_erasure::binding<Concept>& table)
  223. : param<Concept, const T&>(data, table)
  224. {}
  225. any<Concept, T&&> get() const
  226. {
  227. return any<Concept, T&&>(
  228. ::boost::type_erasure::detail::access::data(this->_impl),
  229. ::boost::type_erasure::detail::access::table(this->_impl));
  230. }
  231. };
  232. #endif
  233. #endif
  234. /**
  235. * \brief Metafunction that creates a @ref param.
  236. *
  237. * If @c T is a (cv/reference qualified) placeholder,
  238. * returns @ref param<@ref concept_of "concept_of<Any>::type", T>,
  239. * otherwise, returns T. This metafunction is intended
  240. * to be used for function arguments in specializations of
  241. * @ref concept_interface.
  242. *
  243. * \see derived, rebind_any
  244. */
  245. template<class Any, class T>
  246. struct as_param {
  247. #ifdef BOOST_TYPE_ERASURE_DOXYGEN
  248. typedef detail::unspecified type;
  249. #else
  250. typedef typename ::boost::mpl::if_<
  251. ::boost::type_erasure::is_placeholder<
  252. typename ::boost::remove_cv<
  253. typename ::boost::remove_reference<T>::type>::type>,
  254. param<typename ::boost::type_erasure::concept_of<Any>::type, T>,
  255. T
  256. >::type type;
  257. #endif
  258. };
  259. #ifndef BOOST_NO_CXX11_TEMPLATE_ALIASES
  260. template<class Any, class T>
  261. using as_param_t = typename ::boost::type_erasure::as_param<Any, T>::type;
  262. #endif
  263. }
  264. }
  265. #endif