delegate.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*=============================================================================
  2. Copyright (c) 2012 Paul Fultz II
  3. delgate.h
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #ifndef BOOST_HOF_GUARD_FUNCTION_DELGATE_H
  8. #define BOOST_HOF_GUARD_FUNCTION_DELGATE_H
  9. #include <type_traits>
  10. #include <utility>
  11. #include <boost/hof/config.hpp>
  12. #include <boost/hof/detail/and.hpp>
  13. #include <boost/hof/detail/holder.hpp>
  14. #include <boost/hof/detail/forward.hpp>
  15. #include <boost/hof/detail/using.hpp>
  16. #include <boost/hof/detail/intrinsics.hpp>
  17. #include <boost/hof/detail/noexcept.hpp>
  18. #define BOOST_HOF_ENABLE_IF_CONVERTIBLE(...) \
  19. class=typename std::enable_if<BOOST_HOF_IS_CONVERTIBLE(__VA_ARGS__)>::type
  20. #define BOOST_HOF_ENABLE_IF_CONVERTIBLE_UNPACK(...) \
  21. class=typename std::enable_if<BOOST_HOF_AND_UNPACK(BOOST_HOF_IS_CONVERTIBLE(__VA_ARGS__))>::type
  22. #define BOOST_HOF_ENABLE_IF_BASE_OF(...) \
  23. class=typename std::enable_if<BOOST_HOF_IS_BASE_OF(__VA_ARGS__)>::type
  24. #define BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(...) \
  25. class=typename std::enable_if<BOOST_HOF_IS_CONSTRUCTIBLE(__VA_ARGS__)>::type
  26. #define BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(...) \
  27. BOOST_HOF_NOEXCEPT(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(__VA_ARGS__))
  28. #define BOOST_HOF_INHERIT_DEFAULT(C, ...) \
  29. template<bool FitPrivateEnableBool_##__LINE__=true, \
  30. class=typename std::enable_if<FitPrivateEnableBool_##__LINE__ && boost::hof::detail::is_default_constructible_c<__VA_ARGS__>()>::type> \
  31. constexpr C() BOOST_HOF_NOEXCEPT(boost::hof::detail::is_nothrow_default_constructible_c<__VA_ARGS__>()) {}
  32. #define BOOST_HOF_INHERIT_DEFAULT_EMPTY(C, ...) \
  33. template<bool FitPrivateEnableBool_##__LINE__=true, \
  34. class=typename std::enable_if<FitPrivateEnableBool_##__LINE__ && \
  35. boost::hof::detail::is_default_constructible_c<__VA_ARGS__>() && BOOST_HOF_IS_EMPTY(__VA_ARGS__) \
  36. >::type> \
  37. constexpr C() BOOST_HOF_NOEXCEPT(boost::hof::detail::is_nothrow_default_constructible_c<__VA_ARGS__>()) {}
  38. #if BOOST_HOF_NO_TYPE_PACK_EXPANSION_IN_TEMPLATE
  39. #define BOOST_HOF_DELGATE_PRIMITIVE_CONSTRUCTOR(constexpr_, C, T, var) \
  40. template<class... FitXs, typename boost::hof::detail::enable_if_constructible<C, T, FitXs...>::type = 0> \
  41. constexpr_ C(FitXs&&... fit_xs) \
  42. BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(T, FitXs&&...) \
  43. : var((FitXs&&)boost::hof::forward<FitXs>(fit_xs)...) {}
  44. #else
  45. #define BOOST_HOF_DELGATE_PRIMITIVE_CONSTRUCTOR(constexpr_, C, T, var) \
  46. template<class... FitXs, BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(T, FitXs&&...)> \
  47. constexpr_ C(FitXs&&... fit_xs) \
  48. BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(T, FitXs&&...) \
  49. : var(BOOST_HOF_FORWARD(FitXs)(fit_xs)...) {}
  50. #endif
  51. #define BOOST_HOF_DELEGATE_CONSTRUCTOR(C, T, var) BOOST_HOF_DELGATE_PRIMITIVE_CONSTRUCTOR(constexpr, C, T, var)
  52. // Currently its faster to use `BOOST_HOF_DELEGATE_CONSTRUCTOR` than `using
  53. // Base::Base;`
  54. #if 1
  55. #define BOOST_HOF_INHERIT_CONSTRUCTOR(Derived, Base) BOOST_HOF_DELEGATE_CONSTRUCTOR(Derived, Base, Base)
  56. #else
  57. #define BOOST_HOF_INHERIT_CONSTRUCTOR(Derived, Base) \
  58. using fit_inherit_base = Base; \
  59. using fit_inherit_base::fit_inherit_base; \
  60. Derived()=default; \
  61. template<class FitX, BOOST_HOF_ENABLE_IF_CONVERTIBLE(FitX, Base)> \
  62. constexpr Derived(FitX&& fit_x) : Base(BOOST_HOF_FORWARD(FitX)(fit_x)) {}
  63. #endif
  64. namespace boost { namespace hof {
  65. namespace detail {
  66. template<class... Xs>
  67. constexpr bool is_nothrow_default_constructible_c()
  68. {
  69. return BOOST_HOF_AND_UNPACK(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(Xs));
  70. }
  71. template<class... Xs>
  72. constexpr bool is_default_constructible_c()
  73. {
  74. return BOOST_HOF_AND_UNPACK(BOOST_HOF_IS_DEFAULT_CONSTRUCTIBLE(Xs));
  75. }
  76. template<class... Xs>
  77. BOOST_HOF_USING(is_default_constructible, std::integral_constant<bool, is_default_constructible_c<Xs...>()>);
  78. template<class C, class X, class... Xs>
  79. struct enable_if_constructible
  80. : std::enable_if<is_constructible<X, Xs&&...>::value, int>
  81. {};
  82. }
  83. }} // namespace boost::hof
  84. #endif