Skip to content

Commit 5f92c76

Browse files
authored
Data handling (#11)
* move data into shared_ptr * v2.0.2 * remove default constructor * fix move constructor * reset when observing new event
1 parent d3fbd8a commit 5f92c76

File tree

3 files changed

+36
-48
lines changed

3 files changed

+36
-48
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
33
# ---- Project ----
44

55
project(LarsEvent
6-
VERSION 2.0.1
6+
VERSION 2.0.2
77
LANGUAGES CXX
88
)
99

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ With [CPM](https://github.com/TheLartians/CPM), lars::Event can be added to your
3636
```cmake
3737
CPMAddPackage(
3838
NAME LarsEvent
39-
VERSION 2.0.1
39+
VERSION 2.0.2
4040
GIT_REPOSITORY https://github.com/TheLartians/Event.git
4141
)
4242

include/lars/event.h

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -49,35 +49,29 @@ namespace lars{
4949
};
5050

5151
using HandlerList = std::vector<StoredHandler>;
52-
using EventPointer = std::shared_ptr<const Event *>;
53-
using WeakEventPointer = std::weak_ptr<const Event *>;
5452

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;
5960

6061
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++;
7065
}
71-
66+
7267
public:
7368

7469
struct Observer:public lars::Observer::Base{
75-
WeakEventPointer parent;
70+
std::weak_ptr<Data> data;
7671
HandlerID id;
7772

7873
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){}
8175

8276
Observer(Observer &&other) = default;
8377
Observer(const Observer &other) = delete;
@@ -86,64 +80,58 @@ namespace lars{
8680
Observer & operator=(Observer &&other)=default;
8781

8882
void observe(const Event &event, const Handler &handler){
83+
reset();
8984
*this = event.createObserver(handler);
9085
}
9186

9287
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); }
9592
}
96-
parent.reset();
93+
data.reset();
9794
}
9895

9996
~Observer(){ reset(); }
10097
};
10198

102-
Event():self(std::make_shared<const Event *>(this)){
103-
99+
Event():data(std::make_shared<Data>()){
104100
}
105-
106-
Event(const Event &) = delete;
107-
108-
Event(Event &&other){
109-
*this = std::move(other);
110-
}
111-
112-
Event & operator=(const Event &) = delete;
113101

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);
119107
return *this;
120108
}
121-
109+
122110
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();
126114
for(auto &observer: tmpObservers){
127115
observer.callback(args...);
128116
}
129117
}
130118

131119
Observer createObserver(const Handler &h)const{
132-
return Observer(self, addHandler(h));
120+
return Observer(data, addHandler(h));
133121
}
134122

135123
void connect(const Handler &h)const{
136124
addHandler(h);
137125
}
138126

139127
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();
142130
}
143131

144132
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();
147135
}
148136

149137
};

0 commit comments

Comments
 (0)