with_state.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 
31 
32 namespace atria {
33 namespace xform {
34 
55 template <typename StateT,
56  typename UnwrappedFn,
57  typename WrappedFn>
58 auto with_state(StateT&& st, UnwrappedFn&&, WrappedFn&& fn)
59  -> meta::lazy_enable_if_t<
60  !std::is_same<
63  >::value,
64  std::result_of<WrappedFn(StateT)>
65  >
66 {
67  return std::forward<WrappedFn>(fn)(std::forward<StateT>(st));
68 };
69 
70 template <typename StateT,
71  typename UnwrappedFn,
72  typename WrappedFn>
73 auto with_state(StateT&& st, UnwrappedFn&& fn, WrappedFn&&)
74  -> meta::lazy_enable_if_t<
75  !std::is_same<estd::decay_t<StateT>, any_state>::value
76  && std::is_same<
77  estd::decay_t<StateT>,
79  >::value,
80  std::result_of<UnwrappedFn(StateT)>
81  >
82 {
83  return std::forward<UnwrappedFn>(fn)(std::forward<StateT>(st));
84 };
85 
86 template <typename StateT,
87  typename UnwrappedFn,
88  typename WrappedFn>
89 auto with_state(StateT&& st, UnwrappedFn&& fn1, WrappedFn&& fn2)
90  -> meta::lazy_enable_if_t<
91  std::is_same<
92  estd::decay_t<StateT>,
93  any_state
94  >::value,
95  std::result_of<UnwrappedFn(StateT)>
96  >
97 {
98  using wrapped_state_t = estd::result_of_t<UnwrappedFn(StateT)>;
99 
100  if (!st.template has<wrapped_state_t>()) {
101  return std::forward<UnwrappedFn>(fn1)(
102  std::forward<StateT>(st));
103  } else {
104  return std::forward<WrappedFn>(fn2)(
105  std::forward<StateT>(st).template as<wrapped_state_t>());
106  }
107 };
108 
109 } // namespace xform
110 } // namespace atria
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
C++ amazing templates and reusable implementations awesomeness.
Definition: _doc.hpp:35
Fork me on GitHub