signals.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 
49 #pragma once
50 
51 #include <ableton/build_system/Warnings.hpp>
52 ABL_DISABLE_WARNINGS
53 #include <boost/signals2/signal.hpp>
54 ABL_RESTORE_WARNINGS
55 #include <memory>
56 #include <vector>
57 #include <functional>
58 
59 namespace atria {
60 namespace funken {
61 
62 namespace detail {
63 
67 constexpr struct
68 {
69  template <typename T1, typename T2>
70  bool operator() (const T1& a, const T2& b)
71  {
72  return
73  !a.owner_before(b) &&
74  !b.owner_before(a);
75  }
76 } owner_equals {};
77 
84 struct down_signal_base
85 {
86  virtual ~down_signal_base();
87  virtual void send_down() = 0;
88  virtual void notify() = 0;
89 };
90 
94 template <typename T>
95 struct up_signal_base
96 {
97  virtual ~up_signal_base() {}
98  virtual void send_up(const T&) = 0;
99  virtual void send_up(T&&) = 0;
100 };
101 
106 template <typename T>
107 class down_signal
108  : public std::enable_shared_from_this<down_signal<T> >
109  , public down_signal_base
110 {
111 public:
112  using value_type = T;
113 
114  down_signal(down_signal&&) = default;
115  down_signal(const down_signal&) = delete;
116  down_signal& operator=(down_signal&&) = default;
117  down_signal& operator=(const down_signal&) = delete;
118 
119  down_signal(T value)
120  : current_(std::move(value))
121  , last_(current_)
122  , last_notified_(current_)
123  {}
124 
125  virtual void recompute() {}
126  virtual void recompute_deep() {}
127 
128  const value_type& current() const { return current_; }
129  const value_type& last() const { return last_; }
130 
131  void link(std::weak_ptr<down_signal_base> child)
132  {
133  using namespace std;
134  using std::placeholders::_1;
135  assert(find_if(begin(children_), end(children_),
136  bind(owner_equals, child, _1))
137  == end(children_) &&
138  "Child signal must not be linked twice");
139  children_.push_back(child);
140  }
141 
142  template <typename U>
143  void push_down(U&& value)
144  {
145  if (value != current_)
146  {
147  current_ = std::forward<U>(value);
148  needs_send_down_ = true;
149  }
150  }
151 
152  void send_down() final
153  {
154  recompute();
155  if (needs_send_down_)
156  {
157  last_ = current_;
158  needs_send_down_ = false;
159  needs_notify_ = true;
160 
161  for (auto& wchild : children_)
162  {
163  if (auto child = wchild.lock())
164  {
165  child->send_down();
166  }
167  }
168  }
169  }
170 
171  void notify() final
172  {
173  using namespace std;
174  if (!needs_send_down_ && needs_notify_)
175  {
176  needs_notify_ = false;
177  observers_(last_notified_, last_);
178  last_notified_ = last_;
179 
180  auto garbage = false;
181  for (std::size_t i = 0, size = children_.size(); i < size; ++i)
182  {
183  if (auto child = children_[i].lock())
184  {
185  child->notify();
186  }
187  else
188  {
189  garbage = true;
190  }
191  }
192 
193  if (garbage)
194  {
195  collect();
196  }
197  }
198  }
199 
200  template <typename Fn>
201  auto observe(Fn&& f)
202  -> boost::signals2::connection
203  {
204  return observers_.connect(std::forward<Fn>(f));
205  }
206 
207  auto observers()
208  -> boost::signals2::signal<void(const value_type&,
209  const value_type&)>&
210  {
211  return observers_;
212  }
213 
214 private:
215  void collect()
216  {
217  using namespace std;
218  children_.erase(
219  remove_if(
220  begin(children_),
221  end(children_),
222  mem_fn(&weak_ptr<down_signal_base>::expired)),
223  end(children_));
224  }
225 
226  bool needs_send_down_ = false;
227  bool needs_notify_ = false;
228  value_type current_;
229  value_type last_;
230  value_type last_notified_;
231  std::vector<std::weak_ptr<down_signal_base> > children_;
232  boost::signals2::signal<void(const value_type&,
233  const value_type&)> observers_;
234 };
235 
239 template <typename T>
240 class up_down_signal
241  : public down_signal<T>
242  , public up_signal_base<T>
243 {
244  using down_signal<T>::down_signal;
245 };
246 
247 } // namespace detail
248 
249 } // namespace funken
250 } // namespace atria
STL namespace.
C++ amazing templates and reusable implementations awesomeness.
Definition: _doc.hpp:35
Fork me on GitHub