Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:16

0001 /*=============================================================================
0002     Copyright (c) 2016 Paul Fultz II
0003     limit.h
0004     Distributed under the Boost Software License, Version 1.0. (See accompanying
0005     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 ==============================================================================*/
0007 
0008 #ifndef BOOST_HOF_GUARD_LIMIT_H
0009 #define BOOST_HOF_GUARD_LIMIT_H
0010 
0011 /// limit
0012 /// =====
0013 /// 
0014 /// Description
0015 /// -----------
0016 /// 
0017 /// The `limit` function decorator annotates the function with the max number
0018 /// of parameters. The `limit_c` version can be used to give the max number
0019 /// directly(instead of relying on an integral constant). The parameter limit
0020 /// can be read by using the [`function_param_limit`](function_param_limit)
0021 /// trait. Using `limit` is useful to improve error reporting with partially
0022 /// evaluated functions.
0023 /// 
0024 /// Synopsis
0025 /// --------
0026 /// 
0027 ///     template<class IntegralConstant>
0028 ///     constexpr auto limit(IntegralConstant);
0029 /// 
0030 ///     template<std::size_t N, class F>
0031 ///     constexpr auto limit_c(F);
0032 /// 
0033 /// Requirements
0034 /// ------------
0035 /// 
0036 /// IntegralConstant must be:
0037 /// 
0038 /// * IntegralConstant
0039 /// 
0040 /// F must be:
0041 /// 
0042 /// * [ConstInvocable](ConstInvocable)
0043 /// * MoveConstructible
0044 /// 
0045 /// Example
0046 /// -------
0047 /// 
0048 ///     #include <boost/hof.hpp>
0049 ///     #include <cassert>
0050 ///     using namespace boost::hof;
0051 /// 
0052 ///     struct sum_f
0053 ///     {
0054 ///         template<class T>
0055 ///         int operator()(T x, T y) const
0056 ///         {
0057 ///             return x+y;
0058 ///         }
0059 ///     };
0060 ///     BOOST_HOF_STATIC_FUNCTION(sum) = limit_c<2>(sum_f());
0061 /// 
0062 ///     int main() {
0063 ///         assert(3 == sum(1, 2));
0064 ///     }
0065 /// 
0066 /// See Also
0067 /// --------
0068 /// 
0069 /// * [Partial function evaluation](<Partial function evaluation>)
0070 /// * [function_param_limit](function_param_limit)
0071 /// 
0072 
0073 #include <boost/hof/detail/callable_base.hpp>
0074 #include <boost/hof/detail/forward.hpp>
0075 #include <boost/hof/detail/delegate.hpp>
0076 #include <boost/hof/detail/move.hpp>
0077 #include <boost/hof/detail/static_const_var.hpp>
0078 #include <boost/hof/always.hpp>
0079 #include <boost/hof/function_param_limit.hpp>
0080 
0081 namespace boost { namespace hof {
0082 
0083 namespace detail {
0084 // TODO: Make this work with fit_rewritable1_tag
0085 template<std::size_t N, class F>
0086 struct limit_adaptor : detail::callable_base<F>
0087 {
0088     typedef std::integral_constant<std::size_t, N> fit_function_param_limit;
0089     BOOST_HOF_INHERIT_CONSTRUCTOR(limit_adaptor, detail::callable_base<F>)
0090 
0091     template<class... Ts>
0092     constexpr const detail::callable_base<F>& base_function(Ts&&... xs) const
0093     {
0094         return boost::hof::always_ref(*this)(xs...);
0095     }
0096 
0097     BOOST_HOF_RETURNS_CLASS(limit_adaptor);
0098 
0099     template<class... Ts, class=typename std::enable_if<(sizeof...(Ts) <= N)>::type>
0100     constexpr BOOST_HOF_SFINAE_RESULT(const detail::callable_base<F>&, id_<Ts>...) 
0101     operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
0102     (
0103         (BOOST_HOF_MANGLE_CAST(const detail::callable_base<F>&)(BOOST_HOF_CONST_THIS->base_function(xs...)))
0104             (BOOST_HOF_FORWARD(Ts)(xs)...)
0105     );
0106 
0107 };
0108 
0109 template<std::size_t N>
0110 struct make_limit_f
0111 {
0112     constexpr make_limit_f()
0113     {}
0114     template<class F>
0115     constexpr limit_adaptor<N, F> operator()(F f) const
0116     {
0117         return limit_adaptor<N, F>(static_cast<F&&>(f));
0118     }
0119 };
0120 
0121 struct limit_f
0122 {
0123     template<class IntegralConstant, std::size_t N=IntegralConstant::type::value>
0124     constexpr make_limit_f<N> operator()(IntegralConstant) const
0125     {
0126         return {};
0127     }
0128 };
0129 
0130 }
0131 
0132 template<std::size_t N, class F>
0133 constexpr detail::limit_adaptor<N, F> limit_c(F f)
0134 {
0135     return detail::limit_adaptor<N, F>(static_cast<F&&>(f));
0136 }
0137 
0138 BOOST_HOF_DECLARE_STATIC_VAR(limit, detail::limit_f);
0139 
0140 }} // namespace boost::hof
0141 
0142 #endif