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>
34 #include <boost/utility/value_init.hpp>
36 #include <common/tweener.h>
37 #include <common/except.h>
39 namespace caspar { namespace core {
43 struct impl_base : std::enable_shared_from_this<impl_base>
45 std::vector<std::shared_ptr<impl_base>> dependencies_;
46 mutable std::vector<std::pair<
48 std::function<void ()>>> on_change_;
54 virtual void evaluate() const = 0;
56 void depend_on(const std::shared_ptr<impl_base>& dependency)
58 auto self = shared_from_this();
60 if (dependency->depends_on(self))
61 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("Can't have circular dependencies between bindings"));
63 dependency->on_change(self, [=] { evaluate(); });
64 dependencies_.push_back(dependency);
67 bool depends_on(const std::shared_ptr<impl_base>& other) const
69 for (auto& dependency : dependencies_)
71 if (dependency == other)
74 if (dependency->depends_on(other))
82 const std::weak_ptr<void>& dependant,
83 const std::function<void ()>& listener) const
85 on_change_.push_back(std::make_pair(dependant, listener));
96 struct impl : public detail::impl_base
99 std::function<T ()> expression_;
100 mutable bool evaluated_ = false;
103 : value_(boost::value_initialized<T>())
112 template<typename Expr>
113 impl(const Expr& expression)
114 : value_(boost::value_initialized<T>())
115 , expression_(expression)
129 return static_cast<bool>(expression_);
135 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Bound value cannot be set"));
145 void evaluate() const override
149 auto new_value = expression_();
151 if (new_value != value_)
161 using impl_base::on_change;
162 void on_change() const
164 auto copy = on_change_;
166 for (int i = static_cast<int>(copy.size()) - 1; i >= 0; --i)
168 auto strong = copy[i].first.lock();
173 on_change_.erase(on_change_.begin() + i);
177 void bind(const std::shared_ptr<impl>& other)
181 expression_ = [other]{ return other->get(); };
189 expression_ = std::function<T ()>();
190 dependencies_.clear();
195 template<typename> friend class binding;
197 std::shared_ptr<impl> impl_;
204 explicit binding(T value)
205 : impl_(new impl(value))
210 template<typename Expr>
211 explicit binding(const Expr& expression)
212 : impl_(new impl(expression))
218 template<typename Expr, typename T2>
219 binding(const Expr& expression, const binding<T2>& dep)
220 : impl_(new impl(expression))
227 template<typename Expr, typename T2, typename T3>
229 const Expr& expression,
230 const binding<T2>& dep1,
231 const binding<T3>& dep2)
232 : impl_(new impl(expression))
239 void* identity() const
254 void bind(const binding<T>& other)
256 impl_->bind(other.impl_);
261 return impl_->bound();
264 template<typename T2>
265 void depend_on(const binding<T2>& other)
267 impl_->depend_on(other.impl_);
270 binding<T> operator+(T other) const
272 return transformed([other](T self) { return self + other; });
275 binding<T> operator+(const binding<T>& other) const
277 return composed(other, [](T self, T o) { return self + o; });
280 binding<T>& operator++()
290 binding<T> operator++(int)
292 binding<T> pre_val(get());
298 binding<T> operator-() const
300 return transformed([](T self) { return -self; });
303 binding<T> operator-(const binding<T>& other) const
305 return *this + -other;
308 binding<T> operator-(T other) const
310 return *this + -other;
313 binding<T>& operator--()
323 binding<T> operator--(int)
325 binding<T> pre_val(get());
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<T> operator%(T other) const
353 return transformed([other](T self) { return self % other; });
356 binding<T> 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 binding<bool> operator>=(T other) const
413 return transformed([other](T self) { return self >= other; });
416 binding<bool> operator>=(const binding<T>& other) const
418 return composed(other, [](T self, T o) { return self >= o; });
421 template<typename T2>
422 typename std::enable_if<
423 std::is_same<T, T2>::value,
430 template<typename T2>
431 typename std::enable_if<
432 (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,
436 return transformed([](T val) { return static_cast<T2>(val); });
439 template<typename T2>
440 typename std::enable_if<
441 (std::is_same<std::wstring, T>::value || std::is_same<std::wstring, T2>::value) && !std::is_same<T, T2>::value,
445 return transformed([](T val) { return boost::lexical_cast<T2>(val); });
448 // Func -> R (T self_val)
449 // Returns binding<R>
450 template<typename Func>
451 auto transformed(const Func& func) const -> binding<decltype(func(impl_->value_))>
453 typedef decltype(func(impl_->value_)) R;
457 [self, func] { return func(self->get()); },
461 // Func -> R (T self_val, T2 other_val)
462 // Returns binding<R>
463 template<typename Func, typename T2>
464 auto composed(const binding<T2>& other, const Func& func) const -> binding<decltype(func(impl_->value_, other.impl_->value_))>
466 typedef decltype(func(impl_->value_, other.impl_->value_)) R;
468 auto o = other.impl_;
471 [self, o, func] { return func(self->get(), o->get()); },
476 template<typename TweenerFunc, typename FrameCounter>
478 const binding<FrameCounter>& frame_counter,
479 const binding<FrameCounter>& duration,
480 const binding<TweenerFunc>& tweener_func) const
483 auto f = frame_counter.impl_;
484 auto d = duration.impl_;
485 auto tw = tweener_func.impl_;
486 FrameCounter start_frame = frame_counter.get();
487 FrameCounter current_frame = start_frame;
488 T current_source = get();
489 T current_destination = current_source;
490 T current_result = current_source;
492 auto result = binding<T>(
495 auto frame_diff = f->get() - current_frame;
496 bool new_frame = f->get() != current_frame;
499 return current_result;
501 bool new_tween = current_destination != self->get();
502 auto time = current_frame - start_frame + (new_tween ? frame_diff : 0) + 1;
504 current_frame = f->get();
506 current_source = new_tween ? tw->get()(time, current_source, current_destination - current_source, dur) : current_source;
507 current_destination = self->get();
510 start_frame = current_frame;
512 time = current_frame - start_frame;
515 current_result = tw->get()(time, current_source, current_destination - current_source, dur);
518 current_result = current_destination;
519 current_source = current_destination;
522 return current_result;
525 result.depend_on(*this);
526 result.depend_on(frame_counter);
527 result.depend_on(tweener_func);
538 const std::weak_ptr<void>& dependant,
539 const std::function<void ()>& listener) const
541 impl_->on_change(dependant, listener);
544 std::shared_ptr<void> on_change(
545 const std::function<void ()>& listener) const
547 std::shared_ptr<void> subscription(new char);
549 on_change(subscription, listener);
554 binding(const std::shared_ptr<impl>& self)
560 static binding<bool> operator||(const binding<bool>& lhs, const binding<bool>& rhs)
562 return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs || rhs; });
565 static binding<bool> operator||(const binding<bool>& lhs, bool rhs)
567 return lhs.transformed([rhs](bool lhs) { return lhs || rhs; });
570 static binding<bool> operator&&(const binding<bool>& lhs, const binding<bool>& rhs)
572 return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs && rhs; });
575 static binding<bool> operator&&(const binding<bool>& lhs, bool rhs)
577 return lhs.transformed([rhs](bool lhs) { return lhs && rhs; });
580 static binding<bool> operator!(const binding<bool>& self)
582 return self.transformed([](bool self) { return !self; });
586 class ternary_builder
588 binding<bool> condition_;
589 binding<T> true_result_;
592 const binding<bool>& condition, const binding<T>& true_result)
593 : condition_(condition)
594 , true_result_(true_result)
598 binding<T> otherwise(const binding<T>& false_result)
600 auto condition = condition_;
601 auto true_result = true_result_;
603 binding<T> result([condition, true_result, false_result]()
605 return condition.get() ? true_result.get() : false_result.get();
608 result.depend_on(condition);
609 result.depend_on(true_result);
610 result.depend_on(false_result);
615 binding<T> otherwise(T false_result)
617 return otherwise(binding<T>(false_result));
623 binding<bool> condition_;
625 when(const binding<bool>& condition)
626 : condition_(condition)
631 ternary_builder<T> then(const binding<T>& true_result)
633 return ternary_builder<T>(condition_, true_result);
637 ternary_builder<T> then(T true_result)
639 return then(binding<T>(true_result));
643 /*template<typename T, typename T2>
644 binding<T> add_tween(
645 const binding<T>& to_tween,
646 const binding<T2>& counter,
649 const std::wstring& easing)
651 tweener tween(easing);
653 double start_val = to_tween.as<double>().get();
654 double destination_val = static_cast<double>(destination_value);
655 double start_time = counter.as<double>().get();
656 double dur = static_cast<double>(duration);
658 return when(counter < duration)
659 .then(counter.as<double>().transformed([=](double t)
661 return tween(t - start_time, start_val, destination_val, dur);
663 .otherwise(destination_value);
666 template<typename T, typename T2>
668 const binding<T>& to_delay,
669 const binding<T>& after_delay,
670 const binding<T2>& counter,
673 return when(counter < duration)
675 .otherwise(after_delay);