Skip to content

Commit

Permalink
Add C++20 3-way comparison operator and fix broken comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
falbrechtskirchinger committed Apr 29, 2022
1 parent ee51661 commit 9c834ea
Show file tree
Hide file tree
Showing 11 changed files with 1,082 additions and 454 deletions.
17 changes: 13 additions & 4 deletions cmake/test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ endfunction()
#############################################################################
# json_test_add_test_for(
# <file>
# [NAME <name>]
# MAIN <main>
# [CXX_STANDARDS <version_number>...] [FORCE])
#
Expand All @@ -165,22 +166,30 @@ endfunction()
#
# if C++ standard <version_number> is supported by the compiler and the
# source file contains JSON_HAS_CPP_<version_number>.
# Use NAME <name> to override the filename-derived test name.
# Use FORCE to create the test regardless of the file containing
# JSON_HAS_CPP_<version_number>.
# Test targets are linked against <main>.
# CXX_STANDARDS defaults to "11".
#############################################################################

function(json_test_add_test_for file)
cmake_parse_arguments(args "FORCE" "MAIN" "CXX_STANDARDS" ${ARGN})

get_filename_component(file_basename ${file} NAME_WE)
string(REGEX REPLACE "unit-([^$]+)" "test-\\1" test_name ${file_basename})
cmake_parse_arguments(args "FORCE" "MAIN;NAME" "CXX_STANDARDS" ${ARGN})

if("${args_MAIN}" STREQUAL "")
message(FATAL_ERROR "Required argument MAIN <main> missing.")
endif()

if("${args_NAME}" STREQUAL "")
get_filename_component(file_basename ${file} NAME_WE)
string(REGEX REPLACE "unit-([^$]+)" "test-\\1" test_name ${file_basename})
else()
set(test_name ${args_NAME})
if(NOT test_name MATCHES "test-[^$]+")
message(FATAL_ERROR "Test name must start with 'test-'.")
endif()
endif()

if("${args_CXX_STANDARDS}" STREQUAL "")
set(args_CXX_STANDARDS 11)
endif()
Expand Down
5 changes: 5 additions & 0 deletions doc/mkdocs/docs/api/basic_json/operator_spaceship.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# <small>nlohmann::basic_json::</small>operator<=>

## Version history

- Added in version 3.11.0.
1 change: 1 addition & 0 deletions doc/mkdocs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ nav:
- 'operator value_t': api/basic_json/operator_value_t.md
- 'operator[]': api/basic_json/operator[].md
- 'operator=': api/basic_json/operator=.md
- 'operator<=>': api/basic_json/operator_spaceship.md
- 'operator==': api/basic_json/operator_eq.md
- 'operator!=': api/basic_json/operator_ne.md
- 'operator<': api/basic_json/operator_lt.md
Expand Down
22 changes: 17 additions & 5 deletions include/nlohmann/detail/macro_scope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,22 @@
#endif

#ifndef JSON_HAS_THREE_WAY_COMPARISON
#if defined(__cpp_lib_three_way_comparison) && __cpp_lib_three_way_comparison >= 201907L \
&& defined(__cpp_impl_three_way_comparison)&& __cpp_impl_three_way_comparison >= 201907L
#define JSON_HAS_THREE_WAY_COMPARISON 1
#else
#define JSON_HAS_THREE_WAY_COMPARISON 0
#if defined(__cpp_impl_three_way_comparison) && __cpp_impl_three_way_comparison >= 201907L
#if !defined(__cpp_lib_three_way_comparison) && defined(__has_include)
#if __has_include(<compare>)
#include <compare>
#endif
#endif
#if defined(__cpp_lib_three_way_comparison) && __cpp_lib_three_way_comparison >= 201907L
#define JSON_HAS_THREE_WAY_COMPARISON 1
#endif
#endif
#endif

#ifndef JSON_HAS_THREE_WAY_COMPARISON
#define JSON_HAS_THREE_WAY_COMPARISON 0
#endif

// disable documentation warnings on clang
#if defined(__clang__)
#pragma clang diagnostic push
Expand Down Expand Up @@ -423,3 +431,7 @@
#ifndef JSON_DIAGNOSTICS
#define JSON_DIAGNOSTICS 0
#endif

#ifndef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
#define JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON 0
#endif
2 changes: 2 additions & 0 deletions include/nlohmann/detail/macro_unscope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#undef JSON_HAS_FILESYSTEM
#undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
#undef JSON_HAS_THREE_WAY_COMPARISON
#undef JSON_HAS_RANGES
#undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
#endif

#include <nlohmann/thirdparty/hedley/hedley_undef.hpp>
29 changes: 28 additions & 1 deletion include/nlohmann/detail/value_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
#include <cstdint> // uint8_t
#include <string> // string

#include <nlohmann/detail/macro_scope.hpp>
#if JSON_HAS_THREE_WAY_COMPARISON
#include <compare> // partial_ordering
#endif

namespace nlohmann
{
namespace detail
Expand Down Expand Up @@ -64,7 +69,11 @@ Returns an ordering that is similar to Python:
@since version 1.0.0
*/
inline bool operator<(const value_t lhs, const value_t rhs) noexcept
#if JSON_HAS_THREE_WAY_COMPARISON
inline std::partial_ordering operator<=>(const value_t lhs, const value_t rhs) noexcept // *NOPAD*
#else
inline bool operator<(const value_t lhs, const value_t rhs) noexcept
#endif
{
static constexpr std::array<std::uint8_t, 9> order = {{
0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */,
Expand All @@ -75,7 +84,25 @@ inline bool operator<(const value_t lhs, const value_t rhs) noexcept

const auto l_index = static_cast<std::size_t>(lhs);
const auto r_index = static_cast<std::size_t>(rhs);
#if JSON_HAS_THREE_WAY_COMPARISON
if (l_index < order.size() && r_index < order.size())
{
return order[l_index] <=> order[r_index]; // *NOPAD*
}
return std::partial_ordering::unordered;
#else
return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index];
#endif
}

// GCC disagrees with Clang, MSVC, and ICC; selects builtin operator over user-defined
// (see GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105200)
// ICC may produce incorrect result; test and add if needed
#if JSON_HAS_THREE_WAY_COMPARISON && defined(__GNUC__)
inline bool operator<(const value_t lhs, const value_t rhs) noexcept
{
return std::is_lt(lhs <=> rhs); // *NOPAD*
}
#endif
} // namespace detail
} // namespace nlohmann
Loading

0 comments on commit 9c834ea

Please sign in to comment.