limit.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*=============================================================================
  2. Copyright (c) 2016 Paul Fultz II
  3. limit.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_LIMIT_H
  8. #define BOOST_HOF_GUARD_LIMIT_H
  9. /// limit
  10. /// =====
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `limit` function decorator annotates the function with the max number
  16. /// of parameters. The `limit_c` version can be used to give the max number
  17. /// directly(instead of relying on an integral constant). The parameter limit
  18. /// can be read by using the [`function_param_limit`](function_param_limit)
  19. /// trait. Using `limit` is useful to improve error reporting with partially
  20. /// evaluated functions.
  21. ///
  22. /// Synopsis
  23. /// --------
  24. ///
  25. /// template<class IntegralConstant>
  26. /// constexpr auto limit(IntegralConstant);
  27. ///
  28. /// template<std::size_t N, class F>
  29. /// constexpr auto limit_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. /// using namespace boost::hof;
  49. ///
  50. /// struct sum_f
  51. /// {
  52. /// template<class T>
  53. /// int operator()(T x, T y) const
  54. /// {
  55. /// return x+y;
  56. /// }
  57. /// };
  58. /// BOOST_HOF_STATIC_FUNCTION(sum) = limit_c<2>(sum_f());
  59. ///
  60. /// int main() {
  61. /// assert(3 == sum(1, 2));
  62. /// }
  63. ///
  64. /// See Also
  65. /// --------
  66. ///
  67. /// * [Partial function evaluation](<Partial function evaluation>)
  68. /// * [function_param_limit](function_param_limit)
  69. ///
  70. #include <boost/hof/detail/callable_base.hpp>
  71. #include <boost/hof/detail/forward.hpp>
  72. #include <boost/hof/detail/delegate.hpp>
  73. #include <boost/hof/detail/move.hpp>
  74. #include <boost/hof/detail/static_const_var.hpp>
  75. #include <boost/hof/always.hpp>
  76. #include <boost/hof/function_param_limit.hpp>
  77. namespace boost { namespace hof {
  78. namespace detail {
  79. // TODO: Make this work with fit_rewritable1_tag
  80. template<std::size_t N, class F>
  81. struct limit_adaptor : detail::callable_base<F>
  82. {
  83. typedef std::integral_constant<std::size_t, N> fit_function_param_limit;
  84. BOOST_HOF_INHERIT_CONSTRUCTOR(limit_adaptor, detail::callable_base<F>)
  85. template<class... Ts>
  86. constexpr const detail::callable_base<F>& base_function(Ts&&... xs) const
  87. {
  88. return boost::hof::always_ref(*this)(xs...);
  89. }
  90. BOOST_HOF_RETURNS_CLASS(limit_adaptor);
  91. template<class... Ts, class=typename std::enable_if<(sizeof...(Ts) <= N)>::type>
  92. constexpr BOOST_HOF_SFINAE_RESULT(const detail::callable_base<F>&, id_<Ts>...)
  93. operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
  94. (
  95. (BOOST_HOF_MANGLE_CAST(const detail::callable_base<F>&)(BOOST_HOF_CONST_THIS->base_function(xs...)))
  96. (BOOST_HOF_FORWARD(Ts)(xs)...)
  97. );
  98. };
  99. template<std::size_t N>
  100. struct make_limit_f
  101. {
  102. constexpr make_limit_f()
  103. {}
  104. template<class F>
  105. constexpr limit_adaptor<N, F> operator()(F f) const
  106. {
  107. return limit_adaptor<N, F>(static_cast<F&&>(f));
  108. }
  109. };
  110. struct limit_f
  111. {
  112. template<class IntegralConstant, std::size_t N=IntegralConstant::type::value>
  113. constexpr make_limit_f<N> operator()(IntegralConstant) const
  114. {
  115. return {};
  116. }
  117. };
  118. }
  119. template<std::size_t N, class F>
  120. constexpr detail::limit_adaptor<N, F> limit_c(F f)
  121. {
  122. return detail::limit_adaptor<N, F>(static_cast<F&&>(f));
  123. }
  124. BOOST_HOF_DECLARE_STATIC_VAR(limit, detail::limit_f);
  125. }} // namespace boost::hof
  126. #endif