inout.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 
33 
34 namespace atria {
35 namespace funken {
36 
37 namespace detail {
38 
39 template <typename SignalT>
40 class inoutput_impl
41  : private watchable<meta::value_t<SignalT> >
42 {
43  template <typename T> friend class inoutput_impl;
44  friend class detail::access;
45 
46  using base_t = watchable<meta::value_t<SignalT> >;
47 
48  using signal_ptr_t = std::shared_ptr<SignalT>;
49  signal_ptr_t signal_;
50  const signal_ptr_t& signal() const { return signal_; }
51 
52 public:
53  using value_type = meta::value_t<SignalT>;
54 
55  inoutput_impl(signal_ptr_t sig)
56  : signal_(std::move(sig)) {}
57 
58  inoutput_impl() = default;
59  inoutput_impl(inoutput_impl&&) = default;
60  inoutput_impl& operator=(inoutput_impl&&) = default;
61  inoutput_impl(const inoutput_impl&) = delete;
62  inoutput_impl& operator=(const inoutput_impl&) = delete;
63 
64  template <typename T>
65  inoutput_impl& operator=(inoutput_impl<T>&& x)
66  {
67  base_t::operator=(std::move(x));
68  signal_ = std::move(x.signal_);
69  return *this;
70  }
71 
72  template <typename T>
73  inoutput_impl(inoutput_impl<T>&& x)
74  : base_t(std::move(x))
75  , signal_(std::move(x.signal_))
76  {}
77 
78  auto get() const
79  -> ABL_DECLTYPE_RETURN(signal_->last())
80 
81  template <typename T>
82  void set(T&& value)
83  {
84  return signal_->send_up(std::forward<T>(value));
85  }
86 };
87 
88 } // namespace detail
89 
95 template <typename T>
96 class inoutput : public detail::inoutput_impl<detail::up_down_signal<T> >
97 {
98  using base_t = detail::inoutput_impl<detail::up_down_signal<T> >;
99  using base_t::base_t;
100 };
101 
106 template <typename InoutT>
107 auto inout(InoutT&& object)
109  (Inout_value<InoutT>()),
110  detail::inoutput_impl<detail::signal_type_t<InoutT> > >
111 {
112  return detail::access::signal(std::forward<InoutT>(object));
113 }
114 
115 } // namespace funken
116 } // namespace atria
#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
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 inout(InoutT &&object) -> estd::enable_if_t< (Inout_value< InoutT >()), detail::inoutput_impl< detail::signal_type_t< InoutT > > >
Creates an inout from another inout value.
Definition: inout.hpp:107
This module implements the signal flow in funken.
Provides access to reading and writing values of type T.
Definition: inout.hpp:96
C++ amazing templates and reusable implementations awesomeness.
Definition: _doc.hpp:35
Fork me on GitHub