I would like to have "yet another" callback registration stuff. Different event types extending a common base event type will trigger associated callback functions.
here is the initial draft or idea
#include <functional>
#include <iostream>
#include <unordered_map>
class BaseEvent
{
public:
virtual ~BaseEvent() {}
};
class DerivedEvent_1 : public BaseEvent {};
class DerivedEvent_2 : public BaseEvent {};
// a container holding callback functions
std::unordered_map<size_t/*event*/, std::function<void(BaseEvent)>/*callback*/> _callbacks;
// register callback funtion associated with specific event
template<typename EVT>
void registerCallback(std::function<void(EVT)> cb)
{
std::cout << "store callback associated with event " << typeid(EVT).name() << " [" << typeid(EVT).hash_code() << "]" << std::endl;
//_functions[ typeid(EVT).hash_code() ] = cb; // FIXME
}
// trigger callback function
void triggerCallback(const BaseEvent* e)
{
std::cout << "trigger callback with event " << typeid(*e).name() << " [" << typeid(*e).hash_code() << "]" << std::endl;
//_functions[ typeid(*e).hash_code() ] (*e); // FIXME
}
// callback function for DerivedEvent_1
void callback_1(DerivedEvent_1 event_1)
{
std::cout << "callback_1 called" << std::endl;
}
// callback function for DerivedEvent_2
void callback_2(DerivedEvent_2 event_2)
{
std::cout << "callback_2 called" << std::endl;
}
int main(int argc, char *argv[])
{
registerCallback<DerivedEvent_1>( [](DerivedEvent_1 e) { callback_1(e); } );
registerCallback<DerivedEvent_2>( [](DerivedEvent_2 e) { callback_2(e); } );
DerivedEvent_1 e1;
DerivedEvent_2 e2;
triggerCallback(&e1);
triggerCallback(&e2);
return 1;
}
so far so good without real implementation...
$ g++ -std=c++11 -o testStdFunction testStdFunvtion.cpp
$ ./testStdFunction
store callback associated with event 14DerivedEvent_1 [4527193776]
store callback associated with event 14DerivedEvent_2 [4527193680]
trigger callback with event 14DerivedEvent_1 [4527193776]
trigger callback with event 14DerivedEvent_2 [4527193680]
The situations and questions are:
- Events are class or struct which can have specific attributes as payload
- I would like to keep callback functions (e.g. void callback_1(DerivedEvent_1 event_1) without pointer as argument. Reason: those callback functions may already exist in code base and I would like to not change it or make extra wrapper.
- how can I let _callbacks map to have std::function with different signatures? i.e. to let the _callbacks map can hold std::function
The intention is to fix the FIXME code in registerCallback and triggerCallback. so they will look like this after running the code
$ g++ -std=c++11 -o testStdFunction testStdFunvtion.cpp
$ ./testStdFunction
store callback associated with event 14DerivedEvent_1 [4527193776]
store callback associated with event 14DerivedEvent_2 [4527193680]
trigger callback with event 14DerivedEvent_1 [4527193776]
callback_1 called
trigger callback with event 14DerivedEvent_2 [4527193680]
callback_2 called