2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
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.
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.
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/>.
19 * Author: Helge Norberg, helge.norberg@svt.se
30 #include <type_traits>
33 #include <boost/lexical_cast.hpp>
34 #include <boost/utility/value_init.hpp>
35 #include <boost/range/algorithm/remove_if.hpp>
37 #include <common/tweener.h>
38 #include <common/except.h>
40 namespace caspar { namespace core {
44 struct impl_base : std::enable_shared_from_this<impl_base>
46 std::vector<std::shared_ptr<impl_base>> dependencies_;
47 mutable std::vector<std::pair<
49 std::function<void ()>>> on_change_;
55 virtual void evaluate() const = 0;
57 void depend_on(const std::shared_ptr<impl_base>& dependency)
59 auto self = shared_from_this();
61 if (dependency->depends_on(self))
62 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("Can't have circular dependencies between bindings"));
64 dependency->on_change(self, [=] { evaluate(); });
65 dependencies_.push_back(dependency);
68 bool depends_on(const std::shared_ptr<impl_base>& other) const
70 for (auto& dependency : dependencies_)
72 if (dependency == other)
75 if (dependency->depends_on(other))
83 const std::weak_ptr<void>& dependant,
84 const std::function<void ()>& listener) const
86 on_change_.push_back(std::make_pair(dependant, listener));
97 struct impl : public detail::impl_base
100 std::function<T ()> expression_;
101 mutable bool evaluated_ = false;
104 : value_(boost::value_initialized<T>())
113 template<typename Expr>
114 impl(const Expr& expression)
115 : value_(boost::value_initialized<T>())
116 , expression_(expression)
130 return static_cast<bool>(expression_);
136 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Bound value cannot be set"));
146 void evaluate() const override
150 auto new_value = expression_();
154 if (new_value != value_)
164 using impl_base::on_change;
165 void on_change() const
167 auto copy = on_change_;
168 bool need_to_clean_up = false;
170 for (auto& listener : copy)
172 auto strong = listener.first.lock();
177 need_to_clean_up = true;
180 if (need_to_clean_up)
181 boost::remove_if(on_change_, [&](const std::pair<std::weak_ptr<void>, std::function<void()>>& l)
183 return l.first.expired();
187 void bind(const std::shared_ptr<impl>& other)
191 expression_ = [other]{ return other->get(); };
199 expression_ = std::function<T ()>();
200 dependencies_.clear();
205 template<typename> friend class binding;
207 std::shared_ptr<impl> impl_;
214 explicit binding(T value)
215 : impl_(new impl(value))
220 template<typename Expr>
221 explicit binding(const Expr& expression)
222 : impl_(new impl(expression))
228 template<typename Expr, typename T2>
229 binding(const Expr& expression, const binding<T2>& dep)
230 : impl_(new impl(expression))
237 template<typename Expr, typename T2, typename T3>
239 const Expr& expression,
240 const binding<T2>& dep1,
241 const binding<T3>& dep2)
242 : impl_(new impl(expression))
249 void* identity() const
264 void bind(const binding<T>& other)
266 impl_->bind(other.impl_);
271 return impl_->bound();
274 template<typename T2>
275 void depend_on(const binding<T2>& other)
277 impl_->depend_on(other.impl_);
280 binding<T> operator+(T other) const
282 return transformed([other](T self) { return self + other; });
285 binding<T> operator+(const binding<T>& other) const
287 return composed(other, [](T self, T o) { return self + o; });
290 binding<T>& operator++()
300 binding<T> operator++(int)
302 binding<T> pre_val(get());
308 binding<T> operator-() const
310 return transformed([](T self) { return -self; });
313 binding<T> operator-(const binding<T>& other) const
315 return *this + -other;
318 binding<T> operator-(T other) const
320 return *this + -other;
323 binding<T>& operator--()
333 binding<T> operator--(int)
335 binding<T> pre_val(get());
341 binding<T> operator*(T other) const
343 return transformed([other](T self) { return self * other; });
346 binding<T> operator*(const binding<T>& other) const
348 return composed(other, [](T self, T o) { return self * o; });
351 binding<T> operator/(T other) const
353 return transformed([other](T self) { return self / other; });
356 binding<T> operator/(const binding<T>& other) const
358 return composed(other, [](T self, T o) { return self / o; });
361 binding<T> operator%(T other) const
363 return transformed([other](T self) { return self % other; });
366 binding<T> operator%(const binding<T>& other) const
368 return composed(other, [](T self, T o) { return self % o; });
371 binding<bool> operator==(T other) const
373 return transformed([other](T self) { return self == other; });
376 binding<bool> operator==(const binding<T>& other) const
378 return composed(other, [](T self, T o) { return self == o; });
381 binding<bool> operator!=(T other) const
383 return transformed([other](T self) { return self != other; });
386 binding<bool> operator!=(const binding<T>& other) const
388 return composed(other, [](T self, T o) { return self != o; });
391 binding<bool> operator<(T other) const
393 return transformed([other](T self) { return self < other; });
396 binding<bool> operator<(const binding<T>& other) const
398 return composed(other, [](T self, T o) { return self < o; });
401 binding<bool> operator>(T other) const
403 return transformed([other](T self) { return self > other; });
406 binding<bool> operator>(const binding<T>& other) const
408 return composed(other, [](T self, T o) { return self > o; });
411 binding<bool> operator<=(T other) const
413 return transformed([other](T self) { return self <= other; });
416 binding<bool> operator<=(const binding<T>& other) const
418 return composed(other, [](T self, T o) { return self <= o; });
421 binding<bool> operator>=(T other) const
423 return transformed([other](T self) { return self >= other; });
426 binding<bool> operator>=(const binding<T>& other) const
428 return composed(other, [](T self, T o) { return self >= o; });
431 template<typename T2>
432 typename std::enable_if<
433 std::is_same<T, T2>::value,
440 template<typename T2>
441 typename std::enable_if<
442 (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,
446 return transformed([](T val) { return static_cast<T2>(val); });
449 template<typename T2>
450 typename std::enable_if<
451 (std::is_same<std::wstring, T>::value || std::is_same<std::wstring, T2>::value) && !std::is_same<T, T2>::value,
455 return transformed([](T val) { return boost::lexical_cast<T2>(val); });
458 // Func -> R (T self_val)
459 // Returns binding<R>
460 template<typename Func>
461 auto transformed(const Func& func) const -> binding<decltype(func(impl_->value_))>
463 typedef decltype(func(impl_->value_)) R;
467 [self, func] { return func(self->get()); },
471 // Func -> R (T self_val, T2 other_val)
472 // Returns binding<R>
473 template<typename Func, typename T2>
474 auto composed(const binding<T2>& other, const Func& func) const -> binding<decltype(func(impl_->value_, other.impl_->value_))>
476 typedef decltype(func(impl_->value_, other.impl_->value_)) R;
478 auto o = other.impl_;
481 [self, o, func] { return func(self->get(), o->get()); },
486 template<typename TweenerFunc, typename FrameCounter>
488 const binding<FrameCounter>& frame_counter,
489 const binding<FrameCounter>& duration,
490 const binding<TweenerFunc>& tweener_func) const
493 auto f = frame_counter.impl_;
494 auto d = duration.impl_;
495 auto tw = tweener_func.impl_;
496 FrameCounter start_frame = frame_counter.get();
497 FrameCounter current_frame = start_frame;
498 T current_source = get();
499 T current_destination = current_source;
500 T current_result = current_source;
502 auto result = binding<T>(
505 auto frame_diff = f->get() - current_frame;
506 bool new_frame = f->get() != current_frame;
509 return current_result;
511 bool new_tween = current_destination != self->get();
512 auto time = current_frame - start_frame + (new_tween ? frame_diff : 0) + 1;
514 current_frame = f->get();
516 current_source = new_tween ? tw->get()(time, current_source, current_destination - current_source, dur) : current_source;
517 current_destination = self->get();
520 start_frame = current_frame;
522 time = current_frame - start_frame;
525 current_result = tw->get()(time, current_source, current_destination - current_source, dur);
528 current_result = current_destination;
529 current_source = current_destination;
532 return current_result;
535 result.depend_on(*this);
536 result.depend_on(frame_counter);
537 result.depend_on(tweener_func);
548 const std::weak_ptr<void>& dependant,
549 const std::function<void ()>& listener) const
551 impl_->on_change(dependant, listener);
554 std::shared_ptr<void> on_change(
555 const std::function<void ()>& listener) const
557 std::shared_ptr<void> subscription(new char);
559 on_change(subscription, listener);
564 binding(const std::shared_ptr<impl>& self)
570 static binding<bool> operator||(const binding<bool>& lhs, const binding<bool>& rhs)
572 return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs || rhs; });
575 static binding<bool> operator||(const binding<bool>& lhs, bool rhs)
577 return lhs.transformed([rhs](bool lhs) { return lhs || rhs; });
580 static binding<bool> operator&&(const binding<bool>& lhs, const binding<bool>& rhs)
582 return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs && rhs; });
585 static binding<bool> operator&&(const binding<bool>& lhs, bool rhs)
587 return lhs.transformed([rhs](bool lhs) { return lhs && rhs; });
590 static binding<bool> operator!(const binding<bool>& self)
592 return self.transformed([](bool self) { return !self; });
596 class ternary_builder
598 binding<bool> condition_;
599 binding<T> true_result_;
602 const binding<bool>& condition, const binding<T>& true_result)
603 : condition_(condition)
604 , true_result_(true_result)
608 binding<T> otherwise(const binding<T>& false_result)
610 auto condition = condition_;
611 auto true_result = true_result_;
613 binding<T> result([condition, true_result, false_result]()
615 return condition.get() ? true_result.get() : false_result.get();
618 result.depend_on(condition);
619 result.depend_on(true_result);
620 result.depend_on(false_result);
625 binding<T> otherwise(T false_result)
627 return otherwise(binding<T>(false_result));
633 binding<bool> condition_;
635 when(const binding<bool>& condition)
636 : condition_(condition)
641 ternary_builder<T> then(const binding<T>& true_result)
643 return ternary_builder<T>(condition_, true_result);
647 ternary_builder<T> then(T true_result)
649 return then(binding<T>(true_result));
653 /*template<typename T, typename T2>
654 binding<T> add_tween(
655 const binding<T>& to_tween,
656 const binding<T2>& counter,
659 const std::wstring& easing)
661 tweener tween(easing);
663 double start_val = to_tween.as<double>().get();
664 double destination_val = static_cast<double>(destination_value);
665 double start_time = counter.as<double>().get();
666 double dur = static_cast<double>(duration);
668 return when(counter < duration)
669 .then(counter.as<double>().transformed([=](double t)
671 return tween(t - start_time, start_val, destination_val, dur);
673 .otherwise(destination_value);
676 template<typename T, typename T2>
678 const binding<T>& to_delay,
679 const binding<T>& after_delay,
680 const binding<T2>& counter,
683 return when(counter < duration)
685 .otherwise(after_delay);