I have a template class with two parameters:
template<class TEvent, class TData>
class EventPool {
public:
using EventObserver = std::function<void(const TData &)>;
using EventData = TData;
using EventType = TEvent;
public:
EventObserverId observe(TEvent event, EventObserver &&observer) { ... }
void deleteObserver(EventObserverId observerId) { ... }
void dispatch(TEvent event, const TData &data) { ... }
};
I also have another container class that stores many pools:
class EventSystem {
using CollisionPool = EventPool<CollisionEvent, CollisionData>;
using MouseButtonPool = EventPool<MouseButtonEvent, MouseButtonData>;
using KeyboardPool = EventPool<KeyboardButtonEvent, KeyboardButtonData>;
public:
private:
std::tuple<CollisionPool, MouseButtonPool, KeyboardPool> mPools;
};
Now, I want to create a template based function that automatically infers the correct pool and calls the functions inside the pool. For example:
template<class TEvent>
EventObserverId observe(TEvent event, DeriveThePoolHere::EventObserver &&observer) {
auto &pool = getPoolByFromEventType<TEvent>();
return pool.observe(event, observer);
}
template<class TEvent>
void deleteObserver(TEvent event, EventObserverId observerId) {
auto &pool = getPoolFromEevntType<TEvent>();
pool.deleteObserver(observerId);
}
void dispatch(TEvent event, const DeriveThePoolHere::EventData &data) {
auto &pool = getPoolFromEventType<TEvent>();
pool.dispatch(event, data);
}
If I can somehow create a type map that maps an event to the event data, I can solve this problem but I am not sure how to do this in C++.