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>
36 #include <common/except.h>
38 namespace caspar { namespace core {
42 struct impl_base : std::enable_shared_from_this<impl_base>
44 std::vector<std::shared_ptr<impl_base>> dependencies_;
45 mutable std::vector<std::pair<
47 std::function<void ()>>> on_change_;
53 virtual void evaluate() const = 0;
55 void depend_on(const std::shared_ptr<impl_base>& dependency)
57 auto self = shared_from_this();
59 if (dependency->depends_on(self))
60 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("Can't have circular dependencies between bindings"));
62 dependency->on_change(self, [=] { evaluate(); });
63 dependencies_.push_back(dependency);
66 bool depends_on(const std::shared_ptr<impl_base>& other) const
68 for (auto& dependency : dependencies_)
70 if (dependency == other)
73 if (dependency->depends_on(other))
81 const std::weak_ptr<void>& dependant,
82 const std::function<void ()>& listener) const
84 on_change_.push_back(std::make_pair(dependant, listener));
95 struct impl : public detail::impl_base
98 std::function<T ()> expression_;
99 mutable bool evaluated_ = false;
111 template<typename Expr>
112 impl(const Expr& expression)
113 : expression_(expression)
127 return static_cast<bool>(expression_);
133 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Bound value cannot be set"));
143 void evaluate() const override
147 auto new_value = expression_();
149 if (new_value != value_)
159 using impl_base::on_change;
160 void on_change() const
162 auto copy = on_change_;
164 for (int i = static_cast<int>(copy.size()) - 1; i >= 0; --i)
166 auto strong = copy[i].first.lock();
171 on_change_.erase(on_change_.begin() + i);
175 void bind(const std::shared_ptr<impl>& other)
179 expression_ = [other]{ return other->get(); };
187 expression_ = std::function<T ()>();
188 dependencies_.clear();
193 template<typename> friend class binding;
195 std::shared_ptr<impl> impl_;
202 explicit binding(T value)
203 : impl_(new impl(value))
208 template<typename Expr>
209 explicit binding(const Expr& expression)
210 : impl_(new impl(expression))
216 template<typename Expr, typename T2>
217 binding(const Expr& expression, const binding<T2>& dep)
218 : impl_(new impl(expression))
225 template<typename Expr, typename T2, typename T3>
227 const Expr& expression,
228 const binding<T2>& dep1,
229 const binding<T3>& dep2)
230 : impl_(new impl(expression))
237 void* identity() const
252 void bind(const binding<T>& other)
254 impl_->bind(other.impl_);
259 return impl_->bound();
262 template<typename T2>
263 void depend_on(const binding<T2>& other)
265 impl_->depend_on(other.impl_);
268 binding<T> operator+(T other) const
270 return transformed([other](T self) { return self + other; });
273 binding<T> operator+(const binding<T>& other) const
275 return composed(other, [](T self, T o) { return self + o; });
278 binding<T>& operator++()
288 binding<T> operator++(int)
290 binding<T> pre_val(get());
296 binding<T> operator-() const
298 return transformed([](T self) { return -self; });
301 binding<T> operator-(const binding<T>& other) const
303 return *this + -other;
306 binding<T> operator-(T other) const
308 return *this + -other;
311 binding<T>& operator--()
321 binding<T> operator--(int)
323 binding<T> pre_val(get());
329 binding<T> operator*(T other) const
331 return transformed([other](T self) { return self * other; });
334 binding<T> operator*(const binding<T>& other) const
336 return composed(other, [](T self, T o) { return self * o; });
339 binding<T> operator/(T other) const
341 return transformed([other](T self) { return self / other; });
344 binding<T> operator/(const binding<T>& other) const
346 return composed(other, [](T self, T o) { return self / o; });
349 binding<T> operator%(T other) const
351 return transformed([other](T self) { return self % other; });
354 binding<T> operator%(const binding<T>& other) const
356 return composed(other, [](T self, T o) { return self % o; });
359 binding<bool> operator==(T other) const
361 return transformed([other](T self) { return self == other; });
364 binding<bool> operator==(const binding<T>& other) const
366 return composed(other, [](T self, T o) { return self == o; });
369 binding<bool> operator!=(T other) const
371 return transformed([other](T self) { return self != other; });
374 binding<bool> operator!=(const binding<T>& other) const
376 return composed(other, [](T self, T o) { return self != o; });
379 binding<bool> operator<(T other) const
381 return transformed([other](T self) { return self < other; });
384 binding<bool> operator<(const binding<T>& other) const
386 return composed(other, [](T self, T o) { return self < o; });
389 binding<bool> operator>(T other) const
391 return transformed([other](T self) { return self > other; });
394 binding<bool> operator>(const binding<T>& other) const
396 return composed(other, [](T self, T o) { return self > o; });
399 binding<bool> operator<=(T other) const
401 return transformed([other](T self) { return self <= other; });
404 binding<bool> operator<=(const binding<T>& other) const
406 return composed(other, [](T self, T o) { return self <= o; });
409 binding<bool> operator>=(T other) const
411 return transformed([other](T self) { return self >= other; });
414 binding<bool> operator>=(const binding<T>& other) const
416 return composed(other, [](T self, T o) { return self >= o; });
419 template<typename T2>
420 typename std::enable_if<
421 std::is_same<T, T2>::value,
428 template<typename T2>
429 typename std::enable_if<
430 (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,
434 return transformed([](T val) { return static_cast<T2>(val); });
437 template<typename T2>
438 typename std::enable_if<
439 (std::is_same<std::wstring, T>::value || std::is_same<std::wstring, T2>::value) && !std::is_same<T, T2>::value,
443 return transformed([](T val) { return boost::lexical_cast<T2>(val); });
446 // Func -> R (T self_val)
447 // Returns binding<R>
448 template<typename Func>
449 auto transformed(const Func& func) const -> binding<decltype(func(impl_->value_))>
451 typedef decltype(func(impl_->value_)) R;
455 [self, func] { return func(self->get()); },
459 // Func -> R (T self_val, T2 other_val)
460 // Returns binding<R>
461 template<typename Func, typename T2>
462 auto composed(const binding<T2> other, const Func& func) const -> binding<decltype(func(impl_->value_, other.impl_->value_))>
464 typedef decltype(func(impl_->value_, other.impl_->value_)) R;
466 auto o = other.impl_;
469 [self, o, func] { return func(self->get(), o->get()); },
480 const std::weak_ptr<void>& dependant,
481 const std::function<void ()>& listener) const
483 impl_->on_change(dependant, listener);
486 std::shared_ptr<void> on_change(
487 const std::function<void ()>& listener) const
489 std::shared_ptr<void> subscription(new char);
491 on_change(subscription, listener);
496 binding(const std::shared_ptr<impl>& self)
502 static binding<bool> operator||(const binding<bool>& lhs, const binding<bool>& rhs)
504 return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs || rhs; });
507 static binding<bool> operator||(const binding<bool>& lhs, bool rhs)
509 return lhs.transformed([rhs](bool lhs) { return lhs || rhs; });
512 static binding<bool> operator&&(const binding<bool>& lhs, const binding<bool>& rhs)
514 return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs && rhs; });
517 static binding<bool> operator&&(const binding<bool>& lhs, bool rhs)
519 return lhs.transformed([rhs](bool lhs) { return lhs && rhs; });
522 static binding<bool> operator!(const binding<bool>& self)
524 return self.transformed([](bool self) { return !self; });
528 class ternary_builder
530 binding<bool> condition_;
531 binding<T> true_result_;
534 const binding<bool>& condition, const binding<T>& true_result)
535 : condition_(condition)
536 , true_result_(true_result)
540 binding<T> otherwise(const binding<T>& false_result)
542 auto condition = condition_;
543 auto true_result = true_result_;
545 binding<T> result([condition, true_result, false_result]()
547 return condition.get() ? true_result.get() : false_result.get();
550 result.depend_on(condition);
551 result.depend_on(true_result);
552 result.depend_on(false_result);
557 binding<T> otherwise(T false_result)
559 return otherwise(binding<T>(false_result));
565 binding<bool> condition_;
567 when(const binding<bool>& condition)
568 : condition_(condition)
573 ternary_builder<T> then(const binding<T>& true_result)
575 return ternary_builder<T>(condition_, true_result);
579 ternary_builder<T> then(T true_result)
581 return then(binding<T>(true_result));
585 /*template<typename T, typename T2>
586 binding<T> add_tween(
587 const binding<T>& to_tween,
588 const binding<T2>& counter,
591 const std::wstring& easing)
593 tweener tween(easing);
595 double start_val = to_tween.as<double>().get();
596 double destination_val = static_cast<double>(destination_value);
597 double start_time = counter.as<double>().get();
598 double dur = static_cast<double>(duration);
600 return when(counter < duration)
601 .then(counter.as<double>().transformed([=](double t)
603 return tween(t - start_time, start_val, destination_val, dur);
605 .otherwise(destination_value);
608 template<typename T, typename T2>
610 const binding<T>& to_delay,
611 const binding<T>& after_delay,
612 const binding<T2>& counter,
615 return when(counter < duration)
617 .otherwise(after_delay);