3 Defines `boost::hana::prepend`.
5 @copyright Louis Dionne 2013-2017
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_PREPEND_HPP
11 #define BOOST_HANA_PREPEND_HPP
13 #include <boost/hana/fwd/prepend.hpp>
15 #include <boost/hana/at.hpp>
16 #include <boost/hana/concat.hpp>
17 #include <boost/hana/concept/monad_plus.hpp>
18 #include <boost/hana/concept/sequence.hpp>
19 #include <boost/hana/config.hpp>
20 #include <boost/hana/core/dispatch.hpp>
21 #include <boost/hana/core/make.hpp>
22 #include <boost/hana/length.hpp>
23 #include <boost/hana/lift.hpp>
29 BOOST_HANA_NAMESPACE_BEGIN
31 template <typename Xs, typename X>
32 constexpr auto prepend_t::operator()(Xs&& xs, X&& x) const {
33 using M = typename hana::tag_of<Xs>::type;
34 using Prepend = BOOST_HANA_DISPATCH_IF(prepend_impl<M>,
35 hana::MonadPlus<M>::value
38 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
39 static_assert(hana::MonadPlus<M>::value,
40 "hana::prepend(xs, x) requires 'xs' to be a MonadPlus");
43 return Prepend::apply(static_cast<Xs&&>(xs), static_cast<X&&>(x));
47 template <typename M, bool condition>
48 struct prepend_impl<M, when<condition>> : default_ {
49 template <typename Xs, typename X>
50 static constexpr auto apply(Xs&& xs, X&& x) {
51 return hana::concat(hana::lift<M>(static_cast<X&&>(x)),
52 static_cast<Xs&&>(xs));
57 struct prepend_impl<S, when<Sequence<S>::value>> {
58 template <typename Xs, typename X, std::size_t ...i>
59 static constexpr auto prepend_helper(Xs&& xs, X&& x, std::index_sequence<i...>) {
61 static_cast<X&&>(x), hana::at_c<i>(static_cast<Xs&&>(xs))...
65 template <typename Xs, typename X>
66 static constexpr auto apply(Xs&& xs, X&& x) {
67 constexpr std::size_t N = decltype(hana::length(xs))::value;
68 return prepend_helper(static_cast<Xs&&>(xs), static_cast<X&&>(x),
69 std::make_index_sequence<N>{});
72 BOOST_HANA_NAMESPACE_END
74 #endif // !BOOST_HANA_PREPEND_HPP