From ab36ea7919cf2faa19c0fc9565a5a4c62b1ac835 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 30 Aug 2016 21:49:39 +0100 Subject: Register route handler factories, not route handlers so we can construct them as needed, with arguments, after system start up (incl. config load) --- icespider/compile/routeCompiler.cpp | 2 +- icespider/core/core.cpp | 11 +++++++++-- icespider/core/irouteHandler.cpp | 4 ++-- icespider/core/irouteHandler.h | 6 +++--- icespider/unittests/testApp.cpp | 3 +-- icespider/unittests/testCompile.cpp | 4 ++-- 6 files changed, 18 insertions(+), 12 deletions(-) diff --git a/icespider/compile/routeCompiler.cpp b/icespider/compile/routeCompiler.cpp index e38f240..ae75bf2 100644 --- a/icespider/compile/routeCompiler.cpp +++ b/icespider/compile/routeCompiler.cpp @@ -398,7 +398,7 @@ namespace IceSpider { fprintbf(output, "} // namespace %s\n\n", c->name); fprintf(output, "// Register route handlers.\n"); for (const auto & r : c->routes) { - fprintbf(output, "PLUGIN(%s::%s, IceSpider::IRouteHandler);\n", c->name, r->name); + fprintbf(output, "FACTORY(%s::%s, IceSpider::RouteHandlerFactory);\n", c->name, r->name); } fprintf(output, "\n// End generated code.\n"); } diff --git a/icespider/core/core.cpp b/icespider/core/core.cpp index b21a805..cb0c671 100644 --- a/icespider/core/core.cpp +++ b/icespider/core/core.cpp @@ -10,8 +10,8 @@ namespace IceSpider { // Big enough to map all the request methods (an empty of zero lenght routes as default) routes.resize(HttpMethod::OPTIONS + 1, {{ }}); // Initialize routes - for (const auto & rp : AdHoc::PluginManager::getDefault()->getAll()) { - auto r = rp->implementation(); + for (const auto & rp : AdHoc::PluginManager::getDefault()->getAll()) { + auto r = rp->implementation()->create(); auto & mroutes = routes[r->method]; if (mroutes.size() <= r->pathElementCount()) { mroutes.resize(r->pathElementCount() + 1); @@ -30,6 +30,13 @@ namespace IceSpider { Core::~Core() { if (communicator) communicator->destroy(); + for (auto m : routes) { + for (auto l : m) { + for (auto r : l) { + delete r; + } + } + } } void diff --git a/icespider/core/irouteHandler.cpp b/icespider/core/irouteHandler.cpp index 82c6df1..172c77d 100644 --- a/icespider/core/irouteHandler.cpp +++ b/icespider/core/irouteHandler.cpp @@ -1,8 +1,8 @@ #include "irouteHandler.h" #include "core.h" -#include +#include -INSTANTIATEPLUGINOF(IceSpider::IRouteHandler); +INSTANTIATEVOIDFACTORY(IceSpider::IRouteHandler); namespace IceSpider { IRouteHandler::IRouteHandler(HttpMethod m, const std::string & p) : diff --git a/icespider/core/irouteHandler.h b/icespider/core/irouteHandler.h index bbd9ab3..4832bdb 100644 --- a/icespider/core/irouteHandler.h +++ b/icespider/core/irouteHandler.h @@ -5,12 +5,12 @@ #include "util.h" #include #include -#include +#include #include #include namespace IceSpider { - class DLL_PUBLIC IRouteHandler : public AdHoc::AbstractPluginImplementation, public Path { + class DLL_PUBLIC IRouteHandler : public Path { public: IRouteHandler(HttpMethod, const std::string & path); virtual ~IRouteHandler(); @@ -44,7 +44,7 @@ namespace IceSpider { void addRouteSerializer(const MimeType &, StreamSerializerFactoryPtr); void removeRouteSerializer(const MimeType &); }; - typedef AdHoc::PluginOf RouteHandlers; + typedef AdHoc::Factory RouteHandlerFactory; } #endif diff --git a/icespider/unittests/testApp.cpp b/icespider/unittests/testApp.cpp index ef98e6a..0266226 100644 --- a/icespider/unittests/testApp.cpp +++ b/icespider/unittests/testApp.cpp @@ -1,7 +1,6 @@ #define BOOST_TEST_MODULE TestApp #include -#include #include #include #include @@ -27,7 +26,7 @@ void forceEarlyChangeDir() BOOST_AUTO_TEST_CASE( testLoadConfiguration ) { - BOOST_REQUIRE_EQUAL(8, AdHoc::PluginManager::getDefault()->getAll().size()); + BOOST_REQUIRE_EQUAL(8, AdHoc::PluginManager::getDefault()->getAll().size()); } class TestRequest : public IHttpRequest { diff --git a/icespider/unittests/testCompile.cpp b/icespider/unittests/testCompile.cpp index 6184a7e..130b762 100644 --- a/icespider/unittests/testCompile.cpp +++ b/icespider/unittests/testCompile.cpp @@ -132,10 +132,10 @@ BOOST_AUTO_TEST_CASE( testLoad ) BOOST_TEST_INFO(dlerror()); BOOST_REQUIRE(lib); - BOOST_REQUIRE_EQUAL(8, AdHoc::PluginManager::getDefault()->getAll().size()); + BOOST_REQUIRE_EQUAL(8, AdHoc::PluginManager::getDefault()->getAll().size()); // smoke test (block ensure dlclose dones't cause segfault) { - auto route = AdHoc::PluginManager::getDefault()->get("common::index"); + auto route = AdHoc::PluginManager::getDefault()->get("common::index"); BOOST_REQUIRE(route); } -- cgit v1.2.3