-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathobserver.h
246 lines (213 loc) · 6.06 KB
/
observer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* Copyright (C) 2004-2021 Savoir-faire Linux Inc.
*
* Author: Guillaume Roguez <Guillaume.Roguez@savoirfairelinux.com>
* Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
#include "noncopyable.h"
#include <cstdlib>
#include <cstdint>
#include <memory>
#include <set>
#include <list>
#include <mutex>
#include <functional>
#include <ciso646> // fix windows compiler bug
#include "logger.h"
namespace jami {
template<typename T>
class Observer;
template<typename T>
class Observable;
/*=== Observable =============================================================*/
template<typename T>
class Observable
{
public:
Observable()
: mutex_()
, observers_()
{}
/**
* @brief ~Observable
* Detach all observers to avoid making them call this observable when
* destroyed
*/
virtual ~Observable()
{
std::lock_guard<std::mutex> lk(mutex_);
for (auto& pobs : priority_observers_) {
if (auto so = pobs.lock()) {
so->detached(this);
}
}
for (auto& o : observers_)
o->detached(this);
}
bool attach(Observer<T>* o)
{
std::lock_guard<std::mutex> lk(mutex_);
if (o and observers_.insert(o).second) {
o->attached(this);
return true;
}
return false;
}
void attachPriorityObserver(std::shared_ptr<Observer<T>> o)
{
std::lock_guard<std::mutex> lk(mutex_);
priority_observers_.push_back(o);
o->attached(this);
}
void detachPriorityObserver(Observer<T>* o)
{
std::lock_guard<std::mutex> lk(mutex_);
for (auto it = priority_observers_.begin(); it != priority_observers_.end(); ++it) {
if (auto so = it->lock()) {
if (so.get() == o) {
priority_observers_.erase(it);
}
}
}
}
bool detach(Observer<T>* o)
{
std::lock_guard<std::mutex> lk(mutex_);
if (o and observers_.erase(o)) {
o->detached(this);
return true;
}
return false;
}
int getObserversCount()
{
std::lock_guard<std::mutex> lk(mutex_);
return observers_.size();
}
protected:
void notify(T data)
{
std::lock_guard<std::mutex> lk(mutex_);
for (auto it = priority_observers_.begin(); it != priority_observers_.end();) {
if (auto so = it->lock()) {
try {
so->update(this, data);
++it;
} catch (std::exception& e) {
JAMI_ERR() << e.what();
}
} else {
it = priority_observers_.erase(it);
}
}
for (auto observer : observers_) {
observer->update(this, data);
}
}
private:
NON_COPYABLE(Observable<T>);
protected:
std::mutex mutex_; // lock observers_
std::list<std::weak_ptr<Observer<T>>> priority_observers_;
std::set<Observer<T>*> observers_;
};
template<typename T>
class PublishObservable : public Observable<T>
{
public:
void publish(T data) { this->notify(data); }
};
/*=== Observer =============================================================*/
template<typename T>
class Observer
{
public:
virtual ~Observer() {}
virtual void update(Observable<T>*, const T&) = 0;
virtual void attached(Observable<T>*) {}
virtual void detached(Observable<T>*) {}
};
template<typename T>
class FuncObserver : public Observer<T>
{
public:
using F = std::function<void(const T&)>;
FuncObserver(F f)
: f_(f)
{}
virtual ~FuncObserver() {}
void update(Observable<T>*, const T& t) override { f_(t); }
private:
F f_;
};
/*=== PublishMapSubject ====================================================*/
template<typename T1, typename T2>
class PublishMapSubject : public Observer<T1>, public Observable<T2>
{
public:
using F = std::function<T2(const T1&)>;
PublishMapSubject(F f)
: map_ {f}
{}
void update(Observable<T1>*, const T1& t) override
{
std::lock_guard<std::mutex> lk(this->mutex_);
for (const auto& observer : this->observers_) {
observer->update(this, map_(t));
}
}
/**
* @brief attached
* Here we just make sure that the PublishMapSubject is only attached to one
* Observable at a time.
* @param srcObs
*/
virtual void attached(Observable<T1>* srcObs) override
{
if (obs_ != nullptr && obs_ != srcObs) {
obs_->detach(this);
obs_ = srcObs;
}
}
/**
* @brief detached
* Since a MapSubject is only attached to one Observable, when detached
* We should detach all of it observers
*/
virtual void detached(Observable<T1>*) override
{
std::lock_guard<std::mutex> lk(this->mutex_);
JAMI_WARN() << "PublishMapSubject: detaching observers";
for (auto& o : this->observers_)
o->detached(this);
}
/**
* @brief ~PublishMapSubject()
* Detach all observers to avoid making them call this observable when
* destroyed
**/
~PublishMapSubject()
{
JAMI_WARN() << "~PublishMapSubject()";
detached(nullptr);
}
private:
F map_;
Observable<T1>* obs_ = nullptr;
};
}; // namespace jami