From 3ca7fa124296a39de60676a27f33f545b36f1474 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 29 Sep 2015 00:26:13 +0100 Subject: Use AdHoc plugins for options sources --- project2/cgi/testCgi.cpp | 10 +++++----- project2/common/optionsSource.cpp | 16 ++++++++-------- project2/common/optionsSource.h | 8 ++------ project2/console/p2consoleMain.cpp | 3 ++- project2/daemon/p2daemonMain.cpp | 4 ++-- project2/files/optionsSource.cpp | 14 +++++++++++--- 6 files changed, 30 insertions(+), 25 deletions(-) diff --git a/project2/cgi/testCgi.cpp b/project2/cgi/testCgi.cpp index 4ab13d1..b61a475 100644 --- a/project2/cgi/testCgi.cpp +++ b/project2/cgi/testCgi.cpp @@ -27,14 +27,14 @@ class TestInput : public cgicc::CgiInput, public CgiEnvInput { typedef std::map OptStore; TestInput(int argc, char ** argv) { - OptionsSources::Add("_1", new FileOptions(".testCgi.settings")); - OptionsSources::Add("_2", new CommandLineArguments(argc, argv, - [](const char * url) { urls.push_back(url); })); + AdHoc::PluginManager::getDefault()->add(new FileOptions(".testCgi.settings"), "_1", __FILE__, __LINE__); + AdHoc::PluginManager::getDefault()->add( + new CommandLineArguments(argc, argv, [](const char * url) { urls.push_back(url); }), "_2", __FILE__, __LINE__); } ~TestInput() { - OptionsSources::Remove("_1"); - OptionsSources::Remove("_2"); + AdHoc::PluginManager::getDefault()->remove("_1"); + AdHoc::PluginManager::getDefault()->remove("_2"); } std::string getenv(const std::string & varName) const diff --git a/project2/common/optionsSource.cpp b/project2/common/optionsSource.cpp index c74243d..1739ed5 100644 --- a/project2/common/optionsSource.cpp +++ b/project2/common/optionsSource.cpp @@ -1,6 +1,6 @@ #include "optionsSource.h" #include "logger.h" -#include "instanceStore.impl.h" +#include class DefaultConfigConsumer : public ConfigConsumer { public: @@ -26,19 +26,19 @@ boost::posix_time::ptime OptionsSource::loadedTime = boost::posix_time::special_ void OptionsSource::loadSources(const Options::CurrentPlatform & platform) { - const auto & configs = InstanceSet::GetAll(); - if (std::find_if(configs.begin(), configs.end(), [](const OptionsSourcePtr & c) { return c->modifiedTime() > loadedTime; }) != configs.end()) { + const auto & configs = AdHoc::PluginManager::getDefault()->getAll(); + if (std::find_if(configs.begin(), configs.end(), [](boost::shared_ptr> c) { return c->implementation()->modifiedTime() > loadedTime; }) != configs.end()) { for (auto opts : AdHoc::PluginManager::getDefault()->getAll()) { opts->implementation()->reset(); } DefaultConfigConsumer dcc; - std::set> loadedConfigs; + std::set loadedConfigs; while (loadedConfigs.size() != configs.size()) { for (const auto & c : configs) { - if (loadedConfigs.find(c) == loadedConfigs.end()) { - c->loadInto(dcc, platform); - loadedConfigs.insert(c); + if (loadedConfigs.find(c->implementation()) == loadedConfigs.end()) { + c->implementation()->loadInto(dcc, platform); + loadedConfigs.insert(c->implementation()); } } } @@ -64,5 +64,5 @@ OptionsSource::loadSource(const Options::CurrentPlatform & platform, OptionsSour Logger()->messagebf(LOG_DEBUG, "Loaded configuration at %s", loadedTime); } -INSTANTIATESTORE(std::string, OptionsSource); +INSTANTIATEPLUGINOF(OptionsSource); diff --git a/project2/common/optionsSource.h b/project2/common/optionsSource.h index 7f1deee..32c43ed 100644 --- a/project2/common/optionsSource.h +++ b/project2/common/optionsSource.h @@ -18,7 +18,7 @@ class OptionsSource; typedef boost::shared_ptr OptionsSourcePtr; /// Base class of things that load options -class OptionsSource { +class OptionsSource : public AdHoc::AbstractPluginImplementation { public: virtual ~OptionsSource() = default; @@ -32,12 +32,8 @@ class OptionsSource { static boost::posix_time::ptime loadedTime; }; -typedef PluginsSameBase OptionsSources; #define DECLARE_OPTIONSSOURCE(Id, Inst) \ - static void init_optionsSource() __attribute__ ((constructor(200))); \ - static void init_optionsSource() { OptionsSources::Add(Id, Inst); } \ - static void kill_optionsSource() __attribute__ ((destructor(200))); \ - static void kill_optionsSource() { OptionsSources::Remove(Id); } + NAMEDPLUGIN(Id, Inst, OptionsSource) #endif diff --git a/project2/console/p2consoleMain.cpp b/project2/console/p2consoleMain.cpp index 62f09b0..87a3293 100644 --- a/project2/console/p2consoleMain.cpp +++ b/project2/console/p2consoleMain.cpp @@ -6,7 +6,8 @@ int main(int argc, char ** argv) { - OptionsSources::Add("", new CommandLineArguments(argc, argv, &ConsoleApplicationEngine::appendScript)); + AdHoc::PluginManager::getDefault()->add( + new CommandLineArguments(argc, argv, &ConsoleApplicationEngine::appendScript), "", __FILE__, __LINE__); Plugable::onAllComponents(boost::bind(&ComponentLoader::onBegin, _1)); ConsoleApplicationEngine app; diff --git a/project2/daemon/p2daemonMain.cpp b/project2/daemon/p2daemonMain.cpp index 95de88e..0da41f3 100644 --- a/project2/daemon/p2daemonMain.cpp +++ b/project2/daemon/p2daemonMain.cpp @@ -45,8 +45,8 @@ int main(int argc, char ** argv) { Plugable::onAllComponents(boost::bind(&ComponentLoader::onBegin, _1)); - OptionsSources::Add("_2", new CommandLineArguments(argc, argv, - [](const char * a) { throw UnsupportedArguments(a); })); + AdHoc::PluginManager::getDefault()->add( + new CommandLineArguments(argc, argv, [](const char * a) { throw UnsupportedArguments(a); }), "_2", __FILE__, __LINE__); DaemonAppEngine dae(argc, argv); OptionsSource::loadSources([] { return DaemonAppEngine::reqPlatform;} ); diff --git a/project2/files/optionsSource.cpp b/project2/files/optionsSource.cpp index 8e98df0..2ce320c 100644 --- a/project2/files/optionsSource.cpp +++ b/project2/files/optionsSource.cpp @@ -38,19 +38,27 @@ FileOptions::modifiedTime() const FileOptions::ExtraFileOptions FileOptions::extraFileOptions; +class DefaultFileOptions : public FileOptions { + public: + DefaultFileOptions() : + FileOptions(".p2config") + { + } +}; + // Z... process last :) -DECLARE_OPTIONSSOURCE("Z_configfile", new FileOptions(".p2config")) +DECLARE_OPTIONSSOURCE("Z_configfile", DefaultFileOptions) DECLARE_OPTIONS(FileOptions, "File Options options") ("file.options.read", Options::functions( [](const VariableType & vt) { auto fo = new FileOptions(vt.as()); extraFileOptions[vt] = boost::shared_ptr(fo); - OptionsSources::Add(vt, fo); + AdHoc::PluginManager::getDefault()->add(fo, vt, __FILE__, __LINE__); }, []() { for (const auto & fo : extraFileOptions) { - OptionsSources::Remove(fo.first); + AdHoc::PluginManager::getDefault()->remove(fo.first); } extraFileOptions.clear(); }), -- cgit v1.2.3