transducer.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 
29 #include <atria/meta/pack.hpp>
34 #include <atria/prelude/comp.hpp>
35 
36 #include <boost/mpl/eval_if.hpp>
37 #include <boost/mpl/identity.hpp>
38 
39 #include <functional>
40 #include <cassert>
41 
42 namespace atria {
43 namespace xform {
44 
45 namespace detail {
46 
47 template <typename CompleteT,
48  typename ReduceT>
49 struct transducer_tag
50 {
51  using complete_t = CompleteT;
52  using reduce_t = ReduceT;
53 };
54 
55 template <typename StateT,
56  typename ReducingFnT,
57  typename XformT,
58  typename... OutputTs>
59 struct transducer_state
60 {
61  template <typename WrappedT>
62  struct make_state_wrapper
63  {
64  using xformed_t = typename XformT::result_type;
65  using reduce_t = estd::decay_t<
66  estd::result_of_t<ReducingFnT(StateT, OutputTs...)> >;
67  using complete_t = estd::decay_t<
68  decltype(state_complete(std::declval<reduce_t>()))>;
69  using tag_t = transducer_tag<complete_t, reduce_t >;
70  using type = state_wrapper<tag_t, WrappedT, xformed_t>;
71  };
72 
73  using type = typename boost::mpl::eval_if<
74  is_state_wrapper<StateT>,
75  meta::identity<estd::decay_t<StateT> >,
76  make_state_wrapper<any_state>
77  >::type;
78 };
79 
80 template <typename StateT,
81  typename ReducingFnT,
82  typename XformT,
83  typename... OutputTs>
84 using transducer_state_t = typename transducer_state<
85  StateT, ReducingFnT, XformT, OutputTs...>::type;
86 
87 template <typename... OutputTs>
88 struct transducer_rf_gen
89 {
90  template <typename TagT>
91  struct from_any_state_rf_gen
92  {
93  template <typename ReducingFnT>
94  struct apply
95  {
96  ReducingFnT step;
97 
98  template <typename ...InputTs>
99  any_state operator() (any_state s, InputTs&& ...is)
100  {
101  using reduce_t = typename TagT::reduce_t;
102  using complete_t = typename TagT::complete_t;
103 
104  if (s.has<reduce_t>()) {
105  auto next = step(std::move(s).as<reduce_t>(),
106  std::forward<InputTs>(is)...);
107  s = std::move(next);
108  } else if (s.has<complete_t>()) {
109  auto next = step(std::move(s).as<complete_t>(),
110  std::forward<InputTs>(is)...);
111  s = std::move(next);
112  } else {
113  assert(!"oops!");
114  }
115  return s;
116  }
117  };
118  };
119 
120  template <typename TagT>
121  using from_any_state = transducer_impl<from_any_state_rf_gen<TagT> >;
122 
123  template <typename ReducingFnT,
124  typename XformT>
125  struct apply
126  {
127  ReducingFnT step;
128  XformT xform;
129 
130  template <typename StateT, typename... InputTs>
131  auto operator() (StateT st, InputTs&& ...ins)
132  -> transducer_state_t<StateT, ReducingFnT, XformT, OutputTs...>
133  {
134  using wrapped_t = transducer_state_t<
135  StateT, ReducingFnT, XformT, OutputTs...>;
136  using tag_t = typename wrapped_t::tag;
137 
138  return with_state(
139  std::move(st),
140  [&](StateT&& sst) {
141  auto xformed = comp(xform, from_any_state<tag_t>{})(step);
142  auto next = xformed(std::move(sst), std::forward<InputTs>(ins)...);
143  return wrap_state<tag_t> (
144  std::move(next),
145  std::move(xformed));
146  },
147  [&](wrapped_t&& sst) {
148  auto next = state_wrapper_data(sst)(
149  std::move(state_unwrap(sst)),
150  std::forward<InputTs>(ins)...);
151  state_unwrap(sst) = std::move(next);
152  return std::move(sst);
153  });
154  }
155  };
156 };
157 
158 template <typename C, typename R, typename T>
159 auto state_wrapper_complete(transducer_tag<C, R>, T&& wrapper) -> C
160 {
161  return state_complete(state_unwrap(std::forward<T>(wrapper)))
162  .template as<C>();
163 }
164 
165 template <typename C, typename R, typename T>
166 auto state_wrapper_unwrap_all(transducer_tag<C, R>, T&& wrapper) -> C
167 {
168  return state_unwrap_all(state_unwrap(std::forward<T>(wrapper)))
169  .template as<C>();
170 }
171 
172 template <typename C, typename R, typename T, typename U>
173 auto state_wrapper_rewrap(transducer_tag<C, R>, T&& s, U&& x)
174  -> estd::decay_t<T>
175 {
176  static_assert(std::is_same<estd::decay_t<U>, C>{} ||
177  std::is_same<estd::decay_t<U>, any_state>{},
178  "Yo! you are rewrapping with the wrong thing!");
179  return wrap_state<transducer_tag<C, R> >(
180  state_rewrap(state_unwrap(std::forward<T>(s)), std::forward<U>(x)),
181  state_wrapper_data(std::forward<T>(s)));
182 }
183 
184 template <typename... ArgTs>
185 struct reducing_function
186 {
187  using type = std::function<any_state(any_state, ArgTs...)>;
188 };
189 
190 template <typename InputT, typename OutputT>
191 using transducer_function_t = std::function<
192  meta::unpack_t<reducing_function, InputT> (
193  meta::unpack_t<reducing_function, OutputT>)>;
194 
195 } // namespace detail
196 
255 template <typename InputT=meta::pack<>, typename OutputT=InputT>
256 using transducer = transducer_impl<
257  meta::unpack<detail::transducer_rf_gen, OutputT>,
258  detail::transducer_function_t<InputT, OutputT> >;
259 
260 } // namespace xform
261 } // namespace atria
auto state_wrapper_data(TagT tag, T &&s, D &&) -> decltype(state_wrapper_data(tag, std::forward< T >(s)))
Utility function for easy overloading of state_traits::data for state wrappers with a specific tag...
auto state_wrapper_unwrap_all(TagT, T &&s) -> decltype(state_unwrap_all(state_unwrap(std::forward< T >(s))))
Utility function for easy overloading of state_traits::unwrap_all for state wrappers with a specific ...
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 state_wrapper_rewrap(TagT, T &&s, U &&x) -> decltype(wrap_state< TagT >( state_rewrap(state_unwrap(std::forward< T >(s)), x), state_wrapper_data(std::forward< T >(s))))
Utility function for easy overloading of state_traits::rewrap for state wrappers with a specific tag...
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
auto state_rewrap(T &&s, U &&x) -> decltype(state_traits_t< T >::rewrap(std::forward< T >(s), std::forward< U >(x)))
Convenience function for calling state_traits::unwrap_all
auto state_complete(T &&s) -> decltype(state_traits_t< T >::complete(std::forward< T >(s)))
Convenience function for calling state_traits::complete
auto comp(F &&f) -> F &&
Right-to left function composition.
Definition: comp.hpp:80
transducer_impl< meta::unpack< detail::transducer_rf_gen, OutputT >, detail::transducer_function_t< InputT, OutputT > > transducer
Type erased transducer.
Definition: transducer.hpp:258
C++ amazing templates and reusable implementations awesomeness.
Definition: _doc.hpp:35
auto xformed(Xform &&xform, InTs &&...ins) -> estd::enable_if_t< meta::all(In_value< InTs >()...), detail::xformed_input< typename decltype( detail::make_xform_down_signal( xform, detail::access::signal(ins)...) )::element_type > >
Returns a new in formed by applying a transducer xform on the successive values of the in...
Definition: xformed.hpp:70
auto state_unwrap_all(T &&s) -> decltype(state_traits_t< T >::unwrap_all(std::forward< T >(s)))
Convenience function for calling state_traits::unwrap_all
auto state_wrapper_complete(TagT, T &&s) -> decltype(state_complete(state_unwrap(std::forward< T >(s))))
Utility function for easy overloading of state_traits::complete for state wrappers with a specific ta...
typename std::result_of< T >::type result_of_t
Similar to C++14 std::result_of_t.
Definition: type_traits.hpp:60
Fork me on GitHub