Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /*=============================================================================
0002     Copyright (c) 2015 Paul Fultz II
0003     if_.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_IF_H
0009 #define BOOST_HOF_GUARD_IF_H
0010 
0011 /// if
0012 /// ==
0013 /// 
0014 /// Description
0015 /// -----------
0016 /// 
0017 /// The `if_` function decorator makes the function callable if the boolean
0018 /// condition is true. The `if_c` version can be used to give a boolean
0019 /// condition directly(instead of relying on an integral constant).
0020 /// 
0021 /// When `if_` is false, the function is not callable. It is a subtitution
0022 /// failure to call the function.
0023 /// 
0024 /// Synopsis
0025 /// --------
0026 /// 
0027 ///     template<class IntegralConstant>
0028 ///     constexpr auto if_(IntegralConstant);
0029 /// 
0030 ///     template<bool B, class F>
0031 ///     constexpr auto if_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 /// 
0051 ///     struct sum_f
0052 ///     {
0053 ///         template<class T>
0054 ///         int operator()(T x, T y) const
0055 ///         {
0056 ///             return boost::hof::first_of(
0057 ///                 boost::hof::if_(std::is_integral<T>())(boost::hof::_ + boost::hof::_),
0058 ///                 boost::hof::always(0)
0059 ///             )(x, y);
0060 ///         }
0061 ///     };
0062 /// 
0063 ///     int main() {
0064 ///         assert(sum_f()(1, 2) == 3);
0065 ///         assert(sum_f()("", "") == 0);
0066 ///     }
0067 /// 
0068 /// References
0069 /// ----------
0070 /// 
0071 /// * [static_if](static_if)
0072 /// 
0073 
0074 #include <boost/hof/always.hpp>
0075 #include <boost/hof/detail/callable_base.hpp>
0076 #include <boost/hof/detail/forward.hpp>
0077 #include <boost/hof/detail/delegate.hpp>
0078 #include <boost/hof/detail/move.hpp>
0079 #include <boost/hof/detail/static_const_var.hpp>
0080 
0081 namespace boost { namespace hof {
0082 
0083 namespace detail {
0084 
0085 template<class C, class...>
0086 struct if_depend
0087 : C
0088 {};
0089 
0090 template<bool Cond, class F>
0091 struct if_adaptor : detail::callable_base<F>
0092 {
0093     BOOST_HOF_INHERIT_CONSTRUCTOR(if_adaptor, detail::callable_base<F>)
0094 };
0095 
0096 template<class F>
0097 struct if_adaptor<false, F>
0098 {
0099     template<class... Ts>
0100     constexpr if_adaptor(Ts&&...) noexcept
0101     {}
0102 };
0103 
0104 template<bool Cond>
0105 struct make_if_f
0106 {
0107     constexpr make_if_f() noexcept
0108     {}
0109     template<class F>
0110     constexpr if_adaptor<Cond, F> operator()(F f) const BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(F, F&&)
0111     {
0112         return if_adaptor<Cond, F>(static_cast<F&&>(f));
0113     }
0114 };
0115 
0116 struct if_f
0117 {
0118     constexpr if_f()
0119     {}
0120     template<class Cond, bool B=Cond::type::value>
0121     constexpr make_if_f<B> operator()(Cond) const noexcept
0122     {
0123         return {};
0124     }
0125 };
0126 
0127 }
0128 #if BOOST_HOF_HAS_VARIABLE_TEMPLATES
0129 template<bool B>
0130 BOOST_HOF_STATIC_CONSTEXPR detail::make_if_f<B> if_c = {};
0131 #else
0132 template<bool B, class F>
0133 constexpr detail::if_adaptor<B, F> if_c(F f) BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(F, F&&)
0134 {
0135     return detail::if_adaptor<B, F>(static_cast<F&&>(f));
0136 }
0137 #endif
0138 
0139 BOOST_HOF_DECLARE_STATIC_VAR(if_, detail::if_f);
0140 
0141 }} // namespace boost::hof
0142 
0143 #endif