aboutsummaryrefslogtreecommitdiff
path: root/FileReaderFactory.cpp
blob: 4b72b11fdf6e0d4fd1ddb8ac883c1835ab5f2650 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <memory>

#include "FileReaderFactory.h"
#include "FileReader.h"

using namespace std;

unique_ptr<FileReader> FileReaderFactory::open(const std::string url) {
	FileReader * reader = find_reader(url)->clone();
	reader->open(url);
	return unique_ptr<FileReader>(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;
}