]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*! |
2 | @file | |
3 | Defines `boost::hana::transform`. | |
4 | ||
5 | @copyright Louis Dionne 2013-2016 | |
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_TRANSFORM_HPP | |
11 | #define BOOST_HANA_TRANSFORM_HPP | |
12 | ||
13 | #include <boost/hana/fwd/transform.hpp> | |
14 | ||
15 | #include <boost/hana/bool.hpp> | |
16 | #include <boost/hana/concept/functor.hpp> | |
17 | #include <boost/hana/concept/sequence.hpp> | |
18 | #include <boost/hana/config.hpp> | |
19 | #include <boost/hana/core/dispatch.hpp> | |
20 | #include <boost/hana/core/make.hpp> | |
21 | #include <boost/hana/functional/always.hpp> | |
22 | #include <boost/hana/fwd/adjust_if.hpp> | |
23 | #include <boost/hana/unpack.hpp> | |
24 | ||
25 | ||
26 | BOOST_HANA_NAMESPACE_BEGIN | |
27 | //! @cond | |
28 | template <typename Xs, typename F> | |
29 | constexpr auto transform_t::operator()(Xs&& xs, F&& f) const { | |
30 | using S = typename hana::tag_of<Xs>::type; | |
31 | using Transform = BOOST_HANA_DISPATCH_IF(transform_impl<S>, | |
32 | hana::Functor<S>::value | |
33 | ); | |
34 | ||
35 | #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS | |
36 | static_assert(hana::Functor<S>::value, | |
37 | "hana::transform(xs, f) requires 'xs' to be a Functor"); | |
38 | #endif | |
39 | ||
40 | return Transform::apply(static_cast<Xs&&>(xs), static_cast<F&&>(f)); | |
41 | } | |
42 | //! @endcond | |
43 | ||
44 | template <typename Fun, bool condition> | |
45 | struct transform_impl<Fun, when<condition>> : default_ { | |
46 | template <typename Xs, typename F> | |
47 | static constexpr auto apply(Xs&& xs, F&& f) { | |
48 | return hana::adjust_if(static_cast<Xs&&>(xs), | |
49 | hana::always(hana::true_c), | |
50 | static_cast<F&&>(f)); | |
51 | } | |
52 | }; | |
53 | ||
54 | template <typename S> | |
55 | struct transform_impl<S, when<Sequence<S>::value>> { | |
56 | //! @cond | |
57 | template <typename F> | |
58 | struct transformer { | |
59 | F f; | |
60 | template <typename ...Xs> | |
61 | constexpr auto operator()(Xs&& ...xs) const { | |
62 | return hana::make<S>((*f)(static_cast<Xs&&>(xs))...); | |
63 | } | |
64 | }; | |
65 | //! @endcond | |
66 | ||
67 | template <typename Xs, typename F> | |
68 | static constexpr auto apply(Xs&& xs, F&& f) { | |
69 | // We use a pointer to workaround a Clang 3.5 ICE | |
70 | return hana::unpack(static_cast<Xs&&>(xs), | |
71 | transformer<decltype(&f)>{&f}); | |
72 | } | |
73 | }; | |
74 | BOOST_HANA_NAMESPACE_END | |
75 | ||
76 | #endif // !BOOST_HANA_TRANSFORM_HPP |