#include "ParserFactory.h" #include "Exception.h" #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 (auto & parser : this->parsers) { parser->set_file(file); unsigned int score = parser->heuristic(); if (score <= best_score) continue; best_score = score; best_strategy = parser.get(); } if (best_strategy == nullptr) throw Exception("ParserFactory: unknown file type"); return *best_strategy; }