-
Notifications
You must be signed in to change notification settings - Fork 0
/
subject.h
55 lines (43 loc) · 1.06 KB
/
subject.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
#ifndef SUBJECT_H
#define SUBJECT_H
#include "observer.h"
#include <QVector>
template<typename ... Args>
class Subject
{
public:
using ObserverType = Observer<Args...>;
using ObserverFunction = typename ObserverType::Function;
Subject() = default;
~Subject()
{
reset();
}
ObserverPointer createObserver(ObserverFunction func)
{
return ObserverPointer(new ObserverType(this, func));
}
void notify(Args ...args)
{
for (auto observer : m_observers)
observer->notify(args...);
}
void reset()
{
for (auto observer : m_observers)
observer->reset();
}
void attach(ObserverType *observer)
{
m_observers.append(observer);
}
void detach(ObserverType *observer)
{
m_observers.removeAll(observer);
}
private:
QVector<ObserverType *> m_observers;
};
#define DECLARE_SUBJECT(subject, funcName) \
ObserverPointer funcName(decltype(subject)::ObserverFunction func) { return subject.createObserver(func); }
#endif // SUBJECT_H