I have the following simplified callback map. Please excuse me if the code contains some erros, but this is a very simplified version of the real code to reproduce it here.
struct CallbacksMap { template<typename T, typename U> void Add(T* obj, void (T::*objCallback)(const U&)) { CallbackBaseType* c = new CallbackType<T, U>(obj, objCallback); _callbacks[std::type_index(typeid(U))].push_back(std::unique_ptr<CallbackBaseType>(c)); } template<typename T> void Remove(T* obj){...} template<typename T> void Call(const T& param) { std::vector<std::unique_ptr<T>>& callbacks = _callbacks[std::type_index(typeid(T))]; for(auto callback = callbacks.begin(); callback != callbacks.end(); ++callback) { (*callback)->Call(¶m); } } std::map<std::type_index, std::vector<std::unique_ptr<CallbackBaseType>>> _callbacks; };
In this example, I can call all functions with the same parameter type by calling the Call(param) member function. My problem is that _callbacks performed at runtime, even if the key is known at compile time.
I cannot make the callback list static local for the template function based on the type_index of this type, because I need to track all the objects for the Remove(T* obj) function Remove(T* obj) .
Do you know how I can get the internal structure to avoid this overhead at runtime?
source share