1
0
mirror of synced 2025-01-31 03:53:44 +01:00

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 <hex/providers/provider.hpp>
#include "content/providers/file_provider.hpp"
#include <set> #include <set>
#include <string> #include <string>
#include <vector> #include <vector>

View File

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

View File

@ -1,5 +1,4 @@
#include <hex/api/content_registry.hpp> #include <hex/api/content_registry.hpp>
#include <hex/api/event.hpp>
#include "content/providers/gdb_provider.hpp" #include "content/providers/gdb_provider.hpp"
#include "content/providers/file_provider.hpp" #include "content/providers/file_provider.hpp"
@ -8,41 +7,11 @@
namespace hex::plugin::builtin { namespace hex::plugin::builtin {
void registerProviders() { 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){ ContentRegistry::Provider::add<prv::GDBProvider>("hex.builtin.provider.file", false);
if (unlocalizedName != "hex.builtin.provider.file") return; 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.hpp>
#include <hex/helpers/concepts.hpp> #include <hex/helpers/concepts.hpp>
#include <hex/pattern_language/token.hpp> #include <hex/pattern_language/token.hpp>
#include <hex/api/imhex_api.hpp>
#include <hex/api/event.hpp>
#include <functional> #include <functional>
#include <map> #include <map>
@ -100,11 +102,17 @@ namespace hex {
/* View Registry. Allows adding of new windows */ /* View Registry. Allows adding of new windows */
namespace Views { namespace Views {
namespace impl {
void add(View *view); void add(View *view);
}
template<hex::derived_from<View> T, typename ... Args> template<hex::derived_from<View> T, typename ... Args>
void add(Args&& ... 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(); std::vector<View*>& getEntries();
@ -113,6 +121,9 @@ namespace hex {
/* Tools Registry. Allows adding new entries to the tools window */ /* Tools Registry. Allows adding new entries to the tools window */
namespace Tools { namespace Tools {
namespace impl {
using Callback = std::function<void()>; using Callback = std::function<void()>;
struct Entry { struct Entry {
@ -120,9 +131,11 @@ namespace hex {
Callback function; Callback function;
}; };
void add(const std::string &unlocalizedName, const Callback &function); }
std::vector<Entry>& getEntries(); 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 */ /* Data Inspector Registry. Allows adding of new types to the data inspector */
@ -134,24 +147,31 @@ namespace hex {
Octal Octal
}; };
namespace impl {
using DisplayFunction = std::function<std::string()>; using DisplayFunction = std::function<std::string()>;
using GeneratorFunction = std::function<DisplayFunction(const std::vector<u8>&, std::endian, NumberDisplayStyle)>; using GeneratorFunction = std::function<DisplayFunction(const std::vector<u8>&, std::endian, NumberDisplayStyle)>;
struct Entry { struct Entry {
std::string unlocalizedName; std::string unlocalizedName;
size_t requiredSize; size_t requiredSize;
GeneratorFunction generatorFunction; impl::GeneratorFunction generatorFunction;
}; };
void add(const std::string &unlocalizedName, size_t requiredSize, GeneratorFunction function); }
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 */ /* Data Processor Node Registry. Allows adding new processor nodes to be used in the data processor */
namespace DataProcessorNode { namespace DataProcessorNode {
namespace impl {
using CreatorFunction = std::function<dp::Node*()>; using CreatorFunction = std::function<dp::Node*()>;
struct Entry { struct Entry {
std::string category; std::string category;
std::string name; std::string name;
@ -160,9 +180,12 @@ namespace hex {
void add(const Entry &entry); void add(const Entry &entry);
}
template<hex::derived_from<dp::Node> T, typename ... Args> template<hex::derived_from<dp::Node> T, typename ... Args>
void add(const std::string &unlocalizedCategory, const std::string &unlocalizedName, Args&& ... 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)...); auto node = new T(std::forward<Args>(args)...);
node->setUnlocalizedName(unlocalizedName); node->setUnlocalizedName(unlocalizedName);
@ -173,7 +196,7 @@ namespace hex {
void addSeparator(); 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 */ /* Provider Registry. Allows adding new data providers to be created from the UI */
namespace Provider { 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(); const std::vector<std::string>& getEntries();

View File

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

View File

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

View File

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