From d8289105193707daede1a5b59137f18e20f20aeb Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 18 Oct 2024 15:48:14 +0200 Subject: (2/2) rename --- FileReaderFactory.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 FileReaderFactory.cpp (limited to 'FileReaderFactory.cpp') diff --git a/FileReaderFactory.cpp b/FileReaderFactory.cpp new file mode 100644 index 0000000..4b72b11 --- /dev/null +++ b/FileReaderFactory.cpp @@ -0,0 +1,36 @@ +#include + +#include "FileReaderFactory.h" +#include "FileReader.h" + +using namespace std; + +unique_ptr FileReaderFactory::open(const std::string url) { + FileReader * reader = find_reader(url)->clone(); + reader->open(url); + return unique_ptr(reader); +} + +void FileReaderFactory::register_strategy(const std::string type, const FileReader * node) { + static FactoryMap & map = get_map(); + map[type] = node; +} + +FactoryMap & FileReaderFactory::get_map() { + static FactoryMap map; + return map; +} + +const FileReader * FileReaderFactory::find_reader(const std::string type) { + static FactoryMap & map = get_map(); + + // try to find protocol by prefix + for (auto item : map) { + if (!type.starts_with(item.first)) continue; + return item.second; + } + + // fallback is local file + return map.find("file://")->second; +} + -- cgit v1.2.3