interpose.hpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2014, 2015 Ableton AG, Berlin. All rights reserved.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21 //
22 
27 #pragma once
28 
34 
35 namespace atria {
36 namespace xform {
37 
38 namespace detail {
39 
40 struct interpose_rf_gen
41 {
42  template <typename ReducingFnT,
43  typename IndexSequenceT,
44  typename ValueT>
45  struct apply
46  {
47  ReducingFnT step;
48  IndexSequenceT indices;
49  ValueT value;
50 
51  template <std::size_t... Indices, typename StateT>
52  auto interp(estd::index_sequence<Indices...>, StateT&& s)
54  step(std::forward<StateT>(s), std::get<Indices>(value)...))
55 
56  template <typename StateT, typename ...InputTs>
57  auto operator() (StateT&& s, InputTs&& ...is)
58  -> decltype(wrap_state(step(state_unwrap(s), is...)))
59  {
60  using std::begin;
61  using std::end;
62 
63  using result_t = decltype(wrap_state(step(state_unwrap(s), is...)));
64  using complete_t = decltype(state_complete(s));
65 
66  using wrapped_t = meta::copy_decay_t<StateT, estd::decay_t<result_t>>;
67  using unwrapped_t = meta::copy_decay_t<StateT, estd::decay_t<complete_t>>;
68 
69  return with_state(
70  std::forward<StateT>(s),
71  [&] (unwrapped_t&& st) {
72  return wrap_state(
73  step(std::forward<unwrapped_t>(st),
74  std::forward<InputTs>(is)...));
75  },
76  [&] (wrapped_t&& st) {
77  auto next = interp(indices, state_unwrap(std::forward<wrapped_t>(st)));
78  return wrap_state(
79  !state_is_reduced(next)
80  ? step(std::move(next), std::forward<InputTs>(is)...)
81  : std::move(next));
82  });
83  }
84  };
85 };
86 
87 } // namespace detail
88 
89 template <typename T1, typename T2>
90 using interpose_t = transducer_impl<detail::interpose_rf_gen, T1, T2>;
91 
95 template <typename... ValueTs>
96 constexpr auto interpose(ValueTs&& ...xs)
97  -> interpose_t<estd::make_index_sequence<sizeof...(ValueTs)>,
98  std::tuple<estd::decay_t<ValueTs>...> >
99 {
100  using indices_t = estd::make_index_sequence<sizeof...(ValueTs)>;
101 
103  indices_t{},
104  std::make_tuple(std::forward<ValueTs>(xs)...)
105  };
106 }
107 
108 } // namespace xform
109 } // namespace atria
#define ABL_DECLTYPE_RETURN(body_expr)
Utility for defining generic functions with a deduced return type, that are composed of a single expr...
Definition: utils.hpp:109
auto state_unwrap(T &&s) -> decltype(state_traits_t< T >::unwrap(std::forward< T >(s)))
Convenience function for calling state_traits::unwrap
typename std::decay< T >::type decay_t
Similar to C++14 std::decay_t.
Definition: type_traits.hpp:54
auto with_state(StateT &&st, UnwrappedFn &&, WrappedFn &&fn) -> meta::lazy_enable_if_t< !std::is_same< estd::decay_t< StateT >, estd::decay_t< decltype(state_complete(st))> >::value, std::result_of< WrappedFn(StateT)> >
Given a value st that represents the state of a reduction, this function generically dispatches to th...
Definition: with_state.hpp:58
constexpr auto interpose(ValueTs &&...xs) -> interpose_t< estd::make_index_sequence< sizeof...(ValueTs)>, std::tuple< estd::decay_t< ValueTs >... > >
Like clojure.core/interpose$1.
Definition: interpose.hpp:96
Utility to write simple transducers easily.
auto state_complete(T &&s) -> decltype(state_traits_t< T >::complete(std::forward< T >(s)))
Convenience function for calling state_traits::complete
C++ amazing templates and reusable implementations awesomeness.
Definition: _doc.hpp:35
auto state_is_reduced(T &&s) -> bool
Convenience function for calling state_traits::is_reduced
auto wrap_state(StateT &&next, DataT &&data=DataT{}) -> state_wrapper< TagT, estd::decay_t< StateT >, estd::decay_t< DataT > >
Given a tag TagT and a state next and associated data, returns a state_wrapper instance.
Fork me on GitHub