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 --- ParserFactory.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 ParserFactory.cpp (limited to 'ParserFactory.cpp') diff --git a/ParserFactory.cpp b/ParserFactory.cpp new file mode 100644 index 0000000..49c4d00 --- /dev/null +++ b/ParserFactory.cpp @@ -0,0 +1,32 @@ +#include + +#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); +} + -- cgit v1.2.3