From c26f1550706f1f674d3040ebb42d2b35d21952ff Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 28 Dec 2022 17:35:15 +0200 Subject: [PATCH] Allow new component types to be registered at runtime --- source/lib/builder.cpp | 1 - source/lib/builder.h | 4 +++- source/lib/sourcepackage.cpp | 12 ++---------- source/lib/sourcepackage.h | 23 +++++++++++++++++++++++ 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/source/lib/builder.cpp b/source/lib/builder.cpp index 53b9efb..1535c3e 100644 --- a/source/lib/builder.cpp +++ b/source/lib/builder.cpp @@ -20,7 +20,6 @@ #include "package.h" #include "plugin.h" #include "sharedlibrary.h" -#include "sourcepackage.h" #include "task.h" #include "virtualtarget.h" diff --git a/source/lib/builder.h b/source/lib/builder.h index 0455637..6dc29f0 100644 --- a/source/lib/builder.h +++ b/source/lib/builder.h @@ -12,6 +12,7 @@ #include "config.h" #include "logger.h" #include "packagemanager.h" +#include "sourcepackage.h" #include "target.h" #include "toolchain.h" #include "virtualfilesystem.h" @@ -19,7 +20,6 @@ class FileTarget; class Package; class Plugin; -class SourcePackage; /** This class ties everything else together. It also contains code for loading @@ -59,6 +59,7 @@ private: std::vector plugins; PackageManager package_manager; + SourcePackage::ComponentRegistry component_registry; Architecture native_arch; Architecture *current_arch = 0; @@ -82,6 +83,7 @@ public: void load_plugins(); PackageManager &get_package_manager() { return package_manager; } + SourcePackage::ComponentRegistry &get_component_registry() { return component_registry; } template void call_plugins(F) const; diff --git a/source/lib/sourcepackage.cpp b/source/lib/sourcepackage.cpp index dfbf16e..6261d2c 100644 --- a/source/lib/sourcepackage.cpp +++ b/source/lib/sourcepackage.cpp @@ -170,6 +170,8 @@ SourcePackage::Loader::Loader(SourcePackage &p, const Config::InputOptions *o): add("source_tarball", &Loader::source_archive); add("tarball", &Loader::tarball); add("version", &Loader::version); + + p.builder.get_component_registry().invoke_all(*this); } void SourcePackage::Loader::finish() @@ -200,16 +202,6 @@ void SourcePackage::Loader::feature(const string &n, const string &d) } } -template -void SourcePackage::Loader::component(Args... args, const string &n) -{ - if(any_of(obj.components.begin(), obj.components.end(), [&n](const Component *c){ return c->get_name()==n; })) - throw key_error(n); - C *comp = new C(obj, n, args...); - load_sub(*comp); - obj.components.push_back(comp); -} - void SourcePackage::Loader::build_info() { load_sub(obj.build_info); diff --git a/source/lib/sourcepackage.h b/source/lib/sourcepackage.h index d37ecf4..2c2e57c 100644 --- a/source/lib/sourcepackage.h +++ b/source/lib/sourcepackage.h @@ -1,8 +1,10 @@ #ifndef SOURCEPACKAGE_H_ #define SOURCEPACKAGE_H_ +#include #include #include +#include #include "buildinfo.h" #include "cache.h" #include "component.h" @@ -25,7 +27,15 @@ class SourcePackage: public Package public: class Loader: public Msp::DataFile::DerivedObjectLoader, public FeatureConditional { + friend class SourcePackage; + private: + template + struct AddComponent + { + void operator()(const std::string &n, Loader &l) const { l.add(n, &Loader::component); } + }; + const Config::InputOptions *options; public: @@ -44,6 +54,8 @@ public: void version(const std::string &); }; + using ComponentRegistry = Msp::TypeRegistry; + private: std::string version; std::string interface_version; @@ -88,4 +100,15 @@ private: void save_caches() override; }; + +template +void SourcePackage::Loader::component(Args... args, const std::string &n) +{ + if(std::any_of(obj.components.begin(), obj.components.end(), [&n](const Component *c){ return c->get_name()==n; })) + throw Msp::key_error(n); + C *comp = new C(obj, n, args...); + load_sub(*comp); + obj.components.push_back(comp); +} + #endif -- 2.45.2