@@ -49,35 +49,29 @@ namespace lars{
49
49
};
50
50
51
51
using HandlerList = std::vector<StoredHandler>;
52
- using EventPointer = std::shared_ptr<const Event *>;
53
- using WeakEventPointer = std::weak_ptr<const Event *>;
54
52
55
- mutable HandlerID IDCounter = 0 ;
56
- mutable HandlerList observers;
57
- mutable std::mutex observerMutex;
58
- EventPointer self;
53
+ struct Data {
54
+ HandlerID IDCounter = 0 ;
55
+ HandlerList observers;
56
+ std::mutex observerMutex;
57
+ };
58
+
59
+ std::shared_ptr<Data> data;
59
60
60
61
HandlerID addHandler (Handler h)const {
61
- std::lock_guard<std::mutex> lock (observerMutex);
62
- observers.emplace_back (StoredHandler{IDCounter,h});
63
- return IDCounter++;
64
- }
65
-
66
- void eraseHandler (const HandlerID &id)const {
67
- std::lock_guard<std::mutex> lock (observerMutex);
68
- auto it = std::find_if (observers.begin (), observers.end (), [&](auto &o){ return o.id == id; });
69
- if (it != observers.end ()) { observers.erase (it); }
62
+ std::lock_guard<std::mutex> lock (data->observerMutex );
63
+ data->observers .emplace_back (StoredHandler{data->IDCounter ,h});
64
+ return data->IDCounter ++;
70
65
}
71
-
66
+
72
67
public:
73
68
74
69
struct Observer :public lars ::Observer::Base{
75
- WeakEventPointer parent ;
70
+ std::weak_ptr<Data> data ;
76
71
HandlerID id;
77
72
78
73
Observer (){}
79
- Observer (const EventPointer & _parent, HandlerID _id):parent(_parent), id(_id){
80
- }
74
+ Observer (const std::weak_ptr<Data> &_data, HandlerID _id):data(_data), id(_id){}
81
75
82
76
Observer (Observer &&other) = default ;
83
77
Observer (const Observer &other) = delete ;
@@ -86,64 +80,58 @@ namespace lars{
86
80
Observer & operator =(Observer &&other)=default ;
87
81
88
82
void observe (const Event &event, const Handler &handler){
83
+ reset ();
89
84
*this = event.createObserver (handler);
90
85
}
91
86
92
87
void reset (){
93
- if (auto p = parent.lock ()){
94
- (*p)->eraseHandler (id);
88
+ if (auto d = data.lock ()){
89
+ std::lock_guard<std::mutex> lock (d->observerMutex );
90
+ auto it = std::find_if (d->observers .begin (), d->observers .end (), [&](auto &o){ return o.id == id; });
91
+ if (it != d->observers .end ()) { d->observers .erase (it); }
95
92
}
96
- parent .reset ();
93
+ data .reset ();
97
94
}
98
95
99
96
~Observer (){ reset (); }
100
97
};
101
98
102
- Event ():self(std::make_shared<const Event *>(this )){
103
-
99
+ Event ():data(std::make_shared<Data>()){
104
100
}
105
-
106
- Event (const Event &) = delete ;
107
-
108
- Event (Event &&other){
109
- *this = std::move (other);
110
- }
111
-
112
- Event & operator =(const Event &) = delete ;
113
101
114
- Event & operator =( Event &&other){
115
- self = std::move (other. self );
116
- *self = this ;
117
- observers = std::move ( other. observers );
118
- IDCounter = other.IDCounter ;
102
+ Event ( const Event &) = delete ;
103
+ Event (Event &&other):Event(){ * this = std::move (other); }
104
+ Event & operator =( const Event &) = delete ;
105
+ Event & operator =(Event && other){
106
+ std::swap (data, other.data ) ;
119
107
return *this ;
120
108
}
121
-
109
+
122
110
void emit (Args ... args) const {
123
- observerMutex.lock ();
124
- auto tmpObservers = observers;
125
- observerMutex.unlock ();
111
+ data-> observerMutex .lock ();
112
+ auto tmpObservers = data-> observers ;
113
+ data-> observerMutex .unlock ();
126
114
for (auto &observer: tmpObservers){
127
115
observer.callback (args...);
128
116
}
129
117
}
130
118
131
119
Observer createObserver (const Handler &h)const {
132
- return Observer (self , addHandler (h));
120
+ return Observer (data , addHandler (h));
133
121
}
134
122
135
123
void connect (const Handler &h)const {
136
124
addHandler (h);
137
125
}
138
126
139
127
void clearObservers (){
140
- std::lock_guard<std::mutex> lock (observerMutex);
141
- observers.clear ();
128
+ std::lock_guard<std::mutex> lock (data-> observerMutex );
129
+ data-> observers .clear ();
142
130
}
143
131
144
132
size_t observerCount () const {
145
- std::lock_guard<std::mutex> lock (observerMutex);
146
- return observers.size ();
133
+ std::lock_guard<std::mutex> lock (data-> observerMutex );
134
+ return data-> observers .size ();
147
135
}
148
136
149
137
};
0 commit comments