2023-01-30 09:36:25 +00:00
|
|
|
// Copyright 2023 Dolphin Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/StringLiteral.h"
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
2023-02-03 00:18:37 +00:00
|
|
|
#include <mutex>
|
2023-01-30 09:36:25 +00:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-02-03 00:18:37 +00:00
|
|
|
namespace Common
|
|
|
|
{
|
2023-02-03 01:24:26 +00:00
|
|
|
struct HookBase
|
|
|
|
{
|
|
|
|
virtual ~HookBase() = default;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
HookBase() = default;
|
2023-01-30 09:36:25 +00:00
|
|
|
|
2023-02-03 01:24:26 +00:00
|
|
|
// This shouldn't be copied. And since we always wrap it in unique_ptr, no need to move it either
|
|
|
|
HookBase(const HookBase&) = delete;
|
|
|
|
HookBase(HookBase&&) = delete;
|
|
|
|
HookBase& operator=(const HookBase&) = delete;
|
|
|
|
HookBase& operator=(HookBase&&) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
// EventHook is a handle a registered listener holds.
|
|
|
|
// When the handle is destroyed, the HookableEvent will automatically remove the listener.
|
|
|
|
using EventHook = std::unique_ptr<HookBase>;
|
|
|
|
|
|
|
|
// A hookable event system.
|
|
|
|
//
|
2023-01-30 09:36:25 +00:00
|
|
|
// Define Events in a header as:
|
|
|
|
//
|
2023-02-03 01:24:26 +00:00
|
|
|
// using MyLoveyEvent = HookableEvent<"My lovely event", std::string, u32>;
|
2023-01-30 09:36:25 +00:00
|
|
|
//
|
|
|
|
// Register listeners anywhere you need them as:
|
2023-02-03 01:24:26 +00:00
|
|
|
// EventHook myHook = MyLoveyEvent::Register([](std::string foo, u32 bar) {
|
|
|
|
// fmt::print("I've been triggered with {} and {}", foo, bar)
|
|
|
|
// }, "NameOfHook");
|
2023-01-30 09:36:25 +00:00
|
|
|
//
|
|
|
|
// The hook will be automatically unregistered when the EventHook object goes out of scope.
|
2023-02-03 01:24:26 +00:00
|
|
|
// Trigger events by calling Trigger as:
|
2023-01-30 09:36:25 +00:00
|
|
|
//
|
2023-02-03 01:24:26 +00:00
|
|
|
// MyLoveyEvent::Trigger("Hello world", 42);
|
2023-01-30 09:36:25 +00:00
|
|
|
//
|
2023-01-31 04:29:16 +00:00
|
|
|
template <StringLiteral EventName, typename... CallbackArgs>
|
2023-02-03 00:37:42 +00:00
|
|
|
class HookableEvent
|
2023-01-30 09:36:25 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
using CallbackType = std::function<void(CallbackArgs...)>;
|
|
|
|
|
|
|
|
private:
|
2023-02-03 00:18:37 +00:00
|
|
|
struct HookImpl final : public HookBase
|
2023-01-30 09:36:25 +00:00
|
|
|
{
|
2023-02-03 00:37:42 +00:00
|
|
|
~HookImpl() override { HookableEvent::Remove(this); }
|
2023-02-03 00:18:37 +00:00
|
|
|
HookImpl(CallbackType callback, std::string name)
|
|
|
|
: m_fn(std::move(callback)), m_name(std::move(name))
|
|
|
|
{
|
|
|
|
}
|
2023-01-30 09:36:25 +00:00
|
|
|
CallbackType m_fn;
|
|
|
|
std::string m_name;
|
|
|
|
};
|
|
|
|
|
2023-02-13 05:48:43 +00:00
|
|
|
struct Storage
|
|
|
|
{
|
2023-04-02 22:51:06 +00:00
|
|
|
std::recursive_mutex m_mutex;
|
2023-02-13 05:48:43 +00:00
|
|
|
std::vector<HookImpl*> m_listeners;
|
|
|
|
};
|
|
|
|
|
|
|
|
// We use the "Construct On First Use" idiom to avoid the static initialization order fiasco.
|
|
|
|
// https://isocpp.org/wiki/faq/ctors#static-init-order
|
|
|
|
static Storage& GetStorage()
|
|
|
|
{
|
|
|
|
static Storage storage;
|
|
|
|
return storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Remove(HookImpl* handle)
|
|
|
|
{
|
|
|
|
auto& storage = GetStorage();
|
|
|
|
std::lock_guard lock(storage.m_mutex);
|
|
|
|
|
|
|
|
std::erase(storage.m_listeners, handle);
|
|
|
|
}
|
|
|
|
|
2023-01-31 04:29:16 +00:00
|
|
|
public:
|
2023-01-30 09:36:25 +00:00
|
|
|
// Returns a handle that will unregister the listener when destroyed.
|
2023-03-05 11:05:30 +00:00
|
|
|
[[nodiscard]] static EventHook Register(CallbackType callback, std::string name)
|
2023-01-30 09:36:25 +00:00
|
|
|
{
|
2023-02-13 05:48:43 +00:00
|
|
|
auto& storage = GetStorage();
|
|
|
|
std::lock_guard lock(storage.m_mutex);
|
2023-02-03 01:24:26 +00:00
|
|
|
|
2023-01-30 09:36:25 +00:00
|
|
|
DEBUG_LOG_FMT(COMMON, "Registering {} handler at {} event hook", name, EventName.value);
|
2024-01-31 18:16:48 +00:00
|
|
|
auto handle = std::make_unique<HookImpl>(std::move(callback), std::move(name));
|
2023-02-13 05:48:43 +00:00
|
|
|
storage.m_listeners.push_back(handle.get());
|
2023-01-30 09:36:25 +00:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2023-02-03 00:00:26 +00:00
|
|
|
static void Trigger(const CallbackArgs&... args)
|
2023-01-30 09:36:25 +00:00
|
|
|
{
|
2023-02-13 05:48:43 +00:00
|
|
|
auto& storage = GetStorage();
|
|
|
|
std::lock_guard lock(storage.m_mutex);
|
2023-02-03 01:24:26 +00:00
|
|
|
|
2023-02-13 05:48:43 +00:00
|
|
|
for (const auto& handle : storage.m_listeners)
|
2023-01-30 09:36:25 +00:00
|
|
|
handle->m_fn(args...);
|
|
|
|
}
|
|
|
|
};
|
2023-02-03 00:18:37 +00:00
|
|
|
|
|
|
|
} // namespace Common
|