From df337354cf7187cea73f4310b76fef938cdc8982 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 1 May 2018 19:11:08 +0100 Subject: Tidy parser handlers into lambdas --- libjsonpp/json.ll | 4 +++- libjsonpp/jsonFlexLexer.cpp | 33 ++++++--------------------------- libjsonpp/jsonFlexLexer.h | 8 ++------ 3 files changed, 11 insertions(+), 34 deletions(-) diff --git a/libjsonpp/json.ll b/libjsonpp/json.ll index 860b98c..6cf6302 100644 --- a/libjsonpp/json.ll +++ b/libjsonpp/json.ll @@ -157,6 +157,8 @@ json::jsonFlexLexer::jsonFlexLexer(std::istream & in, const std::string & enc) : encoding(enc) { yy_push_state(VALUE); - acceptValues.push(boost::bind(&jsonFlexLexer::RootValue, this, _1)); + acceptValues.push([this](const auto & value) { + return values.emplace(std::make_shared(value)).get(); + }); } diff --git a/libjsonpp/jsonFlexLexer.cpp b/libjsonpp/jsonFlexLexer.cpp index ec924c4..323e3ed 100644 --- a/libjsonpp/jsonFlexLexer.cpp +++ b/libjsonpp/jsonFlexLexer.cpp @@ -1,6 +1,5 @@ #include #include "jsonFlexLexer.h" -#include #include namespace json { @@ -10,30 +9,6 @@ namespace json { return values.top(); } - Value * - jsonFlexLexer::RootValue(const Value & value) - { - auto v = ValuePtr(new Value(value)); - values.push(v); - return v.get(); - } - - Value * - jsonFlexLexer::ArrayAppend(Array * array, const Value & value) - { - auto v = ValuePtr(new Value(value)); - array->push_back(v); - return v.get(); - } - - Value * - jsonFlexLexer::ObjectMember(Object * object, const Value & value) - { - auto v = ValuePtr(new Value(value)); - (*object)[name] = v; - return v.get(); - } - std::string jsonFlexLexer::encodeBuf() const { @@ -47,14 +22,18 @@ namespace json { jsonFlexLexer::BeginObject() { auto object = boost::get(acceptValues.top()(Object())); - acceptValues.push(boost::bind(&jsonFlexLexer::ObjectMember, this, object, _1)); + acceptValues.push([object,this](const auto & value) { + return object->insert_or_assign(name, std::make_shared(value)).first->second.get(); + }); } void jsonFlexLexer::BeginArray() { auto array = boost::get(acceptValues.top()(Array())); - acceptValues.push(boost::bind(&jsonFlexLexer::ArrayAppend, this, array, _1)); + acceptValues.push([array](const auto & value) { + return array->emplace_back(std::make_shared(value)).get(); + }); } void diff --git a/libjsonpp/jsonFlexLexer.h b/libjsonpp/jsonFlexLexer.h index e3be204..6ea1336 100644 --- a/libjsonpp/jsonFlexLexer.h +++ b/libjsonpp/jsonFlexLexer.h @@ -4,7 +4,7 @@ #include #include "jsonpp.h" #include -#include +#include namespace json { class jsonFlexLexer : public yyFlexLexer { @@ -26,16 +26,12 @@ namespace json { void PushObject(); private: - Value * RootValue(const Value &); - Value * ArrayAppend(Array *, const Value &); - Value * ObjectMember(Object *, const Value &); - std::string encodeBuf() const; std::string buf, name, encoding; std::stack values; - typedef boost::function AcceptValue; + typedef std::function AcceptValue; std::stack acceptValues; }; } -- cgit v1.2.3