Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:43:36

0001 /*!
0002 @file
0003 Defines `boost::hana::eval_if`.
0004 
0005 Copyright Louis Dionne 2013-2022
0006 Distributed under the Boost Software License, Version 1.0.
0007 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
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     //! @cond
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     //! @endcond
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     // Model for arithmetic data types
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     // Model for Constants over a Logical
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 }} // end namespace boost::hana
0092 
0093 #endif // !BOOST_HANA_EVAL_IF_HPP