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>
32 #include <boost/lexical_cast.hpp>
34 namespace caspar { namespace core {
38 struct impl_base : std::enable_shared_from_this<impl_base>
40 std::vector<std::shared_ptr<impl_base>> dependencies_;
41 mutable std::vector<std::pair<
43 std::function<void ()>>> on_change_;
49 virtual void evaluate() = 0;
51 void depend_on(const std::shared_ptr<impl_base>& dependency)
53 auto self = shared_from_this();
55 if (dependency->depends_on(self))
56 throw std::exception("Can't have circular dependencies between bindings");
58 dependency->on_change(self, [=] { evaluate(); });
59 dependencies_.push_back(dependency);
62 bool depends_on(const std::shared_ptr<impl_base>& other) const
64 for (auto& dependency : dependencies_)
66 if (dependency == other)
69 if (dependency->depends_on(other))
77 const std::weak_ptr<void>& dependant,
78 const std::function<void ()>& listener) const
80 on_change_.push_back(std::make_pair(dependant, listener));
91 struct impl : public detail::impl_base
94 std::function<T ()> expression_;
106 template<typename Expr>
107 impl(const Expr& expression)
108 : expression_(expression)
119 return static_cast<bool>(expression_);
126 throw std::exception("Bound value cannot be set");
137 void evaluate() override
141 auto new_value = expression_();
143 if (new_value != value_)
151 using impl_base::on_change;
154 auto copy = on_change_;
156 for (int i = static_cast<int>(copy.size()) - 1; i >= 0; --i)
158 auto strong = copy[i].first.lock();
163 on_change_.erase(on_change_.begin() + i);
167 void bind(const std::shared_ptr<impl>& other)
171 expression_ = [other]{ return other->get(); };
179 expression_ = std::function<T ()>();
180 dependencies_.clear();
185 template<typename> friend class binding;
187 std::shared_ptr<impl> impl_;
194 explicit binding(T value)
195 : impl_(new impl(value))
200 template<typename Expr>
201 explicit binding(const Expr& expression)
202 : impl_(new impl(expression))
208 template<typename Expr, typename T2>
209 binding(const Expr& expression, const binding<T2>& dep)
210 : impl_(new impl(expression))
217 template<typename Expr, typename T2, typename T3>
219 const Expr& expression,
220 const binding<T2>& dep1,
221 const binding<T3>& dep2)
222 : impl_(new impl(expression))
229 void* identity() const
244 void bind(const binding<T>& other)
246 impl_->bind(other.impl_);
251 return impl_->bound();
254 template<typename T2>
255 void depend_on(const binding<T2>& other)
257 impl_->depend_on(other.impl_);
260 binding<T> operator+(T other) const
262 return transformed([other](T self) { return self + other; });
265 binding<T> operator+(const binding<T>& other) const
267 return composed(other, [](T self, T o) { return self + o; });
270 binding<T>& operator++()
280 binding<T> operator++(int)
282 binding<T> pre_val(get());
288 binding<T> operator-() const
290 return transformed([](T self) { return -self; });
293 binding<T> operator-(const binding<T>& other) const
295 return *this + -other;
298 binding<T> operator-(T other) const
300 return *this + -other;
303 binding<T>& operator--()
313 binding<T> operator--(int)
315 binding<T> pre_val(get());
321 binding<T> operator*(T other) const
323 return transformed([other](T self) { return self * other; });
326 binding<T> operator*(const binding<T>& other) const
328 return composed(other, [](T self, T o) { return self * o; });
331 binding<T> operator/(T other) const
333 return transformed([other](T self) { return self / other; });
336 binding<T> operator/(const binding<T>& other) const
338 return composed(other, [](T self, T o) { return self / o; });
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<bool> operator==(T other) const
353 return transformed([other](T self) { return self == other; });
356 binding<bool> operator==(const binding<T>& other) const
358 return composed(other, [](T self, T o) { return self == o; });
361 binding<bool> operator!=(T other) const
363 return transformed([other](T self) { return self != other; });
366 binding<bool> 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 template<typename T2>
412 typename std::enable_if<
413 std::is_same<T, T2>::value,
420 template<typename T2>
421 typename std::enable_if<
422 (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,
426 return transformed([](T val) { return static_cast<T2>(val); });
429 template<typename T2>
430 typename std::enable_if<
431 (std::is_same<std::wstring, T>::value || std::is_same<std::wstring, T2>::value) && !std::is_same<T, T2>::value,
435 return transformed([](T val) { return boost::lexical_cast<T2>(val); });
438 // Func -> R (T self_val)
439 // Returns binding<R>
440 template<typename Func>
441 auto transformed(const Func& func) const -> binding<decltype(func(impl_->value_))>
443 typedef decltype(func(impl_->value_)) R;
447 [self, func] { return func(self->get()); },
451 // Func -> R (T self_val, T2 other_val)
452 // Returns binding<R>
453 template<typename Func, typename T2>
454 auto composed(const binding<T2> other, const Func& func) const -> binding<decltype(func(impl_->value_, other.impl_->value_))>
456 typedef decltype(func(impl_->value_, other.impl_->value_)) R;
458 auto o = other.impl_;
461 [self, o, func] { return func(self->get(), o->get()); },
472 const std::weak_ptr<void>& dependant,
473 const std::function<void ()>& listener) const
475 impl_->on_change(dependant, listener);
478 std::shared_ptr<void> on_change(
479 const std::function<void ()>& listener) const
481 std::shared_ptr<void> subscription(new char);
483 on_change(subscription, listener);
488 binding(const std::shared_ptr<impl>& self)
494 static binding<bool> operator||(const binding<bool>& lhs, const binding<bool>& rhs)
496 return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs || rhs; });
499 static binding<bool> operator||(const binding<bool>& lhs, bool rhs)
501 return lhs.transformed([rhs](bool lhs) { return lhs || rhs; });
504 static binding<bool> operator&&(const binding<bool>& lhs, const binding<bool>& rhs)
506 return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs && rhs; });
509 static binding<bool> operator&&(const binding<bool>& lhs, bool rhs)
511 return lhs.transformed([rhs](bool lhs) { return lhs && rhs; });
514 static binding<bool> operator!(const binding<bool>& self)
516 return self.transformed([](bool self) { return !self; });
520 class ternary_builder
522 binding<bool> condition_;
523 binding<T> true_result_;
526 const binding<bool>& condition, const binding<T>& true_result)
527 : condition_(condition)
528 , true_result_(true_result)
532 binding<T> otherwise(const binding<T>& false_result)
534 auto condition = condition_;
535 auto true_result = true_result_;
537 binding<T> result([condition, true_result, false_result]()
539 return condition.get() ? true_result.get() : false_result.get();
542 result.depend_on(condition);
543 result.depend_on(true_result);
544 result.depend_on(false_result);
549 binding<T> otherwise(T false_result)
551 return otherwise(binding<T>(false_result));
557 binding<bool> condition_;
559 when(const binding<bool>& condition)
560 : condition_(condition)
565 ternary_builder<T> then(const binding<T>& true_result)
567 return ternary_builder<T>(condition_, true_result);
571 ternary_builder<T> then(T true_result)
573 return then(binding<T>(true_result));
577 template<typename T, typename T2>
578 binding<T> add_tween(
579 const binding<T>& to_tween,
580 const binding<T2>& counter,
583 const std::wstring& easing)
585 tweener tween(easing);
587 double start_val = to_tween.as<double>().get();
588 double destination_val = static_cast<double>(destination_value);
589 double start_time = counter.as<double>().get();
590 double dur = static_cast<double>(duration);
592 return when(counter < duration)
593 .then(counter.as<double>().transformed([=](double t)
595 return tween(t - start_time, start_val, destination_val, dur);
597 .otherwise(destination_value);
600 template<typename T, typename T2>
602 const binding<T>& to_delay,
603 const binding<T>& after_delay,
604 const binding<T2>& counter,
607 return when(counter < duration)
609 .otherwise(after_delay);