Listen to an Object

Hi all,

I’m still trying to work out listeners and some of the Poco stuff. I know how to make an object listen to the main app, but i would also like to be able to do the opposite: make the main app listen listen to an object i’ve created and then call a function when that object updates its state.

any advice on how to do this? has anyone done this in their code yet?

thanks.
Drosen

I’m not very familiar with the Poco system… but I’d imagine a basic implementation would be something like…

  
  
class Observable{  
private:  
  bool updated;  
  
public:  
  bool isUpdated(){return this->updated;}  
  void clearUpdateFlag(){this->updated = false;}  
};  
  

and everytime you change your data (ie. in your setter methods, you set the updated flag to true)

Then in your main app’s update() or draw() method (depending how you’re using the data), you’d do something like.

  
  
if(myObservable->isUpdated()){  
  doSomething(myObservable);  
  myObservable->clearUpdateFlag;  
}  
  

Just a guess… I mean there could be something built in with Poco. I thought that automatically the main app listened for any poco events sent from any object.

oh, didn’t see this till now…

to create your own events and listen for them in 0.06 you’ll do something like:

  
  
class myClass{  
  
ofEvent<float> myFloatEvent;  
  
void notifyingMethod(){  
     float a=5;  
     ofNotifyEvent(myFloatEvent,a);  
}  
}  

testApp:

  
  
myClass object;  
  
void setup(){  
    ofAddListener(object.myFloatEvent,this,&testApp::floatMethod);  
}  
  
void floatMethod(float & a){  
    ...  
}  

thanks Arturo + plong0, that looks great. Is that something new to 0.06 or is it already there in 057?

That is 006 event code. Should be released soon. Things are coming together pretty well.

Hello.
I’m sorry for bringing this up again, but I’m having one question:
How would I implement a Listener, if the event happens in testApp? So in my code the class to be notified would be created in testApp, but the notifiedClass has also to know about the class-definition of testApp.

  
  
testApp:  
class testApp{  
notifiedClass theNotifiedClass;  
ofEvent<float> myFloatEvent;  
  
void notifyingMethod(){  
     float a=5;  
     ofNotifyEvent(myFloatEvent,a);  
}  
}  

and in notifiedClass:

  
  
testApp object; //<-that's the problem  
  
void setup(){  
    ofAddListener(object.myFloatEvent,this,&notifiedClass::floatMethod);  
}  
  
void floatMethod(float & a){  
    ...  
}  

I guess the problem has something to do with c++ inheritance? But how to work around such a scenario?

Thanks and regards,
Gestalt

perhaps

  
  
//testApp object;  
  
void setup( ofEvent<float> &eventToListenFor ){  
    ofAddListener( eventToListenFor,this,&notifiedClass::floatMethod);  
}  
  
void floatMethod(float & a){  
    ...  
}  
  

and in testApp:

  
//notifiedObject.setup();  
notifiedObject.setup( myFloatEvent );