| 1 | /*! |
| 2 | @file |
| 3 | Defines `boost::hana::if_`. |
| 4 | |
| 5 | Copyright Louis Dionne 2013-2022 |
| 6 | Distributed under the Boost Software License, Version 1.0. |
| 7 | (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) |
| 8 | */ |
| 9 | |
| 10 | #ifndef BOOST_HANA_IF_HPP |
| 11 | #define BOOST_HANA_IF_HPP |
| 12 | |
| 13 | #include <boost/hana/fwd/if.hpp> |
| 14 | |
| 15 | #include <boost/hana/concept/logical.hpp> |
| 16 | #include <boost/hana/config.hpp> |
| 17 | #include <boost/hana/core/dispatch.hpp> |
| 18 | #include <boost/hana/eval_if.hpp> |
| 19 | |
| 20 | |
| 21 | namespace boost { namespace hana { |
| 22 | //! @cond |
| 23 | template <typename Cond, typename Then, typename Else> |
| 24 | constexpr decltype(auto) if_t::operator()(Cond&& cond, Then&& then_, Else&& else_) const { |
| 25 | using Bool = typename hana::tag_of<Cond>::type; |
| 26 | using If = BOOST_HANA_DISPATCH_IF(if_impl<Bool>, |
| 27 | hana::Logical<Bool>::value |
| 28 | ); |
| 29 | |
| 30 | #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS |
| 31 | static_assert(hana::Logical<Bool>::value, |
| 32 | "hana::if_(cond, then, else) requires 'cond' to be a Logical" ); |
| 33 | #endif |
| 34 | |
| 35 | return If::apply(static_cast<Cond&&>(cond), |
| 36 | static_cast<Then&&>(then_), |
| 37 | static_cast<Else&&>(else_)); |
| 38 | } |
| 39 | //! @endcond |
| 40 | |
| 41 | namespace detail { |
| 42 | template <typename T> |
| 43 | struct hold { |
| 44 | T value; |
| 45 | constexpr T&& operator()() && { return static_cast<T&&>(value); } |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | template <typename L, bool condition> |
| 50 | struct if_impl<L, when<condition>> : default_ { |
| 51 | template <typename C, typename T, typename E> |
| 52 | static constexpr auto apply(C&& c, T&& t, E&& e) { |
| 53 | return hana::eval_if(static_cast<C&&>(c), |
| 54 | detail::hold<T&&>{static_cast<T&&>(t)}, |
| 55 | detail::hold<E&&>{static_cast<E&&>(e)} |
| 56 | ); |
| 57 | } |
| 58 | }; |
| 59 | }} // end namespace boost::hana |
| 60 | |
| 61 | #endif // !BOOST_HANA_IF_HPP |
| 62 | |