| 1 | /*! |
| 2 | @file |
| 3 | Defines `boost::hana::negate`. |
| 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_NEGATE_HPP |
| 11 | #define BOOST_HANA_NEGATE_HPP |
| 12 | |
| 13 | #include <boost/hana/fwd/negate.hpp> |
| 14 | |
| 15 | #include <boost/hana/concept/group.hpp> |
| 16 | #include <boost/hana/config.hpp> |
| 17 | #include <boost/hana/core/dispatch.hpp> |
| 18 | #include <boost/hana/fwd/minus.hpp> |
| 19 | #include <boost/hana/zero.hpp> |
| 20 | |
| 21 | #include <type_traits> |
| 22 | |
| 23 | |
| 24 | namespace boost { namespace hana { |
| 25 | //! @cond |
| 26 | template <typename X> |
| 27 | constexpr decltype(auto) negate_t::operator()(X&& x) const { |
| 28 | using G = typename hana::tag_of<X>::type; |
| 29 | using Negate = BOOST_HANA_DISPATCH_IF(negate_impl<G>, |
| 30 | hana::Group<G>::value |
| 31 | ); |
| 32 | |
| 33 | #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS |
| 34 | static_assert(hana::Group<G>::value, |
| 35 | "hana::negate(x) requires 'x' to be in a Group" ); |
| 36 | #endif |
| 37 | |
| 38 | return Negate::apply(static_cast<X&&>(x)); |
| 39 | } |
| 40 | //! @endcond |
| 41 | |
| 42 | template <typename T, bool condition> |
| 43 | struct negate_impl<T, when<condition>> : default_ { |
| 44 | template <typename X> |
| 45 | static constexpr decltype(auto) apply(X&& x) |
| 46 | { return hana::minus(hana::zero<T>(), static_cast<X&&>(x)); } |
| 47 | }; |
| 48 | |
| 49 | ////////////////////////////////////////////////////////////////////////// |
| 50 | // Model for arithmetic data types |
| 51 | ////////////////////////////////////////////////////////////////////////// |
| 52 | template <typename T> |
| 53 | struct negate_impl<T, when<std::is_arithmetic<T>::value && |
| 54 | !std::is_same<bool, T>::value>> { |
| 55 | template <typename X> |
| 56 | static constexpr decltype(auto) apply(X&& x) |
| 57 | { return -static_cast<X&&>(x); } |
| 58 | }; |
| 59 | }} // end namespace boost::hana |
| 60 | |
| 61 | #endif // !BOOST_HANA_NEGATE_HPP |
| 62 | |