]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*! |
2 | @file | |
3 | Defines `boost::hana::replicate`. | |
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_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 | ||
27 | BOOST_HANA_NAMESPACE_BEGIN | |
28 | template <typename M> | |
29 | struct replicate_t { | |
30 | #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS | |
31 | static_assert(hana::MonadPlus<M>::value, | |
32 | "hana::replicate<M>(x, n) requires 'M' to be a MonadPlus"); | |
33 | #endif | |
34 | ||
35 | template <typename X, typename N> | |
36 | constexpr auto operator()(X&& x, N const& n) const { | |
37 | using Replicate = BOOST_HANA_DISPATCH_IF(replicate_impl<M>, | |
38 | hana::MonadPlus<M>::value && | |
39 | hana::IntegralConstant<N>::value | |
40 | ); | |
41 | ||
42 | #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS | |
43 | static_assert(hana::IntegralConstant<N>::value, | |
44 | "hana::replicate<M>(x, n) requires 'n' to be an IntegralConstant"); | |
45 | #endif | |
46 | ||
47 | return Replicate::apply(static_cast<X&&>(x), n); | |
48 | } | |
49 | }; | |
50 | ||
51 | template <typename M, bool condition> | |
52 | struct replicate_impl<M, when<condition>> : default_ { | |
53 | template <typename X, typename N> | |
54 | static constexpr auto apply(X&& x, N const& n) { | |
55 | return hana::cycle(hana::lift<M>(static_cast<X&&>(x)), n); | |
56 | } | |
57 | }; | |
58 | ||
59 | template <typename S> | |
60 | struct replicate_impl<S, when<Sequence<S>::value>> { | |
61 | template <typename X, std::size_t ...i> | |
62 | static constexpr auto replicate_helper(X&& x, std::index_sequence<i...>) | |
63 | { return hana::make<S>(((void)i, x)...); } | |
64 | ||
65 | template <typename X, typename N> | |
66 | static constexpr auto apply(X&& x, N const&) { | |
67 | constexpr std::size_t n = N::value; | |
68 | return replicate_helper(static_cast<X&&>(x), | |
69 | std::make_index_sequence<n>{}); | |
70 | } | |
71 | }; | |
72 | BOOST_HANA_NAMESPACE_END | |
73 | ||
74 | #endif // !BOOST_HANA_REPLICATE_HPP |