mirror of
https://github.com/nlohmann/json.git
synced 2024-12-20 02:49:03 +08:00
ad103e5b45
* Refactor unit test creation Add functions for creating tests and to supply test- and standard-specific build settings. Raises minimum CMake version to 3.13 in test directory. json_test_add_test_for( <file> MAIN <main> [CXX_STANDARDS <version_number>...] [FORCE]) Given a <file> unit-foo.cpp, produces test-foo_cpp<version_number> if C++ standard <version_number> is supported by the compiler and thesource file contains JSON_HAS_CPP_<version_number>. 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". json_test_set_test_options( all|<tests> [CXX_STANDARDS all|<args>...] [COMPILE_DEFINITIONS <args>...] [COMPILE_FEATURES <args>...] [COMPILE_OPTIONS <args>...] [LINK_LIBRARIES <args>...] [LINK_OPTIONS <args>...]) Supply test- and standard-specific build settings. Specify multiple tests using a list e.g., "test-foo;test-bar". Must be called BEFORE the test is created. * Use CMAKE_MODULE_PATH * Don't undef some macros if JSON_TEST_KEEP_MACROS is defined * Use JSON_TEST_KEEP_MACROS Incidentally enables the regression tests for #2546 and #3070. A CHECK_THROWS_WITH_AS in #3070 was disabled which is tracked in #3377 and a line in from_json(..., std_fs::path&) was marked with LCOV_EXCL_LINE. * Add three-way comparison feature test macro * Disable broken comparison if JSON_HAS_THREE_WAY_COMPARISON * Fix redefinition of inline constexpr statics Redelcaration of inline constexpr static data members in namespace scope was deprecated in C++17. Fixes -Werror=deprecated compilation failures. * Fix more test build failures due to missing noexcept * CI: update cmake_flags test to use CMake 3.13 in test directory Also change default for JSON_BuildTests option to depend on CMake version. * CI: turn *_CXXFLAGS into CMake lists * CI: use JSON_TestStandards to set CXX_STANDARD * CI: pass extra CXXFLAGS to standards tests
159 lines
4.5 KiB
C++
159 lines
4.5 KiB
C++
#pragma once
|
|
|
|
#include <cstddef> // size_t
|
|
#include <type_traits> // conditional, enable_if, false_type, integral_constant, 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>
|
|
|
|
namespace nlohmann
|
|
{
|
|
namespace detail
|
|
{
|
|
|
|
template<typename T>
|
|
using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
|
|
|
|
#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 constexpr T value{};
|
|
};
|
|
|
|
#ifndef JSON_HAS_CPP_17
|
|
|
|
template<typename T>
|
|
constexpr T static_const<T>::value; // NOLINT(readability-redundant-declaration)
|
|
|
|
#endif
|
|
|
|
} // namespace detail
|
|
} // namespace nlohmann
|