How to get the *sender from a dispatched event?

From what I understand, if you have an event dispatched, it sends to all listeners (not just the class you assigned to). So am I correct in that if we want to only have the function respond to ‘it’s’ event (not all events) we can check who the sender is and see if it matches the one in the class (if there’s multiple classes)? I’m trying to figure out how to make an event only respond to it’s own dispatch, not all of them.

For example, if there’s a timer event in class “Ball” and each ball needs it’s own independent timer complete event (not the same event), how do we check for which timer dispatched the event if the event is dispatched to all Ball instances?


In this thread: http://forum.openframeworks.cc/t/openframeworkspoco-events-not-being-listened/4361/0

Arturo mentions you can get the sender for an event using:

  
void functionName(ofEventArgs & args, void * sender);  

However, if I try that I get a compile error on the listener saying “no matching function for call to…”:

  
ofAddListener(EventName, this, &MyClass::functionName);  

How can we get the sender in the event function or is there a different/better way to have a class respond to it’s own event (not all events of its type)?

1 Like

Ok, looks like it was my own error:

void * sender should be the first parameter:

  
void functionName(void * sender, ofEventArgs & args);  

and events seem to be working correctly. It was just my error before. :slight_smile:

I think i have same problem but i don’t resolve it. :slight_smile:
I have this method:

  
  
void testApp::hitBordersHandler(void* sender, BallHitEventArgs &e);  
  

And i have this listener:

  
  
ofAddListener(ball->hitBorder, this, &testApp::hitBordersHandler);  
  

and i have this error “No matching function for call to ‘ofAddListener’”. Do you know why?
Maybe i don’t understand completely difference from two different way to use ofAddListener and how many parameters i have to pass. :-\

can you post how you’ve declared hitBorder

  
  
ofEvent<BallHitEventArgs>   hitBorder;  
  

And this is the class for BallHitEventArgs:

  
  
#pragma once  
  
#include "Ball.h"  
  
class Ball;  
  
class BallHitEventArgs : ofEventArgs {  
public:  
      
	string                  side;  
	Ball*                   ball;  
      
};  
  
  

that seems correct to me can you post the files

Here is my project:
http://www.mauroferrario.com/blog/file/eventsExample.zip

Mauro, did you ever figure this out? I’m having a similar problem and I’m not sure where to go from here…

  
//--------------------------------------------------------------  
class EventClass  
{  
    public:  
        ofEvent<int> myEvent;  
        void notifyEvent();  
};  
  
//--------------------------------------------------------------  
class ListenerClass  
{  
    public:  
        ListenerClass();  
        void eventCallback(void* _sender, int& _i);  
        EventClass* eventClass;  
};  

  
//--------------------------------------------------------------  
void EventClass::notifyEvent()  
{  
    int i = 1;  // dummy int, because ofNotifyEvent(...) needs an argument  
    ofNotifyEvent(myEvent, i, this);  
}  
  
//--------------------------------------------------------------  
ListenerClass::ListenerClass()  
{  
    eventClass = new EventClass();  
  
    // ERROR: "No matching function for call to 'ofAddListener'  
    ofAddListener(eventClass->myEvent, this, &ListenerClass::eventCallback);  
}  
  
//--------------------------------------------------------------  
void ListenerClass::eventCallback(void* _sender, int& _i)  
{  
    // do whatever...  
}  

Thanks!

Maybe eventClass can’t be a pointer?

the correct syntax for the callback is:

  
  
void ListenerClass::eventCallback(const void* _sender, int& _i)   
  

Sweet, adding “const” did the trick. Thanks Arturo!

How to convert sender to the original class type:

  
CLASS_TYPE * myObj = static_cast<CLASS_TYPE*>(const_cast<void*>(_sender));  
myObj->myMethod();  

PS: Sorry for writing in a old post, but it was usefull to me.

2 Likes