From 9d726c25d5b6accabbe70598df644aed809db957 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 12 Aug 2020 13:30:06 +0200 Subject: [PATCH] :recycle: remove "#define private public" --- Makefile | 2 -- .../nlohmann/detail/iterators/iter_impl.hpp | 4 +-- .../detail/iterators/primitive_iterator.hpp | 1 + include/nlohmann/detail/json_pointer.hpp | 4 +++ include/nlohmann/detail/macro_scope.hpp | 7 +++++ include/nlohmann/detail/macro_unscope.hpp | 1 + include/nlohmann/detail/output/serializer.hpp | 3 ++- include/nlohmann/json.hpp | 7 ++++- single_include/nlohmann/json.hpp | 27 ++++++++++++++++--- test/src/unit-allocator.cpp | 3 +-- test/src/unit-class_const_iterator.cpp | 3 +-- test/src/unit-class_iterator.cpp | 3 +-- test/src/unit-class_lexer.cpp | 3 +-- test/src/unit-class_parser.cpp | 3 +-- test/src/unit-constructor1.cpp | 3 +-- test/src/unit-convenience.cpp | 3 +-- test/src/unit-conversions.cpp | 3 +-- test/src/unit-iterators1.cpp | 3 +-- test/src/unit-json_pointer.cpp | 3 +-- test/src/unit-regression1.cpp | 3 +-- test/src/unit-regression2.cpp | 3 +-- test/src/unit-unicode.cpp | 3 +-- 22 files changed, 59 insertions(+), 36 deletions(-) diff --git a/Makefile b/Makefile index 98fa365af..0b310e9ea 100644 --- a/Makefile +++ b/Makefile @@ -81,7 +81,6 @@ doctest: # -Wno-documentation-unknown-command: code uses user-defined commands like @complexity # -Wno-exit-time-destructors: warning in json code triggered by NLOHMANN_JSON_SERIALIZE_ENUM # -Wno-float-equal: not all comparisons in the tests can be replaced by Approx -# -Wno-keyword-macro: unit-tests use "#define private public" # -Wno-missing-prototypes: for NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE # -Wno-padded: padding is nothing to warn about # -Wno-range-loop-analysis: items tests "for(const auto i...)" @@ -98,7 +97,6 @@ pedantic_clang: -Wno-documentation-unknown-command \ -Wno-exit-time-destructors \ -Wno-float-equal \ - -Wno-keyword-macro \ -Wno-missing-prototypes \ -Wno-padded \ -Wno-range-loop-analysis \ diff --git a/include/nlohmann/detail/iterators/iter_impl.hpp b/include/nlohmann/detail/iterators/iter_impl.hpp index e9a394d4c..b4faa88a5 100644 --- a/include/nlohmann/detail/iterators/iter_impl.hpp +++ b/include/nlohmann/detail/iterators/iter_impl.hpp @@ -163,7 +163,7 @@ class iter_impl return *this; } - private: + JSON_PRIVATE_UNLESS_TESTED: /*! @brief set the iterator to the first value @pre The iterator is initialized; i.e. `m_object != nullptr`. @@ -627,7 +627,7 @@ class iter_impl return operator*(); } - private: + JSON_PRIVATE_UNLESS_TESTED: /// associated JSON instance pointer m_object = nullptr; /// the actual iterator of the associated instance diff --git a/include/nlohmann/detail/iterators/primitive_iterator.hpp b/include/nlohmann/detail/iterators/primitive_iterator.hpp index 28d6f1a65..16dcc9f97 100644 --- a/include/nlohmann/detail/iterators/primitive_iterator.hpp +++ b/include/nlohmann/detail/iterators/primitive_iterator.hpp @@ -23,6 +23,7 @@ class primitive_iterator_t static constexpr difference_type begin_value = 0; static constexpr difference_type end_value = begin_value + 1; + JSON_PRIVATE_UNLESS_TESTED: /// iterator as signed integer type difference_type m_it = (std::numeric_limits::min)(); diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index 78bc3a3a3..865376cf1 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -375,6 +375,7 @@ class json_pointer return static_cast(res); } + JSON_PRIVATE_UNLESS_TESTED: json_pointer top() const { if (JSON_HEDLEY_UNLIKELY(empty())) @@ -387,6 +388,7 @@ class json_pointer return result; } + private: /*! @brief create and return a reference to the pointed to value @@ -823,6 +825,7 @@ class json_pointer {} } + JSON_PRIVATE_UNLESS_TESTED: /// escape "~" to "~0" and "/" to "~1" static std::string escape(std::string s) { @@ -838,6 +841,7 @@ class json_pointer replace_substring(s, "~0", "~"); } + private: /*! @param[in] reference_string the reference string to the current value @param[in] value the value to consider diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index 8c9f63296..77acf04c7 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -83,6 +83,13 @@ #define JSON_ASSERT(x) assert(x) #endif +// allow to access some private functions (needed by the test suite) +#if defined(JSON_TESTS_PRIVATE) + #define JSON_PRIVATE_UNLESS_TESTED public +#else + #define JSON_PRIVATE_UNLESS_TESTED private +#endif + /*! @brief macro to briefly define a mapping between an enum and JSON @def NLOHMANN_JSON_SERIALIZE_ENUM diff --git a/include/nlohmann/detail/macro_unscope.hpp b/include/nlohmann/detail/macro_unscope.hpp index eb7065113..5ac66f5af 100644 --- a/include/nlohmann/detail/macro_unscope.hpp +++ b/include/nlohmann/detail/macro_unscope.hpp @@ -14,6 +14,7 @@ #undef JSON_CATCH #undef JSON_THROW #undef JSON_TRY +#undef JSON_PRIVATE_UNLESS_TESTED #undef JSON_HAS_CPP_14 #undef JSON_HAS_CPP_17 #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index 865b8904e..f59e8ad31 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -362,7 +362,7 @@ class serializer } } - private: + JSON_PRIVATE_UNLESS_TESTED: /*! @brief dump escaped string @@ -625,6 +625,7 @@ class serializer } } + private: /*! @brief count digits diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index ff8c46463..cd56dafdc 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -189,6 +189,7 @@ class basic_json /// workaround type for MSVC using basic_json_t = NLOHMANN_BASIC_JSON_TPL; + JSON_PRIVATE_UNLESS_TESTED: // convenience aliases for types residing in namespace detail; using lexer = ::nlohmann::detail::lexer_base; @@ -204,6 +205,7 @@ class basic_json std::move(cb), allow_exceptions, ignore_comments); } + private: using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t; template using internal_iterator = ::nlohmann::detail::internal_iterator; @@ -220,6 +222,7 @@ class basic_json using binary_reader = ::nlohmann::detail::binary_reader; template using binary_writer = ::nlohmann::detail::binary_writer; + JSON_PRIVATE_UNLESS_TESTED: using serializer = ::nlohmann::detail::serializer; public: @@ -934,6 +937,7 @@ class basic_json // JSON value storage // //////////////////////// + JSON_PRIVATE_UNLESS_TESTED: /*! @brief a JSON value @@ -1210,6 +1214,7 @@ class basic_json } }; + private: /*! @brief checks the class invariants @@ -6947,7 +6952,7 @@ class basic_json } - private: + JSON_PRIVATE_UNLESS_TESTED: ////////////////////// // member variables // ////////////////////// diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 7d107f21e..e9dbc87da 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2100,6 +2100,13 @@ JSON_HEDLEY_DIAGNOSTIC_POP #define JSON_ASSERT(x) assert(x) #endif +// allow to access some private functions (needed by the test suite) +#if defined(JSON_TESTS_PRIVATE) + #define JSON_PRIVATE_UNLESS_TESTED public +#else + #define JSON_PRIVATE_UNLESS_TESTED private +#endif + /*! @brief macro to briefly define a mapping between an enum and JSON @def NLOHMANN_JSON_SERIALIZE_ENUM @@ -10642,6 +10649,7 @@ class primitive_iterator_t static constexpr difference_type begin_value = 0; static constexpr difference_type end_value = begin_value + 1; + JSON_PRIVATE_UNLESS_TESTED: /// iterator as signed integer type difference_type m_it = (std::numeric_limits::min)(); @@ -10934,7 +10942,7 @@ class iter_impl return *this; } - private: + JSON_PRIVATE_UNLESS_TESTED: /*! @brief set the iterator to the first value @pre The iterator is initialized; i.e. `m_object != nullptr`. @@ -11398,7 +11406,7 @@ class iter_impl return operator*(); } - private: + JSON_PRIVATE_UNLESS_TESTED: /// associated JSON instance pointer m_object = nullptr; /// the actual iterator of the associated instance @@ -11913,6 +11921,7 @@ class json_pointer return static_cast(res); } + JSON_PRIVATE_UNLESS_TESTED: json_pointer top() const { if (JSON_HEDLEY_UNLIKELY(empty())) @@ -11925,6 +11934,7 @@ class json_pointer return result; } + private: /*! @brief create and return a reference to the pointed to value @@ -12361,6 +12371,7 @@ class json_pointer {} } + JSON_PRIVATE_UNLESS_TESTED: /// escape "~" to "~0" and "/" to "~1" static std::string escape(std::string s) { @@ -12376,6 +12387,7 @@ class json_pointer replace_substring(s, "~0", "~"); } + private: /*! @param[in] reference_string the reference string to the current value @param[in] value the value to consider @@ -15801,7 +15813,7 @@ class serializer } } - private: + JSON_PRIVATE_UNLESS_TESTED: /*! @brief dump escaped string @@ -16064,6 +16076,7 @@ class serializer } } + private: /*! @brief count digits @@ -16682,6 +16695,7 @@ class basic_json /// workaround type for MSVC using basic_json_t = NLOHMANN_BASIC_JSON_TPL; + JSON_PRIVATE_UNLESS_TESTED: // convenience aliases for types residing in namespace detail; using lexer = ::nlohmann::detail::lexer_base; @@ -16697,6 +16711,7 @@ class basic_json std::move(cb), allow_exceptions, ignore_comments); } + private: using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t; template using internal_iterator = ::nlohmann::detail::internal_iterator; @@ -16713,6 +16728,7 @@ class basic_json using binary_reader = ::nlohmann::detail::binary_reader; template using binary_writer = ::nlohmann::detail::binary_writer; + JSON_PRIVATE_UNLESS_TESTED: using serializer = ::nlohmann::detail::serializer; public: @@ -17427,6 +17443,7 @@ class basic_json // JSON value storage // //////////////////////// + JSON_PRIVATE_UNLESS_TESTED: /*! @brief a JSON value @@ -17703,6 +17720,7 @@ class basic_json } }; + private: /*! @brief checks the class invariants @@ -23440,7 +23458,7 @@ class basic_json } - private: + JSON_PRIVATE_UNLESS_TESTED: ////////////////////// // member variables // ////////////////////// @@ -25296,6 +25314,7 @@ inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std #undef JSON_CATCH #undef JSON_THROW #undef JSON_TRY +#undef JSON_PRIVATE_UNLESS_TESTED #undef JSON_HAS_CPP_14 #undef JSON_HAS_CPP_17 #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION diff --git a/test/src/unit-allocator.cpp b/test/src/unit-allocator.cpp index 40361ce00..ad78b8f9e 100644 --- a/test/src/unit-allocator.cpp +++ b/test/src/unit-allocator.cpp @@ -29,10 +29,9 @@ SOFTWARE. #include "doctest_compatibility.h" -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private namespace { diff --git a/test/src/unit-class_const_iterator.cpp b/test/src/unit-class_const_iterator.cpp index 608e6a544..a972fd4c7 100644 --- a/test/src/unit-class_const_iterator.cpp +++ b/test/src/unit-class_const_iterator.cpp @@ -29,10 +29,9 @@ SOFTWARE. #include "doctest_compatibility.h" -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private TEST_CASE("const_iterator class") { diff --git a/test/src/unit-class_iterator.cpp b/test/src/unit-class_iterator.cpp index c06ef0a48..b4ef11e43 100644 --- a/test/src/unit-class_iterator.cpp +++ b/test/src/unit-class_iterator.cpp @@ -29,10 +29,9 @@ SOFTWARE. #include "doctest_compatibility.h" -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private TEST_CASE("iterator class") { diff --git a/test/src/unit-class_lexer.cpp b/test/src/unit-class_lexer.cpp index 15b390cfb..1a4f8ed75 100644 --- a/test/src/unit-class_lexer.cpp +++ b/test/src/unit-class_lexer.cpp @@ -29,10 +29,9 @@ SOFTWARE. #include "doctest_compatibility.h" -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private namespace { diff --git a/test/src/unit-class_parser.cpp b/test/src/unit-class_parser.cpp index 07044ed56..0cffee02a 100644 --- a/test/src/unit-class_parser.cpp +++ b/test/src/unit-class_parser.cpp @@ -29,10 +29,9 @@ SOFTWARE. #include "doctest_compatibility.h" -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private #include diff --git a/test/src/unit-constructor1.cpp b/test/src/unit-constructor1.cpp index 33bd92c3b..70b3e4047 100644 --- a/test/src/unit-constructor1.cpp +++ b/test/src/unit-constructor1.cpp @@ -30,10 +30,9 @@ SOFTWARE. #include "doctest_compatibility.h" DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal") -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private #include #include diff --git a/test/src/unit-convenience.cpp b/test/src/unit-convenience.cpp index 162ad56d8..c75edac4e 100644 --- a/test/src/unit-convenience.cpp +++ b/test/src/unit-convenience.cpp @@ -29,10 +29,9 @@ SOFTWARE. #include "doctest_compatibility.h" -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private #include diff --git a/test/src/unit-conversions.cpp b/test/src/unit-conversions.cpp index de8040c87..c71e230d9 100644 --- a/test/src/unit-conversions.cpp +++ b/test/src/unit-conversions.cpp @@ -29,10 +29,9 @@ SOFTWARE. #include "doctest_compatibility.h" -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private #include #include diff --git a/test/src/unit-iterators1.cpp b/test/src/unit-iterators1.cpp index 45a84a07f..1ff8958fa 100644 --- a/test/src/unit-iterators1.cpp +++ b/test/src/unit-iterators1.cpp @@ -29,10 +29,9 @@ SOFTWARE. #include "doctest_compatibility.h" -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private TEST_CASE("iterators 1") { diff --git a/test/src/unit-json_pointer.cpp b/test/src/unit-json_pointer.cpp index 61b6e323b..14d8cd183 100644 --- a/test/src/unit-json_pointer.cpp +++ b/test/src/unit-json_pointer.cpp @@ -29,10 +29,9 @@ SOFTWARE. #include "doctest_compatibility.h" -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private TEST_CASE("JSON pointers") { diff --git a/test/src/unit-regression1.cpp b/test/src/unit-regression1.cpp index 18820d171..9dcc75b09 100644 --- a/test/src/unit-regression1.cpp +++ b/test/src/unit-regression1.cpp @@ -33,10 +33,9 @@ DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal") // for some reason including this after the json header leads to linker errors with VS 2017... #include -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private #include #include diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index 69e3dea9b..ca50cdd41 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -33,10 +33,9 @@ DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal") // for some reason including this after the json header leads to linker errors with VS 2017... #include -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private #include #include diff --git a/test/src/unit-unicode.cpp b/test/src/unit-unicode.cpp index 8e122d5dd..63a9d5010 100644 --- a/test/src/unit-unicode.cpp +++ b/test/src/unit-unicode.cpp @@ -32,10 +32,9 @@ SOFTWARE. // for some reason including this after the json header leads to linker errors with VS 2017... #include -#define private public +#define JSON_TESTS_PRIVATE #include using nlohmann::json; -#undef private #include #include