mirror of
https://github.com/nlohmann/json.git
synced 2026-10-22 22:08:55 +00:00
* Use standard library helpers where available, with C++11 fallbacks Add detail helpers that call the standard library function when the library provides it (checked with the __cpp_lib_* feature-test macros, so no new ABI-tag macros) and a C++11 implementation otherwise: - detail::clamp (std::clamp): by value, so that static constexpr members need no out-of-line definition before C++17; used in json_view's dump() buffer sizing, where step() can now be a static constexpr member. - detail::bit_cast (std::bit_cast): replaces dtoa_impl::reinterpret_bits and the memcpy bit copies in the number parser, to_chars, the json_view serializer, and binary_writer::to_char_type. - detail::byteswap (std::byteswap): one implementation instead of the binary writer's own byte_swap() overloads and the bswap chain in to_chars' store_msb_first(). - count_leading_zeros/count_trailing_zeros: two 32-bit _BitScanReverse/ _BitScanForward calls for MSVC x86/ARM32 instead of the loop, and std::countl_zero/countr_zero for compilers without builtins. - conjunction, disjunction, negation, bool_constant, and uncvref_t map to their std counterparts in C++17/C++20. Builtins and intrinsics stay ahead of the <bit> functions, as MSVC's std::countl_zero checks for LZCNT at run time unless AVX2 is enabled. The generated code is unchanged with Clang (C++11/20/23, arm64 and x86-64) and GCC 16 for parsing, dumping, and the binary formats. The binary reader keeps its own byte_swap() and the writer its memcpy calls: the smaller code from detail::byteswap/std::bit_cast there made GCC 16 inline get_number()/write_number() differently, which slowed reading by 10-18 % and writing by about 1 %. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Name the target byte order once (JSON_LITTLE_ENDIAN_TARGET, JSON_BIG_ENDIAN_TARGET) to_chars.hpp and bit_ops.hpp repeated the long __BYTE_ORDER__/_MSC_VER conditions; macro_scope.hpp now defines them once (undefined again in macro_unscope.hpp). Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Explain why the serializer growth bound is not a clamp Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
188 lines
5.6 KiB
C++
188 lines
5.6 KiB
C++
// __ _____ _____ _____
|
|
// __| | __| | | | JSON for Modern C++
|
|
// | | |__ | | | | | | version 3.12.0
|
|
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
|
//
|
|
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
|
|
// SPDX-FileCopyrightText: 2018 The Abseil Authors
|
|
// SPDX-License-Identifier: MIT AND Apache-2.0
|
|
|
|
#pragma once
|
|
|
|
#include <cstddef> // size_t
|
|
#include <type_traits> // conditional, enable_if, false_type, integral_constant, is_arithmetic, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type
|
|
#include <utility> // index_sequence, make_index_sequence, index_sequence_for
|
|
|
|
#include <nlohmann/detail/macro_scope.hpp>
|
|
|
|
#ifdef JSON_HAS_CPP_17
|
|
#include <algorithm> // clamp
|
|
#endif
|
|
|
|
NLOHMANN_JSON_NAMESPACE_BEGIN
|
|
namespace detail
|
|
{
|
|
|
|
#ifdef __cpp_lib_remove_cvref
|
|
template<typename T>
|
|
using uncvref_t = std::remove_cvref_t<T>;
|
|
#else
|
|
template<typename T>
|
|
using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
|
|
#endif
|
|
|
|
#ifdef JSON_HAS_CPP_14
|
|
|
|
// the following utilities are natively available in C++14
|
|
using std::enable_if_t;
|
|
using std::index_sequence;
|
|
using std::make_index_sequence;
|
|
using std::index_sequence_for;
|
|
|
|
#else
|
|
|
|
// alias templates to reduce boilerplate
|
|
template<bool B, typename T = void>
|
|
using enable_if_t = typename std::enable_if<B, T>::type;
|
|
|
|
// The following code is taken from https://github.com/abseil/abseil-cpp/blob/10cb35e459f5ecca5b2ff107635da0bfa41011b4/absl/utility/utility.h
|
|
// which is part of Google Abseil (https://github.com/abseil/abseil-cpp), licensed under the Apache License 2.0.
|
|
|
|
//// START OF CODE FROM GOOGLE ABSEIL
|
|
|
|
// integer_sequence
|
|
//
|
|
// Class template representing a compile-time integer sequence. An instantiation
|
|
// of `integer_sequence<T, Ints...>` has a sequence of integers encoded in its
|
|
// type through its template arguments (which is a common need when
|
|
// working with C++11 variadic templates). `absl::integer_sequence` is designed
|
|
// to be a drop-in replacement for C++14's `std::integer_sequence`.
|
|
//
|
|
// Example:
|
|
//
|
|
// template< class T, T... Ints >
|
|
// void user_function(integer_sequence<T, Ints...>);
|
|
//
|
|
// int main()
|
|
// {
|
|
// // user_function's `T` will be deduced to `int` and `Ints...`
|
|
// // will be deduced to `0, 1, 2, 3, 4`.
|
|
// user_function(make_integer_sequence<int, 5>());
|
|
// }
|
|
template <typename T, T... Ints>
|
|
struct integer_sequence
|
|
{
|
|
using value_type = T;
|
|
static constexpr std::size_t size() noexcept
|
|
{
|
|
return sizeof...(Ints);
|
|
}
|
|
};
|
|
|
|
// index_sequence
|
|
//
|
|
// A helper template for an `integer_sequence` of `size_t`,
|
|
// `absl::index_sequence` is designed to be a drop-in replacement for C++14's
|
|
// `std::index_sequence`.
|
|
template <size_t... Ints>
|
|
using index_sequence = integer_sequence<size_t, Ints...>;
|
|
|
|
namespace utility_internal
|
|
{
|
|
|
|
template <typename Seq, size_t SeqSize, size_t Rem>
|
|
struct Extend;
|
|
|
|
// Note that SeqSize == sizeof...(Ints). It's passed explicitly for efficiency.
|
|
template <typename T, T... Ints, size_t SeqSize>
|
|
struct Extend<integer_sequence<T, Ints...>, SeqSize, 0>
|
|
{
|
|
using type = integer_sequence < T, Ints..., (Ints + SeqSize)... >;
|
|
};
|
|
|
|
template <typename T, T... Ints, size_t SeqSize>
|
|
struct Extend<integer_sequence<T, Ints...>, SeqSize, 1>
|
|
{
|
|
using type = integer_sequence < T, Ints..., (Ints + SeqSize)..., 2 * SeqSize >;
|
|
};
|
|
|
|
// Recursion helper for 'make_integer_sequence<T, N>'.
|
|
// 'Gen<T, N>::type' is an alias for 'integer_sequence<T, 0, 1, ... N-1>'.
|
|
template <typename T, size_t N>
|
|
struct Gen
|
|
{
|
|
using type =
|
|
typename Extend < typename Gen < T, N / 2 >::type, N / 2, N % 2 >::type;
|
|
};
|
|
|
|
template <typename T>
|
|
struct Gen<T, 0>
|
|
{
|
|
using type = integer_sequence<T>;
|
|
};
|
|
|
|
} // namespace utility_internal
|
|
|
|
// Compile-time sequences of integers
|
|
|
|
// make_integer_sequence
|
|
//
|
|
// This template alias is equivalent to
|
|
// `integer_sequence<int, 0, 1, ..., N-1>`, and is designed to be a drop-in
|
|
// replacement for C++14's `std::make_integer_sequence`.
|
|
template <typename T, T N>
|
|
using make_integer_sequence = typename utility_internal::Gen<T, N>::type;
|
|
|
|
// make_index_sequence
|
|
//
|
|
// This template alias is equivalent to `index_sequence<0, 1, ..., N-1>`,
|
|
// and is designed to be a drop-in replacement for C++14's
|
|
// `std::make_index_sequence`.
|
|
template <size_t N>
|
|
using make_index_sequence = make_integer_sequence<size_t, N>;
|
|
|
|
// index_sequence_for
|
|
//
|
|
// Converts a typename pack into an index sequence of the same length, and
|
|
// is designed to be a drop-in replacement for C++14's
|
|
// `std::index_sequence_for()`
|
|
template <typename... Ts>
|
|
using index_sequence_for = make_index_sequence<sizeof...(Ts)>;
|
|
|
|
//// END OF CODE FROM GOOGLE ABSEIL
|
|
|
|
#endif
|
|
|
|
// dispatch utility (taken from ranges-v3)
|
|
template<unsigned N> struct priority_tag : priority_tag < N - 1 > {};
|
|
template<> struct priority_tag<0> {};
|
|
|
|
// taken from ranges-v3
|
|
template<typename T>
|
|
struct static_const
|
|
{
|
|
static JSON_INLINE_VARIABLE constexpr T value{};
|
|
};
|
|
|
|
#ifndef JSON_HAS_CPP_17
|
|
template<typename T>
|
|
constexpr T static_const<T>::value;
|
|
#endif
|
|
|
|
/// @a v bounded to [@a lo, @a hi] (requires !(hi < lo)); like std::clamp,
|
|
/// but by value, so that static constexpr members can be passed without an
|
|
/// out-of-line definition before C++17
|
|
template<typename T, enable_if_t<std::is_arithmetic<T>::value, int> = 0>
|
|
constexpr T clamp(T v, T lo, T hi) noexcept
|
|
{
|
|
#ifdef __cpp_lib_clamp
|
|
return std::clamp(v, lo, hi);
|
|
#else
|
|
// NOLINTNEXTLINE(readability-avoid-nested-conditional-operator): a constant expression of C++11
|
|
return v < lo ? lo : (hi < v ? hi : v);
|
|
#endif
|
|
}
|
|
|
|
} // namespace detail
|
|
NLOHMANN_JSON_NAMESPACE_END
|