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>
35 #include <common/tweener.h>
37 namespace caspar { namespace core {
41 struct impl_base : std::enable_shared_from_this<impl_base>
43 std::vector<std::shared_ptr<impl_base>> dependencies_;
44 mutable std::vector<std::pair<
46 std::function<void ()>>> on_change_;
52 virtual void evaluate() const = 0;
54 void depend_on(const std::shared_ptr<impl_base>& dependency)
56 auto self = shared_from_this();
58 if (dependency->depends_on(self))
59 throw std::runtime_error("Can't have circular dependencies between bindings");
61 dependency->on_change(self, [=] { evaluate(); });
62 dependencies_.push_back(dependency);
65 bool depends_on(const std::shared_ptr<impl_base>& other) const
67 for (auto& dependency : dependencies_)
69 if (dependency == other)
72 if (dependency->depends_on(other))
80 const std::weak_ptr<void>& dependant,
81 const std::function<void ()>& listener) const
83 on_change_.push_back(std::make_pair(dependant, listener));
94 struct impl : public detail::impl_base
97 std::function<T ()> expression_;
98 mutable bool evaluated_ = false;
110 template<typename Expr>
111 impl(const Expr& expression)
112 : expression_(expression)
126 return static_cast<bool>(expression_);
133 throw std::runtime_error("Bound value cannot be set");
144 void evaluate() const override
148 auto new_value = expression_();
150 if (new_value != value_)
160 using impl_base::on_change;
161 void on_change() const
163 auto copy = on_change_;
165 for (int i = static_cast<int>(copy.size()) - 1; i >= 0; --i)
167 auto strong = copy[i].first.lock();
172 on_change_.erase(on_change_.begin() + i);
176 void bind(const std::shared_ptr<impl>& other)
180 expression_ = [other]{ return other->get(); };
188 expression_ = std::function<T ()>();
189 dependencies_.clear();
194 template<typename> friend class binding;
196 std::shared_ptr<impl> impl_;
203 explicit binding(T value)
204 : impl_(new impl(value))
209 template<typename Expr>
210 explicit binding(const Expr& expression)
211 : impl_(new impl(expression))
217 template<typename Expr, typename T2>
218 binding(const Expr& expression, const binding<T2>& dep)
219 : impl_(new impl(expression))
226 template<typename Expr, typename T2, typename T3>
228 const Expr& expression,
229 const binding<T2>& dep1,
230 const binding<T3>& dep2)
231 : impl_(new impl(expression))
238 void* identity() const
253 void bind(const binding<T>& other)
255 impl_->bind(other.impl_);
260 return impl_->bound();
263 template<typename T2>
264 void depend_on(const binding<T2>& other)
266 impl_->depend_on(other.impl_);
269 binding<T> operator+(T other) const
271 return transformed([other](T self) { return self + other; });
274 binding<T> operator+(const binding<T>& other) const
276 return composed(other, [](T self, T o) { return self + o; });
279 binding<T>& operator++()
289 binding<T> operator++(int)
291 binding<T> pre_val(get());
297 binding<T> operator-() const
299 return transformed([](T self) { return -self; });
302 binding<T> operator-(const binding<T>& other) const
304 return *this + -other;
307 binding<T> operator-(T other) const
309 return *this + -other;
312 binding<T>& operator--()
322 binding<T> operator--(int)
324 binding<T> pre_val(get());
330 binding<T> operator*(T other) const
332 return transformed([other](T self) { return self * other; });
335 binding<T> operator*(const binding<T>& other) const
337 return composed(other, [](T self, T o) { return self * o; });
340 binding<T> operator/(T other) const
342 return transformed([other](T self) { return self / other; });
345 binding<T> operator/(const binding<T>& other) const
347 return composed(other, [](T self, T o) { return self / o; });
350 binding<T> operator%(T other) const
352 return transformed([other](T self) { return self % other; });
355 binding<T> operator%(const binding<T>& other) const
357 return composed(other, [](T self, T o) { return self % o; });
360 binding<bool> operator==(T other) const
362 return transformed([other](T self) { return self == other; });
365 binding<bool> operator==(const binding<T>& other) const
367 return composed(other, [](T self, T o) { return self == o; });
370 binding<bool> operator!=(T other) const
372 return transformed([other](T self) { return self != other; });
375 binding<bool> operator!=(const binding<T>& other) const
377 return composed(other, [](T self, T o) { return self != o; });
380 binding<bool> operator<(T other) const
382 return transformed([other](T self) { return self < other; });
385 binding<bool> operator<(const binding<T>& other) const
387 return composed(other, [](T self, T o) { return self < o; });
390 binding<bool> operator>(T other) const
392 return transformed([other](T self) { return self > other; });
395 binding<bool> operator>(const binding<T>& other) const
397 return composed(other, [](T self, T o) { return self > o; });
400 binding<bool> operator<=(T other) const
402 return transformed([other](T self) { return self <= other; });
405 binding<bool> operator<=(const binding<T>& other) const
407 return composed(other, [](T self, T o) { return self <= o; });
410 binding<bool> operator>=(T other) const
412 return transformed([other](T self) { return self >= other; });
415 binding<bool> operator>=(const binding<T>& other) const
417 return composed(other, [](T self, T o) { return self >= o; });
420 template<typename T2>
421 typename std::enable_if<
422 std::is_same<T, T2>::value,
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,
435 return transformed([](T val) { return static_cast<T2>(val); });
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,
444 return transformed([](T val) { return boost::lexical_cast<T2>(val); });
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_))>
452 typedef decltype(func(impl_->value_)) R;
456 [self, func] { return func(self->get()); },
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_))>
465 typedef decltype(func(impl_->value_, other.impl_->value_)) R;
467 auto o = other.impl_;
470 [self, o, func] { return func(self->get(), o->get()); },
481 const std::weak_ptr<void>& dependant,
482 const std::function<void ()>& listener) const
484 impl_->on_change(dependant, listener);
487 std::shared_ptr<void> on_change(
488 const std::function<void ()>& listener) const
490 std::shared_ptr<void> subscription(new char);
492 on_change(subscription, listener);
497 binding(const std::shared_ptr<impl>& self)
503 static binding<bool> operator||(const binding<bool>& lhs, const binding<bool>& rhs)
505 return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs || rhs; });
508 static binding<bool> operator||(const binding<bool>& lhs, bool rhs)
510 return lhs.transformed([rhs](bool lhs) { return lhs || rhs; });
513 static binding<bool> operator&&(const binding<bool>& lhs, const binding<bool>& rhs)
515 return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs && rhs; });
518 static binding<bool> operator&&(const binding<bool>& lhs, bool rhs)
520 return lhs.transformed([rhs](bool lhs) { return lhs && rhs; });
523 static binding<bool> operator!(const binding<bool>& self)
525 return self.transformed([](bool self) { return !self; });
529 class ternary_builder
531 binding<bool> condition_;
532 binding<T> true_result_;
535 const binding<bool>& condition, const binding<T>& true_result)
536 : condition_(condition)
537 , true_result_(true_result)
541 binding<T> otherwise(const binding<T>& false_result)
543 auto condition = condition_;
544 auto true_result = true_result_;
546 binding<T> result([condition, true_result, false_result]()
548 return condition.get() ? true_result.get() : false_result.get();
551 result.depend_on(condition);
552 result.depend_on(true_result);
553 result.depend_on(false_result);
558 binding<T> otherwise(T false_result)
560 return otherwise(binding<T>(false_result));
566 binding<bool> condition_;
568 when(const binding<bool>& condition)
569 : condition_(condition)
574 ternary_builder<T> then(const binding<T>& true_result)
576 return ternary_builder<T>(condition_, true_result);
580 ternary_builder<T> then(T true_result)
582 return then(binding<T>(true_result));
586 /*template<typename T, typename T2>
587 binding<T> add_tween(
588 const binding<T>& to_tween,
589 const binding<T2>& counter,
592 const std::wstring& easing)
594 tweener tween(easing);
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);
601 return when(counter < duration)
602 .then(counter.as<double>().transformed([=](double t)
604 return tween(t - start_time, start_val, destination_val, dur);
606 .otherwise(destination_value);
609 template<typename T, typename T2>
611 const binding<T>& to_delay,
612 const binding<T>& after_delay,
613 const binding<T2>& counter,
616 return when(counter < duration)
618 .otherwise(after_delay);