state_wrapper.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 
30 #include <tuple>
31 
32 namespace atria {
33 namespace xform {
34 
39 struct no_tag {};
40 
66 template <typename TagT = no_tag,
67  typename StateT = void,
68  typename DataT = void>
69 struct state_wrapper : std::tuple<StateT, DataT>
70 {
71  using tag = TagT;
72  using base_t = std::tuple<StateT, DataT>;
73 
74  state_wrapper(const state_wrapper&) = default;
75  state_wrapper(state_wrapper&&) = default;
76  state_wrapper& operator=(const state_wrapper&) = default;
77  state_wrapper& operator=(state_wrapper&&) = default;
78 
79  template <typename T, typename U>
80  state_wrapper(T&& st, U&& data)
81  : base_t(std::forward<T>(st), std::forward<U>(data))
82  {}
83 };
84 
89 template <typename StateT, typename DecayedT = estd::decay_t<StateT> >
91  : std::false_type {};
92 
93 template <typename _, typename T, typename S, typename D>
94 struct is_state_wrapper<_, state_wrapper<T, S, D> >
95  : std::true_type {};
96 
103 template <typename TagT=no_tag, typename StateT, typename DataT=TagT>
104 auto wrap_state(StateT&& next, DataT&& data=DataT{})
105  -> state_wrapper<TagT,
108 {
109  return state_wrapper<TagT, estd::decay_t<StateT>, estd::decay_t<DataT> > {
110  std::forward<StateT>(next),
111  std::forward<DataT>(data)
112  };
113 }
114 
122 template <typename TagT, typename T>
123 auto state_wrapper_complete(TagT, T&& s)
125  state_complete(state_unwrap(std::forward<T>(s))))
126 
127 
134 template <typename TagT, typename T>
135 auto state_wrapper_unwrap(TagT, T&& s)
137  std::get<0>(std::forward<T>(s)))
138 
146 template <typename TagT, typename T>
147 auto state_wrapper_unwrap_all(TagT, T&& s)
149  state_unwrap_all(state_unwrap(std::forward<T>(s))))
150 
158  template <typename TagT, typename T, typename U>
159  auto state_wrapper_rewrap(TagT, T&& s, U&& x)
161  wrap_state<TagT>(
162  state_rewrap(state_unwrap(std::forward<T>(s)), x),
163  state_wrapper_data(std::forward<T>(s))))
164 
172 template <typename TagT, typename T, typename D>
173 auto state_wrapper_data(TagT tag, T&& s, D&&)
175  state_wrapper_data(tag, std::forward<T>(s)))
176 
177 template <typename T>
178 auto state_wrapper_data(T&& s)
180  state_wrapper_data(typename estd::decay_t<T>::tag{}, std::forward<T>(s)))
181 
182 template <typename TagT, typename T>
183 auto state_wrapper_data(TagT, T&& s)
185  std::get<1>(std::forward<T>(s)))
186 
187 
194 template <typename TagT, typename DataT>
195 bool state_wrapper_data_is_reduced(TagT, DataT&&)
196 {
197  return false;
198 }
199 
209 template <typename TagT, typename T>
210 bool state_wrapper_is_reduced(TagT tag, T&& s)
211 {
212  return state_wrapper_data_is_reduced(
213  tag, state_wrapper_data(tag, std::forward<T>(s)))
214  || state_is_reduced(state_unwrap(std::forward<T>(s)));
215 }
216 
230 template <typename TagT, typename StateT, typename DataT>
231 struct state_traits<state_wrapper<TagT, StateT, DataT> >
232 {
233  template <typename T>
234  static auto complete(T&& s)
236  state_wrapper_complete(TagT{}, std::forward<T>(s)))
237 
238  template <typename T>
239  static auto is_reduced(T&& s)
241  state_wrapper_is_reduced(TagT{}, std::forward<T>(s)))
242 
243  template <typename T, typename D>
244  static auto data(T&& s, D&& d)
246  state_wrapper_data(TagT{}, std::forward<T>(s), std::forward<D>(d)))
247 
248  template <typename T>
249  static auto unwrap(T&& s)
251  state_wrapper_unwrap(TagT{}, std::forward<T>(s)))
252 
253  template <typename T>
254  static auto unwrap_all(T&& s)
256  state_wrapper_unwrap_all(TagT{}, std::forward<T>(s)))
257 
258  template <typename T, typename U>
259  static auto rewrap(T&& s, U&& x)
261  state_wrapper_rewrap(TagT{}, std::forward<T>(s), std::forward<U>(x)))
262 };
263 
264 } // namespace xform
265 } // namespace atria
static auto complete(T &&state) -> decltype(std::forward< T >(state))
Unwraps all the layers of state wrappers returning the deepmost.
static auto is_reduced(T &&) -> bool
Returns whether the value is idempotent, and thus, the reduction can finish.
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...
#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_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
STL namespace.
bool state_wrapper_is_reduced(TagT tag, T &&s)
Utility function for easy overloading of state_traits::is_reduced for state wrappers with a specific ...
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...
Interface for a type specializing the State concept.
auto state_wrapper_unwrap(TagT, T &&s) -> decltype(std::get< 0 >(std::forward< T >(s)))
Utility function for easy overloading of state_traits::unwrap for state wrappers with a specific tag...
static auto unwrap(T &&state) -> decltype(std::forward< T >(state))
Unwraps this layers of state wrappers, returning the nested state for the next reducing function...
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
Metafunction returning whether StateT is a, or reference to, a state_wrapper instantiation.
static auto data(T &&, D &&d) -> decltype(std::forward< D >(d)())
Returns the associated from the current state.
static auto unwrap_all(T &&state) -> decltype(std::forward< T >(state))
Unwraps all layers of state wrappers, returning the most nested state for the innermost reducing func...
static auto rewrap(T &&, U &&x) -> decltype(std::forward< U >(x))
Copies all layers of state wrappers but replaces the innermost state with substate.
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 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 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.
A decorator for the accumulator of a reduction.
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...
Default tag for state_wrapper.
Fork me on GitHub