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 namespace caspar { namespace core {
34 struct impl_base : std::enable_shared_from_this<impl_base>
36 std::vector<std::shared_ptr<impl_base>> dependencies_;
37 mutable std::vector<std::pair<
39 std::function<void ()>>> on_change_;
45 virtual void evaluate() = 0;
47 void depend_on(const std::shared_ptr<impl_base>& dependency)
49 dependency->on_change(shared_from_this(), [=] { evaluate(); });
50 dependencies_.push_back(dependency);
54 const std::weak_ptr<void>& dependant,
55 const std::function<void ()>& listener) const
57 on_change_.push_back(std::make_pair(dependant, listener));
68 struct impl : public detail::impl_base
71 std::function<T ()> expression_;
83 template<typename Expr>
84 impl(const Expr& expression)
85 : expression_(expression)
96 return static_cast<bool>(expression_);
103 throw std::exception("Bound value cannot be set");
114 void evaluate() override
118 auto new_value = expression_();
120 if (new_value != value_)
128 using impl_base::on_change;
131 auto copy = on_change_;
133 for (int i = static_cast<int>(copy.size()) - 1; i >= 0; --i)
135 auto strong = copy[i].first.lock();
140 on_change_.erase(on_change_.begin() + i);
144 void bind(const std::shared_ptr<impl>& other)
148 expression_ = [other]{ return other->get(); };
156 expression_ = std::function<T ()>();
157 dependencies_.clear();
162 template<typename> friend class binding;
164 std::shared_ptr<impl> impl_;
171 explicit binding(T value)
172 : impl_(new impl(value))
177 template<typename Expr>
178 explicit binding(const Expr& expression)
179 : impl_(new impl(expression))
185 template<typename Expr, typename T2>
186 binding(const Expr& expression, const binding<T2>& dep)
187 : impl_(new impl(expression))
194 template<typename Expr, typename T2, typename T3>
196 const Expr& expression,
197 const binding<T2>& dep1,
198 const binding<T3>& dep2)
199 : impl_(new impl(expression))
206 void* identity() const
221 void bind(const binding<T>& other)
223 impl_->bind(other.impl_);
228 return impl_->bound();
231 template<typename T2>
232 void depend_on(const binding<T2>& other)
234 impl_->depend_on(other.impl_);
237 binding<T> operator+(T other) const
239 return transformed([other](T self) { return self + other; });
242 binding<T> operator+(const binding<T>& other) const
244 return composed(other, [](T self, T o) { return self + o; });
247 binding<T>& operator++()
257 binding<T> operator++(int)
259 binding<T> pre_val(get());
265 binding<T> operator-() const
267 return transformed([](T self) { return -self; });
270 binding<T> operator-(const binding<T>& other) const
272 return *this + -other;
275 binding<T> operator-(T other) const
277 return *this + -other;
280 binding<T>& operator--()
290 binding<T> operator--(int)
292 binding<T> pre_val(get());
298 binding<T> operator*(T other) const
300 return transformed([other](T self) { return self * other; });
303 binding<T> operator*(const binding<T>& other) const
305 return composed(other, [](T self, T o) { return self * o; });
308 binding<T> operator/(T other) const
310 return transformed([other](T self) { return self / other; });
313 binding<T> operator/(const binding<T>& other) const
315 return composed(other, [](T self, T o) { return self / o; });
318 binding<T> operator%(T other) const
320 return transformed([other](T self) { return self % other; });
323 binding<T> operator%(const binding<T>& other) const
325 return composed(other, [](T self, T o) { return self % o; });
328 binding<bool> operator==(T other) const
330 return transformed([other](T self) { return self == other; });
333 binding<bool> operator==(const binding<T>& other) const
335 return composed(other, [](T self, T o) { return self == o; });
338 binding<bool> operator!=(T other) const
340 return transformed([other](T self) { return self != other; });
343 binding<bool> operator!=(const binding<T>& other) const
345 return composed(other, [](T self, T o) { return self != o; });
348 binding<bool> operator<(T other) const
350 return transformed([other](T self) { return self < other; });
353 binding<bool> operator<(const binding<T>& other) const
355 return composed(other, [](T self, T o) { return self < o; });
358 binding<bool> operator>(T other) const
360 return transformed([other](T self) { return self > other; });
363 binding<bool> operator>(const binding<T>& other) const
365 return composed(other, [](T self, T o) { return self > o; });
368 binding<bool> operator<=(T other) const
370 return transformed([other](T self) { return self <= other; });
373 binding<bool> operator<=(const binding<T>& other) const
375 return composed(other, [](T self, T o) { return self <= o; });
378 binding<bool> operator>=(T other) const
380 return transformed([other](T self) { return self >= other; });
383 binding<bool> operator>=(const binding<T>& other) const
385 return composed(other, [](T self, T o) { return self >= o; });
388 template<typename T2>
389 binding<T2> as() const
391 return transformed([](T val) { return static_cast<T2>(val); });
394 // Func -> R (T self_val)
395 // Returns binding<R>
396 template<typename Func>
397 auto transformed(const Func& func) const -> binding<decltype(func(impl_->value_))>
399 typedef decltype(func(impl_->value_)) R;
403 [self, func] { return func(self->get()); },
407 // Func -> R (T self_val, T2 other_val)
408 // Returns binding<R>
409 template<typename Func, typename T2>
410 auto composed(const binding<T2> other, const Func& func) const -> binding<decltype(func(impl_->value_, other.impl_->value_))>
412 typedef decltype(func(impl_->value_, other.impl_->value_)) R;
414 auto o = other.impl_;
417 [self, o, func] { return func(self->get(), o->get()); },
428 const std::weak_ptr<void> dependant,
429 const std::function<void ()>& listener) const
431 impl_->on_change(dependant, listener);
434 std::shared_ptr<void> on_change(
435 const std::function<void ()>& listener) const
437 std::shared_ptr<void> subscription(new char);
439 on_change(subscription, listener);
444 binding(const std::shared_ptr<impl>& self)
450 static binding<bool> operator||(const binding<bool>& lhs, const binding<bool>& rhs)
452 return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs || rhs; });
455 static binding<bool> operator&&(const binding<bool>& lhs, const binding<bool>& rhs)
457 return lhs.composed(rhs, [](bool lhs, bool rhs) { return lhs && rhs; });
460 static binding<bool> operator!(const binding<bool>& self)
462 return self.transformed([](bool self) { return !self; });
466 class ternary_builder
468 binding<bool> condition_;
469 binding<T> true_result_;
472 const binding<bool>& condition, const binding<T>& true_result)
473 : condition_(condition)
474 , true_result_(true_result)
478 binding<T> otherwise(const binding<T>& false_result)
480 auto condition = condition_;
481 auto true_result = true_result_;
483 binding<T> result([condition, true_result, false_result]()
485 return condition.get() ? true_result.get() : false_result.get();
488 result.depend_on(condition);
489 result.depend_on(true_result);
490 result.depend_on(false_result);
495 binding<T> otherwise(T false_result)
497 return otherwise(binding<T>(false_result));
503 binding<bool> condition_;
505 when(const binding<bool>& condition)
506 : condition_(condition)
511 ternary_builder<T> then(const binding<T>& true_result)
513 return ternary_builder<T>(condition_, true_result);
517 ternary_builder<T> then(T true_result)
519 return then(binding<T>(true_result));
523 template<typename T, typename T2>
524 binding<T> add_tween(
525 const binding<T>& to_tween,
526 const binding<T2>& counter,
529 const std::wstring& easing)
531 tweener tween(easing);
533 double start_val = to_tween.as<double>().get();
534 double destination_val = static_cast<double>(destination_value);
535 double start_time = counter.as<double>().get();
536 double dur = static_cast<double>(duration);
538 return when(counter < duration)
539 .then(counter.as<double>().transformed([=](double t)
541 return tween(t - start_time, start_val, destination_val, dur);
543 .otherwise(destination_value);
546 template<typename T, typename T2>
548 const binding<T>& to_delay,
549 const binding<T>& after_delay,
550 const binding<T2>& counter,
553 return when(counter < duration)
555 .otherwise(after_delay);