2023-04-17 16:18:48 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <hex/api/imhex_api.hpp>
|
impr: Refactor and restructure Event Manager (#2082)
### Problem description
This PR addresses issue #2013 that described a cluttered Event Manager.
This is a DX issue and should not impact the users whatsoever.
### Implementation description
The changes revolve around three main points:
1. the Event Manager (`event_manager.hpp`) was split into four
categories: GUI, Interaction, Lifecycle, and Provider, and two types:
Events, and Requests. This results in the following files:
- `events_gui.hpp`
- `events_interaction.hpp`
- `events_lifecycle.hpp`
- `events_provider.hpp`
- `requests_gui.hpp`
- `requests_interaction.hpp`
- `requests_lifecycle.hpp`
- `requests_provider.hpp`
2. Every event and request now has its own piece of documentation, with
a `@brief`, accompanied by a longer comment if needed, and gets its
`@param`s described.
3. The old `event_manager.hpp` import was removed and replaced by the
correct imports wherever needed, as to reduce spread of those files only
to where they are truly useful.
### Additional things
The commits have been split into (chrono-)logical steps:
- `feat`: split the Event Manager, and replace the imports
- `refactor`, `chore`: make various small changes to match the required
structure
- `docs`: add documentation for events and requests
Hopefully, this will help to review the PR.
*Note: Beware of very long rebuild times in between the commits, use
them sparingly! The Actions will ensure this PR builds anyways*
Closes #2013
---------
Signed-off-by: BioTheWolff <47079795+BioTheWolff@users.noreply.github.com>
Co-authored-by: Nik <werwolv98@gmail.com>
2025-01-25 16:32:07 +01:00
|
|
|
#include <hex/api/events/events_provider.hpp>
|
|
|
|
#include <hex/api/events/events_lifecycle.hpp>
|
|
|
|
#include <hex/api/events/requests_provider.hpp>
|
|
|
|
|
2023-04-17 16:18:48 +02:00
|
|
|
|
|
|
|
#include <map>
|
2024-01-11 20:11:22 +01:00
|
|
|
#include <ranges>
|
2023-04-17 16:18:48 +02:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2023-11-18 14:34:33 +01:00
|
|
|
namespace prv {
|
|
|
|
class Provider;
|
|
|
|
}
|
|
|
|
|
2023-04-17 16:18:48 +02:00
|
|
|
template<typename T>
|
|
|
|
class PerProvider {
|
|
|
|
public:
|
|
|
|
PerProvider() { this->onCreate(); }
|
|
|
|
PerProvider(const PerProvider&) = delete;
|
|
|
|
PerProvider(PerProvider&&) = delete;
|
|
|
|
PerProvider& operator=(const PerProvider&) = delete;
|
2023-11-18 14:34:33 +01:00
|
|
|
PerProvider& operator=(PerProvider &&) = delete;
|
2023-04-17 16:18:48 +02:00
|
|
|
|
2023-06-02 10:47:18 +02:00
|
|
|
~PerProvider() { this->onDestroy(); }
|
2023-04-17 16:18:48 +02:00
|
|
|
|
|
|
|
T* operator->() {
|
|
|
|
return &this->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
const T* operator->() const {
|
|
|
|
return &this->get();
|
|
|
|
}
|
|
|
|
|
2024-02-25 14:30:56 +01:00
|
|
|
T& get(const prv::Provider *provider = ImHexApi::Provider::get()) {
|
2023-12-19 13:10:25 +01:00
|
|
|
return m_data[provider];
|
2023-04-17 16:18:48 +02:00
|
|
|
}
|
|
|
|
|
2024-02-25 14:30:56 +01:00
|
|
|
const T& get(const prv::Provider *provider = ImHexApi::Provider::get()) const {
|
2023-12-24 00:06:16 +01:00
|
|
|
return m_data.at(provider);
|
2023-04-17 16:18:48 +02:00
|
|
|
}
|
|
|
|
|
2024-02-25 14:30:56 +01:00
|
|
|
void set(const T &data, const prv::Provider *provider = ImHexApi::Provider::get()) {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_data[provider] = data;
|
2023-06-08 17:02:16 +02:00
|
|
|
}
|
|
|
|
|
2024-02-25 14:30:56 +01:00
|
|
|
void set(T &&data, const prv::Provider *provider = ImHexApi::Provider::get()) {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_data[provider] = std::move(data);
|
2023-06-08 17:02:16 +02:00
|
|
|
}
|
|
|
|
|
2023-04-17 16:18:48 +02:00
|
|
|
T& operator*() {
|
|
|
|
return this->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
const T& operator*() const {
|
|
|
|
return this->get();
|
|
|
|
}
|
|
|
|
|
2023-06-08 17:02:16 +02:00
|
|
|
PerProvider& operator=(const T &data) {
|
|
|
|
this->set(data);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PerProvider& operator=(T &&data) {
|
|
|
|
this->set(std::move(data));
|
2023-04-17 16:18:48 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator T&() {
|
|
|
|
return this->get();
|
|
|
|
}
|
|
|
|
|
2024-01-11 20:11:22 +01:00
|
|
|
auto all() {
|
|
|
|
return m_data | std::views::values;
|
|
|
|
}
|
|
|
|
|
2024-03-15 21:08:03 +01:00
|
|
|
void setOnCreateCallback(std::function<void(prv::Provider *, T&)> callback) {
|
2024-01-11 20:11:22 +01:00
|
|
|
m_onCreateCallback = std::move(callback);
|
|
|
|
}
|
|
|
|
|
2024-05-17 21:56:43 +02:00
|
|
|
void setOnDestroyCallback(std::function<void(prv::Provider *, T&)> callback) {
|
|
|
|
m_onDestroyCallback = std::move(callback);
|
|
|
|
}
|
|
|
|
|
2023-04-17 16:18:48 +02:00
|
|
|
private:
|
|
|
|
void onCreate() {
|
2023-12-08 10:29:44 +01:00
|
|
|
EventProviderOpened::subscribe(this, [this](prv::Provider *provider) {
|
2024-01-11 20:11:22 +01:00
|
|
|
auto [it, inserted] = m_data.emplace(provider, T());
|
|
|
|
auto &[key, value] = *it;
|
|
|
|
if (m_onCreateCallback)
|
2024-03-15 21:08:03 +01:00
|
|
|
m_onCreateCallback(provider, value);
|
2023-04-17 16:18:48 +02:00
|
|
|
});
|
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
EventProviderDeleted::subscribe(this, [this](prv::Provider *provider){
|
2024-05-17 21:56:43 +02:00
|
|
|
if (auto it = m_data.find(provider); it != m_data.end()) {
|
|
|
|
if (m_onDestroyCallback)
|
|
|
|
m_onDestroyCallback(provider, m_data.at(provider));
|
|
|
|
|
|
|
|
m_data.erase(it);
|
|
|
|
}
|
2023-04-17 16:18:48 +02:00
|
|
|
});
|
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
EventImHexClosing::subscribe(this, [this] {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_data.clear();
|
2023-04-17 16:18:48 +02:00
|
|
|
});
|
2023-08-26 01:47:44 +02:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// Moves the data of this PerProvider instance from one provider to another
|
2023-12-08 10:29:44 +01:00
|
|
|
MovePerProviderData::subscribe(this, [this](prv::Provider *from, prv::Provider *to) {
|
2023-10-04 12:00:32 +02:00
|
|
|
// Get the value from the old provider, (removes it from the map)
|
2023-08-26 01:47:44 +02:00
|
|
|
auto node = m_data.extract(from);
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// Ensure the value existed
|
2023-08-26 01:47:44 +02:00
|
|
|
if (node.empty()) return;
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// Delete the value from the new provider, that we want to replace
|
2023-12-19 13:10:25 +01:00
|
|
|
m_data.erase(to);
|
2023-08-26 01:47:44 +02:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// Re-insert it with the key of the new provider
|
2023-08-26 01:47:44 +02:00
|
|
|
node.key() = to;
|
2023-12-19 13:10:25 +01:00
|
|
|
m_data.insert(std::move(node));
|
2023-08-26 01:47:44 +02:00
|
|
|
});
|
2023-04-17 16:18:48 +02:00
|
|
|
}
|
|
|
|
|
2023-06-02 10:47:18 +02:00
|
|
|
void onDestroy() {
|
2024-05-17 21:56:43 +02:00
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
EventProviderOpened::unsubscribe(this);
|
|
|
|
EventProviderDeleted::unsubscribe(this);
|
|
|
|
EventImHexClosing::unsubscribe(this);
|
2023-12-27 11:33:04 +01:00
|
|
|
MovePerProviderData::unsubscribe(this);
|
2023-06-02 10:47:18 +02:00
|
|
|
}
|
|
|
|
|
2023-04-17 16:18:48 +02:00
|
|
|
private:
|
2024-02-25 14:30:56 +01:00
|
|
|
std::map<const prv::Provider *, T> m_data;
|
2024-05-17 21:56:43 +02:00
|
|
|
std::function<void(prv::Provider *, T&)> m_onCreateCallback, m_onDestroyCallback;
|
2023-04-17 16:18:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|