• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*!
2 @file
3 Defines `boost::hana::prepend`.
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_PREPEND_HPP
11 #define BOOST_HANA_PREPEND_HPP
12 
13 #include <boost/hana/fwd/prepend.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>
operator ()(Xs && xs,X && x) const32     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
36         );
37 
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");
41     #endif
42 
43         return Prepend::apply(static_cast<Xs&&>(xs), static_cast<X&&>(x));
44     }
45     //! @endcond
46 
47     template <typename M, bool condition>
48     struct prepend_impl<M, when<condition>> : default_ {
49         template <typename Xs, typename X>
applyprepend_impl50         static constexpr auto apply(Xs&& xs, X&& x) {
51             return hana::concat(hana::lift<M>(static_cast<X&&>(x)),
52                                 static_cast<Xs&&>(xs));
53         }
54     };
55 
56     template <typename S>
57     struct prepend_impl<S, when<Sequence<S>::value>> {
58         template <typename Xs, typename X, std::size_t ...i>
prepend_helperprepend_impl59         static constexpr auto prepend_helper(Xs&& xs, X&& x, std::index_sequence<i...>) {
60             return hana::make<S>(
61                 static_cast<X&&>(x), hana::at_c<i>(static_cast<Xs&&>(xs))...
62             );
63         }
64 
65         template <typename Xs, typename X>
applyprepend_impl66         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>{});
70         }
71     };
72 BOOST_HANA_NAMESPACE_END
73 
74 #endif // !BOOST_HANA_PREPEND_HPP
75