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 : std::enable_shared_from_this<impl>
39 std::function<T ()> expression_;
40 std::vector<std::shared_ptr<impl>> dependencies_;
41 mutable std::vector<std::pair<
43 std::function<void ()>>> on_change_;
54 impl(const std::function<T ()>& expression)
55 : expression_(expression)
66 return static_cast<bool>(expression_);
73 throw std::exception("Bound value cannot be set");
84 void depend_on(const std::shared_ptr<impl>& dependency)
86 dependency->on_change(shared_from_this(), [=] { evaluate(); });
87 dependencies_.push_back(dependency);
91 const std::weak_ptr<void>& dependant,
92 const std::function<void ()>& listener) const
94 on_change_.push_back(std::make_pair(dependant, listener));
101 auto new_value = expression_();
103 if (new_value != value_)
113 auto copy = on_change_;
115 for (int i = static_cast<int>(copy.size()) - 1; i >= 0; --i)
117 auto strong = copy[i].first.lock();
122 on_change_.erase(on_change_.begin() + i);
126 void bind(const std::shared_ptr<impl>& other)
130 expression_ = [&]{ return other.get(); }
138 expression_ = std::function<T ()>();
139 dependencies_.clear();
144 std::shared_ptr<impl> impl_;
151 explicit binding(T value)
152 : impl_(new impl(value))
156 binding(const std::function<T ()>& expression, const binding<T>& dep)
157 : impl_(new impl(expression))
164 const std::function<T ()>& expression,
165 const binding<T>& dep1,
166 const binding<T>& dep2)
167 : impl_(new impl(expression))
184 void bind(const binding<T>& other)
186 impl_->bind(other.impl_);
191 return impl_->bound();
194 void depend_on(const binding<T>& other)
196 impl_->depend_on(other.impl_);
199 binding<T> operator+(T other) const
204 [other, self] { return other + self->get(); },
208 binding<T> operator+(const binding<T>& other) const
211 auto o = other.impl_;
214 [self, o] { return o->get() + self->get(); },
224 binding<T> operator-(T other) const
230 const std::weak_ptr<void> dependant,
231 const std::function<void ()>& listener) const
233 impl_->on_change(dependant, listener);
236 std::shared_ptr<void> on_change(
237 const std::function<void ()>& listener) const
239 std::shared_ptr<void> subscription(new char);
241 impl_->on_change(subscription, listener);
246 binding(const std::shared_ptr<impl>& self)