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-01-30 09:36:25 +00:00
|
|
|
// A hookable event system.
|
|
|
|
|
|
|
|
// Define Events in a header as:
|
|
|
|
//
|
|
|
|
// using MyLoveyEvent = Event<"My lovely event", std::string>;
|
|
|
|
//
|
|
|
|
// Register listeners anywhere you need them as:
|
|
|
|
// EventHook myHook = MyLoveyEvent::Register([](std::string foo) {
|
|
|
|
// // Do something
|
|
|
|
// }, "Name of the hook");
|
|
|
|
//
|
|
|
|
// The hook will be automatically unregistered when the EventHook object goes out of scope.
|
|
|
|
// Trigger events by doing:
|
|
|
|
//
|
|
|
|
// MyLoveyEvent::Trigger("Hello world");
|
|
|
|
//
|
|
|
|
|
|
|
|
struct HookBase
|
|
|
|
{
|
|
|
|
virtual ~HookBase() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
using EventHook = std::unique_ptr<HookBase>;
|
|
|
|
|
2023-01-31 04:29:16 +00:00
|
|
|
template <StringLiteral EventName, typename... CallbackArgs>
|
2023-01-30 09:36:25 +00:00
|
|
|
class Event
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
~HookImpl() override { Event::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-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.
|
|
|
|
static EventHook Register(CallbackType callback, std::string name)
|
|
|
|
{
|
2023-02-03 00:18:37 +00:00
|
|
|
std::lock_guard lock(m_mutex);
|
2023-01-30 09:36:25 +00:00
|
|
|
DEBUG_LOG_FMT(COMMON, "Registering {} handler at {} event hook", name, EventName.value);
|
2023-02-03 00:00:26 +00:00
|
|
|
auto handle = std::make_unique<HookImpl>(callback, std::move(name));
|
2023-01-30 09:36:25 +00:00
|
|
|
m_listeners.push_back(handle.get());
|
|
|
|
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-03 00:18:37 +00:00
|
|
|
std::lock_guard lock(m_mutex);
|
2023-01-30 09:36:25 +00:00
|
|
|
for (auto& handle : m_listeners)
|
|
|
|
handle->m_fn(args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void Remove(HookImpl* handle)
|
|
|
|
{
|
2023-02-03 00:18:37 +00:00
|
|
|
std::lock_guard lock(m_mutex);
|
|
|
|
std::erase(m_listeners, handle);
|
2023-01-30 09:36:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline static std::vector<HookImpl*> m_listeners = {};
|
2023-02-03 00:18:37 +00:00
|
|
|
inline static std::mutex m_mutex;
|
2023-01-30 09:36:25 +00:00
|
|
|
};
|
2023-02-03 00:18:37 +00:00
|
|
|
|
|
|
|
} // namespace Common
|