3 Defines `boost::hana::replicate`.
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)
10 #ifndef BOOST_HANA_REPLICATE_HPP
11 #define BOOST_HANA_REPLICATE_HPP
13 #include <boost/hana/fwd/replicate.hpp>
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>
27 BOOST_HANA_NAMESPACE_BEGIN
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");
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
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");
47 return Replicate::apply(static_cast<X&&>(x), n);
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);
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)...); }
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>{});
72 BOOST_HANA_NAMESPACE_END
74 #endif // !BOOST_HANA_REPLICATE_HPP