Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / hana / replace.hpp
1 /*!
2 @file
3 Defines `boost::hana::replace`.
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_REPLACE_HPP
11 #define BOOST_HANA_REPLACE_HPP
12
13 #include <boost/hana/fwd/replace.hpp>
14
15 #include <boost/hana/concept/functor.hpp>
16 #include <boost/hana/config.hpp>
17 #include <boost/hana/core/dispatch.hpp>
18 #include <boost/hana/equal.hpp>
19 #include <boost/hana/replace_if.hpp>
20
21
22 BOOST_HANA_NAMESPACE_BEGIN
23     //! @cond
24     template <typename Xs, typename OldVal, typename NewVal>
25     constexpr auto replace_t::operator()(Xs&& xs, OldVal&& oldval, NewVal&& newval) const {
26         using S = typename hana::tag_of<Xs>::type;
27         using Replace = BOOST_HANA_DISPATCH_IF(replace_impl<S>,
28             hana::Functor<S>::value
29         );
30
31     #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
32         static_assert(hana::Functor<S>::value,
33         "hana::replace(xs, oldval, newval) requires 'xs' to be a Functor");
34     #endif
35
36         return Replace::apply(static_cast<Xs&&>(xs),
37                               static_cast<OldVal&&>(oldval),
38                               static_cast<NewVal&&>(newval));
39     }
40     //! @endcond
41
42     template <typename Fun, bool condition>
43     struct replace_impl<Fun, when<condition>> : default_ {
44         template <typename Xs, typename OldVal, typename NewVal>
45         static constexpr decltype(auto)
46         apply(Xs&& xs, OldVal&& oldval, NewVal&& newval) {
47             return hana::replace_if(
48                 static_cast<Xs&&>(xs),
49                 hana::equal.to(static_cast<OldVal&&>(oldval)),
50                 static_cast<NewVal&&>(newval)
51             );
52         }
53     };
54 BOOST_HANA_NAMESPACE_END
55
56 #endif // !BOOST_HANA_REPLACE_HPP