2023-04-17 16:18:48 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <hex/api/imhex_api.hpp>
|
2023-11-18 14:50:43 +01:00
|
|
|
#include <hex/api/event_manager.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);
|
|
|
|
}
|
|
|
|
|
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){
|
2023-12-19 13:10:25 +01:00
|
|
|
m_data.erase(provider);
|
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() {
|
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-03-15 21:08:03 +01:00
|
|
|
std::function<void(prv::Provider *, T&)> m_onCreateCallback;
|
2023-04-17 16:18:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|