]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*! |
2 | @file | |
3 | Defines `boost::hana::replicate`. | |
4 | ||
b32b8144 | 5 | @copyright Louis Dionne 2013-2017 |
7c673cae FG |
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_REPLICATE_HPP | |
11 | #define BOOST_HANA_REPLICATE_HPP | |
12 | ||
13 | #include <boost/hana/fwd/replicate.hpp> | |
14 | ||
15 | #include <boost/hana/concept/integral_constant.hpp> | |
16 | #include <boost/hana/concept/monad_plus.hpp> | |
17 | #include <boost/hana/config.hpp> | |
18 | #include <boost/hana/core/dispatch.hpp> | |
19 | #include <boost/hana/core/make.hpp> | |
20 | #include <boost/hana/cycle.hpp> | |
21 | #include <boost/hana/lift.hpp> | |
22 | ||
23 | #include <cstddef> | |
24 | #include <utility> | |
25 | ||
26 | ||
1e59de90 | 27 | namespace boost { namespace hana { |
92f5a8d4 | 28 | //! @cond |
7c673cae | 29 | template <typename M> |
92f5a8d4 TL |
30 | template <typename X, typename N> |
31 | constexpr auto replicate_t<M>::operator()(X&& x, N const& n) const { | |
32 | using Replicate = BOOST_HANA_DISPATCH_IF(replicate_impl<M>, | |
33 | hana::MonadPlus<M>::value && | |
34 | hana::IntegralConstant<N>::value | |
35 | ); | |
36 | ||
7c673cae FG |
37 | #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS |
38 | static_assert(hana::MonadPlus<M>::value, | |
39 | "hana::replicate<M>(x, n) requires 'M' to be a MonadPlus"); | |
7c673cae | 40 | |
92f5a8d4 TL |
41 | static_assert(hana::IntegralConstant<N>::value, |
42 | "hana::replicate<M>(x, n) requires 'n' to be an IntegralConstant"); | |
43 | #endif | |
7c673cae | 44 | |
92f5a8d4 TL |
45 | return Replicate::apply(static_cast<X&&>(x), n); |
46 | } | |
47 | //! @endcond | |
7c673cae FG |
48 | |
49 | template <typename M, bool condition> | |
50 | struct replicate_impl<M, when<condition>> : default_ { | |
51 | template <typename X, typename N> | |
52 | static constexpr auto apply(X&& x, N const& n) { | |
53 | return hana::cycle(hana::lift<M>(static_cast<X&&>(x)), n); | |
54 | } | |
55 | }; | |
56 | ||
57 | template <typename S> | |
58 | struct replicate_impl<S, when<Sequence<S>::value>> { | |
59 | template <typename X, std::size_t ...i> | |
60 | static constexpr auto replicate_helper(X&& x, std::index_sequence<i...>) | |
61 | { return hana::make<S>(((void)i, x)...); } | |
62 | ||
63 | template <typename X, typename N> | |
64 | static constexpr auto apply(X&& x, N const&) { | |
65 | constexpr std::size_t n = N::value; | |
66 | return replicate_helper(static_cast<X&&>(x), | |
67 | std::make_index_sequence<n>{}); | |
68 | } | |
69 | }; | |
1e59de90 | 70 | }} // end namespace boost::hana |
7c673cae FG |
71 | |
72 | #endif // !BOOST_HANA_REPLICATE_HPP |