Skip to content

Commit 7d10a73

Browse files
authored
ofEvents::did_notify() (#7773)
1 parent cb8d7af commit 7d10a73

File tree

6 files changed

+798
-729
lines changed

6 files changed

+798
-729
lines changed

examples/events/notifyEventExample/bin/data/.gitkeep

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "ofApp.h"
2+
#include "ofMain.h"
3+
4+
int main() {
5+
6+
ofGLWindowSettings settings;
7+
auto window = ofCreateWindow(settings);
8+
ofRunApp(window, std::make_shared<ofApp>());
9+
ofRunMainLoop();
10+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "ofApp.h"
2+
3+
void ofApp::update() {
4+
5+
// you can check the state of a specific ofParameter
6+
// (wrapper around it's own "hidden Event")
7+
// no listeners/callbacks are required
8+
9+
if (size.didNotify()) {
10+
ofLogNotice("new size") << size;
11+
}
12+
13+
if (refresh.didNotify()) {
14+
color = ofColor::fromHsb(ofRandom(360), 128, 128);
15+
ofLogNotice("boosch") << color;
16+
}
17+
18+
if (animate.didNotify()) { // this is the ofAbstractParameter::did_notify()
19+
if (animate != animating) { // sanity check
20+
animating = animate;
21+
if (animating) {
22+
ofLogNotice("stop animation");
23+
} else {
24+
ofLogNotice("begin animation");
25+
size = 0;
26+
}
27+
}
28+
}
29+
30+
if (auto what = params.parameterChangedE().did_notify()) { // this is the Events::did_notify()
31+
// something happended in params, but you don't know what
32+
}
33+
}
34+
35+
void ofApp::draw() {
36+
ofBackground(0);
37+
if (animating) {
38+
ofSetColor(color);
39+
ofDrawCircle(ofGetWidth() / 2, ofGetHeight() / 2, size);
40+
size = size + 1;
41+
if (size > 100) size -= 100;
42+
}
43+
ofDrawBitmapString("<a> to toggle automation", 10, 10);
44+
ofDrawBitmapString("<s> to randomize size", 10, 20);
45+
ofDrawBitmapString("<c> to change color", 10, 30);
46+
}
47+
48+
void ofApp::keyPressed(int key) {
49+
if (key == 'a') animate = !animate;
50+
if (key == 'r') size = ofRandom(50, 100);
51+
if (key == 'c') refresh.trigger();
52+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
3+
#include "ofMain.h"
4+
5+
// note: this example, inspired by simpleEvents, is kept voluntarily
6+
// as small as possible to give a sense of the bare minimum requirements
7+
8+
class ofApp : public ofBaseApp {
9+
10+
ofParameter<bool> animate { "animate", false };
11+
ofParameter<void> refresh { "refresh" };
12+
ofParameter<float> size { "size", 1.0f, 0.0f, 100.0f };
13+
ofParameterGroup params { "myCallbacksAndSettings", animate, refresh, size };
14+
15+
bool animating { false };
16+
ofColor color { ofColor::white };
17+
18+
public:
19+
void update() override;
20+
void draw() override;
21+
void keyPressed(int key) override;
22+
};

libs/openFrameworks/events/ofEvent.h

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -563,33 +563,38 @@ class ofEvent: public of::priv::BaseEvent<of::priv::Function<T,Mutex>,Mutex>{
563563
ofEvent<T,Mutex>::self->remove(*make_function(std::function<typename of::priv::callable_traits<TFunction>::function_type>(function), priority)->id);
564564
}
565565

566-
inline bool notify(const void* sender, T & param){
567-
if(ofEvent<T,Mutex>::self->enabled && !ofEvent<T,Mutex>::self->functions.empty()){
568-
std::unique_lock<Mutex> lck(ofEvent<T,Mutex>::self->mtx);
569-
auto functions_copy = ofEvent<T,Mutex>::self->functions;
570-
lck.unlock();
571-
for(auto & f: functions_copy){
572-
if(f->notify(sender,param)){
573-
return true;
574-
}
575-
}
566+
/// \brief checks the state of the event
567+
/// \returns true if the Event's state was notified since the last check
568+
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;
576574
}
577-
return false;
578575
}
576+
std::atomic<bool> notified_ { false };
579577

580-
inline bool notify(T & param){
581-
if(ofEvent<T,Mutex>::self->enabled && !ofEvent<T,Mutex>::self->functions.empty()){
582-
std::unique_lock<Mutex> lck(ofEvent<T,Mutex>::self->mtx);
583-
auto functions_copy = ofEvent<T,Mutex>::self->functions;
584-
lck.unlock();
585-
for(auto & f: functions_copy){
586-
if(f->notify(nullptr,param)){
587-
return true;
578+
inline bool notify(const void* sender, T & param) {
579+
if (ofEvent<T,Mutex>::self->enabled) {
580+
notified_.store(true, std::memory_order_relaxed);
581+
if (!ofEvent<T,Mutex>::self->functions.empty()) {
582+
std::unique_lock<Mutex> lck(ofEvent<T,Mutex>::self->mtx);
583+
auto functions_copy = ofEvent<T,Mutex>::self->functions;
584+
lck.unlock();
585+
for (auto & f: functions_copy) {
586+
if (f->notify(sender,param)) {
587+
return true;
588+
}
588589
}
589590
}
590591
}
591592
return false;
592593
}
594+
595+
inline bool notify(T & param){
596+
return this->notify(nullptr, param);
597+
}
593598
};
594599

595600

@@ -721,32 +726,37 @@ class ofEvent<void,Mutex>: public of::priv::BaseEvent<of::priv::Function<void,Mu
721726
ofEvent<void,Mutex>::self->remove(*make_function(std::function<typename of::priv::callable_traits<TFunction>::function_type>(function),priority)->id);
722727
}
723728

729+
/// \brief checks the state of the event
730+
/// \returns true if the Event's state was notified since the last check
731+
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+
}
738+
}
739+
std::atomic<bool> notified_;
740+
724741
bool notify(const void* sender){
725-
if(ofEvent<void,Mutex>::self->enabled && !ofEvent<void,Mutex>::self->functions.empty()){
726-
std::unique_lock<Mutex> lck(ofEvent<void,Mutex>::self->mtx);
727-
auto functions_copy = ofEvent<void,Mutex>::self->functions;
728-
lck.unlock();
729-
for(auto & f: functions_copy){
730-
if(f->notify(sender)){
731-
return true;
742+
if(ofEvent<void,Mutex>::self->enabled) {
743+
notified_.store(true, std::memory_order_relaxed);
744+
if (!ofEvent<void,Mutex>::self->functions.empty()) {
745+
std::unique_lock<Mutex> lck(ofEvent<void,Mutex>::self->mtx);
746+
auto functions_copy = ofEvent<void,Mutex>::self->functions;
747+
lck.unlock();
748+
for (auto & f: functions_copy) {
749+
if (f->notify(sender)) {
750+
return true;
751+
}
732752
}
733753
}
734754
}
735755
return false;
736756
}
737757

738758
bool notify(){
739-
if(ofEvent<void,Mutex>::self->enabled && !ofEvent<void,Mutex>::self->functions.empty()){
740-
std::unique_lock<Mutex> lck(ofEvent<void,Mutex>::self->mtx);
741-
auto functions_copy = ofEvent<void,Mutex>::self->functions;
742-
lck.unlock();
743-
for(auto & f: functions_copy){
744-
if(f->notify(nullptr)){
745-
return true;
746-
}
747-
}
748-
}
749-
return false;
759+
return this->notify(nullptr);
750760
}
751761
};
752762

0 commit comments

Comments
 (0)