state_traits.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 <atria/estd/utility.hpp>
31 #include <atria/meta/utils.hpp>
32 
33 namespace atria {
34 namespace xform {
35 
52 template <typename StateT>
54 {
59  template <typename T>
60  static auto is_reduced(T&&) -> bool
61  { return false; }
62 
67  template <typename T, typename D>
68  static auto data(T&&, D&& d)
69  -> ABL_DECLTYPE_RETURN(std::forward<D>(d)())
70 
71 
74  template <typename T>
75  static auto complete(T&& state)
76  -> ABL_DECLTYPE_RETURN(std::forward<T>(state))
77 
82  template <typename T>
83  static auto unwrap(T&& state)
84  -> ABL_DECLTYPE_RETURN(std::forward<T>(state))
85 
90  template <typename T>
91  static auto unwrap_all(T&& state)
92  -> ABL_DECLTYPE_RETURN(std::forward<T>(state))
93 
98  template <typename T, typename U>
99  static auto rewrap(T&&, U&& x)
100  -> ABL_DECLTYPE_RETURN(std::forward<U>(x))
101 };
102 
103 template <typename T>
104 using state_traits_t = state_traits<estd::decay_t<T> >;
105 
109 template <typename T>
110 auto state_complete(T&& s)
112  state_traits_t<T>::complete(std::forward<T>(s)))
113 
117 template <typename T>
118 auto state_is_reduced(T&& s) -> bool
119 {
120  return state_traits_t<T>::is_reduced(std::forward<T>(s));
121 }
122 
126 template <typename T, typename D>
127 auto state_data(T&& s, D&& d)
129  state_traits_t<T>::data(std::forward<T>(s),
130  std::forward<D>(d)))
131 
132 
135 template <typename T>
136 auto state_unwrap(T&& s)
138  state_traits_t<T>::unwrap(std::forward<T>(s)))
139 
143 template <typename T>
144 auto state_unwrap_all(T&& s)
146  state_traits_t<T>::unwrap_all(std::forward<T>(s)))
147 
151 template <typename T, typename U>
152 auto state_rewrap(T&& s, U&& x)
154  state_traits_t<T>::rewrap(std::forward<T>(s), std::forward<U>(x)))
155 
156 } // namespace xform
157 } // 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.
#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
STL namespace.
typename std::decay< T >::type decay_t
Similar to C++14 std::decay_t.
Definition: type_traits.hpp:54
auto state_data(T &&s, D &&d) -> decltype(state_traits_t< T >::data(std::forward< T >(s), std::forward< D >(d)))
Convenience function for calling state_traits::data
Interface for a type specializing the State concept.
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
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
Fork me on GitHub