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>
32 #include <atria/prelude/comp.hpp>
33 #include <functional>
34 
35 namespace atria {
36 namespace xform {
37 namespace impure {
38 
39 namespace detail {
40 
41 template <typename... OutputTs>
42 struct transducer_rf_gen
43 {
44  template <typename StateT>
45  struct from_any_state_rf_gen
46  {
47  template <typename ReducingFnT>
48  struct apply
49  {
50  ReducingFnT step;
51 
52  template <typename ...InputTs>
53  any_state operator() (any_state s, InputTs&& ...is)
54  {
55  auto next = step(std::move(s).as<StateT>(),
56  std::forward<InputTs>(is)...);
57  s = std::move(next);
58  return s;
59  }
60  };
61  };
62 
63  template <typename StateT>
64  using from_any_state = transducer_impl<from_any_state_rf_gen<StateT> >;
65 
66  template <typename ReducingFnT,
67  typename XformT>
68  struct apply
69  {
70  ReducingFnT step;
71  XformT xform;
72 
73  apply(ReducingFnT step_, XformT xform_)
74  : step(std::move(step_)), xform(std::move(xform_)) {}
75 
76  using xformed_t = typename XformT::result_type;
77  xformed_t xformed = {};
78 
79  template <typename StateT, typename... InputTs>
80  auto operator() (StateT&& s, InputTs&& ...ins)
82  !std::is_same<estd::decay_t<StateT>, any_state>{},
83  any_state>
84  {
85  xformed = comp(xform, from_any_state<StateT>{})(step);
86  return xformed(std::forward<StateT>(s), std::forward<InputTs>(ins)...);
87  }
88 
89  template <typename StateT, typename... InputTs>
90  auto operator() (StateT&& s, InputTs&& ...ins)
92  std::is_same<estd::decay_t<StateT>, any_state>{},
93  any_state>
94  {
95  return xformed(std::forward<StateT>(s), std::forward<InputTs>(ins)...);
96  }
97  };
98 };
99 
100 template <typename... ArgTs>
101 struct reducing_function
102 {
103  using type = std::function<any_state(any_state, ArgTs...)>;
104 };
105 
106 template <typename InputT, typename OutputT>
107 using transducer_function_t = std::function<
108  meta::unpack_t<reducing_function, InputT> (
109  meta::unpack_t<reducing_function, OutputT>)>;
110 
111 } // namespace detail
112 
113 template <typename InputT, typename OutputT=InputT>
114 using transducer = transducer_impl<
115  meta::unpack<detail::transducer_rf_gen, OutputT>,
116  detail::transducer_function_t<InputT, OutputT> >;
117 
118 } // namespace impure
119 } // namespace xform
120 } // namespace atria
typename std::enable_if< X, T >::type enable_if_t
Similar to C++14 std::enable_if_t.
Definition: type_traits.hpp:84
STL namespace.
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
Fork me on GitHub