I have created simple callback based event manager and it works, but I have some errors with zero template arguments.
class event_manager
{
public:
template <typename... T>
static void register_event(const unsigned long e, std::function<void(T...)> ec)
{
events.insert({ e, ec });
}
template <typename... T>
static void fire_event(const unsigned long e, T... args)
{
for (auto it : events)
{
if (it.first == e)
{
boost::any_cast<std::function<void(T...)>>(it.second)(args...);
}
}
}
private:
static std::unordered_multimap<unsigned int, boost::any> events;
};
And I'm using this code to add callback:
event_manager::register_event<unsigned int>(DVU_EVENT_KEY_PRESSED, [](unsigned int key)
{
//Works!
});
event_manager::register_event(DVU_EVENT_IDLE, []()
{
//Could not deduce template argument
});
And the second question:
Is it possible to change code to remove <unsigned int>
-like template specification?
Example:
event_manager::register_event(DVU_EVENT_KEY_PRESSED, [](unsigned int key){}));