From 8c0562c8ef37cd5e80c58609f7bc7ae352365f65 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 22 Oct 2024 14:44:47 +0200 Subject: refactor parser factory --- ParserFactory.cpp | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'ParserFactory.cpp') diff --git a/ParserFactory.cpp b/ParserFactory.cpp index 49c4d00..53bfbc5 100644 --- a/ParserFactory.cpp +++ b/ParserFactory.cpp @@ -1,32 +1,33 @@ -#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"); +#include "XMLParser.h" +#include "TXTParser.h" +#include "CSVParser.h" + +using namespace std; + +ParserFactory::ParserFactory() { + this->parsers.push_back(unique_ptr(new XMLParser())); + this->parsers.push_back(unique_ptr(new TXTParser())); + this->parsers.push_back(unique_ptr(new CSVParser())); +} +Parser & ParserFactory::get_parser(FileReader & file) { unsigned int best_score = 0; Parser * best_strategy = nullptr; - for (Parser * strategy : col) { - unsigned int score = strategy->heuristic(file); + for (auto & parser : this->parsers) { + parser->set_file(file); + unsigned int score = parser->heuristic(); if (score <= best_score) continue; best_score = score; - best_strategy = strategy; + best_strategy = parser.get(); } if (best_strategy == nullptr) - throw Exception("unknown file type"); - - best_strategy->parse(file, deserializer); -} + throw Exception("ParserFactory: unknown file type"); -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); + return *best_strategy; } -- cgit v1.2.3