diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-22 14:44:47 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-22 14:44:47 +0200 |
commit | 8c0562c8ef37cd5e80c58609f7bc7ae352365f65 (patch) | |
tree | 42e9906c5ddf1b0e719ee3eb7b965a444be64c38 /ParserFactory.cpp | |
parent | bc02054d56118110a36aea72d21f9d5e73d07d1f (diff) |
refactor parser factory
Diffstat (limited to 'ParserFactory.cpp')
-rw-r--r-- | ParserFactory.cpp | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/ParserFactory.cpp b/ParserFactory.cpp index 49c4d00..53bfbc5 100644 --- a/ParserFactory.cpp +++ b/ParserFactory.cpp @@ -1,32 +1,33 @@ -#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"); +#include "XMLParser.h" +#include "TXTParser.h" +#include "CSVParser.h" + +using namespace std; + +ParserFactory::ParserFactory() { + this->parsers.push_back(unique_ptr<Parser>(new XMLParser())); + this->parsers.push_back(unique_ptr<Parser>(new TXTParser())); + this->parsers.push_back(unique_ptr<Parser>(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; } |