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>
36 #include <common/tweener.h>
37 #include <common/except.h>
39 namespace caspar { namespace core {
43 struct impl_base : std::enable_shared_from_this<impl_base>
45 std::vector<std::shared_ptr<impl_base>> dependencies_;
46 mutable std::vector<std::pair<
48 std::function<void ()>>> on_change_;
54 virtual void evaluate() const = 0;
56 void depend_on(const std::shared_ptr<impl_base>& dependency)
58 auto self = shared_from_this();
60 if (dependency->depends_on(self))
61 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("Can't have circular dependencies between bindings"));
63 dependency->on_change(self, [=] { evaluate(); });
64 dependencies_.push_back(dependency);
67 bool depends_on(const std::shared_ptr<impl_base>& other) const
69 for (auto& dependency : dependencies_)
71 if (dependency == other)
74 if (dependency->depends_on(other))
82 const std::weak_ptr<void>& dependant,
83 const std::function<void ()>& listener) const
85 on_change_.push_back(std::make_pair(dependant, listener));
96 struct impl : public detail::impl_base
99 std::function<T ()> expression_;
100 mutable bool evaluated_ = false;
103 : value_(boost::value_initialized<T>())
112 template<typename Expr>
113 impl(const Expr& expression)
114 : value_(boost::value_initialized<T>())
115 , expression_(expression)
129 return static_cast<bool>(expression_);
135 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Bound value cannot be set"));
145 void evaluate() const override
149 auto new_value = expression_();
153 if (new_value != value_)
163 using impl_base::on_change;
164 void on_change() const
166 auto copy = on_change_;
168 for (int i = static_cast<int>(copy.size()) - 1; i >= 0; --i)
170 auto strong = copy[i].first.lock();
175 on_change_.erase(on_change_.begin() + i);
179 void bind(const std::shared_ptr<impl>& other)
183 expression_ = [other]{ return other->get(); };
191 expression_ = std::function<T ()>();
192 dependencies_.clear();
197 template<typename> friend class binding;
199 std::shared_ptr<impl> impl_;
206 explicit binding(T value)
207 : impl_(new impl(value))
212 template<typename Expr>
213 explicit binding(const Expr& expression)
214 : impl_(new impl(expression))
220 template<typename Expr, typename T2>
221 binding(const Expr& expression, const binding<T2>& dep)
222 : impl_(new impl(expression))
229 template<typename Expr, typename T2, typename T3>
231 const Expr& expression,
232 const binding<T2>& dep1,
233 const binding<T3>& dep2)
234 : impl_(new impl(expression))
241 void* identity() const
256 void bind(const binding<T>& other)
258 impl_->bind(other.impl_);
263 return impl_->bound();
266 template<typename T2>
267 void depend_on(const binding<T2>& other)
269 impl_->depend_on(other.impl_);
272 binding<T> operator+(T other) const
274 return transformed([other](T self) { return self + other; });
277 binding<T> operator+(const binding<T>& other) const
279 return composed(other, [](T self, T o) { return self + o; });
282 binding<T>& operator++()
292 binding<T> operator++(int)
294 binding<T> pre_val(get());
300 binding<T> operator-() const
302 return transformed([](T self) { return -self; });
305 binding<T> operator-(const binding<T>& other) const
307 return *this + -other;
310 binding<T> operator-(T other) const
312 return *this + -other;
315 binding<T>& operator--()
325 binding<T> operator--(int)
327 binding<T> pre_val(get());
333 binding<T> operator*(T other) const
335 return transformed([other](T self) { return self * other; });
338 binding<T> operator*(const binding<T>& other) const
340 return composed(other, [](T self, T o) { return self * o; });
343 binding<T> operator/(T other) const
345 return transformed([other](T self) { return self / other; });
348 binding<T> operator/(const binding<T>& other) const
350 return composed(other, [](T self, T o) { return self / o; });
353 binding<T> operator%(T other) const
355 return transformed([other](T self) { return self % other; });
358 binding<T> operator%(const binding<T>& other) const
360 return composed(other, [](T self, T o) { return self % o; });
363 binding<bool> operator==(T other) const
365 return transformed([other](T self) { return self == other; });
368 binding<bool> operator==(const binding<T>& other) const
370 return composed(other, [](T self, T o) { return self == o; });
373 binding<bool> operator!=(T other) const
375 return transformed([other](T self) { return self != other; });
378 binding<bool> operator!=(const binding<T>& other) const
380 return composed(other, [](T self, T o) { return self != o; });
383 binding<bool> operator<(T other) const
385 return transformed([other](T self) { return self < other; });
388 binding<bool> operator<(const binding<T>& other) const
390 return composed(other, [](T self, T o) { return self < o; });
393 binding<bool> operator>(T other) const
395 return transformed([other](T self) { return self > other; });
398 binding<bool> operator>(const binding<T>& other) const
400 return composed(other, [](T self, T o) { return self > o; });
403 binding<bool> operator<=(T other) const
405 return transformed([other](T self) { return self <= other; });
408 binding<bool> operator<=(const binding<T>& other) const
410 return composed(other, [](T self, T o) { return self <= o; });
413 binding<bool> operator>=(T other) const
415 return transformed([other](T self) { return self >= other; });
418 binding<bool> operator>=(const binding<T>& other) const
420 return composed(other, [](T self, T o) { return self >= o; });
423 template<typename T2>
424 typename std::enable_if<
425 std::is_same<T, T2>::value,
432 template<typename T2>
433 typename std::enable_if<
434 (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,
438 return transformed([](T val) { return static_cast<T2>(val); });
441 template<typename T2>
442 typename std::enable_if<
443 (std::is_same<std::wstring, T>::value || std::is_same<std::wstring, T2>::value) && !std::is_same<T, T2>::value,
447 return transformed([](T val) { return boost::lexical_cast<T2>(val); });
450 // Func -> R (T self_val)
451 // Returns binding<R>
452 template<typename Func>
453 auto transformed(const Func& func) const -> binding<decltype(func(impl_->value_))>
455 typedef decltype(func(impl_->value_)) R;
459 [self, func] { return func(self->get()); },
463 // Func -> R (T self_val, T2 other_val)
464 // Returns binding<R>
465 template<typename Func, typename T2>
466 auto composed(const binding<T2>& other, const Func& func) const -> binding<decltype(func(impl_->value_, other.impl_->value_))>
468 typedef decltype(func(impl_->value_, other.impl_->value_)) R;
470 auto o = other.impl_;
473 [self, o, func] { return func(self->get(), o->get()); },
478 template<typename TweenerFunc, typename FrameCounter>
480 const binding<FrameCounter>& frame_counter,
481 const binding<FrameCounter>& duration,
482 const binding<TweenerFunc>& tweener_func) const
485 auto f = frame_counter.impl_;
486 auto d = duration.impl_;
487 auto tw = tweener_func.impl_;
488 FrameCounter start_frame = frame_counter.get();
489 FrameCounter current_frame = start_frame;
490 T current_source = get();
491 T current_destination = current_source;
492 T current_result = current_source;
494 auto result = binding<T>(
497 auto frame_diff = f->get() - current_frame;
498 bool new_frame = f->get() != current_frame;
501 return current_result;
503 bool new_tween = current_destination != self->get();
504 auto time = current_frame - start_frame + (new_tween ? frame_diff : 0) + 1;
506 current_frame = f->get();
508 current_source = new_tween ? tw->get()(time, current_source, current_destination - current_source, dur) : current_source;
509 current_destination = self->get();
512 start_frame = current_frame;
514 time = current_frame - start_frame;
517 current_result = tw->get()(time, current_source, current_destination - current_source, dur);
520 current_result = current_destination;
521 current_source = current_destination;
524 return current_result;
527 result.depend_on(*this);
528 result.depend_on(frame_counter);
529 result.depend_on(tweener_func);
540 const std::weak_ptr<void>& dependant,
541 const std::function<void ()>& listener) const
543 impl_->on_change(dependant, listener);
546 std::shared_ptr<void> on_change(
547 const std::function<void ()>& listener) const
549 std::shared_ptr<void> subscription(new char);
551 on_change(subscription, listener);
556 binding(const std::shared_ptr<impl>& self)
562 static binding<bool> operator||(const binding<bool>& lhs, const binding<bool>& rhs)
564 return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs || rhs; });
567 static binding<bool> operator||(const binding<bool>& lhs, bool rhs)
569 return lhs.transformed([rhs](bool lhs) { return lhs || rhs; });
572 static binding<bool> operator&&(const binding<bool>& lhs, const binding<bool>& rhs)
574 return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs && rhs; });
577 static binding<bool> operator&&(const binding<bool>& lhs, bool rhs)
579 return lhs.transformed([rhs](bool lhs) { return lhs && rhs; });
582 static binding<bool> operator!(const binding<bool>& self)
584 return self.transformed([](bool self) { return !self; });
588 class ternary_builder
590 binding<bool> condition_;
591 binding<T> true_result_;
594 const binding<bool>& condition, const binding<T>& true_result)
595 : condition_(condition)
596 , true_result_(true_result)
600 binding<T> otherwise(const binding<T>& false_result)
602 auto condition = condition_;
603 auto true_result = true_result_;
605 binding<T> result([condition, true_result, false_result]()
607 return condition.get() ? true_result.get() : false_result.get();
610 result.depend_on(condition);
611 result.depend_on(true_result);
612 result.depend_on(false_result);
617 binding<T> otherwise(T false_result)
619 return otherwise(binding<T>(false_result));
625 binding<bool> condition_;
627 when(const binding<bool>& condition)
628 : condition_(condition)
633 ternary_builder<T> then(const binding<T>& true_result)
635 return ternary_builder<T>(condition_, true_result);
639 ternary_builder<T> then(T true_result)
641 return then(binding<T>(true_result));
645 /*template<typename T, typename T2>
646 binding<T> add_tween(
647 const binding<T>& to_tween,
648 const binding<T2>& counter,
651 const std::wstring& easing)
653 tweener tween(easing);
655 double start_val = to_tween.as<double>().get();
656 double destination_val = static_cast<double>(destination_value);
657 double start_time = counter.as<double>().get();
658 double dur = static_cast<double>(duration);
660 return when(counter < duration)
661 .then(counter.as<double>().transformed([=](double t)
663 return tween(t - start_time, start_val, destination_val, dur);
665 .otherwise(destination_value);
668 template<typename T, typename T2>
670 const binding<T>& to_delay,
671 const binding<T>& after_delay,
672 const binding<T2>& counter,
675 return when(counter < duration)
677 .otherwise(after_delay);