]> git.sesse.net Git - casparcg/blob - core/producer/binding.h
More binding functionality
[casparcg] / core / producer / binding.h
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #pragma once
23
24 #include <functional>
25 #include <memory>
26 #include <vector>
27 #include <map>
28 #include <algorithm>
29
30 namespace caspar { namespace core {
31
32 namespace detail {
33
34 struct impl_base : std::enable_shared_from_this<impl_base>
35 {
36         std::vector<std::shared_ptr<impl_base>> dependencies_;
37         mutable std::vector<std::pair<
38                         std::weak_ptr<void>,
39                         std::function<void ()>>> on_change_;
40
41         virtual ~impl_base()
42         {
43         }
44
45         virtual void evaluate() = 0;
46
47         void depend_on(const std::shared_ptr<impl_base>& dependency)
48         {
49                 dependency->on_change(shared_from_this(), [=] { evaluate(); });
50                 dependencies_.push_back(dependency);
51         }
52
53         void on_change(
54                         const std::weak_ptr<void>& dependant,
55                         const std::function<void ()>& listener) const
56         {
57                 on_change_.push_back(std::make_pair(dependant, listener));
58         }
59 };
60
61 }
62
63 template <typename T>
64 class binding
65 {
66 private:
67
68         struct impl : public detail::impl_base
69         {
70                 T value_;
71                 std::function<T ()> expression_;
72
73                 impl()
74                         : value_()
75                 {
76                 }
77
78                 impl(T value)
79                         : value_(value)
80                 {
81                 }
82
83                 impl(const std::function<T ()>& expression)
84                         : expression_(expression)
85                 {
86                 }
87
88                 T get() const
89                 {
90                         return value_;
91                 }
92
93                 bool bound() const
94                 {
95                         return static_cast<bool>(expression_);
96                 }
97
98                 void set(T value)
99                 {
100                         if (bound())
101                         {
102                                 throw std::exception("Bound value cannot be set");
103                         }
104
105                         if (value == value_)
106                                 return;
107
108                         value_ = value;
109
110                         on_change();
111                 }
112
113                 void evaluate() override
114                 {
115                         if (expression_)
116                         {
117                                 auto new_value = expression_();
118
119                                 if (new_value != value_)
120                                 {
121                                         value_ = new_value;
122                                         on_change();
123                                 }
124                         }
125                 }
126
127                 using impl_base::on_change;
128                 void on_change()
129                 {
130                         auto copy = on_change_;
131
132                         for (int i = static_cast<int>(copy.size()) - 1; i >= 0; --i)
133                         {
134                                 auto strong = copy[i].first.lock();
135
136                                 if (strong)
137                                         copy[i].second();
138                                 else
139                                         on_change_.erase(on_change_.begin() + i);
140                         }
141                 }
142
143                 void bind(const std::shared_ptr<impl>& other)
144                 {
145                         unbind();
146                         depend_on(other);
147                         expression_ = [&]{ return other.get(); }
148                         evaluate();
149                 }
150
151                 void unbind()
152                 {
153                         if (expression_)
154                         {
155                                 expression_ = std::function<T ()>();
156                                 dependencies_.clear();
157                         }
158                 }
159         };
160
161         template<typename> friend class binding;
162
163         std::shared_ptr<impl> impl_;
164 public:
165         binding()
166                 : impl_(new impl)
167         {
168         }
169
170         explicit binding(T value)
171                 : impl_(new impl(value))
172         {
173         }
174
175         template<typename T2>
176         binding(const std::function<T ()>& expression, const binding<T2>& dep)
177                 : impl_(new impl(expression))
178         {
179                 depend_on(dep);
180                 impl_->evaluate();
181         }
182
183         template<typename T2, typename T3>
184         binding(
185                         const std::function<T ()>& expression,
186                         const binding<T2>& dep1,
187                         const binding<T3>& dep2)
188                 : impl_(new impl(expression))
189         {
190                 depend_on(dep1);
191                 depend_on(dep2);
192                 impl_->evaluate();
193         }
194
195         T get() const
196         {
197                 return impl_->get();
198         }
199
200         void set(T value)
201         {
202                 impl_->set(value);
203         }
204
205         void bind(const binding<T>& other)
206         {
207                 impl_->bind(other.impl_);
208         }
209
210         bool bound() const
211         {
212                 return impl_->bound();
213         }
214
215         template<typename T2>
216         void depend_on(const binding<T2>& other)
217         {
218                 impl_->depend_on(other.impl_);
219         }
220
221         binding<T> operator+(T other) const
222         {
223                 return transformed([other](T self) { return self + other; });
224         }
225
226         binding<T> operator+(const binding<T>& other) const
227         {
228                 auto self = impl_;
229                 auto o = other.impl_;
230
231                 return binding<T>(
232                                 [self, o] { return o->get() + self->get(); },
233                                 *this,
234                                 other);
235         }
236
237         binding<T> operator-() const
238         {
239                 return transformed([](T self) { return -self; });
240         }
241
242         binding<T> operator-(const binding<T>& other) const
243         {
244                 return *this + -other;
245         }
246
247         binding<T> operator-(T other) const
248         {
249                 return *this + -other;
250         }
251
252         binding<T> operator*(T other) const
253         {
254                 return transformed([other](T self) { return self * other; });
255         }
256
257         binding<T> operator*(const binding<T>& other) const
258         {
259                 auto self = impl_;
260                 auto o = other.impl_;
261
262                 return binding<T>(
263                                 [self, o] { return o->get() * self->get(); },
264                                 *this,
265                                 other);
266         }
267
268         binding<T> operator/(T other) const
269         {
270                 return transformed([other](T self) { return self / other; });
271         }
272
273         binding<T> operator/(const binding<T>& other) const
274         {
275                 auto self = impl_;
276                 auto o = other.impl_;
277
278                 return binding<T>(
279                                 [self, o] { return self->get() / o->get(); },
280                                 *this,
281                                 other);
282         }
283
284         binding<bool> operator==(T other) const
285         {
286                 return transformed([other](T self) { return self == other; });
287         }
288
289         binding<bool> operator==(const binding<T>& other) const
290         {
291                 auto self = impl_;
292                 auto o = other.impl_;
293
294                 return binding<bool>(
295                                 [self, o] { return self->get() == o->get(); },
296                                 *this,
297                                 other);
298         }
299
300         binding<bool> operator!=(T other) const
301         {
302                 return transformed([other](T self) { return self != other; });
303         }
304
305         binding<bool> operator!=(const binding<T>& other) const
306         {
307                 auto self = impl_;
308                 auto o = other.impl_;
309
310                 return binding<bool>(
311                                 [self, o] { return self->get() != o->get(); },
312                                 *this,
313                                 other);
314         }
315
316         template<typename T2>
317         binding<T2> as() const
318         {
319                 return transformed([](T val) { return static_cast<T2>(val); });
320         }
321
322         template<typename Func>
323         auto transformed(const Func& func) const -> binding<decltype(func(impl_->value_))>
324         {
325                 typedef decltype(func(impl_->value_)) R;
326                 auto self = impl_;
327
328                 return binding<R>(
329                                 [self, func] { return func(self->get()); },
330                                 *this);
331         }
332
333         void unbind()
334         {
335                 impl_->unbind();
336         }
337
338         void on_change(
339                         const std::weak_ptr<void> dependant,
340                         const std::function<void ()>& listener) const
341         {
342                 impl_->on_change(dependant, listener);
343         }
344
345         std::shared_ptr<void> on_change(
346                         const std::function<void ()>& listener) const
347         {
348                 std::shared_ptr<void> subscription(new char);
349
350                 on_change(subscription, listener);
351                 
352                 return subscription;
353         }
354 private:
355         binding(const std::shared_ptr<impl>& self)
356                 : impl_(self)
357         {
358         }
359 };
360
361 }}