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