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 impl(const std::function<T ()>& expression)
84 : expression_(expression)
95 return static_cast<bool>(expression_);
102 throw std::exception("Bound value cannot be set");
113 void evaluate() override
117 auto new_value = expression_();
119 if (new_value != value_)
127 using impl_base::on_change;
130 auto copy = on_change_;
132 for (int i = static_cast<int>(copy.size()) - 1; i >= 0; --i)
134 auto strong = copy[i].first.lock();
139 on_change_.erase(on_change_.begin() + i);
143 void bind(const std::shared_ptr<impl>& other)
147 expression_ = [&]{ return other.get(); }
155 expression_ = std::function<T ()>();
156 dependencies_.clear();
161 template<typename> friend class binding;
163 std::shared_ptr<impl> impl_;
170 explicit binding(T value)
171 : impl_(new impl(value))
175 template<typename T2>
176 binding(const std::function<T ()>& expression, const binding<T2>& dep)
177 : impl_(new impl(expression))
183 template<typename T2, typename T3>
185 const std::function<T ()>& expression,
186 const binding<T2>& dep1,
187 const binding<T3>& dep2)
188 : impl_(new impl(expression))
205 void bind(const binding<T>& other)
207 impl_->bind(other.impl_);
212 return impl_->bound();
215 template<typename T2>
216 void depend_on(const binding<T2>& other)
218 impl_->depend_on(other.impl_);
221 binding<T> operator+(T other) const
223 return transformed([other](T self) { return self + other; });
226 binding<T> operator+(const binding<T>& other) const
229 auto o = other.impl_;
232 [self, o] { return o->get() + self->get(); },
237 binding<T> operator-() const
239 return transformed([](T self) { return -self; });
242 binding<T> operator-(const binding<T>& other) const
244 return *this + -other;
247 binding<T> operator-(T other) const
249 return *this + -other;
252 binding<T> operator*(T other) const
254 return transformed([other](T self) { return self * other; });
257 binding<T> operator*(const binding<T>& other) const
260 auto o = other.impl_;
263 [self, o] { return o->get() * self->get(); },
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
276 auto o = other.impl_;
279 [self, o] { return self->get() / o->get(); },
284 binding<bool> operator==(T other) const
286 return transformed([other](T self) { return self == other; });
289 binding<bool> operator==(const binding<T>& other) const
292 auto o = other.impl_;
294 return binding<bool>(
295 [self, o] { return self->get() == o->get(); },
300 binding<bool> operator!=(T other) const
302 return transformed([other](T self) { return self != other; });
305 binding<bool> operator!=(const binding<T>& other) const
308 auto o = other.impl_;
310 return binding<bool>(
311 [self, o] { return self->get() != o->get(); },
316 template<typename T2>
317 binding<T2> as() const
319 return transformed([](T val) { return static_cast<T2>(val); });
322 template<typename Func>
323 auto transformed(const Func& func) const -> binding<decltype(func(impl_->value_))>
325 typedef decltype(func(impl_->value_)) R;
329 [self, func] { return func(self->get()); },
339 const std::weak_ptr<void> dependant,
340 const std::function<void ()>& listener) const
342 impl_->on_change(dependant, listener);
345 std::shared_ptr<void> on_change(
346 const std::function<void ()>& listener) const
348 std::shared_ptr<void> subscription(new char);
350 on_change(subscription, listener);
355 binding(const std::shared_ptr<impl>& self)