]> git.sesse.net Git - casparcg/blob - core/producer/binding.h
Merge branch '2.1.0' of https://github.com/CasparCG/Server into 2.1.0
[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                 template<typename Expr>
84                 impl(const Expr& expression)
85                         : expression_(expression)
86                 {
87                 }
88
89                 T get() const
90                 {
91                         return value_;
92                 }
93
94                 bool bound() const
95                 {
96                         return static_cast<bool>(expression_);
97                 }
98
99                 void set(T value)
100                 {
101                         if (bound())
102                         {
103                                 throw std::exception("Bound value cannot be set");
104                         }
105
106                         if (value == value_)
107                                 return;
108
109                         value_ = value;
110
111                         on_change();
112                 }
113
114                 void evaluate() override
115                 {
116                         if (expression_)
117                         {
118                                 auto new_value = expression_();
119
120                                 if (new_value != value_)
121                                 {
122                                         value_ = new_value;
123                                         on_change();
124                                 }
125                         }
126                 }
127
128                 using impl_base::on_change;
129                 void on_change()
130                 {
131                         auto copy = on_change_;
132
133                         for (int i = static_cast<int>(copy.size()) - 1; i >= 0; --i)
134                         {
135                                 auto strong = copy[i].first.lock();
136
137                                 if (strong)
138                                         copy[i].second();
139                                 else
140                                         on_change_.erase(on_change_.begin() + i);
141                         }
142                 }
143
144                 void bind(const std::shared_ptr<impl>& other)
145                 {
146                         unbind();
147                         depend_on(other);
148                         expression_ = [other]{ return other->get(); };
149                         evaluate();
150                 }
151
152                 void unbind()
153                 {
154                         if (expression_)
155                         {
156                                 expression_ = std::function<T ()>();
157                                 dependencies_.clear();
158                         }
159                 }
160         };
161
162         template<typename> friend class binding;
163
164         std::shared_ptr<impl> impl_;
165 public:
166         binding()
167                 : impl_(new impl)
168         {
169         }
170
171         explicit binding(T value)
172                 : impl_(new impl(value))
173         {
174         }
175
176         // Expr -> T ()
177         template<typename Expr>
178         explicit binding(const Expr& expression)
179                 : impl_(new impl(expression))
180         {
181                 impl_->evaluate();
182         }
183
184         // Expr -> T ()
185         template<typename Expr, typename T2>
186         binding(const Expr& expression, const binding<T2>& dep)
187                 : impl_(new impl(expression))
188         {
189                 depend_on(dep);
190                 impl_->evaluate();
191         }
192
193         // Expr -> T ()
194         template<typename Expr, typename T2, typename T3>
195         binding(
196                         const Expr& expression,
197                         const binding<T2>& dep1,
198                         const binding<T3>& dep2)
199                 : impl_(new impl(expression))
200         {
201                 depend_on(dep1);
202                 depend_on(dep2);
203                 impl_->evaluate();
204         }
205
206         void* identity() const
207         {
208                 return impl_.get();
209         }
210
211         T get() const
212         {
213                 return impl_->get();
214         }
215
216         void set(T value)
217         {
218                 impl_->set(value);
219         }
220
221         void bind(const binding<T>& other)
222         {
223                 impl_->bind(other.impl_);
224         }
225
226         bool bound() const
227         {
228                 return impl_->bound();
229         }
230
231         template<typename T2>
232         void depend_on(const binding<T2>& other)
233         {
234                 impl_->depend_on(other.impl_);
235         }
236
237         binding<T> operator+(T other) const
238         {
239                 return transformed([other](T self) { return self + other; });
240         }
241
242         binding<T> operator+(const binding<T>& other) const
243         {
244                 return composed(other, [](T self, T o) { return self + o; });
245         }
246
247         binding<T>& operator++()
248         {
249                 T new_value = get();
250                 ++new_value;
251
252                 set(new_value);
253
254                 return *this;
255         }
256
257         binding<T> operator++(int)
258         {
259                 binding<T> pre_val(get());
260                 ++*this;
261
262                 return pre_val;
263         }
264
265         binding<T> operator-() const
266         {
267                 return transformed([](T self) { return -self; });
268         }
269
270         binding<T> operator-(const binding<T>& other) const
271         {
272                 return *this + -other;
273         }
274
275         binding<T> operator-(T other) const
276         {
277                 return *this + -other;
278         }
279
280         binding<T>& operator--()
281         {
282                 T new_value = get();
283                 --new_value;
284
285                 set(new_value);
286
287                 return *this;
288         }
289
290         binding<T> operator--(int)
291         {
292                 binding<T> pre_val(get());
293                 --*this;
294
295                 return pre_val;
296         }
297
298         binding<T> operator*(T other) const
299         {
300                 return transformed([other](T self) { return self * other; });
301         }
302
303         binding<T> operator*(const binding<T>& other) const
304         {
305                 return composed(other, [](T self, T o) { return self * o; });
306         }
307
308         binding<T> operator/(T other) const
309         {
310                 return transformed([other](T self) { return self / other; });
311         }
312
313         binding<T> operator/(const binding<T>& other) const
314         {
315                 return composed(other, [](T self, T o) { return self / o; });
316         }
317
318         binding<T> operator%(T other) const
319         {
320                 return transformed([other](T self) { return self % other; });
321         }
322
323         binding<T> operator%(const binding<T>& other) const
324         {
325                 return composed(other, [](T self, T o) { return self % o; });
326         }
327
328         binding<bool> operator==(T other) const
329         {
330                 return transformed([other](T self) { return self == other; });
331         }
332
333         binding<bool> operator==(const binding<T>& other) const
334         {
335                 return composed(other, [](T self, T o) { return self == o; });
336         }
337
338         binding<bool> operator!=(T other) const
339         {
340                 return transformed([other](T self) { return self != other; });
341         }
342
343         binding<bool> operator!=(const binding<T>& other) const
344         {
345                 return composed(other, [](T self, T o) { return self != o; });
346         }
347
348         binding<bool> operator<(T other) const
349         {
350                 return transformed([other](T self) { return self < other; });
351         }
352
353         binding<bool> operator<(const binding<T>& other) const
354         {
355                 return composed(other, [](T self, T o) { return self < o; });
356         }
357
358         binding<bool> operator>(T other) const
359         {
360                 return transformed([other](T self) { return self > other; });
361         }
362
363         binding<bool> operator>(const binding<T>& other) const
364         {
365                 return composed(other, [](T self, T o) { return self > o; });
366         }
367
368         binding<bool> operator<=(T other) const
369         {
370                 return transformed([other](T self) { return self <= other; });
371         }
372
373         binding<bool> operator<=(const binding<T>& other) const
374         {
375                 return composed(other, [](T self, T o) { return self <= o; });
376         }
377
378         binding<bool> operator>=(T other) const
379         {
380                 return transformed([other](T self) { return self >= other; });
381         }
382
383         binding<bool> operator>=(const binding<T>& other) const
384         {
385                 return composed(other, [](T self, T o) { return self >= o; });
386         }
387
388         template<typename T2>
389         binding<T2> as() const
390         {
391                 return transformed([](T val) { return static_cast<T2>(val); });
392         }
393
394         // Func -> R (T self_val)
395         // Returns binding<R>
396         template<typename Func>
397         auto transformed(const Func& func) const -> binding<decltype(func(impl_->value_))>
398         {
399                 typedef decltype(func(impl_->value_)) R;
400                 auto self = impl_;
401
402                 return binding<R>(
403                                 [self, func] { return func(self->get()); },
404                                 *this);
405         }
406
407         // Func -> R (T self_val, T2 other_val)
408         // Returns binding<R>
409         template<typename Func, typename T2>
410         auto composed(const binding<T2> other, const Func& func) const -> binding<decltype(func(impl_->value_, other.impl_->value_))>
411         {
412                 typedef decltype(func(impl_->value_, other.impl_->value_)) R;
413                 auto self = impl_;
414                 auto o = other.impl_;
415
416                 return binding<R>(
417                                 [self, o, func] { return func(self->get(), o->get()); },
418                                 *this,
419                                 other);
420         }
421
422         void unbind()
423         {
424                 impl_->unbind();
425         }
426
427         void on_change(
428                         const std::weak_ptr<void> dependant,
429                         const std::function<void ()>& listener) const
430         {
431                 impl_->on_change(dependant, listener);
432         }
433
434         std::shared_ptr<void> on_change(
435                         const std::function<void ()>& listener) const
436         {
437                 std::shared_ptr<void> subscription(new char);
438
439                 on_change(subscription, listener);
440                 
441                 return subscription;
442         }
443 private:
444         binding(const std::shared_ptr<impl>& self)
445                 : impl_(self)
446         {
447         }
448 };
449
450 static binding<bool> operator||(const binding<bool>& lhs, const binding<bool>& rhs)
451 {
452         return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs || rhs; });
453 }
454
455 static binding<bool> operator&&(const binding<bool>& lhs, const binding<bool>& rhs)
456 {
457         return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs && rhs; });
458 }
459
460 static binding<bool> operator!(const binding<bool>& self)
461 {
462         return self.transformed([](bool self) { return !self; });
463 }
464
465 template<typename T>
466 class ternary_builder
467 {
468         binding<bool> condition_;
469         binding<T> true_result_;
470 public:
471         ternary_builder(
472                         const binding<bool>& condition, const binding<T>& true_result)
473                 : condition_(condition)
474                 , true_result_(true_result)
475         {
476         }
477
478         binding<T> otherwise(const binding<T>& false_result)
479         {
480                 auto condition = condition_;
481                 auto true_result = true_result_;
482
483                 binding<T> result([condition, true_result, false_result]()
484                 {
485                         return condition.get() ? true_result.get() : false_result.get();                
486                 });
487
488                 result.depend_on(condition);
489                 result.depend_on(true_result);
490                 result.depend_on(false_result);
491
492                 return result;
493         }
494
495         binding<T> otherwise(T false_result)
496         {
497                 return otherwise(binding<T>(false_result));
498         }
499 };
500
501 class when
502 {
503         binding<bool> condition_;
504 public:
505         when(const binding<bool>& condition)
506                 : condition_(condition)
507         {
508         }
509
510         template<typename T>
511         ternary_builder<T> then(const binding<T>& true_result)
512         {
513                 return ternary_builder<T>(condition_, true_result);
514         }
515
516         template<typename T>
517         ternary_builder<T> then(T true_result)
518         {
519                 return then(binding<T>(true_result));
520         }
521 };
522
523 template<typename T, typename T2>
524 binding<T> add_tween(
525                 const binding<T>& to_tween,
526                 const binding<T2>& counter,
527                 T destination_value,
528                 T2 duration,
529                 const std::wstring& easing)
530 {
531         tweener tween(easing);
532         
533         double start_val = to_tween.as<double>().get();
534         double destination_val = static_cast<double>(destination_value);
535         double start_time = counter.as<double>().get();
536         double dur = static_cast<double>(duration);
537
538         return when(counter < duration)
539                 .then(counter.as<double>().transformed([=](double t)
540                 {
541                         return tween(t - start_time, start_val, destination_val, dur);
542                 }).as<T>())
543                 .otherwise(destination_value);
544 }
545
546 template<typename T, typename T2>
547 binding<T> delay(
548                 const binding<T>& to_delay,
549                 const binding<T>& after_delay,
550                 const binding<T2>& counter,
551                 T2 duration)
552 {
553         return when(counter < duration)
554                         .then(to_delay)
555                         .otherwise(after_delay);
556 }
557
558 }}