From 5084159048b66141477793ba3083e86080a6952c Mon Sep 17 00:00:00 2001 From: Niels Date: Thu, 16 Jul 2015 18:45:19 +0200 Subject: [PATCH] added changes from #105: MSVC fixes - additionally, switch off optimization flags to maybe allow build to complete on AppVeyor --- CMakeLists.txt | 2 +- src/json.hpp | 49 +++++++++++++++++++++++++++-------------------- src/json.hpp.re2c | 49 +++++++++++++++++++++++++++-------------------- test/unit.cpp | 11 +++++++---- 4 files changed, 64 insertions(+), 47 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 36548140..350c54ec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ add_executable(json_unit if(MSVC) set(CMAKE_CXX_FLAGS - "/EHsc" + "/EHsc /Od" ) add_definitions(-D_SCL_SECURE_NO_WARNINGS) else(MSVC) diff --git a/src/json.hpp b/src/json.hpp index 9511f02c..a0c06191 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -137,7 +137,7 @@ class basic_json { private: /// workaround type for MSVC - using __basic_json = + using basic_json_t = basic_json; public: @@ -890,10 +890,10 @@ class basic_json */ template ::value and - not std::is_same::value and - not std::is_same::value and - not std::is_same::value and + not std::is_same::value and + not std::is_same::value and + not std::is_same::value and + not std::is_same::value and not std::is_same::value and not std::is_same::value and std::is_constructible::value, int>::type @@ -1401,8 +1401,8 @@ class basic_json */ template ::value or - std::is_same::value + std::is_same::value or + std::is_same::value , int>::type = 0> basic_json(InputIT first, InputIT last) : m_type(first.m_object->m_type) @@ -1503,7 +1503,7 @@ class basic_json @throw std::bad_alloc if allocation for object, array, or string fails. @liveexample{The following code shows an example for the copy - constructor.,basic_json__basic_json} + constructor.,basic_jsonbasic_json_t} @ingroup container */ @@ -1958,7 +1958,7 @@ class basic_json template ::value and - std::is_convertible<__basic_json, typename T::mapped_type>::value + std::is_convertible::value , int>::type = 0> T get_impl(T*) const { @@ -1994,8 +1994,8 @@ class basic_json /// get an array (explicit) template ::value and - not std::is_same<__basic_json, typename T::value_type>::value and + std::is_convertible::value and + not std::is_same::value and not std::is_arithmetic::value and not std::is_convertible::value and not has_mapped_type::value @@ -2024,8 +2024,8 @@ class basic_json /// get an array (explicit) template ::value and - not std::is_same<__basic_json, T>::value + std::is_convertible::value and + not std::is_same::value , int>::type = 0> std::vector get_impl(std::vector*) const { @@ -2838,8 +2838,8 @@ class basic_json */ template ::value or - std::is_same::value + std::is_same::value or + std::is_same::value , int>::type = 0> InteratorType erase(InteratorType pos) @@ -2931,8 +2931,8 @@ class basic_json */ template ::value or - std::is_same::value + std::is_same::value or + std::is_same::value , int>::type = 0> InteratorType erase(InteratorType first, InteratorType last) @@ -4285,8 +4285,9 @@ class basic_json } // We only reach this line if we cannot compare values. In that case, - // we compare types. - return lhs_type < rhs_type; + // we compare types. Note we have to call the operator explicitly, + // because MSVC has problems otherwise. + return operator<(lhs_type, rhs_type); } /*! @@ -4851,8 +4852,14 @@ class basic_json difference_type m_it = std::numeric_limits::min(); }; - /// an iterator value - union internal_iterator + /*! + @brief an iterator value + + @note This structure could easily be a union, but MSVC currently does not + allow unions members with complex constructors, see + https://github.com/nlohmann/json/pull/105. + */ + struct internal_iterator { /// iterator for JSON objects typename object_t::iterator object_iterator; diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 59b7e20d..a52383d5 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -137,7 +137,7 @@ class basic_json { private: /// workaround type for MSVC - using __basic_json = + using basic_json_t = basic_json; public: @@ -890,10 +890,10 @@ class basic_json */ template ::value and - not std::is_same::value and - not std::is_same::value and - not std::is_same::value and + not std::is_same::value and + not std::is_same::value and + not std::is_same::value and + not std::is_same::value and not std::is_same::value and not std::is_same::value and std::is_constructible::value, int>::type @@ -1401,8 +1401,8 @@ class basic_json */ template ::value or - std::is_same::value + std::is_same::value or + std::is_same::value , int>::type = 0> basic_json(InputIT first, InputIT last) : m_type(first.m_object->m_type) @@ -1503,7 +1503,7 @@ class basic_json @throw std::bad_alloc if allocation for object, array, or string fails. @liveexample{The following code shows an example for the copy - constructor.,basic_json__basic_json} + constructor.,basic_jsonbasic_json_t} @ingroup container */ @@ -1958,7 +1958,7 @@ class basic_json template ::value and - std::is_convertible<__basic_json, typename T::mapped_type>::value + std::is_convertible::value , int>::type = 0> T get_impl(T*) const { @@ -1994,8 +1994,8 @@ class basic_json /// get an array (explicit) template ::value and - not std::is_same<__basic_json, typename T::value_type>::value and + std::is_convertible::value and + not std::is_same::value and not std::is_arithmetic::value and not std::is_convertible::value and not has_mapped_type::value @@ -2024,8 +2024,8 @@ class basic_json /// get an array (explicit) template ::value and - not std::is_same<__basic_json, T>::value + std::is_convertible::value and + not std::is_same::value , int>::type = 0> std::vector get_impl(std::vector*) const { @@ -2838,8 +2838,8 @@ class basic_json */ template ::value or - std::is_same::value + std::is_same::value or + std::is_same::value , int>::type = 0> InteratorType erase(InteratorType pos) @@ -2931,8 +2931,8 @@ class basic_json */ template ::value or - std::is_same::value + std::is_same::value or + std::is_same::value , int>::type = 0> InteratorType erase(InteratorType first, InteratorType last) @@ -4285,8 +4285,9 @@ class basic_json } // We only reach this line if we cannot compare values. In that case, - // we compare types. - return lhs_type < rhs_type; + // we compare types. Note we have to call the operator explicitly, + // because MSVC has problems otherwise. + return operator<(lhs_type, rhs_type); } /*! @@ -4851,8 +4852,14 @@ class basic_json difference_type m_it = std::numeric_limits::min(); }; - /// an iterator value - union internal_iterator + /*! + @brief an iterator value + + @note This structure could easily be a union, but MSVC currently does not + allow unions members with complex constructors, see + https://github.com/nlohmann/json/pull/105. + */ + struct internal_iterator { /// iterator for JSON objects typename object_t::iterator object_iterator; diff --git a/test/unit.cpp b/test/unit.cpp index e3830519..f1863fb7 100644 --- a/test/unit.cpp +++ b/test/unit.cpp @@ -6939,7 +6939,7 @@ TEST_CASE("lexicographical comparison operators") CAPTURE(i); CAPTURE(j); // check precomputed values - CHECK( (j_types[i] < j_types[j]) == expected[i][j] ); + CHECK(operator<(j_types[i], j_types[j]) == expected[i][j]); } } } @@ -8163,9 +8163,11 @@ TEST_CASE("parser class") SECTION("escaped") { // quotation mark "\"" - CHECK(json::parser("\"\\\"\"").parse() == R"("\"")"_json); + auto r1 = R"("\"")"_json; + CHECK(json::parser("\"\\\"\"").parse() == r1); // reverse solidus "\\" - CHECK(json::parser("\"\\\\\"").parse() == R"("\\")"_json); + auto r2 = R"("\\")"_json; + CHECK(json::parser("\"\\\\\"").parse() == r2); // solidus CHECK(json::parser("\"\\/\"").parse() == R"("/")"_json); // backspace @@ -9738,7 +9740,8 @@ TEST_CASE("regression tests") { auto s = "[\"\\\"foo\\\"\"]"; json j = json::parse(s); - CHECK(j == R"(["\"foo\""])"_json); + auto expected = R"(["\"foo\""])"_json; + CHECK(j == expected); } }