summaryrefslogtreecommitdiff
path: root/src/core/Registry.cpp
diff options
context:
space:
mode:
authorAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2015-01-20 00:53:49 +0100
committerAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2015-01-20 00:53:49 +0100
commit47311cc8b211a7fef033d744d9eba9f308726ea8 (patch)
tree348726aef17297729233b93b6d7eef86f25c7a78 /src/core/Registry.cpp
parentd836d70ea2352dcf277c6fce91ba1ded3f074b44 (diff)
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)
Diffstat (limited to 'src/core/Registry.cpp')
-rw-r--r--src/core/Registry.cpp47
1 files changed, 33 insertions, 14 deletions
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 <http://www.gnu.org/licenses/>.
*/
-#include <core/common/Logger.hpp>
#include <core/parser/Parser.hpp>
+#include <core/resource/Resource.hpp>
+#include <core/resource/ResourceLocator.hpp>
+
+#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;
}
}