]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*! |
2 | @file | |
3 | Defines `boost::hana::plus`. | |
4 | ||
5 | @copyright Louis Dionne 2013-2016 | |
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_PLUS_HPP | |
11 | #define BOOST_HANA_PLUS_HPP | |
12 | ||
13 | #include <boost/hana/fwd/plus.hpp> | |
14 | ||
15 | #include <boost/hana/concept/constant.hpp> | |
16 | #include <boost/hana/concept/monoid.hpp> | |
17 | #include <boost/hana/config.hpp> | |
18 | #include <boost/hana/core/common.hpp> | |
19 | #include <boost/hana/core/to.hpp> | |
20 | #include <boost/hana/core/dispatch.hpp> | |
21 | #include <boost/hana/detail/canonical_constant.hpp> | |
22 | #include <boost/hana/detail/has_common_embedding.hpp> | |
23 | ||
24 | #include <type_traits> | |
25 | ||
26 | ||
27 | BOOST_HANA_NAMESPACE_BEGIN | |
28 | //! @cond | |
29 | template <typename X, typename Y> | |
30 | constexpr decltype(auto) plus_t::operator()(X&& x, Y&& y) const { | |
31 | using T = typename hana::tag_of<X>::type; | |
32 | using U = typename hana::tag_of<Y>::type; | |
33 | using Plus = BOOST_HANA_DISPATCH_IF(decltype(plus_impl<T, U>{}), | |
34 | hana::Monoid<T>::value && | |
35 | hana::Monoid<U>::value && | |
36 | !is_default<plus_impl<T, U>>::value | |
37 | ); | |
38 | ||
39 | #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS | |
40 | static_assert(hana::Monoid<T>::value, | |
41 | "hana::plus(x, y) requires 'x' to be a Monoid"); | |
42 | ||
43 | static_assert(hana::Monoid<U>::value, | |
44 | "hana::plus(x, y) requires 'y' to be a Monoid"); | |
45 | ||
46 | static_assert(!is_default<plus_impl<T, U>>::value, | |
47 | "hana::plus(x, y) requires 'x' and 'y' to be embeddable " | |
48 | "in a common Monoid"); | |
49 | #endif | |
50 | ||
51 | return Plus::apply(static_cast<X&&>(x), static_cast<Y&&>(y)); | |
52 | } | |
53 | //! @endcond | |
54 | ||
55 | template <typename T, typename U, bool condition> | |
56 | struct plus_impl<T, U, when<condition>> : default_ { | |
57 | template <typename ...Args> | |
58 | static constexpr auto apply(Args&& ...) = delete; | |
59 | }; | |
60 | ||
61 | // Cross-type overload | |
62 | template <typename T, typename U> | |
63 | struct plus_impl<T, U, when< | |
64 | detail::has_nontrivial_common_embedding<Monoid, T, U>::value | |
65 | >> { | |
66 | using C = typename common<T, U>::type; | |
67 | template <typename X, typename Y> | |
68 | static constexpr decltype(auto) apply(X&& x, Y&& y) { | |
69 | return hana::plus(hana::to<C>(static_cast<X&&>(x)), | |
70 | hana::to<C>(static_cast<Y&&>(y))); | |
71 | } | |
72 | }; | |
73 | ||
74 | ////////////////////////////////////////////////////////////////////////// | |
75 | // Model for non-boolean arithmetic data types | |
76 | ////////////////////////////////////////////////////////////////////////// | |
77 | template <typename T> | |
78 | struct plus_impl<T, T, when< | |
79 | std::is_arithmetic<T>::value && | |
80 | !std::is_same<T, bool>::value | |
81 | >> { | |
82 | template <typename X, typename Y> | |
83 | static constexpr decltype(auto) apply(X&& x, Y&& y) | |
84 | { return static_cast<X&&>(x) + static_cast<Y&&>(y); } | |
85 | }; | |
86 | ||
87 | ////////////////////////////////////////////////////////////////////////// | |
88 | // Model for Constants over a Monoid | |
89 | ////////////////////////////////////////////////////////////////////////// | |
90 | namespace detail { | |
91 | template <typename C, typename X, typename Y> | |
92 | struct constant_from_plus { | |
93 | static constexpr auto value = hana::plus(hana::value<X>(), hana::value<Y>()); | |
94 | using hana_tag = detail::CanonicalConstant<typename C::value_type>; | |
95 | }; | |
96 | } | |
97 | ||
98 | template <typename C> | |
99 | struct plus_impl<C, C, when< | |
100 | hana::Constant<C>::value && | |
101 | Monoid<typename C::value_type>::value | |
102 | >> { | |
103 | template <typename X, typename Y> | |
104 | static constexpr decltype(auto) apply(X const&, Y const&) | |
105 | { return hana::to<C>(detail::constant_from_plus<C, X, Y>{}); } | |
106 | }; | |
107 | BOOST_HANA_NAMESPACE_END | |
108 | ||
109 | #endif // !BOOST_HANA_PLUS_HPP |