]> git.sesse.net Git - casparcg/blob - core/producer/binding.h
Draft of interaction with producers and scene_producer
[casparcg] / core / producer / binding.h
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #pragma once
23
24 #include <functional>
25 #include <memory>
26 #include <vector>
27 #include <map>
28 #include <algorithm>
29
30 namespace caspar { namespace core {
31
32 template <typename T>
33 class binding
34 {
35 private:
36         struct impl : std::enable_shared_from_this<impl>
37         {
38                 T value_;
39                 std::function<T ()> expression_;
40                 std::vector<std::shared_ptr<impl>> dependencies_;
41                 mutable std::vector<std::pair<
42                                 std::weak_ptr<void>,
43                                 std::function<void ()>>> on_change_;
44
45                 impl()
46                 {
47                 }
48
49                 impl(T value)
50                         : value_(value)
51                 {
52                 }
53
54                 impl(const std::function<T ()>& expression)
55                         : expression_(expression)
56                 {
57                 }
58
59                 T get() const
60                 {
61                         return value_;
62                 }
63
64                 bool bound() const
65                 {
66                         return static_cast<bool>(expression_);
67                 }
68
69                 void set(T value)
70                 {
71                         if (bound())
72                         {
73                                 throw std::exception("Bound value cannot be set");
74                         }
75
76                         if (value == value_)
77                                 return;
78
79                         value_ = value;
80
81                         on_change();
82                 }
83
84                 void depend_on(const std::shared_ptr<impl>& dependency)
85                 {
86                         dependency->on_change(shared_from_this(), [=] { evaluate(); });
87                         dependencies_.push_back(dependency);
88                 }
89
90                 void on_change(
91                                 const std::weak_ptr<void>& dependant,
92                                 const std::function<void ()>& listener) const
93                 {
94                         on_change_.push_back(std::make_pair(dependant, listener));
95                 }
96
97                 void evaluate()
98                 {
99                         if (expression_)
100                         {
101                                 auto new_value = expression_();
102
103                                 if (new_value != value_)
104                                 {
105                                         value_ = new_value;
106                                         on_change();
107                                 }
108                         }
109                 }
110
111                 void on_change()
112                 {
113                         auto copy = on_change_;
114
115                         for (int i = static_cast<int>(copy.size()) - 1; i >= 0; --i)
116                         {
117                                 auto strong = copy[i].first.lock();
118
119                                 if (strong)
120                                         copy[i].second();
121                                 else
122                                         on_change_.erase(on_change_.begin() + i);
123                         }
124                 }
125
126                 void bind(const std::shared_ptr<impl>& other)
127                 {
128                         unbind();
129                         depend_on(other);
130                         expression_ = [&]{ return other.get(); }
131                         evaluate();
132                 }
133
134                 void unbind()
135                 {
136                         if (expression_)
137                         {
138                                 expression_ = std::function<T ()>();
139                                 dependencies_.clear();
140                         }
141                 }
142         };
143
144         std::shared_ptr<impl> impl_;
145 public:
146         binding()
147                 : impl_(new impl)
148         {
149         }
150
151         explicit binding(T value)
152                 : impl_(new impl(value))
153         {
154         }
155
156         binding(const std::function<T ()>& expression, const binding<T>& dep)
157                 : impl_(new impl(expression))
158         {
159                 depend_on(dep);
160                 impl_->evaluate();
161         }
162
163         binding(
164                         const std::function<T ()>& expression,
165                         const binding<T>& dep1,
166                         const binding<T>& dep2)
167                 : impl_(new impl(expression))
168         {
169                 depend_on(dep1);
170                 depend_on(dep2);
171                 impl_->evaluate();
172         }
173
174         T get() const
175         {
176                 return impl_->get();
177         }
178
179         void set(T value)
180         {
181                 impl_->set(value);
182         }
183
184         void bind(const binding<T>& other)
185         {
186                 impl_->bind(other.impl_);
187         }
188
189         bool bound() const
190         {
191                 return impl_->bound();
192         }
193
194         void depend_on(const binding<T>& other)
195         {
196                 impl_->depend_on(other.impl_);
197         }
198
199         binding<T> operator+(T other) const
200         {
201                 auto self = impl_;
202
203                 return binding<T>(
204                                 [other, self] { return other + self->get(); },
205                                 other);
206         }
207
208         binding<T> operator+(const binding<T>& other) const
209         {
210                 auto self = impl_;
211                 auto o = other.impl_;
212
213                 return binding<T>(
214                                 [self, o] { return o->get() + self->get(); },
215                                 *this,
216                                 other);
217         }
218
219         void unbind()
220         {
221                 impl_->unbind();
222         }
223
224         binding<T> operator-(T other) const
225         {
226                 return add(-other);
227         }
228
229         void on_change(
230                         const std::weak_ptr<void> dependant,
231                         const std::function<void ()>& listener) const
232         {
233                 impl_->on_change(dependant, listener);
234         }
235
236         std::shared_ptr<void> on_change(
237                         const std::function<void ()>& listener) const
238         {
239                 std::shared_ptr<void> subscription(new char);
240                 
241                 impl_->on_change(subscription, listener);
242                 
243                 return subscription;
244         }
245 private:
246         binding(const std::shared_ptr<impl>& self)
247                 : impl_(self)
248         {
249         }
250 };
251
252 }}