| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <algorithm> |
| 7 |
#include <functional> |
| 8 |
#include <memory> |
| 9 |
#include <mutex> |
| 10 |
#include <vector> |
| 11 |
|
| 12 |
#include "Common/Logging/Log.h" |
| 13 |
|
| 14 |
namespace Common |
| 15 |
{ |
| 16 |
struct HookBase |
| 17 |
{ |
| 18 |
|
| 19 |
virtual ~HookBase() = 0; |
| 20 |
}; |
| 21 |
inline HookBase::~HookBase() = default; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
using EventHook = std::unique_ptr<HookBase>; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
template <typename... CallbackArgs> |
| 47 |
class HookableEvent |
| 48 |
{ |
| 49 |
public: |
| 50 |
using CallbackType = std::function<void(CallbackArgs...)>; |
| 51 |
|
| 52 |
|
| 53 |
[[nodiscard]] EventHook Register(CallbackType callback) |
| 54 |
{ |
| 55 |
DEBUG_LOG_FMT(COMMON, "Registering event hook handler"); |
| 56 |
std::lock_guard lg(m_storage->listeners_mutex); |
| 57 |
|
| 58 |
auto& new_listener = |
| 59 |
m_storage->listeners.emplace_back(std::make_unique<Listener>(std::move(callback))); |
| 60 |
|
| 61 |
return std::make_unique<HookImpl>(m_storage, new_listener.get()); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
void Trigger(const CallbackArgs&... args) |
| 69 |
{ |
| 70 |
std::lock_guard lg(m_storage->listeners_mutex); |
| 71 |
m_storage->is_triggering = true; |
| 72 |
|
| 73 |
|
| 74 |
for (std::size_t i = 0; i != m_storage->listeners.size(); ++i) |
| 75 |
{ |
| 76 |
auto& listener = m_storage->listeners[i]; |
| 77 |
|
| 78 |
if (listener->is_pending_removal) |
| 79 |
continue; |
| 80 |
|
| 81 |
std::invoke(listener->callback, args...); |
| 82 |
} |
| 83 |
|
| 84 |
m_storage->is_triggering = false; |
| 85 |
std::erase_if(m_storage->listeners, std::mem_fn(&Listener::is_pending_removal)); |
| 86 |
} |
| 87 |
|
| 88 |
private: |
| 89 |
struct Listener |
| 90 |
{ |
| 91 |
const CallbackType callback; |
| 92 |
bool is_pending_removal{}; |
| 93 |
}; |
| 94 |
|
| 95 |
struct Storage |
| 96 |
{ |
| 97 |
std::recursive_mutex listeners_mutex; |
| 98 |
std::vector<std::unique_ptr<Listener>> listeners; |
| 99 |
bool is_triggering{}; |
| 100 |
}; |
| 101 |
|
| 102 |
struct HookImpl final : HookBase |
| 103 |
{ |
| 104 |
HookImpl(std::weak_ptr<Storage> storage, Listener* listener) |
| 105 |
: weak_storage{std::move(storage)}, listener_ptr{listener} |
| 106 |
{ |
| 107 |
} |
| 108 |
|
| 109 |
~HookImpl() override |
| 110 |
{ |
| 111 |
const auto storage = weak_storage.lock(); |
| 112 |
if (storage == nullptr) |
| 113 |
{ |
| 114 |
DEBUG_LOG_FMT(COMMON, "Handler outlived event hook"); |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
DEBUG_LOG_FMT(COMMON, "Removing event hook handler"); |
| 119 |
|
| 120 |
std::lock_guard lg(storage->listeners_mutex); |
| 121 |
|
| 122 |
if (storage->is_triggering) |
| 123 |
{ |
| 124 |
|
| 125 |
|
| 126 |
listener_ptr->is_pending_removal = true; |
| 127 |
} |
| 128 |
else |
| 129 |
{ |
| 130 |
|
| 131 |
storage->listeners.erase(std::ranges::find_if( |
| 132 |
storage->listeners, [&](auto& ptr) { return ptr.get() == listener_ptr; })); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
const std::weak_ptr<Storage> weak_storage; |
| 137 |
Listener* const listener_ptr; |
| 138 |
}; |
| 139 |
|
| 140 |
|
| 141 |
std::shared_ptr<Storage> m_storage{std::make_shared<Storage>()}; |
| 142 |
}; |
| 143 |
|
| 144 |
} |