File indexing completed on 2025-01-18 09:38:05
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_HANA_REPLICATE_HPP
0011 #define BOOST_HANA_REPLICATE_HPP
0012
0013 #include <boost/hana/fwd/replicate.hpp>
0014
0015 #include <boost/hana/concept/integral_constant.hpp>
0016 #include <boost/hana/concept/monad_plus.hpp>
0017 #include <boost/hana/config.hpp>
0018 #include <boost/hana/core/dispatch.hpp>
0019 #include <boost/hana/core/make.hpp>
0020 #include <boost/hana/cycle.hpp>
0021 #include <boost/hana/lift.hpp>
0022
0023 #include <cstddef>
0024 #include <utility>
0025
0026
0027 namespace boost { namespace hana {
0028
0029 template <typename M>
0030 template <typename X, typename N>
0031 constexpr auto replicate_t<M>::operator()(X&& x, N const& n) const {
0032 using Replicate = BOOST_HANA_DISPATCH_IF(replicate_impl<M>,
0033 hana::MonadPlus<M>::value &&
0034 hana::IntegralConstant<N>::value
0035 );
0036
0037 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
0038 static_assert(hana::MonadPlus<M>::value,
0039 "hana::replicate<M>(x, n) requires 'M' to be a MonadPlus");
0040
0041 static_assert(hana::IntegralConstant<N>::value,
0042 "hana::replicate<M>(x, n) requires 'n' to be an IntegralConstant");
0043 #endif
0044
0045 return Replicate::apply(static_cast<X&&>(x), n);
0046 }
0047
0048
0049 template <typename M, bool condition>
0050 struct replicate_impl<M, when<condition>> : default_ {
0051 template <typename X, typename N>
0052 static constexpr auto apply(X&& x, N const& n) {
0053 return hana::cycle(hana::lift<M>(static_cast<X&&>(x)), n);
0054 }
0055 };
0056
0057 template <typename S>
0058 struct replicate_impl<S, when<Sequence<S>::value>> {
0059 template <typename X, std::size_t ...i>
0060 static constexpr auto replicate_helper(X&& x, std::index_sequence<i...>)
0061 { return hana::make<S>(((void)i, x)...); }
0062
0063 template <typename X, typename N>
0064 static constexpr auto apply(X&& x, N const&) {
0065 constexpr std::size_t n = N::value;
0066 return replicate_helper(static_cast<X&&>(x),
0067 std::make_index_sequence<n>{});
0068 }
0069 };
0070 }}
0071
0072 #endif