mirror of
https://github.com/nlohmann/json.git
synced 2025-07-28 12:02:00 +03:00
Allow custom base class as node customization point (#3110)
Co-authored-by: Niels Lohmann <niels.lohmann@gmail.com> Co-authored-by: Florian Albrechtskirchinger <falbrechtskirchinger@gmail.com> Co-authored-by: barcode <barcode@example.com>
This commit is contained in:
@ -2590,12 +2590,13 @@ JSON_HEDLEY_DIAGNOSTIC_POP
|
||||
class NumberUnsignedType, class NumberFloatType, \
|
||||
template<typename> class AllocatorType, \
|
||||
template<typename, typename = void> class JSONSerializer, \
|
||||
class BinaryType>
|
||||
class BinaryType, \
|
||||
class CustomBaseClass>
|
||||
|
||||
#define NLOHMANN_BASIC_JSON_TPL \
|
||||
basic_json<ObjectType, ArrayType, StringType, BooleanType, \
|
||||
NumberIntegerType, NumberUnsignedType, NumberFloatType, \
|
||||
AllocatorType, JSONSerializer, BinaryType>
|
||||
AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>
|
||||
|
||||
// Macros to simplify conversion from/to types
|
||||
|
||||
@ -3389,7 +3390,8 @@ NLOHMANN_JSON_NAMESPACE_END
|
||||
template<typename U> class AllocatorType = std::allocator,
|
||||
template<typename T, typename SFINAE = void> class JSONSerializer =
|
||||
adl_serializer,
|
||||
class BinaryType = std::vector<std::uint8_t>>
|
||||
class BinaryType = std::vector<std::uint8_t>, // cppcheck-suppress syntaxError
|
||||
class CustomBaseClass = void>
|
||||
class basic_json;
|
||||
|
||||
/// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
|
||||
@ -13673,6 +13675,40 @@ NLOHMANN_JSON_NAMESPACE_END
|
||||
|
||||
// #include <nlohmann/detail/iterators/primitive_iterator.hpp>
|
||||
|
||||
// #include <nlohmann/detail/json_custom_base_class.hpp>
|
||||
|
||||
|
||||
#include <type_traits> // conditional, is_same
|
||||
|
||||
// #include <nlohmann/detail/abi_macros.hpp>
|
||||
|
||||
|
||||
NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||
namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
@brief Default base class of the @ref basic_json class.
|
||||
|
||||
So that the correct implementations of the copy / move ctors / assign operators
|
||||
of @ref basic_json do not require complex case distinctions
|
||||
(no base class / custom base class used as customization point),
|
||||
@ref basic_json always has a base class.
|
||||
By default, this class is used because it is empty and thus has no effect
|
||||
on the behavior of @ref basic_json.
|
||||
*/
|
||||
struct json_default_base {};
|
||||
|
||||
template<class T>
|
||||
using json_base_class = typename std::conditional <
|
||||
std::is_same<T, void>::value,
|
||||
json_default_base,
|
||||
T
|
||||
>::type;
|
||||
|
||||
} // namespace detail
|
||||
NLOHMANN_JSON_NAMESPACE_END
|
||||
|
||||
// #include <nlohmann/detail/json_pointer.hpp>
|
||||
// __ _____ _____ _____
|
||||
// __| | __| | | | JSON for Modern C++
|
||||
@ -19271,6 +19307,7 @@ The invariants are checked by member function assert_invariant().
|
||||
*/
|
||||
NLOHMANN_BASIC_JSON_TPL_DECLARATION
|
||||
class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
|
||||
: public ::nlohmann::detail::json_base_class<CustomBaseClass>
|
||||
{
|
||||
private:
|
||||
template<detail::value_t> friend struct detail::external_constructor;
|
||||
@ -19297,6 +19334,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
|
||||
/// workaround type for MSVC
|
||||
using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
|
||||
using json_base_class_t = ::nlohmann::detail::json_base_class<CustomBaseClass>;
|
||||
|
||||
JSON_PRIVATE_UNLESS_TESTED:
|
||||
// convenience aliases for types residing in namespace detail;
|
||||
@ -20310,7 +20348,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
/// @brief copy constructor
|
||||
/// @sa https://json.nlohmann.me/api/basic_json/basic_json/
|
||||
basic_json(const basic_json& other)
|
||||
: m_type(other.m_type)
|
||||
: json_base_class_t(other),
|
||||
m_type(other.m_type)
|
||||
{
|
||||
// check of passed value is valid
|
||||
other.assert_invariant();
|
||||
@ -20378,11 +20417,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
/// @brief move constructor
|
||||
/// @sa https://json.nlohmann.me/api/basic_json/basic_json/
|
||||
basic_json(basic_json&& other) noexcept
|
||||
: m_type(std::move(other.m_type)),
|
||||
: json_base_class_t(std::move(other)),
|
||||
m_type(std::move(other.m_type)),
|
||||
m_value(std::move(other.m_value))
|
||||
{
|
||||
// check that passed value is valid
|
||||
other.assert_invariant(false);
|
||||
other.assert_invariant(false); // NOLINT(bugprone-use-after-move,hicpp-invalid-access-moved)
|
||||
|
||||
// invalidate payload
|
||||
other.m_type = value_t::null;
|
||||
@ -20398,7 +20438,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
std::is_nothrow_move_constructible<value_t>::value&&
|
||||
std::is_nothrow_move_assignable<value_t>::value&&
|
||||
std::is_nothrow_move_constructible<json_value>::value&&
|
||||
std::is_nothrow_move_assignable<json_value>::value
|
||||
std::is_nothrow_move_assignable<json_value>::value&&
|
||||
std::is_nothrow_move_assignable<json_base_class_t>::value
|
||||
)
|
||||
{
|
||||
// check that passed value is valid
|
||||
@ -20407,6 +20448,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
using std::swap;
|
||||
swap(m_type, other.m_type);
|
||||
swap(m_value, other.m_value);
|
||||
json_base_class_t::operator=(std::move(other));
|
||||
|
||||
set_parents();
|
||||
assert_invariant();
|
||||
|
Reference in New Issue
Block a user