35 #define CALLBACK_HOOK_INVALID_IDX 0x0
53 template<
class R,
class... ArgTypes>
70 while (m_callbacks.find(
id) != m_callbacks.end())
75 m_callbacks[
id] = callback;
86 void add_callback(
const std::string&
name,
const std::function<R(ArgTypes...)>& callback)
88 auto it = m_name_to_id_map.find(
name);
89 if (it != m_name_to_id_map.end())
91 remove_callback(it->second);
93 m_name_to_id_map[
name] = add_callback(callback);
103 auto it = m_callbacks.find(
id);
104 if (it != m_callbacks.end())
106 m_callbacks.erase(it);
107 for (
auto it2 = m_name_to_id_map.begin(); it2 != m_name_to_id_map.end(); it2++)
109 if ((*it2).second ==
id)
111 m_name_to_id_map.erase(it2);
125 auto it = m_name_to_id_map.find(
id);
126 if (it != m_name_to_id_map.end())
128 remove_callback((*it).second);
139 if (this->
size() == 0)
143 for (
const auto& i : m_callbacks)
157 R
inline call(
const u64 idx, ArgTypes... args)
159 auto it = m_callbacks.find(idx);
160 if (it == m_callbacks.end())
164 return (it->second)(args...);
175 R
inline call(
const std::string& idx, ArgTypes... args)
177 auto it = m_name_to_id_map.find(idx);
178 if (it == m_name_to_id_map.end())
182 return call((*it).second, args...);
193 return m_callbacks.find(
id) != m_callbacks.end();
204 return m_name_to_id_map.find(
name) != m_name_to_id_map.end();
214 return m_callbacks.size();
225 for (
const auto& it : m_callbacks)
227 res.insert(it.first);
240 for (
const auto& it : m_name_to_id_map)
251 std::map<
u64, std::function<R(ArgTypes...)>> m_callbacks;
253 std::map<std::string, u64> m_name_to_id_map;
#define CALLBACK_HOOK_INVALID_IDX
u64 add_callback(const std::function< R(ArgTypes...)> &callback, u64 id=CALLBACK_HOOK_INVALID_IDX)
bool is_callback_registered(const u64 id)
void add_callback(const std::string &name, const std::function< R(ArgTypes...)> &callback)
void remove_callback(const u64 id)
std::set< u64 > get_ids()
void remove_callback(const std::string &id)
void operator()(ArgTypes... args)
std::string get_name(u64 id)
bool is_callback_registered(const std::string &name)
R call(const std::string &idx, ArgTypes... args)
R call(const u64 idx, ArgTypes... args)