constructible.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #if !defined(BOOST_PP_IS_ITERATING)
  11. #ifndef BOOST_TYPE_ERASURE_CONSTRUCTIBLE_HPP_INCLUDED
  12. #define BOOST_TYPE_ERASURE_CONSTRUCTIBLE_HPP_INCLUDED
  13. #include <boost/detail/workaround.hpp>
  14. #include <boost/preprocessor/iteration/iterate.hpp>
  15. #include <boost/preprocessor/repetition/enum_params.hpp>
  16. #include <boost/preprocessor/repetition/enum_binary_params.hpp>
  17. #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
  18. #include <boost/type_erasure/detail/storage.hpp>
  19. #include <boost/type_erasure/call.hpp>
  20. #include <boost/type_erasure/concept_interface.hpp>
  21. #include <boost/type_erasure/config.hpp>
  22. #include <boost/type_erasure/param.hpp>
  23. namespace boost {
  24. namespace type_erasure {
  25. template<class Sig>
  26. struct constructible;
  27. namespace detail {
  28. template<class Sig>
  29. struct null_construct;
  30. template<class C>
  31. struct get_null_vtable_entry;
  32. template<class C, class Sig>
  33. struct vtable_adapter;
  34. }
  35. #ifdef BOOST_TYPE_ERASURE_DOXYGEN
  36. /**
  37. * The @ref constructible concept enables calling the
  38. * constructor of a type contained by an @ref any.
  39. * @c Sig should be a function signature. The return
  40. * type is the placeholder specifying the type to
  41. * be constructed. The arguments are the argument
  42. * types of the constructor. The arguments of
  43. * @c Sig may be placeholders.
  44. *
  45. * \note @ref constructible may not be specialized and
  46. * may not be passed to \call as it depends on the
  47. * implementation details of @ref any.
  48. */
  49. template<class Sig>
  50. struct constructible {};
  51. #elif !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
  52. !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \
  53. !BOOST_WORKAROUND(BOOST_MSVC, == 1800)
  54. template<class R, class... T>
  55. struct constructible<R(T...)>
  56. {
  57. static ::boost::type_erasure::detail::storage
  58. apply(T... arg)
  59. {
  60. ::boost::type_erasure::detail::storage result;
  61. result.data = new R(::std::forward<T>(arg)...);
  62. return result;
  63. }
  64. };
  65. /// INTERNAL ONLY
  66. template<class Base, class Tag, class... T>
  67. struct concept_interface<
  68. ::boost::type_erasure::constructible<Tag(T...)>,
  69. Base,
  70. Tag
  71. > : Base
  72. {
  73. using Base::_boost_type_erasure_deduce_constructor;
  74. ::boost::type_erasure::constructible<Tag(T...)>*
  75. _boost_type_erasure_deduce_constructor(
  76. typename ::boost::type_erasure::as_param<Base, T>::type...) const
  77. {
  78. return 0;
  79. }
  80. };
  81. namespace detail {
  82. template<class... T>
  83. struct null_construct<void(T...)>
  84. {
  85. static ::boost::type_erasure::detail::storage
  86. value(T...)
  87. {
  88. ::boost::type_erasure::detail::storage result;
  89. result.data = 0;
  90. return result;
  91. }
  92. };
  93. template<class T, class R, class... U>
  94. struct get_null_vtable_entry<vtable_adapter<constructible<T(const T&)>, R(U...)> >
  95. {
  96. typedef null_construct<void(U...)> type;
  97. };
  98. }
  99. #else
  100. #define BOOST_PP_FILENAME_1 <boost/type_erasure/constructible.hpp>
  101. #define BOOST_PP_ITERATION_LIMITS (0, BOOST_TYPE_ERASURE_MAX_ARITY)
  102. #include BOOST_PP_ITERATE()
  103. #endif
  104. }
  105. }
  106. #endif
  107. #else
  108. #define N BOOST_PP_ITERATION()
  109. #define BOOST_TYPE_ERASURE_ARG_DECL(z, n, data) \
  110. typename ::boost::type_erasure::as_param< \
  111. Base, \
  112. BOOST_PP_CAT(T, n) \
  113. >::type
  114. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  115. #define BOOST_TYPE_ERASURE_FORWARD_I(z, n, data) ::std::forward<BOOST_PP_CAT(T, n)>(BOOST_PP_CAT(arg, n))
  116. #define BOOST_TYPE_ERASURE_FORWARD(n) BOOST_PP_ENUM(n, BOOST_TYPE_ERASURE_FORWARD_I, ~)
  117. #else
  118. #define BOOST_TYPE_ERASURE_FORWARD(n) BOOST_PP_ENUM_PARAMS(n, arg)
  119. #endif
  120. template<class R BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)>
  121. struct constructible<R(BOOST_PP_ENUM_PARAMS(N, T))>
  122. {
  123. static ::boost::type_erasure::detail::storage
  124. apply(BOOST_PP_ENUM_BINARY_PARAMS(N, T, arg))
  125. {
  126. ::boost::type_erasure::detail::storage result;
  127. result.data = new R(BOOST_TYPE_ERASURE_FORWARD(N));
  128. return result;
  129. }
  130. };
  131. template<class Base BOOST_PP_ENUM_TRAILING_PARAMS(N, class T), class Tag>
  132. struct concept_interface<
  133. ::boost::type_erasure::constructible<Tag(BOOST_PP_ENUM_PARAMS(N, T))>,
  134. Base,
  135. Tag
  136. > : Base
  137. {
  138. using Base::_boost_type_erasure_deduce_constructor;
  139. ::boost::type_erasure::constructible<Tag(BOOST_PP_ENUM_PARAMS(N, T))>*
  140. _boost_type_erasure_deduce_constructor(
  141. BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_ARG_DECL, ~)) const
  142. {
  143. return 0;
  144. }
  145. };
  146. namespace detail {
  147. template<BOOST_PP_ENUM_PARAMS(N, class T)>
  148. struct null_construct<void(BOOST_PP_ENUM_PARAMS(N, T))>
  149. {
  150. static ::boost::type_erasure::detail::storage
  151. value(BOOST_PP_ENUM_PARAMS(N, T))
  152. {
  153. ::boost::type_erasure::detail::storage result;
  154. result.data = 0;
  155. return result;
  156. }
  157. };
  158. template<class T, class R BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)>
  159. struct get_null_vtable_entry<vtable_adapter<constructible<T(const T&)>, R(BOOST_PP_ENUM_PARAMS(N, T))> >
  160. {
  161. typedef null_construct<void(BOOST_PP_ENUM_PARAMS(N, T))> type;
  162. };
  163. }
  164. #undef BOOST_TYPE_ERASURE_FORWARD
  165. #undef BOOST_TYPE_ERASURE_FORWARD_I
  166. #undef BOOST_TYPE_ERASURE_ARG_DECL
  167. #undef N
  168. #endif