From 44f5b165601b81b4e00d6ebb866c2986cf43ad49 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 1 May 2018 19:42:42 +0100 Subject: std::variant over boost::variant --- libjsonpp/jsonFlexLexer.cpp | 4 +-- libjsonpp/jsonpp.h | 4 +-- libjsonpp/parse.cpp | 2 +- libjsonpp/serialize.cpp | 6 ++-- libjsonpp/testEncoding.cpp | 2 +- libjsonpp/testParse.cpp | 72 ++++++++++++++++++++++----------------------- 6 files changed, 45 insertions(+), 45 deletions(-) diff --git a/libjsonpp/jsonFlexLexer.cpp b/libjsonpp/jsonFlexLexer.cpp index 323e3ed..b761eb1 100644 --- a/libjsonpp/jsonFlexLexer.cpp +++ b/libjsonpp/jsonFlexLexer.cpp @@ -21,7 +21,7 @@ namespace json { void jsonFlexLexer::BeginObject() { - auto object = boost::get(acceptValues.top()(Object())); + auto object = std::get_if(acceptValues.top()(Object())); acceptValues.push([object,this](const auto & value) { return object->insert_or_assign(name, std::make_shared(value)).first->second.get(); }); @@ -30,7 +30,7 @@ namespace json { void jsonFlexLexer::BeginArray() { - auto array = boost::get(acceptValues.top()(Array())); + auto array = std::get_if(acceptValues.top()(Array())); acceptValues.push([array](const auto & value) { return array->emplace_back(std::make_shared(value)).get(); }); diff --git a/libjsonpp/jsonpp.h b/libjsonpp/jsonpp.h index 5845ec4..7e5a393 100644 --- a/libjsonpp/jsonpp.h +++ b/libjsonpp/jsonpp.h @@ -3,7 +3,7 @@ #include #include -#include +#include #include #include #include @@ -23,7 +23,7 @@ namespace json { typedef std::shared_ptr ValuePtr; typedef std::map Object; typedef std::list Array; - typedef boost::variant VT; + typedef std::variant VT; class Value : public VT { public: Value() : VT(Null()) { } diff --git a/libjsonpp/parse.cpp b/libjsonpp/parse.cpp index 3c5189b..5dca4e8 100644 --- a/libjsonpp/parse.cpp +++ b/libjsonpp/parse.cpp @@ -24,7 +24,7 @@ namespace json { } Object parseObject(const Glib::ustring & s) { - return boost::get(parseValue(s)); + return std::get(parseValue(s)); } Value parseValue(Glib::ustring::const_iterator & s) { diff --git a/libjsonpp/serialize.cpp b/libjsonpp/serialize.cpp index 7c5cd06..e0d24eb 100644 --- a/libjsonpp/serialize.cpp +++ b/libjsonpp/serialize.cpp @@ -7,7 +7,7 @@ namespace json { const std::string null("null"); const std::string utf8("utf-8"); - class JsonSerialize : public boost::static_visitor<> { + class JsonSerialize { public: JsonSerialize(std::ostream & out, const std::string & encoding) : s(out), @@ -17,8 +17,8 @@ namespace json { << std::setfill('0') // for String \uNNNN ; } - void operator()(const Value & v) const { - boost::apply_visitor(*this, v); + void operator()(const VT & v) const { + std::visit(*this, v); } void operator()(const std::string & str) const { (*this)(str, e); diff --git a/libjsonpp/testEncoding.cpp b/libjsonpp/testEncoding.cpp index a983bdf..f13afb7 100644 --- a/libjsonpp/testEncoding.cpp +++ b/libjsonpp/testEncoding.cpp @@ -22,7 +22,7 @@ BOOST_AUTO_TEST_CASE( parse_latin1 ) { std::stringstream ss("\"A \xD9\xF1\xEE\xE7\xF4\xD0\xE8 string.\""); BOOST_REQUIRE_EQUAL(19, ss.str().length()); - BOOST_REQUIRE_EQUAL("A ÙñîçôÐè string.", boost::get(json::parseValue(ss, "latin1"))); + BOOST_REQUIRE_EQUAL("A ÙñîçôÐè string.", std::get(json::parseValue(ss, "latin1"))); } BOOST_AUTO_TEST_CASE( write_latin1 ) diff --git a/libjsonpp/testParse.cpp b/libjsonpp/testParse.cpp index 30cf85e..aa086e9 100644 --- a/libjsonpp/testParse.cpp +++ b/libjsonpp/testParse.cpp @@ -12,13 +12,13 @@ const boost::filesystem::path root(XSTR(ROOT)); BOOST_AUTO_TEST_CASE( parse_bool_true ) { const Glib::ustring val(" true "); - BOOST_REQUIRE_EQUAL(true, boost::get(json::parseValue(val))); + BOOST_REQUIRE_EQUAL(true, std::get(json::parseValue(val))); } BOOST_AUTO_TEST_CASE( parse_bool_false ) { const Glib::ustring val(" false "); - BOOST_REQUIRE_EQUAL(false, boost::get(json::parseValue(val))); + BOOST_REQUIRE_EQUAL(false, std::get(json::parseValue(val))); } BOOST_AUTO_TEST_CASE( parse_invalid_value ) @@ -30,69 +30,69 @@ BOOST_AUTO_TEST_CASE( parse_invalid_value ) BOOST_AUTO_TEST_CASE( parse_int_1 ) { const Glib::ustring val(" 1 "); - BOOST_REQUIRE_EQUAL(1, boost::get(json::parseValue(val))); + BOOST_REQUIRE_EQUAL(1, std::get(json::parseValue(val))); } BOOST_AUTO_TEST_CASE( parse_int_neg48 ) { const Glib::ustring val(" -48 "); - BOOST_REQUIRE_EQUAL(-48, boost::get(json::parseValue(val))); + BOOST_REQUIRE_EQUAL(-48, std::get(json::parseValue(val))); } BOOST_AUTO_TEST_CASE( parse_int_48 ) { const Glib::ustring val(" 48 "); - BOOST_REQUIRE_EQUAL(48, boost::get(json::parseValue(val))); + BOOST_REQUIRE_EQUAL(48, std::get(json::parseValue(val))); } BOOST_AUTO_TEST_CASE( parse_float_pi ) { const Glib::ustring val(" 3.14159265359 "); - BOOST_REQUIRE_CLOSE(3.14159, boost::get(json::parseValue(val)), 0.001); + BOOST_REQUIRE_CLOSE(3.14159, std::get(json::parseValue(val)), 0.001); } BOOST_AUTO_TEST_CASE( parse_array ) { const Glib::ustring val(" [ 1, 2, 3, [ 4, 5 ], {\"val1\": 6, \"val2\": [7, 8]}, 9, 10 ] "); - auto arr = boost::get(json::parseValue(val)); + auto arr = std::get(json::parseValue(val)); BOOST_REQUIRE_EQUAL(7, arr.size()); auto itr = arr.begin(); - BOOST_REQUIRE_EQUAL(1, boost::get(**itr++)); - BOOST_REQUIRE_EQUAL(2, boost::get(**itr++)); - BOOST_REQUIRE_EQUAL(3, boost::get(**itr++)); - boost::get(**itr++); - boost::get(**itr++); - BOOST_REQUIRE_EQUAL(9, boost::get(**itr++)); - BOOST_REQUIRE_EQUAL(10, boost::get(**itr++)); + BOOST_REQUIRE_EQUAL(1, std::get(**itr++)); + BOOST_REQUIRE_EQUAL(2, std::get(**itr++)); + BOOST_REQUIRE_EQUAL(3, std::get(**itr++)); + std::get(**itr++); + std::get(**itr++); + BOOST_REQUIRE_EQUAL(9, std::get(**itr++)); + BOOST_REQUIRE_EQUAL(10, std::get(**itr++)); } BOOST_AUTO_TEST_CASE( parse_array_of_strings ) { const Glib::ustring val(" [ \"en\", \"de\", \"ro\", \"es\", \"fa\" ] "); - boost::get(json::parseValue(val)); + std::get(json::parseValue(val)); } BOOST_AUTO_TEST_CASE( parse_null ) { const Glib::ustring val(" null "); - boost::get(json::parseValue(val)); + std::get(json::parseValue(val)); } BOOST_AUTO_TEST_CASE( parse_empty_array ) { const Glib::ustring val(" [ ] "); - BOOST_REQUIRE(boost::get(json::parseValue(val)).empty()); + BOOST_REQUIRE(std::get(json::parseValue(val)).empty()); } BOOST_AUTO_TEST_CASE( parse_empty_arrary_in_object ) { const Glib::ustring val(" { \"v1\": [ ], \"v2\": 100 } "); auto value = json::parseValue(val); - BOOST_REQUIRE_EQUAL(3, value.which()); - auto obj = boost::get(value); + BOOST_REQUIRE_EQUAL(3, value.index()); + auto obj = std::get(value); BOOST_REQUIRE_EQUAL(2, obj.size()); - BOOST_REQUIRE(boost::get(*obj["v1"]).empty()); - BOOST_REQUIRE_EQUAL(100, boost::get(*obj["v2"])); + BOOST_REQUIRE(std::get(*obj["v1"]).empty()); + BOOST_REQUIRE_EQUAL(100, std::get(*obj["v2"])); } BOOST_AUTO_TEST_CASE( parse_broken_array ) @@ -111,27 +111,27 @@ BOOST_AUTO_TEST_CASE( parse_object ) { const Glib::ustring val(" { \"a\": 1, \"b\": 2 } "); auto value = json::parseValue(val); - BOOST_REQUIRE_EQUAL(3, value.which()); - auto obj = boost::get(value); + BOOST_REQUIRE_EQUAL(3, value.index()); + auto obj = std::get(value); BOOST_REQUIRE_EQUAL(2, obj.size()); - BOOST_REQUIRE_EQUAL(1, boost::get(*obj["a"])); - BOOST_REQUIRE_EQUAL(2, boost::get(*obj["b"])); + BOOST_REQUIRE_EQUAL(1, std::get(*obj["a"])); + BOOST_REQUIRE_EQUAL(2, std::get(*obj["b"])); } BOOST_AUTO_TEST_CASE( parse_string_simple ) { const Glib::ustring val(" \"simple string\" "); - BOOST_REQUIRE_EQUAL("simple string", boost::get(json::parseValue(val))); + BOOST_REQUIRE_EQUAL("simple string", std::get(json::parseValue(val))); } BOOST_AUTO_TEST_CASE( parse_object_withStringContainingQuote ) { const Glib::ustring val(" { \"key1\": \"value1\", \"key2\": \"value\\\"2\\\"\", \"key3\": 3 } "); - auto obj = boost::get(json::parseValue(val)); + auto obj = std::get(json::parseValue(val)); BOOST_REQUIRE_EQUAL(3, obj.size()); - BOOST_REQUIRE_EQUAL("value1", boost::get(*obj["key1"])); - BOOST_REQUIRE_EQUAL("value\"2\"", boost::get(*obj["key2"])); - BOOST_REQUIRE_EQUAL(3, boost::get(*obj["key3"])); + BOOST_REQUIRE_EQUAL("value1", std::get(*obj["key1"])); + BOOST_REQUIRE_EQUAL("value\"2\"", std::get(*obj["key2"])); + BOOST_REQUIRE_EQUAL(3, std::get(*obj["key3"])); } BOOST_AUTO_TEST_CASE( parse_string_invalid_missingOpeningQuote ) @@ -143,25 +143,25 @@ BOOST_AUTO_TEST_CASE( parse_string_invalid_missingOpeningQuote ) BOOST_AUTO_TEST_CASE( parse_string_escapedQuote ) { const Glib::ustring val(" \"A \\\"quoted\\\" string.\" "); - BOOST_REQUIRE_EQUAL("A \"quoted\" string.", boost::get(json::parseValue(val))); + BOOST_REQUIRE_EQUAL("A \"quoted\" string.", std::get(json::parseValue(val))); } BOOST_AUTO_TEST_CASE( parse_string_escapedWhitespace ) { const Glib::ustring val(" \"A whitespace\\t\\r\\n\\b\\f string.\" "); - BOOST_REQUIRE_EQUAL("A whitespace\t\r\n\b\f string.", boost::get(json::parseValue(val))); + BOOST_REQUIRE_EQUAL("A whitespace\t\r\n\b\f string.", std::get(json::parseValue(val))); } BOOST_AUTO_TEST_CASE( parse_string_escapedSlashes ) { const Glib::ustring val(" \"A whitespace\\\\ \\/ string.\" "); - BOOST_REQUIRE_EQUAL("A whitespace\\ / string.", boost::get(json::parseValue(val))); + BOOST_REQUIRE_EQUAL("A whitespace\\ / string.", std::get(json::parseValue(val))); } BOOST_AUTO_TEST_CASE( parse_string_literalUnicode ) { const Glib::ustring val(" \"A Űņĩćőđē string.\" "); - BOOST_REQUIRE_EQUAL("A Űņĩćőđē string.", boost::get(json::parseValue(val))); + BOOST_REQUIRE_EQUAL("A Űņĩćőđē string.", std::get(json::parseValue(val))); } BOOST_AUTO_TEST_CASE( parse_string_unknownEscape ) @@ -179,7 +179,7 @@ BOOST_AUTO_TEST_CASE( parse_string_shortUnicodeEscape ) BOOST_AUTO_TEST_CASE( parse_string_escapedUnicode ) { const Glib::ustring val(" \"A \\u0170\\u0146\\u0129\\u0107\\u0151\\u0111\\u0113 string.\" "); - BOOST_REQUIRE_EQUAL("A Űņĩćőđē string.", boost::get(json::parseValue(val))); + BOOST_REQUIRE_EQUAL("A Űņĩćőđē string.", std::get(json::parseValue(val))); } BOOST_AUTO_TEST_CASE( parse_sample_complexFile1 ) @@ -198,6 +198,6 @@ BOOST_AUTO_TEST_CASE( parse_from_itr ) { const Glib::ustring val(" \"A \\u0170\\u0146\\u0129\\u0107\\u0151\\u0111\\u0113 string.\" "); auto itr = val.begin(); - BOOST_REQUIRE_EQUAL("A Űņĩćőđē string.", boost::get(json::parseValue(itr))); + BOOST_REQUIRE_EQUAL("A Űņĩćőđē string.", std::get(json::parseValue(itr))); } -- cgit v1.2.3