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() = 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_;
109 template<typename Expr>
110 impl(const Expr& expression)
111 : expression_(expression)
122 return static_cast<bool>(expression_);
129 throw std::runtime_error("Bound value cannot be set");
140 void evaluate() override
144 auto new_value = expression_();
146 if (new_value != value_)
154 using impl_base::on_change;
157 auto copy = on_change_;
159 for (int i = static_cast<int>(copy.size()) - 1; i >= 0; --i)
161 auto strong = copy[i].first.lock();
166 on_change_.erase(on_change_.begin() + i);
170 void bind(const std::shared_ptr<impl>& other)
174 expression_ = [other]{ return other->get(); };
182 expression_ = std::function<T ()>();
183 dependencies_.clear();
188 template<typename> friend class binding;
190 std::shared_ptr<impl> impl_;
197 explicit binding(T value)
198 : impl_(new impl(value))
203 template<typename Expr>
204 explicit binding(const Expr& expression)
205 : impl_(new impl(expression))
211 template<typename Expr, typename T2>
212 binding(const Expr& expression, const binding<T2>& dep)
213 : impl_(new impl(expression))
220 template<typename Expr, typename T2, typename T3>
222 const Expr& expression,
223 const binding<T2>& dep1,
224 const binding<T3>& dep2)
225 : impl_(new impl(expression))
232 void* identity() const
247 void bind(const binding<T>& other)
249 impl_->bind(other.impl_);
254 return impl_->bound();
257 template<typename T2>
258 void depend_on(const binding<T2>& other)
260 impl_->depend_on(other.impl_);
263 binding<T> operator+(T other) const
265 return transformed([other](T self) { return self + other; });
268 binding<T> operator+(const binding<T>& other) const
270 return composed(other, [](T self, T o) { return self + o; });
273 binding<T>& operator++()
283 binding<T> operator++(int)
285 binding<T> pre_val(get());
291 binding<T> operator-() const
293 return transformed([](T self) { return -self; });
296 binding<T> operator-(const binding<T>& other) const
298 return *this + -other;
301 binding<T> operator-(T other) const
303 return *this + -other;
306 binding<T>& operator--()
316 binding<T> operator--(int)
318 binding<T> pre_val(get());
324 binding<T> operator*(T other) const
326 return transformed([other](T self) { return self * other; });
329 binding<T> operator*(const binding<T>& other) const
331 return composed(other, [](T self, T o) { return self * o; });
334 binding<T> operator/(T other) const
336 return transformed([other](T self) { return self / other; });
339 binding<T> operator/(const binding<T>& other) const
341 return composed(other, [](T self, T o) { return self / o; });
344 binding<T> operator%(T other) const
346 return transformed([other](T self) { return self % other; });
349 binding<T> operator%(const binding<T>& other) const
351 return composed(other, [](T self, T o) { return self % o; });
354 binding<bool> operator==(T other) const
356 return transformed([other](T self) { return self == other; });
359 binding<bool> operator==(const binding<T>& other) const
361 return composed(other, [](T self, T o) { return self == o; });
364 binding<bool> operator!=(T other) const
366 return transformed([other](T self) { return self != other; });
369 binding<bool> operator!=(const binding<T>& other) const
371 return composed(other, [](T self, T o) { return self != o; });
374 binding<bool> operator<(T other) const
376 return transformed([other](T self) { return self < other; });
379 binding<bool> operator<(const binding<T>& other) const
381 return composed(other, [](T self, T o) { return self < o; });
384 binding<bool> operator>(T other) const
386 return transformed([other](T self) { return self > other; });
389 binding<bool> operator>(const binding<T>& other) const
391 return composed(other, [](T self, T o) { return self > o; });
394 binding<bool> operator<=(T other) const
396 return transformed([other](T self) { return self <= other; });
399 binding<bool> operator<=(const binding<T>& other) const
401 return composed(other, [](T self, T o) { return self <= o; });
404 binding<bool> operator>=(T other) const
406 return transformed([other](T self) { return self >= other; });
409 binding<bool> operator>=(const binding<T>& other) const
411 return composed(other, [](T self, T o) { return self >= o; });
414 template<typename T2>
415 typename std::enable_if<
416 std::is_same<T, T2>::value,
423 template<typename T2>
424 typename std::enable_if<
425 (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,
429 return transformed([](T val) { return static_cast<T2>(val); });
432 template<typename T2>
433 typename std::enable_if<
434 (std::is_same<std::wstring, T>::value || std::is_same<std::wstring, T2>::value) && !std::is_same<T, T2>::value,
438 return transformed([](T val) { return boost::lexical_cast<T2>(val); });
441 // Func -> R (T self_val)
442 // Returns binding<R>
443 template<typename Func>
444 auto transformed(const Func& func) const -> binding<decltype(func(impl_->value_))>
446 typedef decltype(func(impl_->value_)) R;
450 [self, func] { return func(self->get()); },
454 // Func -> R (T self_val, T2 other_val)
455 // Returns binding<R>
456 template<typename Func, typename T2>
457 auto composed(const binding<T2> other, const Func& func) const -> binding<decltype(func(impl_->value_, other.impl_->value_))>
459 typedef decltype(func(impl_->value_, other.impl_->value_)) R;
461 auto o = other.impl_;
464 [self, o, func] { return func(self->get(), o->get()); },
475 const std::weak_ptr<void>& dependant,
476 const std::function<void ()>& listener) const
478 impl_->on_change(dependant, listener);
481 std::shared_ptr<void> on_change(
482 const std::function<void ()>& listener) const
484 std::shared_ptr<void> subscription(new char);
486 on_change(subscription, listener);
491 binding(const std::shared_ptr<impl>& self)
497 static binding<bool> operator||(const binding<bool>& lhs, const binding<bool>& rhs)
499 return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs || rhs; });
502 static binding<bool> operator||(const binding<bool>& lhs, bool rhs)
504 return lhs.transformed([rhs](bool lhs) { return lhs || rhs; });
507 static binding<bool> operator&&(const binding<bool>& lhs, const binding<bool>& rhs)
509 return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs && rhs; });
512 static binding<bool> operator&&(const binding<bool>& lhs, bool rhs)
514 return lhs.transformed([rhs](bool lhs) { return lhs && rhs; });
517 static binding<bool> operator!(const binding<bool>& self)
519 return self.transformed([](bool self) { return !self; });
523 class ternary_builder
525 binding<bool> condition_;
526 binding<T> true_result_;
529 const binding<bool>& condition, const binding<T>& true_result)
530 : condition_(condition)
531 , true_result_(true_result)
535 binding<T> otherwise(const binding<T>& false_result)
537 auto condition = condition_;
538 auto true_result = true_result_;
540 binding<T> result([condition, true_result, false_result]()
542 return condition.get() ? true_result.get() : false_result.get();
545 result.depend_on(condition);
546 result.depend_on(true_result);
547 result.depend_on(false_result);
552 binding<T> otherwise(T false_result)
554 return otherwise(binding<T>(false_result));
560 binding<bool> condition_;
562 when(const binding<bool>& condition)
563 : condition_(condition)
568 ternary_builder<T> then(const binding<T>& true_result)
570 return ternary_builder<T>(condition_, true_result);
574 ternary_builder<T> then(T true_result)
576 return then(binding<T>(true_result));
580 /*template<typename T, typename T2>
581 binding<T> add_tween(
582 const binding<T>& to_tween,
583 const binding<T2>& counter,
586 const std::wstring& easing)
588 tweener tween(easing);
590 double start_val = to_tween.as<double>().get();
591 double destination_val = static_cast<double>(destination_value);
592 double start_time = counter.as<double>().get();
593 double dur = static_cast<double>(duration);
595 return when(counter < duration)
596 .then(counter.as<double>().transformed([=](double t)
598 return tween(t - start_time, start_val, destination_val, dur);
600 .otherwise(destination_value);
603 template<typename T, typename T2>
605 const binding<T>& to_delay,
606 const binding<T>& after_delay,
607 const binding<T2>& counter,
610 return when(counter < duration)
612 .otherwise(after_delay);