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>
|
|
|
|
#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
|
|
|
|
|
|
|
PerProvider(T data) : m_data({ { ImHexApi::Provider::get(), std::move(data) } }) { this->onCreate(); }
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
T& get(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
|
|
|
}
|
|
|
|
|
|
|
|
const T& get(prv::Provider *provider = ImHexApi::Provider::get()) const {
|
2023-12-19 13:10:25 +01:00
|
|
|
return m_data[provider];
|
2023-04-17 16:18:48 +02:00
|
|
|
}
|
|
|
|
|
2023-06-08 17:02:16 +02:00
|
|
|
void set(const T &data, 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
|
|
|
}
|
|
|
|
|
|
|
|
void set(T &&data, 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void onCreate() {
|
2023-12-08 10:29:44 +01:00
|
|
|
EventProviderOpened::subscribe(this, [this](prv::Provider *provider) {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_data.emplace(provider, T());
|
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-06-02 10:47:18 +02:00
|
|
|
}
|
|
|
|
|
2023-04-17 16:18:48 +02:00
|
|
|
private:
|
|
|
|
std::map<prv::Provider *, T> m_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|