]> git.sesse.net Git - casparcg/blob - core/producer/binding.h
* Merged chroma key feature, but removed unsupported parameters and color names....
[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 <string>
28 #include <map>
29 #include <algorithm>
30 #include <type_traits>
31 #include <stdexcept>
32
33 #include <boost/lexical_cast.hpp>
34
35 #include <common/tweener.h>
36
37 namespace caspar { namespace core {
38
39 namespace detail {
40
41 struct impl_base : std::enable_shared_from_this<impl_base>
42 {
43         std::vector<std::shared_ptr<impl_base>> dependencies_;
44         mutable std::vector<std::pair<
45                         std::weak_ptr<void>,
46                         std::function<void ()>>> on_change_;
47
48         virtual ~impl_base()
49         {
50         }
51
52         virtual void evaluate() const = 0;
53
54         void depend_on(const std::shared_ptr<impl_base>& dependency)
55         {
56                 auto self = shared_from_this();
57
58                 if (dependency->depends_on(self))
59                         throw std::runtime_error("Can't have circular dependencies between bindings");
60
61                 dependency->on_change(self, [=] { evaluate(); });
62                 dependencies_.push_back(dependency);
63         }
64
65         bool depends_on(const std::shared_ptr<impl_base>& other) const
66         {
67                 for (auto& dependency : dependencies_)
68                 {
69                         if (dependency == other)
70                                 return true;
71                         
72                         if (dependency->depends_on(other))
73                                 return true;            
74                 }
75
76                 return false;
77         }
78
79         void on_change(
80                         const std::weak_ptr<void>& dependant,
81                         const std::function<void ()>& listener) const
82         {
83                 on_change_.push_back(std::make_pair(dependant, listener));
84         }
85 };
86
87 }
88
89 template <typename T>
90 class binding
91 {
92 private:
93
94         struct impl : public detail::impl_base
95         {
96                 mutable T                       value_;
97                 std::function<T ()> expression_;
98                 mutable bool            evaluated_              = false;
99
100                 impl()
101                         : value_()
102                 {
103                 }
104
105                 impl(T value)
106                         : value_(value)
107                 {
108                 }
109
110                 template<typename Expr>
111                 impl(const Expr& expression)
112                         : expression_(expression)
113                 {
114                 }
115
116                 T get() const
117                 {
118                         if (!evaluated_)
119                                 evaluate();
120
121                         return value_;
122                 }
123
124                 bool bound() const
125                 {
126                         return static_cast<bool>(expression_);
127                 }
128
129                 void set(T value)
130                 {
131                         if (bound())
132                         {
133                                 throw std::runtime_error("Bound value cannot be set");
134                         }
135
136                         if (value == value_)
137                                 return;
138
139                         value_ = value;
140
141                         on_change();
142                 }
143
144                 void evaluate() const override
145                 {
146                         if (expression_)
147                         {
148                                 auto new_value = expression_();
149
150                                 if (new_value != value_)
151                                 {
152                                         value_ = new_value;
153                                         on_change();
154                                 }
155                         }
156
157                         evaluated_ = true;
158                 }
159
160                 using impl_base::on_change;
161                 void on_change() const
162                 {
163                         auto copy = on_change_;
164
165                         for (int i = static_cast<int>(copy.size()) - 1; i >= 0; --i)
166                         {
167                                 auto strong = copy[i].first.lock();
168
169                                 if (strong)
170                                         copy[i].second();
171                                 else
172                                         on_change_.erase(on_change_.begin() + i);
173                         }
174                 }
175
176                 void bind(const std::shared_ptr<impl>& other)
177                 {
178                         unbind();
179                         depend_on(other);
180                         expression_ = [other]{ return other->get(); };
181                         //evaluate();
182                 }
183
184                 void unbind()
185                 {
186                         if (expression_)
187                         {
188                                 expression_ = std::function<T ()>();
189                                 dependencies_.clear();
190                         }
191                 }
192         };
193
194         template<typename> friend class binding;
195
196         std::shared_ptr<impl> impl_;
197 public:
198         binding()
199                 : impl_(new impl)
200         {
201         }
202
203         explicit binding(T value)
204                 : impl_(new impl(value))
205         {
206         }
207
208         // Expr -> T ()
209         template<typename Expr>
210         explicit binding(const Expr& expression)
211                 : impl_(new impl(expression))
212         {
213                 //impl_->evaluate();
214         }
215
216         // Expr -> T ()
217         template<typename Expr, typename T2>
218         binding(const Expr& expression, const binding<T2>& dep)
219                 : impl_(new impl(expression))
220         {
221                 depend_on(dep);
222                 //impl_->evaluate();
223         }
224
225         // Expr -> T ()
226         template<typename Expr, typename T2, typename T3>
227         binding(
228                         const Expr& expression,
229                         const binding<T2>& dep1,
230                         const binding<T3>& dep2)
231                 : impl_(new impl(expression))
232         {
233                 depend_on(dep1);
234                 depend_on(dep2);
235                 //impl_->evaluate();
236         }
237
238         void* identity() const
239         {
240                 return impl_.get();
241         }
242
243         T get() const
244         {
245                 return impl_->get();
246         }
247
248         void set(T value)
249         {
250                 impl_->set(value);
251         }
252
253         void bind(const binding<T>& other)
254         {
255                 impl_->bind(other.impl_);
256         }
257
258         bool bound() const
259         {
260                 return impl_->bound();
261         }
262
263         template<typename T2>
264         void depend_on(const binding<T2>& other)
265         {
266                 impl_->depend_on(other.impl_);
267         }
268
269         binding<T> operator+(T other) const
270         {
271                 return transformed([other](T self) { return self + other; });
272         }
273
274         binding<T> operator+(const binding<T>& other) const
275         {
276                 return composed(other, [](T self, T o) { return self + o; });
277         }
278
279         binding<T>& operator++()
280         {
281                 T new_value = get();
282                 ++new_value;
283
284                 set(new_value);
285
286                 return *this;
287         }
288
289         binding<T> operator++(int)
290         {
291                 binding<T> pre_val(get());
292                 ++*this;
293
294                 return pre_val;
295         }
296
297         binding<T> operator-() const
298         {
299                 return transformed([](T self) { return -self; });
300         }
301
302         binding<T> operator-(const binding<T>& other) const
303         {
304                 return *this + -other;
305         }
306
307         binding<T> operator-(T other) const
308         {
309                 return *this + -other;
310         }
311
312         binding<T>& operator--()
313         {
314                 T new_value = get();
315                 --new_value;
316
317                 set(new_value);
318
319                 return *this;
320         }
321
322         binding<T> operator--(int)
323         {
324                 binding<T> pre_val(get());
325                 --*this;
326
327                 return pre_val;
328         }
329
330         binding<T> operator*(T other) const
331         {
332                 return transformed([other](T self) { return self * other; });
333         }
334
335         binding<T> operator*(const binding<T>& other) const
336         {
337                 return composed(other, [](T self, T o) { return self * o; });
338         }
339
340         binding<T> operator/(T other) const
341         {
342                 return transformed([other](T self) { return self / other; });
343         }
344
345         binding<T> operator/(const binding<T>& other) const
346         {
347                 return composed(other, [](T self, T o) { return self / o; });
348         }
349
350         binding<T> operator%(T other) const
351         {
352                 return transformed([other](T self) { return self % other; });
353         }
354
355         binding<T> operator%(const binding<T>& other) const
356         {
357                 return composed(other, [](T self, T o) { return self % o; });
358         }
359
360         binding<bool> operator==(T other) const
361         {
362                 return transformed([other](T self) { return self == other; });
363         }
364
365         binding<bool> operator==(const binding<T>& other) const
366         {
367                 return composed(other, [](T self, T o) { return self == o; });
368         }
369
370         binding<bool> operator!=(T other) const
371         {
372                 return transformed([other](T self) { return self != other; });
373         }
374
375         binding<bool> operator!=(const binding<T>& other) const
376         {
377                 return composed(other, [](T self, T o) { return self != o; });
378         }
379
380         binding<bool> operator<(T other) const
381         {
382                 return transformed([other](T self) { return self < other; });
383         }
384
385         binding<bool> operator<(const binding<T>& other) const
386         {
387                 return composed(other, [](T self, T o) { return self < o; });
388         }
389
390         binding<bool> operator>(T other) const
391         {
392                 return transformed([other](T self) { return self > other; });
393         }
394
395         binding<bool> operator>(const binding<T>& other) const
396         {
397                 return composed(other, [](T self, T o) { return self > o; });
398         }
399
400         binding<bool> operator<=(T other) const
401         {
402                 return transformed([other](T self) { return self <= other; });
403         }
404
405         binding<bool> operator<=(const binding<T>& other) const
406         {
407                 return composed(other, [](T self, T o) { return self <= o; });
408         }
409
410         binding<bool> operator>=(T other) const
411         {
412                 return transformed([other](T self) { return self >= other; });
413         }
414
415         binding<bool> operator>=(const binding<T>& other) const
416         {
417                 return composed(other, [](T self, T o) { return self >= o; });
418         }
419
420         template<typename T2>
421         typename std::enable_if<
422                         std::is_same<T, T2>::value,
423                         binding<T2>
424                 >::type as() const
425         {
426                 return *this;
427         }
428
429         template<typename T2>
430         typename std::enable_if<
431                         (std::is_arithmetic<T>::value || std::is_same<bool, T>::value) && (std::is_arithmetic<T2>::value || std::is_same<bool, T2>::value) && !std::is_same<T, T2>::value,
432                         binding<T2>
433                 >::type as() const
434         {
435                 return transformed([](T val) { return static_cast<T2>(val); });
436         }
437
438         template<typename T2>
439         typename std::enable_if<
440                         (std::is_same<std::wstring, T>::value || std::is_same<std::wstring, T2>::value) && !std::is_same<T, T2>::value,
441                         binding<T2>
442                 >::type as() const
443         {
444                 return transformed([](T val) { return boost::lexical_cast<T2>(val); });
445         }
446
447         // Func -> R (T self_val)
448         // Returns binding<R>
449         template<typename Func>
450         auto transformed(const Func& func) const -> binding<decltype(func(impl_->value_))>
451         {
452                 typedef decltype(func(impl_->value_)) R;
453                 auto self = impl_;
454
455                 return binding<R>(
456                                 [self, func] { return func(self->get()); },
457                                 *this);
458         }
459
460         // Func -> R (T self_val, T2 other_val)
461         // Returns binding<R>
462         template<typename Func, typename T2>
463         auto composed(const binding<T2> other, const Func& func) const -> binding<decltype(func(impl_->value_, other.impl_->value_))>
464         {
465                 typedef decltype(func(impl_->value_, other.impl_->value_)) R;
466                 auto self = impl_;
467                 auto o = other.impl_;
468
469                 return binding<R>(
470                                 [self, o, func] { return func(self->get(), o->get()); },
471                                 *this,
472                                 other);
473         }
474
475         void unbind()
476         {
477                 impl_->unbind();
478         }
479
480         void on_change(
481                         const std::weak_ptr<void>& dependant,
482                         const std::function<void ()>& listener) const
483         {
484                 impl_->on_change(dependant, listener);
485         }
486
487         std::shared_ptr<void> on_change(
488                         const std::function<void ()>& listener) const
489         {
490                 std::shared_ptr<void> subscription(new char);
491
492                 on_change(subscription, listener);
493                 
494                 return subscription;
495         }
496 private:
497         binding(const std::shared_ptr<impl>& self)
498                 : impl_(self)
499         {
500         }
501 };
502
503 static binding<bool> operator||(const binding<bool>& lhs, const binding<bool>& rhs)
504 {
505         return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs || rhs; });
506 }
507
508 static binding<bool> operator||(const binding<bool>& lhs, bool rhs)
509 {
510         return lhs.transformed([rhs](bool lhs) { return lhs || rhs; });
511 }
512
513 static binding<bool> operator&&(const binding<bool>& lhs, const binding<bool>& rhs)
514 {
515         return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs && rhs; });
516 }
517
518 static binding<bool> operator&&(const binding<bool>& lhs, bool rhs)
519 {
520         return lhs.transformed([rhs](bool lhs) { return lhs && rhs; });
521 }
522
523 static binding<bool> operator!(const binding<bool>& self)
524 {
525         return self.transformed([](bool self) { return !self; });
526 }
527
528 template<typename T>
529 class ternary_builder
530 {
531         binding<bool> condition_;
532         binding<T> true_result_;
533 public:
534         ternary_builder(
535                         const binding<bool>& condition, const binding<T>& true_result)
536                 : condition_(condition)
537                 , true_result_(true_result)
538         {
539         }
540
541         binding<T> otherwise(const binding<T>& false_result)
542         {
543                 auto condition = condition_;
544                 auto true_result = true_result_;
545
546                 binding<T> result([condition, true_result, false_result]()
547                 {
548                         return condition.get() ? true_result.get() : false_result.get();                
549                 });
550
551                 result.depend_on(condition);
552                 result.depend_on(true_result);
553                 result.depend_on(false_result);
554
555                 return result;
556         }
557
558         binding<T> otherwise(T false_result)
559         {
560                 return otherwise(binding<T>(false_result));
561         }
562 };
563
564 class when
565 {
566         binding<bool> condition_;
567 public:
568         when(const binding<bool>& condition)
569                 : condition_(condition)
570         {
571         }
572
573         template<typename T>
574         ternary_builder<T> then(const binding<T>& true_result)
575         {
576                 return ternary_builder<T>(condition_, true_result);
577         }
578
579         template<typename T>
580         ternary_builder<T> then(T true_result)
581         {
582                 return then(binding<T>(true_result));
583         }
584 };
585
586 /*template<typename T, typename T2>
587 binding<T> add_tween(
588                 const binding<T>& to_tween,
589                 const binding<T2>& counter,
590                 T destination_value,
591                 T2 duration,
592                 const std::wstring& easing)
593 {
594         tweener tween(easing);
595         
596         double start_val = to_tween.as<double>().get();
597         double destination_val = static_cast<double>(destination_value);
598         double start_time = counter.as<double>().get();
599         double dur = static_cast<double>(duration);
600
601         return when(counter < duration)
602                 .then(counter.as<double>().transformed([=](double t)
603                 {
604                         return tween(t - start_time, start_val, destination_val, dur);
605                 }).as<T>())
606                 .otherwise(destination_value);
607 }*/
608
609 template<typename T, typename T2>
610 binding<T> delay(
611                 const binding<T>& to_delay,
612                 const binding<T>& after_delay,
613                 const binding<T2>& counter,
614                 T2 duration)
615 {
616         return when(counter < duration)
617                         .then(to_delay)
618                         .otherwise(after_delay);
619 }
620
621 }}