File indexing completed on 2025-01-30 09:43:36
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_HANA_EVAL_IF_HPP
0011 #define BOOST_HANA_EVAL_IF_HPP
0012
0013 #include <boost/hana/fwd/eval_if.hpp>
0014
0015 #include <boost/hana/bool.hpp>
0016 #include <boost/hana/concept/constant.hpp>
0017 #include <boost/hana/concept/logical.hpp>
0018 #include <boost/hana/config.hpp>
0019 #include <boost/hana/core/dispatch.hpp>
0020 #include <boost/hana/eval.hpp>
0021 #include <boost/hana/if.hpp>
0022
0023 #include <type_traits>
0024
0025
0026 namespace boost { namespace hana {
0027
0028 template <typename Cond, typename Then, typename Else>
0029 constexpr decltype(auto) eval_if_t::operator()(Cond&& cond, Then&& then_, Else&& else_) const {
0030 using Bool = typename hana::tag_of<Cond>::type;
0031 using EvalIf = BOOST_HANA_DISPATCH_IF(eval_if_impl<Bool>,
0032 hana::Logical<Bool>::value
0033 );
0034
0035 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
0036 static_assert(hana::Logical<Bool>::value,
0037 "hana::eval_if(cond, then, else) requires 'cond' to be a Logical");
0038 #endif
0039
0040 return EvalIf::apply(static_cast<Cond&&>(cond),
0041 static_cast<Then&&>(then_),
0042 static_cast<Else&&>(else_));
0043 }
0044
0045
0046 template <typename L, bool condition>
0047 struct eval_if_impl<L, when<condition>> : default_ {
0048 template <typename ...Args>
0049 static constexpr auto apply(Args&& ...) = delete;
0050 };
0051
0052
0053
0054
0055 template <typename L>
0056 struct eval_if_impl<L, when<std::is_arithmetic<L>::value>> {
0057 template <typename Cond, typename T, typename E>
0058 static constexpr auto apply(Cond const& cond, T&& t, E&& e) {
0059 return cond ? hana::eval(static_cast<T&&>(t))
0060 : hana::eval(static_cast<E&&>(e));
0061 }
0062 };
0063
0064
0065
0066
0067 template <typename C>
0068 struct eval_if_impl<C, when<
0069 hana::Constant<C>::value &&
0070 Logical<typename C::value_type>::value
0071 >> {
0072 template <typename Then, typename Else>
0073 static constexpr decltype(auto)
0074 eval_if_helper(hana::true_, Then&& t, Else&&)
0075 { return hana::eval(static_cast<Then&&>(t)); }
0076
0077 template <typename Then, typename Else>
0078 static constexpr decltype(auto)
0079 eval_if_helper(hana::false_, Then&&, Else&& e)
0080 { return hana::eval(static_cast<Else&&>(e)); }
0081
0082 template <typename Cond, typename Then, typename Else>
0083 static constexpr decltype(auto) apply(Cond const&, Then&& t, Else&& e) {
0084 constexpr auto cond = hana::value<Cond>();
0085 constexpr bool truth_value = hana::if_(cond, true, false);
0086 return eval_if_helper(hana::bool_<truth_value>{},
0087 static_cast<Then&&>(t),
0088 static_cast<Else&&>(e));
0089 }
0090 };
0091 }}
0092
0093 #endif