• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*=============================================================================
2    Copyright (c) 2015 Paul Fultz II
3    rotate.h
4    Distributed under the Boost Software License, Version 1.0. (See accompanying
5    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6==============================================================================*/
7
8#ifndef BOOST_HOF_GUARD_ROTATE_H
9#define BOOST_HOF_GUARD_ROTATE_H
10
11/// rotate
12/// ====
13///
14/// Description
15/// -----------
16///
17/// The `rotate` function adaptor moves the first parameter to the last
18/// parameter.
19///
20/// Synopsis
21/// --------
22///
23///     template<class F>
24///     rotate_adaptor<F> rotate(F f);
25///
26/// Semantics
27/// ---------
28///
29///     assert(rotate(f)(x, xs...) == f(xs..., x));
30///
31/// Requirements
32/// ------------
33///
34/// F must be:
35///
36/// * [ConstInvocable](ConstInvocable)
37/// * MoveConstructible
38///
39/// Example
40/// -------
41///
42///     #include <boost/hof.hpp>
43///     #include <cassert>
44///
45///     int main() {
46///         int r = boost::hof::rotate(boost::hof::_ - boost::hof::_)(2, 5);
47///         assert(r == 3);
48///     }
49///
50
51#include <boost/hof/detail/result_of.hpp>
52#include <boost/hof/reveal.hpp>
53#include <boost/hof/detail/make.hpp>
54#include <boost/hof/detail/static_const_var.hpp>
55
56namespace boost { namespace hof {
57
58template<class F>
59struct rotate_adaptor : detail::callable_base<F>
60{
61    typedef rotate_adaptor fit_rewritable1_tag;
62    BOOST_HOF_INHERIT_CONSTRUCTOR(rotate_adaptor, detail::callable_base<F>);
63
64    template<class... Ts>
65    constexpr const detail::callable_base<F>& base_function(Ts&&... xs) const noexcept
66    {
67        return boost::hof::always_ref(*this)(xs...);
68    }
69
70    struct rotate_failure
71    {
72        template<class Failure>
73        struct apply
74        {
75            template<class T, class... Ts>
76            struct of
77            : Failure::template of<Ts..., T>
78            {};
79        };
80    };
81
82    struct failure
83    : failure_map<rotate_failure, detail::callable_base<F>>
84    {};
85
86    BOOST_HOF_RETURNS_CLASS(rotate_adaptor);
87
88    template<class T, class... Ts>
89    constexpr BOOST_HOF_SFINAE_RESULT(const detail::callable_base<F>&, id_<Ts>..., id_<T>)
90    operator()(T&& x, Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
91    (
92        (BOOST_HOF_MANGLE_CAST(const detail::callable_base<F>&)(BOOST_HOF_CONST_THIS->base_function(xs...)))
93            (BOOST_HOF_FORWARD(Ts)(xs)..., BOOST_HOF_FORWARD(T)(x))
94    );
95};
96
97BOOST_HOF_DECLARE_STATIC_VAR(rotate, detail::make<rotate_adaptor>);
98
99}} // namespace boost::hof
100
101#endif
102