aboutsummaryrefslogtreecommitdiff
path: root/ParserFactory.cpp
blob: 49c4d0075cb80d91aea7e6f09eaddc0f21793127 (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
#include <algorithm>

#include "ParserFactory.h"
#include "Exception.h"

void ParserFactory::parse(FileReader & file, MuseumDeserializer & deserializer) {
	auto & col = ParserFactory::get_collection();
	if (col.size() < 1)
		throw Exception("no parsers registered");

	unsigned int best_score = 0;
	Parser * best_strategy = nullptr;
	for (Parser * strategy : col) {
		unsigned int score = strategy->heuristic(file);
		if (score <= best_score) continue;

		best_score = score;
		best_strategy = strategy;
	}

	if (best_strategy == nullptr)
		throw Exception("unknown file type");

	best_strategy->parse(file, deserializer);
}

void ParserFactory::register_strategy(Parser * ps) {
	auto & col = ParserFactory::get_collection();
	if (std::find(col.begin(), col.end(), ps) != col.end()) return;
	col.push_back(ps);
}