Skip to content

Commit bda1575

Browse files
authored
restore default-copy-constructibility of ofEvent (#7969)
1 parent dff62dc commit bda1575

2 files changed

Lines changed: 23 additions & 22 deletions

File tree

examples/events/notifyEventExample/src/ofApp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void ofApp::update() {
2727
}
2828
}
2929

30-
if (auto what = params.parameterChangedE().did_notify()) { // this is the Events::did_notify()
30+
if (auto what = params.parameterChangedE().didNotify()) { // this is the Events::did_notify()
3131
// something happended in params, but you don't know what
3232
}
3333
}
@@ -41,7 +41,7 @@ void ofApp::draw() {
4141
if (size > 100) size -= 100;
4242
}
4343
ofDrawBitmapString("<a> to toggle automation", 10, 10);
44-
ofDrawBitmapString("<s> to randomize size", 10, 20);
44+
ofDrawBitmapString("<r> to randomize size", 10, 20);
4545
ofDrawBitmapString("<c> to change color", 10, 30);
4646
}
4747

libs/openFrameworks/events/ofEvent.h

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,20 @@ namespace priv{
205205
struct Data{
206206
Mutex mtx;
207207
std::vector<std::shared_ptr<Function>> functions;
208+
std::atomic<bool> notified_ { false };
208209
bool enabled = true;
210+
211+
bool didNotify() {
212+
if (notified_.load(std::memory_order_relaxed)) {
213+
notified_.store(false, std::memory_order_seq_cst);
214+
return true;
215+
} else {
216+
return false;
217+
}
218+
}
219+
void setNotified(bool state) {
220+
notified_.store(state, std::memory_order_relaxed);
221+
}
209222

210223
void remove(const BaseFunctionId & id){
211224
std::unique_lock<Mutex> lck(mtx);
@@ -425,9 +438,9 @@ class ofEventListeners{
425438
listeners.clear();
426439
}
427440

428-
bool empty() const {
429-
return listeners.size() == 0 ;
430-
}
441+
bool empty() const {
442+
return listeners.size() == 0 ;
443+
}
431444
private:
432445
std::deque<ofEventListener> listeners;
433446
};
@@ -566,18 +579,12 @@ class ofEvent: public of::priv::BaseEvent<of::priv::Function<T,Mutex>,Mutex>{
566579
/// \brief checks the state of the event
567580
/// \returns true if the Event's state was notified since the last check
568581
bool didNotify() {
569-
if (notified_.load(std::memory_order_relaxed)) {
570-
notified_.store(false, std::memory_order_seq_cst);
571-
return true;
572-
} else {
573-
return false;
574-
}
582+
return ofEvent<T,Mutex>::self->didNotify();
575583
}
576-
std::atomic<bool> notified_ { false };
577-
584+
578585
inline bool notify(const void* sender, T & param) {
579586
if (ofEvent<T,Mutex>::self->enabled) {
580-
notified_.store(true, std::memory_order_relaxed);
587+
ofEvent<T,Mutex>::self->setNotified(true);
581588
if (!ofEvent<T,Mutex>::self->functions.empty()) {
582589
std::unique_lock<Mutex> lck(ofEvent<T,Mutex>::self->mtx);
583590
auto functions_copy = ofEvent<T,Mutex>::self->functions;
@@ -729,18 +736,12 @@ class ofEvent<void,Mutex>: public of::priv::BaseEvent<of::priv::Function<void,Mu
729736
/// \brief checks the state of the event
730737
/// \returns true if the Event's state was notified since the last check
731738
bool didNotify() {
732-
if (notified_.load(std::memory_order_relaxed)) {
733-
notified_.store(false, std::memory_order_seq_cst);
734-
return true;
735-
} else {
736-
return false;
737-
}
739+
return ofEvent<void,Mutex>::self->didNotify();
738740
}
739-
std::atomic<bool> notified_;
740741

741742
bool notify(const void* sender){
742743
if(ofEvent<void,Mutex>::self->enabled) {
743-
notified_.store(true, std::memory_order_relaxed);
744+
ofEvent<void,Mutex>::self->setNotified(true);
744745
if (!ofEvent<void,Mutex>::self->functions.empty()) {
745746
std::unique_lock<Mutex> lck(ofEvent<void,Mutex>::self->mtx);
746747
auto functions_copy = ofEvent<void,Mutex>::self->functions;

0 commit comments

Comments
 (0)