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

Added getInstance on belcard parser to prevent creating a grammar each time we...

Added getInstance on belcard parser to prevent creating a grammar each time we want to parse a property
parent eaeab2d6
......@@ -36,6 +36,7 @@ using namespace::std;
namespace belcard {
class BelCardGeneric {
friend class BelCardParser;
public:
template<typename T>
static shared_ptr<T> create() {
......@@ -44,7 +45,7 @@ namespace belcard {
template<typename T>
BELCARD_PUBLIC static bool isValid(const shared_ptr<T> &property) {
return T::parse(property->toString()) != NULL;
return T::parse(property->toString()) != nullptr;
}
BELCARD_PUBLIC BelCardGeneric() { }
......
......@@ -19,9 +19,6 @@
#ifndef belcard_parser_hpp
#define belcard_parser_hpp
#include "belcard.hpp"
#include "belcard_params.hpp"
#include <belr/grammarbuilder.hh>
#include <belr/abnf.hh>
......@@ -36,13 +33,21 @@
using namespace::belr;
namespace belcard {
class BelCardGeneric;
class BelCardList;
class BelCard;
class BelCardParser {
friend class BelCardProperty;
private:
Parser<shared_ptr<BelCardGeneric>> *_parser;
protected:
shared_ptr<BelCardGeneric> _parse(const string &input, const string &rule);
public:
BELCARD_PUBLIC static shared_ptr<BelCardParser> getInstance();
BELCARD_PUBLIC BelCardParser();
BELCARD_PUBLIC ~BelCardParser();
......
......@@ -24,6 +24,7 @@
#include "belcard/belcard_generic.hpp"
#include "belcard_params.hpp"
#include "belcard/belcard_parser.hpp"
#include <string>
#include <list>
......@@ -62,15 +63,14 @@ namespace belcard {
public:
template <typename T>
static shared_ptr<T> parseProperty(const string& rule, const string& input) {
ABNFGrammarBuilder grammar_builder;
shared_ptr<Grammar> grammar = grammar_builder.createFromAbnf((const char*)vcard_grammar, make_shared<CoreRules>());
Parser<shared_ptr<BelCardGeneric>> parser(grammar);
BelCardParam::setAllParamsHandlersAndCollectors(&parser);
T::setHandlerAndCollectors(&parser);
shared_ptr<BelCardGeneric> ret = parser.parseInput(rule, input, NULL);
return dynamic_pointer_cast<T>(ret);
shared_ptr<BelCardParser> parser = BelCardParser::getInstance();
shared_ptr<BelCardGeneric> ret = parser->_parser->parseInput(rule, input, NULL);
if (ret) {
return dynamic_pointer_cast<T>(ret);
}
return nullptr;
}
BELCARD_PUBLIC static shared_ptr<BelCardProperty> parse(const string& input);
BELCARD_PUBLIC static void setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser);
......
......@@ -29,6 +29,11 @@ using namespace::std;
using namespace::belr;
using namespace::belcard;
shared_ptr<BelCardParser> BelCardParser::getInstance() {
static shared_ptr<BelCardParser> parser(new BelCardParser);
return parser;
}
BelCardParser::BelCardParser() {
ABNFGrammarBuilder grammar_builder;
shared_ptr<Grammar> grammar = grammar_builder.createFromAbnf((const char*)vcard_grammar, make_shared<CoreRules>());
......
......@@ -201,4 +201,4 @@ void BelCardProperty::serialize(ostream& output) const {
output << ";" << (**it);
}
output << ":" << getValue() << "\r\n";
}
}
\ No newline at end of file
......@@ -17,6 +17,7 @@
*/
#include "belcard/belcard.hpp"
#include "belcard/belcard_parser.hpp"
#include "belcard/belcard_utils.hpp"
#include "belcard-tester.hpp"
......
......@@ -16,6 +16,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "belcard/belcard.hpp"
#include "belcard/belcard_parser.hpp"
#include <iostream>
......
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