9
3

if.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*=============================================================================
  2. Copyright (c) 2015 Paul Fultz II
  3. if_.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_IF_H
  8. #define BOOST_HOF_GUARD_IF_H
  9. /// if
  10. /// ==
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `if_` function decorator makes the function callable if the boolean
  16. /// condition is true. The `if_c` version can be used to give a boolean
  17. /// condition directly(instead of relying on an integral constant).
  18. ///
  19. /// When `if_` is false, the function is not callable. It is a subtitution
  20. /// failure to call the function.
  21. ///
  22. /// Synopsis
  23. /// --------
  24. ///
  25. /// template<class IntegralConstant>
  26. /// constexpr auto if_(IntegralConstant);
  27. ///
  28. /// template<bool B, class F>
  29. /// constexpr auto if_c(F);
  30. ///
  31. /// Requirements
  32. /// ------------
  33. ///
  34. /// IntegralConstant must be:
  35. ///
  36. /// * IntegralConstant
  37. ///
  38. /// F must be:
  39. ///
  40. /// * [ConstInvocable](ConstInvocable)
  41. /// * MoveConstructible
  42. ///
  43. /// Example
  44. /// -------
  45. ///
  46. /// #include <boost/hof.hpp>
  47. /// #include <cassert>
  48. ///
  49. /// struct sum_f
  50. /// {
  51. /// template<class T>
  52. /// int operator()(T x, T y) const
  53. /// {
  54. /// return boost::hof::first_of(
  55. /// boost::hof::if_(std::is_integral<T>())(boost::hof::_ + boost::hof::_),
  56. /// boost::hof::always(0)
  57. /// )(x, y);
  58. /// }
  59. /// };
  60. ///
  61. /// int main() {
  62. /// assert(sum_f()(1, 2) == 3);
  63. /// assert(sum_f()("", "") == 0);
  64. /// }
  65. ///
  66. /// References
  67. /// ----------
  68. ///
  69. /// * [static_if](static_if)
  70. ///
  71. #include <boost/hof/always.hpp>
  72. #include <boost/hof/detail/callable_base.hpp>
  73. #include <boost/hof/detail/forward.hpp>
  74. #include <boost/hof/detail/delegate.hpp>
  75. #include <boost/hof/detail/move.hpp>
  76. #include <boost/hof/detail/static_const_var.hpp>
  77. namespace boost { namespace hof {
  78. namespace detail {
  79. template<class C, class...>
  80. struct if_depend
  81. : C
  82. {};
  83. template<bool Cond, class F>
  84. struct if_adaptor : detail::callable_base<F>
  85. {
  86. BOOST_HOF_INHERIT_CONSTRUCTOR(if_adaptor, detail::callable_base<F>)
  87. };
  88. template<class F>
  89. struct if_adaptor<false, F>
  90. {
  91. template<class... Ts>
  92. constexpr if_adaptor(Ts&&...) noexcept
  93. {}
  94. };
  95. template<bool Cond>
  96. struct make_if_f
  97. {
  98. constexpr make_if_f() noexcept
  99. {}
  100. template<class F>
  101. constexpr if_adaptor<Cond, F> operator()(F f) const BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(F, F&&)
  102. {
  103. return if_adaptor<Cond, F>(static_cast<F&&>(f));
  104. }
  105. };
  106. struct if_f
  107. {
  108. constexpr if_f()
  109. {}
  110. template<class Cond, bool B=Cond::type::value>
  111. constexpr make_if_f<B> operator()(Cond) const noexcept
  112. {
  113. return {};
  114. }
  115. };
  116. }
  117. #if BOOST_HOF_HAS_VARIABLE_TEMPLATES
  118. template<bool B>
  119. BOOST_HOF_STATIC_CONSTEXPR detail::make_if_f<B> if_c = {};
  120. #else
  121. template<bool B, class F>
  122. constexpr detail::if_adaptor<B, F> if_c(F f) BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(F, F&&)
  123. {
  124. return detail::if_adaptor<B, F>(static_cast<F&&>(f));
  125. }
  126. #endif
  127. BOOST_HOF_DECLARE_STATIC_VAR(if_, detail::if_f);
  128. }} // namespace boost::hof
  129. #endif