Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / hana / fill.hpp
1 /*!
2 @file
3 Defines `boost::hana::fill`.
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_FILL_HPP
11 #define BOOST_HANA_FILL_HPP
12
13 #include <boost/hana/fwd/fill.hpp>
14
15 #include <boost/hana/concept/functor.hpp>
16 #include <boost/hana/concept/sequence.hpp>
17 #include <boost/hana/config.hpp>
18 #include <boost/hana/core/dispatch.hpp>
19 #include <boost/hana/core/make.hpp>
20 #include <boost/hana/functional/always.hpp>
21 #include <boost/hana/transform.hpp>
22 #include <boost/hana/unpack.hpp>
23
24
25 BOOST_HANA_NAMESPACE_BEGIN
26     //! @cond
27     template <typename Xs, typename Value>
28     constexpr auto fill_t::operator()(Xs&& xs, Value&& value) const {
29         using S = typename hana::tag_of<Xs>::type;
30         using Fill = BOOST_HANA_DISPATCH_IF(fill_impl<S>,
31             hana::Functor<S>::value
32         );
33
34     #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
35         static_assert(hana::Functor<S>::value,
36         "hana::fill(xs, value) requires 'xs' to be a Functor");
37     #endif
38
39         return Fill::apply(static_cast<Xs&&>(xs),
40                            static_cast<Value&&>(value));
41     }
42     //! @endcond
43
44     template <typename Fun, bool condition>
45     struct fill_impl<Fun, when<condition>> : default_ {
46         template <typename Xs, typename Value>
47         static constexpr auto apply(Xs&& xs, Value&& v) {
48             return hana::transform(static_cast<Xs&&>(xs),
49                                    hana::always(static_cast<Value&&>(v))
50             );
51         }
52     };
53
54     template <typename S>
55     struct fill_impl<S, when<Sequence<S>::value>> {
56         //! @cond
57         template <typename V>
58         struct filler {
59             V const& v;
60             template <typename ...Xs>
61             constexpr auto operator()(Xs const& ...xs) const {
62                 return hana::make<S>(((void)xs, v)...);
63             }
64         };
65         //! @endcond
66
67         template <typename Xs, typename V>
68         static constexpr auto apply(Xs const& xs, V const& v) {
69             return hana::unpack(xs, filler<V>{v});
70         }
71     };
72 BOOST_HANA_NAMESPACE_END
73
74 #endif // !BOOST_HANA_FILL_HPP