From 47311cc8b211a7fef033d744d9eba9f308726ea8 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Tue, 20 Jan 2015 00:53:49 +0100 Subject: Refactored stuff surrounding the ResourceLocator class, implemented StaticResourceLocator which can be used for registering static resources (mainly for testing or if certain resources need to be available from the executable) --- src/core/Registry.cpp | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) (limited to 'src/core/Registry.cpp') diff --git a/src/core/Registry.cpp b/src/core/Registry.cpp index 74d1cf8..b714241 100644 --- a/src/core/Registry.cpp +++ b/src/core/Registry.cpp @@ -16,8 +16,11 @@ along with this program. If not, see . */ -#include #include +#include +#include + +#include "Registry.hpp" namespace ousia { @@ -25,9 +28,9 @@ using namespace parser; /* Class Registry */ -void Registry::registerParser(parser::Parser *parser) +void Registry::registerParser(parser::Parser &parser) { - parsers.push_back(parser); + parsers.push_back(&parser); for (const auto &mime : parser->mimetypes()) { //TODO: This does not allow for multiple parsers with the same mimetype. // Is that how its supposed to be? @@ -44,24 +47,40 @@ Parser *Registry::getParserForMimetype(const std::string &mimetype) const return nullptr; } -void Registry::registerResourceLocator(ResourceLocator *locator) +void Registry::registerResourceLocator(ResourceLocator &locator) { - locators.push_back(locator); + locators.push_back(&locator); } -ResourceLocator::Location Registry::locateResource( - const std::string &path, const std::string &relativeTo, - ResourceLocator::Type type) const +bool Registry::locateResource(Resource &resource, const std::string &path, + ResourceType type, + const Resource &relativeTo) const { - ResourceLocator::Location *last; + // Try the locator of the given "relativeTo" resource first + if (relativeTo.isValid()) { + if (relativeTo.getLocator().locate(resource, path, type, relativeTo)) { + return true; + } + } + + // Iterate over all registered locators and try to resolve the given path for (auto &locator : locators) { - ResourceLocator::Location loc = locator->locate(path, relativeTo, type); - if (loc.found) { - return loc; + if (locator->locate(resource, path, type, relativeTo)) { + return true; } - last = &loc; } - return *last; + + // If this did not work out, retry but use the UNKNOWN type. + if (type != ResourceType::UNKNOWN) { + for (auto &locator : locators) { + if (locator->locate(resource, path, ResourceType::UNKNOWN, + relativeTo)) { + return true; + } + } + } + + return false; } } -- cgit v1.2.3