Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / hana / append.hpp
1 /*!
2 @file
3 Defines `boost::hana::append`.
4
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)
8  */
9
10 #ifndef BOOST_HANA_APPEND_HPP
11 #define BOOST_HANA_APPEND_HPP
12
13 #include <boost/hana/fwd/append.hpp>
14
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>
24
25 #include <cstddef>
26 #include <utility>
27
28
29 BOOST_HANA_NAMESPACE_BEGIN
30     //! @cond
31     template <typename Xs, typename X>
32     constexpr auto append_t::operator()(Xs&& xs, X&& x) const {
33         using M = typename hana::tag_of<Xs>::type;
34         using Append = BOOST_HANA_DISPATCH_IF(append_impl<M>,
35             hana::MonadPlus<M>::value
36         );
37
38     #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
39         static_assert(hana::MonadPlus<M>::value,
40         "hana::append(xs, x) requires 'xs' to be a MonadPlus");
41     #endif
42
43         return Append::apply(static_cast<Xs&&>(xs), static_cast<X&&>(x));
44     }
45     //! @endcond
46
47     template <typename M, bool condition>
48     struct append_impl<M, when<condition>> : default_ {
49         template <typename Xs, typename X>
50         static constexpr auto apply(Xs&& xs, X&& x) {
51             return hana::concat(static_cast<Xs&&>(xs),
52                                 hana::lift<M>(static_cast<X&&>(x)));
53         }
54     };
55
56     template <typename S>
57     struct append_impl<S, when<Sequence<S>::value>> {
58         template <typename Xs, typename X, std::size_t ...i>
59         static constexpr auto append_helper(Xs&& xs, X&& x, std::index_sequence<i...>) {
60             return hana::make<S>(
61                 hana::at_c<i>(static_cast<Xs&&>(xs))..., static_cast<X&&>(x)
62             );
63         }
64
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 append_helper(static_cast<Xs&&>(xs), static_cast<X&&>(x),
69                                  std::make_index_sequence<N>{});
70         }
71     };
72 BOOST_HANA_NAMESPACE_END
73
74 #endif // !BOOST_HANA_APPEND_HPP