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_;
78 impl(const std::function<T ()>& expression)
79 : expression_(expression)
90 return static_cast<bool>(expression_);
97 throw std::exception("Bound value cannot be set");
108 void evaluate() override
112 auto new_value = expression_();
114 if (new_value != value_)
122 using impl_base::on_change;
125 auto copy = on_change_;
127 for (int i = static_cast<int>(copy.size()) - 1; i >= 0; --i)
129 auto strong = copy[i].first.lock();
134 on_change_.erase(on_change_.begin() + i);
138 void bind(const std::shared_ptr<impl>& other)
142 expression_ = [&]{ return other.get(); }
150 expression_ = std::function<T ()>();
151 dependencies_.clear();
156 std::shared_ptr<impl> impl_;
163 explicit binding(T value)
164 : impl_(new impl(value))
168 template<typename T2>
169 binding(const std::function<T ()>& expression, const binding<T2>& dep)
170 : impl_(new impl(expression))
176 template<typename T2, typename T3>
178 const std::function<T ()>& expression,
179 const binding<T2>& dep1,
180 const binding<T3>& dep2)
181 : impl_(new impl(expression))
198 void bind(const binding<T>& other)
200 impl_->bind(other.impl_);
205 return impl_->bound();
208 void depend_on(const binding<T>& other)
210 impl_->depend_on(other.impl_);
213 binding<T> operator+(T other) const
218 [other, self] { return other + self->get(); },
222 binding<T> operator+(const binding<T>& other) const
225 auto o = other.impl_;
228 [self, o] { return o->get() + self->get(); },
233 binding<T> operator-() const
238 [self] { return -self->get(); },
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
257 [other, self] { return other * self->get(); },
261 binding<T> operator*(const binding<T>& other) const
264 auto o = other.impl_;
267 [self, o] { return o->get() * self->get(); },
272 binding<T> operator/(T other) const
277 [other, self] { return self->get() / other; },
281 binding<T> operator/(const binding<T>& other) const
284 auto o = other.impl_;
287 [self, o] { return self->get() / o->get(); },
292 binding<bool> operator==(T other) const
296 return binding<bool>(
297 [other, self] { return self->get() == other; },
301 binding<bool> operator==(const binding<T>& other) const
304 auto o = other.impl_;
306 return binding<bool>(
307 [self, o] { return self->get() == o->get(); },
312 binding<bool> operator!=(T other) const
316 return binding<bool>(
317 [other, self] { return self->get() != other; },
321 binding<bool> operator!=(const binding<T>& other) const
324 auto o = other.impl_;
326 return binding<bool>(
327 [self, o] { return self->get() != o->get(); },
338 const std::weak_ptr<void> dependant,
339 const std::function<void ()>& listener) const
341 impl_->on_change(dependant, listener);
344 std::shared_ptr<void> on_change(
345 const std::function<void ()>& listener) const
347 std::shared_ptr<void> subscription(new char);
349 on_change(subscription, listener);
354 binding(const std::shared_ptr<impl>& self)