From fdfdb70f7565711c6bd54ac5fe00eeac957e9dc6 Mon Sep 17 00:00:00 2001 From: Thomas Schaub Date: Sun, 17 Mar 2019 09:46:12 +0100 Subject: [PATCH] Introduce JSON_HAS_STRING_VIEW --- include/nlohmann/detail/macro_scope.hpp | 13 +++++++++++++ include/nlohmann/detail/macro_unscope.hpp | 2 ++ include/nlohmann/json.hpp | 4 ++-- single_include/nlohmann/json.hpp | 19 +++++++++++++++++-- test/src/unit-conversions.cpp | 8 ++++---- 5 files changed, 38 insertions(+), 8 deletions(-) diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index e41180a92..deb88cf2d 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -18,6 +18,13 @@ #endif #endif +#if defined(__has_include) && !defined(__INTELLISENSE__) && \ + !(defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1600) + #define JSON_HAS_INCLUDE(x) __has_include(x) +#else + #define JSON_HAS_INCLUDE(x) 0 +#endif + // C++ language standard detection #if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464 #define JSON_HAS_CPP_17 @@ -26,6 +33,12 @@ #define JSON_HAS_CPP_14 #endif +#if (JSON_HAS_INCLUDE() && \ + (__cplusplus > 201402L || defined(_LIBCPP_VERSION))) || \ + (defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910) +# define JSON_HAS_STRING_VIEW +#endif + // disable float-equal warnings on GCC/clang #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) #pragma GCC diagnostic push diff --git a/include/nlohmann/detail/macro_unscope.hpp b/include/nlohmann/detail/macro_unscope.hpp index 592debf7e..d46404988 100644 --- a/include/nlohmann/detail/macro_unscope.hpp +++ b/include/nlohmann/detail/macro_unscope.hpp @@ -17,7 +17,9 @@ #undef JSON_UNLIKELY #undef JSON_DEPRECATED #undef JSON_NODISCARD +#undef JSON_HAS_INCLUDE #undef JSON_HAS_CPP_14 #undef JSON_HAS_CPP_17 +#undef JSON_HAS_STRING_VIEW #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION #undef NLOHMANN_BASIC_JSON_TPL diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 9a7b0da3c..702878bf3 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -72,7 +72,7 @@ SOFTWARE. #include #include -#if defined(JSON_HAS_CPP_17) +#if defined(JSON_HAS_STRING_VIEW) #include #endif @@ -2868,7 +2868,7 @@ class basic_json #ifndef _MSC_VER // fix for issue #167 operator<< ambiguity under VS2015 and not std::is_same>::value #endif -#if defined(JSON_HAS_CPP_17) +#if defined(JSON_HAS_STRING_VIEW) and not std::is_same::value #endif and detail::is_detected::value diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index e8863cb20..a3c889fb0 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -469,6 +469,13 @@ class other_error : public exception #endif #endif +#if defined(__has_include) && !defined(__INTELLISENSE__) && \ + !(defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1600) + #define JSON_HAS_INCLUDE(x) __has_include(x) +#else + #define JSON_HAS_INCLUDE(x) 0 +#endif + // C++ language standard detection #if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464 #define JSON_HAS_CPP_17 @@ -477,6 +484,12 @@ class other_error : public exception #define JSON_HAS_CPP_14 #endif +#if (JSON_HAS_INCLUDE() && \ + (__cplusplus > 201402L || defined(_LIBCPP_VERSION))) || \ + (defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910) +# define JSON_HAS_STRING_VIEW +#endif + // disable float-equal warnings on GCC/clang #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) #pragma GCC diagnostic push @@ -12841,7 +12854,7 @@ class serializer // #include -#if defined(JSON_HAS_CPP_17) +#if defined(JSON_HAS_STRING_VIEW) #include #endif @@ -15637,7 +15650,7 @@ class basic_json #ifndef _MSC_VER // fix for issue #167 operator<< ambiguity under VS2015 and not std::is_same>::value #endif -#if defined(JSON_HAS_CPP_17) +#if defined(JSON_HAS_STRING_VIEW) and not std::is_same::value #endif and detail::is_detected::value @@ -20841,8 +20854,10 @@ inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std #undef JSON_UNLIKELY #undef JSON_DEPRECATED #undef JSON_NODISCARD +#undef JSON_HAS_INCLUDE #undef JSON_HAS_CPP_14 #undef JSON_HAS_CPP_17 +#undef JSON_HAS_STRING_VIEW #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION #undef NLOHMANN_BASIC_JSON_TPL diff --git a/test/src/unit-conversions.cpp b/test/src/unit-conversions.cpp index 48c6b7b61..87a7233c1 100644 --- a/test/src/unit-conversions.cpp +++ b/test/src/unit-conversions.cpp @@ -47,7 +47,7 @@ using nlohmann::json; #define JSON_HAS_CPP_14 #endif -#if defined(JSON_HAS_CPP_17) +#if defined(JSON_HAS_STRING_VIEW) #include #endif @@ -369,7 +369,7 @@ TEST_CASE("value conversion") std::string s = j.get(); CHECK(json(s) == j); } -#if defined(JSON_HAS_CPP_17) +#if defined(JSON_HAS_STRING_VIEW) SECTION("std::string_view") { std::string_view s = j.get(); @@ -418,7 +418,7 @@ TEST_CASE("value conversion") "[json.exception.type_error.302] type must be string, but is number"); } -#if defined(JSON_HAS_CPP_17) +#if defined(JSON_HAS_STRING_VIEW) SECTION("exception in case of a non-string type using string_view") { CHECK_THROWS_AS(json(json::value_t::null).get(), json::type_error&); @@ -491,7 +491,7 @@ TEST_CASE("value conversion") CHECK(json(s) == j); } -#if defined(JSON_HAS_CPP_17) +#if defined(JSON_HAS_STRING_VIEW) SECTION("std::string_view") { static_assert(!is_implicitly_convertible::value);