Commit b5b2beb2 authored by Sylvain Berfini's avatar Sylvain Berfini 🎩

Use singleton for parser

parent eca3e11f
......@@ -33,10 +33,15 @@ namespace belcard {
ABNFGrammarBuilder _grammar_builder;
shared_ptr<Grammar> _grammar;
BelCardParser();
shared_ptr<BelCardGeneric> _parse(const string &input, const string &rule);
public:
BelCardParser();
static BelCardParser& getInstance() {
static BelCardParser *instance = new BelCardParser();
return *instance;
}
~BelCardParser();
shared_ptr<BelCard> parseOne(const string &input);
......
......@@ -38,17 +38,16 @@ int main(int argc, char *argv[]) {
file = argv[1];
auto t_start = std::chrono::high_resolution_clock::now();
BelCardParser *parser = new BelCardParser();
BelCardParser parser = BelCardParser::getInstance();
auto t_end = std::chrono::high_resolution_clock::now();
auto t_start_2 = std::chrono::high_resolution_clock::now();
shared_ptr<BelCardList> belCards = parser->parseFile(file);
shared_ptr<BelCardList> belCards = parser.parseFile(file);
auto t_end_2 = std::chrono::high_resolution_clock::now();
cout << *belCards << endl;
cout << "Parser initialized in " << std::chrono::duration<double, std::milli>(t_end - t_start).count() << " milliseconds" << endl;
cout << "Parsing done in " << std::chrono::duration<double, std::milli>(t_end_2 - t_start_2).count() << " milliseconds" << endl;
delete parser;
return 0;
}
\ No newline at end of file
......@@ -57,29 +57,25 @@ static void unfolding(void) {
static void vcard_parsing(void) {
string vcard = openFile("vcards/vcard.vcf");
BelCardParser *parser = new BelCardParser();
shared_ptr<BelCard> belCard = parser->parseOne(vcard);
BelCardParser parser = BelCardParser::getInstance();
shared_ptr<BelCard> belCard = parser.parseOne(vcard);
BC_ASSERT_TRUE_FATAL(belCard != NULL);
BC_ASSERT_TRUE(belCard->assertRFCCompliance());
string vcard2 = belCard->toFoldedString();
BC_ASSERT_EQUAL(vcard2.compare(vcard), 0, int, "%d");
delete parser;
}
static void vcards_parsing(void) {
string vcards = openFile("vcards/vcards.vcf");
BelCardParser *parser = new BelCardParser();
shared_ptr<BelCardList> belCards = parser->parse(vcards);
BelCardParser parser = BelCardParser::getInstance();
shared_ptr<BelCardList> belCards = parser.parse(vcards);
BC_ASSERT_TRUE_FATAL(belCards != NULL);
BC_ASSERT_TRUE(belCards->getCards().size() == 2);
string vcards2 = belCards->toString();
BC_ASSERT_EQUAL(vcards2.compare(vcards), 0, int, "%d");
delete parser;
}
static void create_vcard_from_api(void) {
......@@ -93,13 +89,12 @@ static void create_vcard_from_api(void) {
BC_ASSERT_TRUE(belCard->assertRFCCompliance());
string vcard = belCard->toString();
BelCardParser *parser = new BelCardParser();
shared_ptr<BelCard> belCard2 = parser->parseOne(vcard);
BelCardParser parser = BelCardParser::getInstance();
shared_ptr<BelCard> belCard2 = parser.parseOne(vcard);
BC_ASSERT_TRUE_FATAL(belCard2 != NULL);
BC_ASSERT_TRUE(belCard2->assertRFCCompliance());
string vcard2 = belCard2->toString();
BC_ASSERT_TRUE(vcard.compare(vcard2) == 0);
delete parser;
}
static void property_sort_using_pref_param(void) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment