Using boost::signal2 for generic event handling with overloaded methods

Hi People, I'm studying boost to understand if I can remove from a project a homemade event-dispatcher with boost::signal2 but I found a lot of troubles and I cannot solve it. My actual code which I would migrate to boost is made by a "Sender" and "Multiple Receiver" The sender has the ability to register many "signals" (I use a map for do this" ) and each listener has a map of "slots" connected to the sender of the sender. The sender code is: struct Event {}; struct ID {}; struct OneSecondElapsedEvent : public Event { static ID id; }; struct UpdatePresentationTracksReqEvent : public Event { UpdatePresentationTracksReqEvent() {}; static ID id; }; typedef boost::signals2::signal<void (Event*)> signal_t; typedef signal_t::slot_type slot_t; class MainTask: public Elt::Thread , public Sender { boost::ptr_map<ID*, signal_t> signals_map; public: sig; MainTask(char* taskName); virtual ~MainTask(); void Main(); void registerListener(IListener* listener) { if(listener != NULL) { SlotMap::const_iterator it = listener->slots_map.begin(); for(;it != listener->slots_map.end();it++) { ID *id = it->first; const slot_t *handler = it->second; signals_map[id].connect(*handler); } } else printf("Listener NULLO!!!!\n"); } }; now my generic "listener" or Receiver should be able to register to sender and the code is: #include "SpActiveContext.h" #include <cassert> #include "ActiveObject.h" #include "GenericMessageHandler/ActiveMessage.h" #include "boost/bind.hpp" #include "boost/function.hpp" #include <stdio.h> #include "EventDefinitions.h" #include "MainTask.h" SpActiveContext::SpActiveContext(SpContext * component): _ActiveObject("SpActiveContext"), _Component(component){ assert(_Component); * //boost::function<void (OneSecondElapsedEvent*)> g = boost::bind(&SpActiveContext::Handle, this, _1);* * * * //const slot_t& t = static_cast<void (SpActiveContext::*)(OneSecondElapsedEvent* )>(&SpActiveContext::Handle);* * //registerEvent(OneSecondElapsedEvent::id, t);* * * * //typedef boost::signals2::signal<void (Event*)> signal_t;* * //typedef signal_t::slot_type slot_t;* * * * //boost::bind(&SpActiveContext::Handle(OneSecondElapsedEvent*), this, _1); * * * * //void (SpActiveContext::*Handle) (OneSecondElapsedEvent*) = &SpActiveContext::Handle;* * * * //boost::bind( static_cast<void (SpActiveContext::*)( int )>(&Foo::Bar) , foo, _1);* * * * * * //const slot_t1& t = boost::bind( static_cast<void (SpActiveContext::*)(OneSecondElapsedEvent*)>(&SpActiveContext::Handle) , this, _1);* * //registerEvent(OneSecondElapsedEvent::id, t );* * * * //const slot_t1& t = boost::bind( static_cast<void (SpActiveContext::*)( UpdatePresentationTracksReqEvent *)>(&SpActiveContext::Handle) , this, _1); ** //registerEvent(UpdatePresentationTracksReqEvent ::id, t ); * } void SpActiveContext::Handle(OneSecondElapsedEvent* msg) { printf("OneSecondElapsedEvent 2\n"); } void SpActiveContext::Handle(UpdatePresentationTracksReqEvent* msg) { printf("UpdatePresentationTracksReqEvent\n"); } In the constructor I tried to "connect" the function "Handle(*OneSecondElapsedEvent*)" to the slot but I cannot to this..the compiler write errors sayng that cannot connvert from "Event*" to "OneSecondElapsedEvent*".* * * *Is there any solution to make it working?* * * *Thanks in advance,* *AlexGiul* * *

<...>
struct Event {}; struct ID {}; struct OneSecondElapsedEvent : public Event { static ID id; }; struct UpdatePresentationTracksReqEvent : public Event { UpdatePresentationTracksReqEvent() {}; static ID id; }; typedef boost::signals2::signal<void (Event*)> signal_t; typedef signal_t::slot_type slot_t; <...> In the constructor I tried to "connect" the function "Handle(OneSecondElapsedEvent*)" to the slot but I cannot to this..the compiler write errors sayng that cannot connvert from "Event*" to "OneSecondElapsedEvent*". Is there any solution to make it working?
So, long story short, you try to connect a slot with signature <void(Derived *)> to a signal<void(Base *)>, don't you? This won't work, of course.
participants (2)
-
Alessandro Giulianelli
-
Igor R