or.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*!
  2. @file
  3. Defines `boost::hana::or_`.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_OR_HPP
  9. #define BOOST_HANA_OR_HPP
  10. #include <boost/hana/fwd/or.hpp>
  11. #include <boost/hana/concept/logical.hpp>
  12. #include <boost/hana/config.hpp>
  13. #include <boost/hana/core/dispatch.hpp>
  14. #include <boost/hana/detail/variadic/foldl1.hpp>
  15. #include <boost/hana/if.hpp>
  16. BOOST_HANA_NAMESPACE_BEGIN
  17. //! @cond
  18. template <typename X, typename Y>
  19. constexpr decltype(auto) or_t::operator()(X&& x, Y&& y) const {
  20. using Bool = typename hana::tag_of<X>::type;
  21. using Or = BOOST_HANA_DISPATCH_IF(or_impl<Bool>,
  22. hana::Logical<Bool>::value
  23. );
  24. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  25. static_assert(hana::Logical<Bool>::value,
  26. "hana::or_(x, y) requires 'x' to be a Logical");
  27. #endif
  28. return Or::apply(static_cast<X&&>(x), static_cast<Y&&>(y));
  29. }
  30. template <typename X, typename ...Y>
  31. constexpr decltype(auto) or_t::operator()(X&& x, Y&& ...y) const {
  32. return detail::variadic::foldl1(
  33. *this,
  34. static_cast<X&&>(x),
  35. static_cast<Y&&>(y)...
  36. );
  37. }
  38. //! @endcond
  39. template <typename L, bool condition>
  40. struct or_impl<L, when<condition>> : hana::default_ {
  41. template <typename X, typename Y>
  42. static constexpr decltype(auto) apply(X&& x, Y&& y) {
  43. //! @todo How to forward `x` here? Since the arguments to `if_`
  44. //! can be evaluated in any order, we have to be careful not to
  45. //! use `x` in a moved-from state.
  46. return hana::if_(x, x, static_cast<Y&&>(y));
  47. }
  48. };
  49. BOOST_HANA_NAMESPACE_END
  50. #endif // !BOOST_HANA_OR_HPP