api: Improved Provider registering api, hide implementation functions better

This commit is contained in:
WerWolv 2021-12-12 11:52:58 +01:00
parent 821eb4568e
commit b2a9965617
7 changed files with 90 additions and 79 deletions

View File

@ -2,8 +2,6 @@
#include <hex/providers/provider.hpp>
#include "content/providers/file_provider.hpp"
#include <set>
#include <string>
#include <vector>

View File

@ -23,7 +23,7 @@ namespace hex::plugin::builtin {
private:
struct InspectorCacheEntry {
std::string unlocalizedName;
ContentRegistry::DataInspector::DisplayFunction displayFunction;
ContentRegistry::DataInspector::impl::DisplayFunction displayFunction;
};
bool m_shouldInvalidate = true;

View File

@ -1,5 +1,4 @@
#include <hex/api/content_registry.hpp>
#include <hex/api/event.hpp>
#include "content/providers/gdb_provider.hpp"
#include "content/providers/file_provider.hpp"
@ -8,41 +7,11 @@
namespace hex::plugin::builtin {
void registerProviders() {
ContentRegistry::Provider::add("hex.builtin.provider.gdb");
ContentRegistry::Provider::add("hex.builtin.provider.disk");
(void) EventManager::subscribe<RequestCreateProvider>([](const std::string &unlocalizedName, hex::prv::Provider **provider){
if (unlocalizedName != "hex.builtin.provider.file") return;
ContentRegistry::Provider::add<prv::GDBProvider>("hex.builtin.provider.file", false);
ContentRegistry::Provider::add<prv::GDBProvider>("hex.builtin.provider.gdb");
ContentRegistry::Provider::add<prv::DiskProvider>("hex.builtin.provider.disk");
auto newProvider = new prv::FileProvider();
hex::ImHexApi::Provider::add(newProvider);
if (provider != nullptr)
*provider = newProvider;
});
(void) EventManager::subscribe<RequestCreateProvider>([](const std::string &unlocalizedName, hex::prv::Provider **provider){
if (unlocalizedName != "hex.builtin.provider.gdb") return;
auto newProvider = new prv::GDBProvider();
hex::ImHexApi::Provider::add(newProvider);
if (provider != nullptr)
*provider = newProvider;
});
(void) EventManager::subscribe<RequestCreateProvider>([](const std::string &unlocalizedName, hex::prv::Provider **provider){
if (unlocalizedName != "hex.builtin.provider.disk") return;
auto newProvider = new prv::DiskProvider();
hex::ImHexApi::Provider::add(newProvider);
if (provider != nullptr)
*provider = newProvider;
});
}
}

View File

@ -3,6 +3,8 @@
#include <hex.hpp>
#include <hex/helpers/concepts.hpp>
#include <hex/pattern_language/token.hpp>
#include <hex/api/imhex_api.hpp>
#include <hex/api/event.hpp>
#include <functional>
#include <map>
@ -100,11 +102,17 @@ namespace hex {
/* View Registry. Allows adding of new windows */
namespace Views {
void add(View *view);
namespace impl {
void add(View *view);
}
template<hex::derived_from<View> T, typename ... Args>
void add(Args&& ... args) {
return add(new T(std::forward<Args>(args)...));
return impl::add(new T(std::forward<Args>(args)...));
}
std::vector<View*>& getEntries();
@ -113,16 +121,21 @@ namespace hex {
/* Tools Registry. Allows adding new entries to the tools window */
namespace Tools {
using Callback = std::function<void()>;
struct Entry {
std::string name;
Callback function;
};
namespace impl {
void add(const std::string &unlocalizedName, const Callback &function);
using Callback = std::function<void()>;
std::vector<Entry>& getEntries();
struct Entry {
std::string name;
Callback function;
};
}
void add(const std::string &unlocalizedName, const impl::Callback &function);
std::vector<impl::Entry>& getEntries();
}
/* Data Inspector Registry. Allows adding of new types to the data inspector */
@ -134,35 +147,45 @@ namespace hex {
Octal
};
using DisplayFunction = std::function<std::string()>;
using GeneratorFunction = std::function<DisplayFunction(const std::vector<u8>&, std::endian, NumberDisplayStyle)>;
namespace impl {
struct Entry {
std::string unlocalizedName;
size_t requiredSize;
GeneratorFunction generatorFunction;
};
using DisplayFunction = std::function<std::string()>;
using GeneratorFunction = std::function<DisplayFunction(const std::vector<u8>&, std::endian, NumberDisplayStyle)>;
void add(const std::string &unlocalizedName, size_t requiredSize, GeneratorFunction function);
struct Entry {
std::string unlocalizedName;
size_t requiredSize;
impl::GeneratorFunction generatorFunction;
};
std::vector<Entry>& getEntries();
}
void add(const std::string &unlocalizedName, size_t requiredSize, impl::GeneratorFunction function);
std::vector<impl::Entry>& getEntries();
}
/* Data Processor Node Registry. Allows adding new processor nodes to be used in the data processor */
namespace DataProcessorNode {
using CreatorFunction = std::function<dp::Node*()>;
struct Entry {
std::string category;
std::string name;
CreatorFunction creatorFunction;
};
namespace impl {
using CreatorFunction = std::function<dp::Node*()>;
struct Entry {
std::string category;
std::string name;
CreatorFunction creatorFunction;
};
void add(const Entry &entry);
}
void add(const Entry &entry);
template<hex::derived_from<dp::Node> T, typename ... Args>
void add(const std::string &unlocalizedCategory, const std::string &unlocalizedName, Args&& ... args) {
add(Entry{ unlocalizedCategory.c_str(), unlocalizedName.c_str(),
add(impl::Entry{ unlocalizedCategory.c_str(), unlocalizedName.c_str(),
[=]{
auto node = new T(std::forward<Args>(args)...);
node->setUnlocalizedName(unlocalizedName);
@ -173,7 +196,7 @@ namespace hex {
void addSeparator();
std::vector<Entry>& getEntries();
std::vector<impl::Entry>& getEntries();
}
@ -202,7 +225,28 @@ namespace hex {
/* Provider Registry. Allows adding new data providers to be created from the UI */
namespace Provider {
void add(const std::string &unlocalizedName);
namespace impl {
void addProviderName(const std::string &unlocalizedName);
}
template<hex::derived_from<hex::prv::Provider> T>
void add(const std::string &unlocalizedName, bool addToList = true) {
(void) EventManager::subscribe<RequestCreateProvider>([expectedName = unlocalizedName](const std::string &name, hex::prv::Provider **provider){
if (name != expectedName) return;
auto newProvider = new T();
hex::ImHexApi::Provider::add(newProvider);
if (provider != nullptr)
*provider = newProvider;
});
if (addToList)
impl::addProviderName(unlocalizedName);
}
const std::vector<std::string>& getEntries();

View File

@ -62,8 +62,8 @@ namespace hex {
static std::vector<ContentRegistry::CommandPaletteCommands::Entry> commandPaletteCommands;
static std::map<std::string, ContentRegistry::PatternLanguageFunctions::Function> patternLanguageFunctions;
static std::vector<View*> views;
static std::vector<ContentRegistry::Tools::Entry> toolsEntries;
static std::vector<ContentRegistry::DataInspector::Entry> dataInspectorEntries;
static std::vector<ContentRegistry::Tools::impl::Entry> toolsEntries;
static std::vector<ContentRegistry::DataInspector::impl::Entry> dataInspectorEntries;
static u32 patternPaletteOffset;
static std::string popupMessage;
static std::list<ImHexApi::Bookmarks::Entry> bookmarkEntries;
@ -79,7 +79,7 @@ namespace hex {
static std::vector<std::string> providerNames;
static std::vector<ContentRegistry::DataProcessorNode::Entry> dataProcessorNodes;
static std::vector<ContentRegistry::DataProcessorNode::impl::Entry> dataProcessorNodes;
static u32 dataProcessorNodeIdCounter;
static u32 dataProcessorLinkIdCounter;
static u32 dataProcessorAttrIdCounter;

View File

@ -184,7 +184,7 @@ namespace hex {
/* Views */
void ContentRegistry::Views::add(View *view) {
void ContentRegistry::Views::impl::add(View *view) {
getEntries().emplace_back(view);
}
@ -196,27 +196,27 @@ namespace hex {
/* Tools */
void ContentRegistry::Tools:: add(const std::string &unlocalizedName, const std::function<void()> &function) {
getEntries().emplace_back(Entry{ unlocalizedName, function });
getEntries().emplace_back(impl::Entry{ unlocalizedName, function });
}
std::vector<ContentRegistry::Tools::Entry>& ContentRegistry::Tools::getEntries() {
std::vector<ContentRegistry::Tools::impl::Entry>& ContentRegistry::Tools::getEntries() {
return SharedData::toolsEntries;
}
/* Data Inspector */
void ContentRegistry::DataInspector::add(const std::string &unlocalizedName, size_t requiredSize, ContentRegistry::DataInspector::GeneratorFunction function) {
void ContentRegistry::DataInspector::add(const std::string &unlocalizedName, size_t requiredSize, ContentRegistry::DataInspector::impl::GeneratorFunction function) {
getEntries().push_back({ unlocalizedName, requiredSize, std::move(function) });
}
std::vector<ContentRegistry::DataInspector::Entry>& ContentRegistry::DataInspector::getEntries() {
std::vector<ContentRegistry::DataInspector::impl::Entry>& ContentRegistry::DataInspector::getEntries() {
return SharedData::dataInspectorEntries;
}
/* Data Processor Nodes */
void ContentRegistry::DataProcessorNode::add(const Entry &entry) {
void ContentRegistry::DataProcessorNode::impl::add(const impl::Entry &entry) {
getEntries().push_back(entry);
}
@ -224,7 +224,7 @@ namespace hex {
getEntries().push_back({ "", "", []{ return nullptr; } });
}
std::vector<ContentRegistry::DataProcessorNode::Entry>& ContentRegistry::DataProcessorNode::getEntries() {
std::vector<ContentRegistry::DataProcessorNode::impl::Entry>& ContentRegistry::DataProcessorNode::getEntries() {
return SharedData::dataProcessorNodes;
}
@ -273,7 +273,7 @@ namespace hex {
/* Providers */
void ContentRegistry::Provider::add(const std::string &unlocalizedName) {
void ContentRegistry::Provider::impl::addProviderName(const std::string &unlocalizedName) {
SharedData::providerNames.push_back(unlocalizedName);
}

View File

@ -14,8 +14,8 @@ namespace hex {
std::vector<ContentRegistry::CommandPaletteCommands::Entry> SharedData::commandPaletteCommands;
std::map<std::string, ContentRegistry::PatternLanguageFunctions::Function> SharedData::patternLanguageFunctions;
std::vector<View*> SharedData::views;
std::vector<ContentRegistry::Tools::Entry> SharedData::toolsEntries;
std::vector<ContentRegistry::DataInspector::Entry> SharedData::dataInspectorEntries;
std::vector<ContentRegistry::Tools::impl::Entry> SharedData::toolsEntries;
std::vector<ContentRegistry::DataInspector::impl::Entry> SharedData::dataInspectorEntries;
u32 SharedData::patternPaletteOffset;
std::string SharedData::popupMessage;
std::list<ImHexApi::Bookmarks::Entry> SharedData::bookmarkEntries;
@ -31,7 +31,7 @@ namespace hex {
std::vector<std::string> SharedData::providerNames;
std::vector<ContentRegistry::DataProcessorNode::Entry> SharedData::dataProcessorNodes;
std::vector<ContentRegistry::DataProcessorNode::impl::Entry> SharedData::dataProcessorNodes;
u32 SharedData::dataProcessorNodeIdCounter = 1;
u32 SharedData::dataProcessorLinkIdCounter = 1;
u32 SharedData::dataProcessorAttrIdCounter = 1;