| 1 | /*! |
|---|---|
| 2 | @file |
| 3 | Defines `boost::hana::sum`. |
| 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_SUM_HPP |
| 11 | #define BOOST_HANA_SUM_HPP |
| 12 | |
| 13 | #include <boost/hana/fwd/sum.hpp> |
| 14 | |
| 15 | #include <boost/hana/concept/foldable.hpp> |
| 16 | #include <boost/hana/concept/monoid.hpp> |
| 17 | #include <boost/hana/config.hpp> |
| 18 | #include <boost/hana/core/dispatch.hpp> |
| 19 | #include <boost/hana/fold_left.hpp> |
| 20 | #include <boost/hana/integral_constant.hpp> // required by fwd decl |
| 21 | #include <boost/hana/plus.hpp> |
| 22 | #include <boost/hana/zero.hpp> |
| 23 | |
| 24 | |
| 25 | namespace boost { namespace hana { |
| 26 | //! @cond |
| 27 | template <typename M> |
| 28 | template <typename Xs> |
| 29 | constexpr decltype(auto) sum_t<M>::operator()(Xs&& xs) const { |
| 30 | using S = typename hana::tag_of<Xs>::type; |
| 31 | using Sum = BOOST_HANA_DISPATCH_IF(sum_impl<S>, |
| 32 | hana::Foldable<S>::value |
| 33 | ); |
| 34 | |
| 35 | #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS |
| 36 | static_assert(hana::Monoid<M>::value, |
| 37 | "hana::sum<M> requires 'M' to be a Monoid"); |
| 38 | |
| 39 | static_assert(hana::Foldable<S>::value, |
| 40 | "hana::sum<M>(xs) requires 'xs' to be Foldable"); |
| 41 | #endif |
| 42 | |
| 43 | return Sum::template apply<M>(static_cast<Xs&&>(xs)); |
| 44 | } |
| 45 | //! @endcond |
| 46 | |
| 47 | template <typename T, bool condition> |
| 48 | struct sum_impl<T, when<condition>> : default_ { |
| 49 | template <typename M, typename Xs> |
| 50 | static constexpr decltype(auto) apply(Xs&& xs) { |
| 51 | return hana::fold_left(static_cast<Xs&&>(xs), hana::zero<M>(), hana::plus); |
| 52 | } |
| 53 | }; |
| 54 | }} // end namespace boost::hana |
| 55 | |
| 56 | #endif // !BOOST_HANA_SUM_HPP |
| 57 |
