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>
33 #include <boost/foreach.hpp>
35 namespace caspar { namespace core {
39 struct impl_base : std::enable_shared_from_this<impl_base>
41 std::vector<std::shared_ptr<impl_base>> dependencies_;
42 mutable std::vector<std::pair<
44 std::function<void ()>>> on_change_;
50 virtual void evaluate() = 0;
52 void depend_on(const std::shared_ptr<impl_base>& dependency)
54 auto self = shared_from_this();
56 if (dependency->depends_on(self))
57 throw std::exception("Can't have circular dependencies between bindings");
59 dependency->on_change(self, [=] { evaluate(); });
60 dependencies_.push_back(dependency);
63 bool depends_on(const std::shared_ptr<impl_base>& other) const
65 BOOST_FOREACH(auto& dependency, dependencies_)
67 if (dependency == other)
70 if (dependency->depends_on(other))
78 const std::weak_ptr<void>& dependant,
79 const std::function<void ()>& listener) const
81 on_change_.push_back(std::make_pair(dependant, listener));
92 struct impl : public detail::impl_base
95 std::function<T ()> expression_;
107 template<typename Expr>
108 impl(const Expr& expression)
109 : expression_(expression)
120 return static_cast<bool>(expression_);
127 throw std::exception("Bound value cannot be set");
138 void evaluate() override
142 auto new_value = expression_();
144 if (new_value != value_)
152 using impl_base::on_change;
155 auto copy = on_change_;
157 for (int i = static_cast<int>(copy.size()) - 1; i >= 0; --i)
159 auto strong = copy[i].first.lock();
164 on_change_.erase(on_change_.begin() + i);
168 void bind(const std::shared_ptr<impl>& other)
172 expression_ = [other]{ return other->get(); };
180 expression_ = std::function<T ()>();
181 dependencies_.clear();
186 template<typename> friend class binding;
188 std::shared_ptr<impl> impl_;
195 explicit binding(T value)
196 : impl_(new impl(value))
201 template<typename Expr>
202 explicit binding(const Expr& expression)
203 : impl_(new impl(expression))
209 template<typename Expr, typename T2>
210 binding(const Expr& expression, const binding<T2>& dep)
211 : impl_(new impl(expression))
218 template<typename Expr, typename T2, typename T3>
220 const Expr& expression,
221 const binding<T2>& dep1,
222 const binding<T3>& dep2)
223 : impl_(new impl(expression))
230 void* identity() const
245 void bind(const binding<T>& other)
247 impl_->bind(other.impl_);
252 return impl_->bound();
255 template<typename T2>
256 void depend_on(const binding<T2>& other)
258 impl_->depend_on(other.impl_);
261 binding<T> operator+(T other) const
263 return transformed([other](T self) { return self + other; });
266 binding<T> operator+(const binding<T>& other) const
268 return composed(other, [](T self, T o) { return self + o; });
271 binding<T>& operator++()
281 binding<T> operator++(int)
283 binding<T> pre_val(get());
289 binding<T> operator-() const
291 return transformed([](T self) { return -self; });
294 binding<T> operator-(const binding<T>& other) const
296 return *this + -other;
299 binding<T> operator-(T other) const
301 return *this + -other;
304 binding<T>& operator--()
314 binding<T> operator--(int)
316 binding<T> pre_val(get());
322 binding<T> operator*(T other) const
324 return transformed([other](T self) { return self * other; });
327 binding<T> operator*(const binding<T>& other) const
329 return composed(other, [](T self, T o) { return self * o; });
332 binding<T> operator/(T other) const
334 return transformed([other](T self) { return self / other; });
337 binding<T> operator/(const binding<T>& other) const
339 return composed(other, [](T self, T o) { return self / o; });
342 binding<T> operator%(T other) const
344 return transformed([other](T self) { return self % other; });
347 binding<T> operator%(const binding<T>& other) const
349 return composed(other, [](T self, T o) { return self % o; });
352 binding<bool> operator==(T other) const
354 return transformed([other](T self) { return self == other; });
357 binding<bool> operator==(const binding<T>& other) const
359 return composed(other, [](T self, T o) { return self == o; });
362 binding<bool> operator!=(T other) const
364 return transformed([other](T self) { return self != other; });
367 binding<bool> operator!=(const binding<T>& other) const
369 return composed(other, [](T self, T o) { return self != o; });
372 binding<bool> operator<(T other) const
374 return transformed([other](T self) { return self < other; });
377 binding<bool> operator<(const binding<T>& other) const
379 return composed(other, [](T self, T o) { return self < o; });
382 binding<bool> operator>(T other) const
384 return transformed([other](T self) { return self > other; });
387 binding<bool> operator>(const binding<T>& other) const
389 return composed(other, [](T self, T o) { return self > o; });
392 binding<bool> operator<=(T other) const
394 return transformed([other](T self) { return self <= other; });
397 binding<bool> operator<=(const binding<T>& other) const
399 return composed(other, [](T self, T o) { return self <= o; });
402 binding<bool> operator>=(T other) const
404 return transformed([other](T self) { return self >= other; });
407 binding<bool> operator>=(const binding<T>& other) const
409 return composed(other, [](T self, T o) { return self >= o; });
412 template<typename T2>
413 typename std::enable_if<
414 std::is_same<T, T2>::value,
421 template<typename T2>
422 typename std::enable_if<
423 (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,
427 return transformed([](T val) { return static_cast<T2>(val); });
430 template<typename T2>
431 typename std::enable_if<
432 (std::is_same<std::wstring, T>::value || std::is_same<std::wstring, T2>::value) && !std::is_same<T, T2>::value,
436 return transformed([](T val) { return boost::lexical_cast<T2>(val); });
439 // Func -> R (T self_val)
440 // Returns binding<R>
441 template<typename Func>
442 auto transformed(const Func& func) const -> binding<decltype(func(impl_->value_))>
444 typedef decltype(func(impl_->value_)) R;
448 [self, func] { return func(self->get()); },
452 // Func -> R (T self_val, T2 other_val)
453 // Returns binding<R>
454 template<typename Func, typename T2>
455 auto composed(const binding<T2> other, const Func& func) const -> binding<decltype(func(impl_->value_, other.impl_->value_))>
457 typedef decltype(func(impl_->value_, other.impl_->value_)) R;
459 auto o = other.impl_;
462 [self, o, func] { return func(self->get(), o->get()); },
473 const std::weak_ptr<void>& dependant,
474 const std::function<void ()>& listener) const
476 impl_->on_change(dependant, listener);
479 std::shared_ptr<void> on_change(
480 const std::function<void ()>& listener) const
482 std::shared_ptr<void> subscription(new char);
484 on_change(subscription, listener);
489 binding(const std::shared_ptr<impl>& self)
495 static binding<bool> operator||(const binding<bool>& lhs, const binding<bool>& rhs)
497 return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs || rhs; });
500 static binding<bool> operator||(const binding<bool>& lhs, bool rhs)
502 return lhs.transformed([rhs](bool lhs) { return lhs || rhs; });
505 static binding<bool> operator&&(const binding<bool>& lhs, const binding<bool>& rhs)
507 return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs && rhs; });
510 static binding<bool> operator&&(const binding<bool>& lhs, bool rhs)
512 return lhs.transformed([rhs](bool lhs) { return lhs && rhs; });
515 static binding<bool> operator!(const binding<bool>& self)
517 return self.transformed([](bool self) { return !self; });
521 class ternary_builder
523 binding<bool> condition_;
524 binding<T> true_result_;
527 const binding<bool>& condition, const binding<T>& true_result)
528 : condition_(condition)
529 , true_result_(true_result)
533 binding<T> otherwise(const binding<T>& false_result)
535 auto condition = condition_;
536 auto true_result = true_result_;
538 binding<T> result([condition, true_result, false_result]()
540 return condition.get() ? true_result.get() : false_result.get();
543 result.depend_on(condition);
544 result.depend_on(true_result);
545 result.depend_on(false_result);
550 binding<T> otherwise(T false_result)
552 return otherwise(binding<T>(false_result));
558 binding<bool> condition_;
560 when(const binding<bool>& condition)
561 : condition_(condition)
566 ternary_builder<T> then(const binding<T>& true_result)
568 return ternary_builder<T>(condition_, true_result);
572 ternary_builder<T> then(T true_result)
574 return then(binding<T>(true_result));
578 template<typename T, typename T2>
579 binding<T> add_tween(
580 const binding<T>& to_tween,
581 const binding<T2>& counter,
584 const std::wstring& easing)
586 tweener tween(easing);
588 double start_val = to_tween.as<double>().get();
589 double destination_val = static_cast<double>(destination_value);
590 double start_time = counter.as<double>().get();
591 double dur = static_cast<double>(duration);
593 return when(counter < duration)
594 .then(counter.as<double>().transformed([=](double t)
596 return tween(t - start_time, start_val, destination_val, dur);
598 .otherwise(destination_value);
601 template<typename T, typename T2>
603 const binding<T>& to_delay,
604 const binding<T>& after_delay,
605 const binding<T2>& counter,
608 return when(counter < duration)
610 .otherwise(after_delay);