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 {
36 struct impl_base : std::enable_shared_from_this<impl_base>
38 std::vector<std::shared_ptr<impl_base>> dependencies_;
39 mutable std::vector<std::pair<
41 std::function<void ()>>> on_change_;
47 virtual void evaluate() = 0;
49 void depend_on(const std::shared_ptr<impl_base>& dependency)
51 dependency->on_change(shared_from_this(), [=] { evaluate(); });
52 dependencies_.push_back(dependency);
56 const std::weak_ptr<void>& dependant,
57 const std::function<void ()>& listener) const
59 on_change_.push_back(std::make_pair(dependant, listener));
63 struct impl : public impl_base
66 std::function<T ()> expression_;
77 impl(const std::function<T ()>& expression)
78 : expression_(expression)
89 return static_cast<bool>(expression_);
96 throw std::exception("Bound value cannot be set");
107 void evaluate() override
111 auto new_value = expression_();
113 if (new_value != value_)
121 using impl_base::on_change;
124 auto copy = on_change_;
126 for (int i = static_cast<int>(copy.size()) - 1; i >= 0; --i)
128 auto strong = copy[i].first.lock();
133 on_change_.erase(on_change_.begin() + i);
137 void bind(const std::shared_ptr<impl>& other)
141 expression_ = [&]{ return other.get(); }
149 expression_ = std::function<T ()>();
150 dependencies_.clear();
155 std::shared_ptr<impl> impl_;
162 explicit binding(T value)
163 : impl_(new impl(value))
167 template<typename T2>
168 binding(const std::function<T ()>& expression, const binding<T2>& dep)
169 : impl_(new impl(expression))
175 template<typename T2, typename T3>
177 const std::function<T ()>& expression,
178 const binding<T2>& dep1,
179 const binding<T3>& dep2)
180 : impl_(new impl(expression))
197 void bind(const binding<T>& other)
199 impl_->bind(other.impl_);
204 return impl_->bound();
207 void depend_on(const binding<T>& other)
209 impl_->depend_on(other.impl_);
212 binding<T> operator+(T other) const
217 [other, self] { return other + self->get(); },
221 binding<T> operator+(const binding<T>& other) const
224 auto o = other.impl_;
227 [self, o] { return o->get() + self->get(); },
232 binding<T> operator-() const
237 [self] { return -self->get(); },
241 binding<T> operator-(const binding<T>& other) const
243 return *this + -other;
246 binding<T> operator-(T other) const
248 return *this + -other;
251 binding<T> operator*(T other) const
256 [other, self] { return other * self->get(); },
260 binding<T> operator*(const binding<T>& other) const
263 auto o = other.impl_;
266 [self, o] { return o->get() * self->get(); },
271 binding<T> operator/(T other) const
276 [other, self] { return self->get() / other; },
280 binding<T> operator/(const binding<T>& other) const
283 auto o = other.impl_;
286 [self, o] { return self->get() / o->get(); },
291 binding<bool> operator==(T other) const
295 return binding<bool>(
296 [other, self] { return self->get() == other; },
300 binding<bool> operator==(const binding<T>& other) const
303 auto o = other.impl_;
305 return binding<bool>(
306 [self, o] { return self->get() == o->get(); },
311 binding<bool> operator!=(T other) const
315 return binding<bool>(
316 [other, self] { return self->get() != other; },
320 binding<bool> operator!=(const binding<T>& other) const
323 auto o = other.impl_;
325 return binding<bool>(
326 [self, o] { return self->get() != o->get(); },
337 const std::weak_ptr<void> dependant,
338 const std::function<void ()>& listener) const
340 impl_->on_change(dependant, listener);
343 std::shared_ptr<void> on_change(
344 const std::function<void ()>& listener) const
346 std::shared_ptr<void> subscription(new char);
348 on_change(subscription, listener);
353 binding(const std::shared_ptr<impl>& self)