Supporters update.
[dcpomatic.git] / src / lib / change_signaller.h
1 /*
2     Copyright (C) 2018-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #ifndef DCPOMATIC_CHANGE_SIGNALLER_H
23 #define DCPOMATIC_CHANGE_SIGNALLER_H
24
25
26 #include <boost/thread.hpp>
27 #include <vector>
28
29
30 enum class ChangeType
31 {
32         PENDING,
33         DONE,
34         CANCELLED
35 };
36
37
38 template <class T, class P>
39 class ChangeSignal
40 {
41 public:
42         ChangeSignal(T* thing_, P property_, ChangeType type_)
43                 : thing(thing_)
44                 , property(property_)
45                 , type(type_)
46         {}
47
48         T* thing;
49         P property;
50         ChangeType type;
51 };
52
53
54 class ChangeSignalDespatcherBase
55 {
56 protected:
57         static boost::mutex _instance_mutex;
58 };
59
60
61 template <class T, class P>
62 class ChangeSignalDespatcher : public ChangeSignalDespatcherBase
63 {
64 public:
65         ChangeSignalDespatcher() = default;
66
67         ChangeSignalDespatcher(ChangeSignalDespatcher const&) = delete;
68         ChangeSignalDespatcher& operator=(ChangeSignalDespatcher const&) = delete;
69
70         void signal_change(ChangeSignal<T, P> const& signal)
71         {
72                 if (_suspended) {
73                         boost::mutex::scoped_lock lm(_mutex);
74                         _pending.push_back(signal);
75                 } else {
76                         signal.thing->signal_change(signal.type, signal.property);
77                 }
78         }
79
80         void suspend()
81         {
82                 boost::mutex::scoped_lock lm(_mutex);
83                 _suspended = true;
84         }
85
86         void resume()
87         {
88                 boost::mutex::scoped_lock lm(_mutex);
89                 auto pending = _pending;
90                 lm.unlock();
91
92                 for (auto signal: pending) {
93                         signal.thing->signal_change(signal.type, signal.property);
94                 }
95
96                 lm.lock();
97                 _pending.clear();
98                 _suspended = false;
99         }
100
101         static ChangeSignalDespatcher* instance()
102         {
103                 static boost::mutex _instance_mutex;
104                 static boost::mutex::scoped_lock lm(_instance_mutex);
105                 static ChangeSignalDespatcher<T, P>* _instance;
106                 if (!_instance) {
107                         _instance = new ChangeSignalDespatcher<T, P>();
108                 }
109                 return _instance;
110         }
111
112 private:
113         std::vector<ChangeSignal<T, P>> _pending;
114         bool _suspended = false;
115         boost::mutex _mutex;
116 };
117
118
119 template <class T, class P>
120 class ChangeSignaller
121 {
122 public:
123         ChangeSignaller (T* t, P p)
124                 : _thing(t)
125                 , _property(p)
126                 , _done(true)
127         {
128                 ChangeSignalDespatcher<T, P>::instance()->signal_change({_thing, _property, ChangeType::PENDING});
129         }
130
131         ~ChangeSignaller ()
132         {
133                 ChangeSignalDespatcher<T, P>::instance()->signal_change({_thing, _property, _done ? ChangeType::DONE : ChangeType::CANCELLED});
134         }
135
136         void abort ()
137         {
138                 _done = false;
139         }
140
141 private:
142         T* _thing;
143         P _property;
144         bool _done;
145 };
146
147
148 #endif