From efe20d5391fea93ef84315868471c741ced75dc5 Mon Sep 17 00:00:00 2001 From: Kunal Tyagi Date: Sat, 26 Jun 2021 21:56:17 +0900 Subject: [PATCH] Add macros for template version of DEFINE_TYPE_{NON_,}INTRUSIVE --- include/nlohmann/detail/macro_scope.hpp | 34 +++++++++++++++++++++++++ single_include/nlohmann/json.hpp | 34 +++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index 4a948af44..04d7561e0 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -469,6 +469,40 @@ { \ } +/*! +@brief macro to briefly define intrusive serialization of a given type to/from any basic_json object +@def NLOHMANN_DEFINE_TYPE_INTRUSIVE_T +@since version 3.9.2 +*/ +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_T(Type, ...) \ + template \ + friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ + } \ + template \ + friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ + } + +/*! +@brief macro to briefly define non-intrusive serialization of a given type to/from any basic_json object +@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T +@since version 3.9.2 +*/ +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T(Type, ...) \ + template \ + void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ + } \ + template \ + void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ + } + #ifndef JSON_USE_IMPLICIT_CONVERSIONS #define JSON_USE_IMPLICIT_CONVERSIONS 1 #endif diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index aeb63f946..0c3e09c41 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2684,6 +2684,40 @@ using is_detected_convertible = { \ } +/*! +@brief macro to briefly define intrusive serialization of a given type to/from any basic_json object +@def NLOHMANN_DEFINE_TYPE_INTRUSIVE_T +@since version 3.9.2 +*/ +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_T(Type, ...) \ + template \ + friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ + } \ + template \ + friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ + } \ + +/*! +@brief macro to briefly define non-intrusive serialization of a given type to/from any basic_json object +@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T +@since version 3.9.2 +*/ +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T(Type, ...) \ + template \ + void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ + } \ + template \ + void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ + } + #ifndef JSON_USE_IMPLICIT_CONVERSIONS #define JSON_USE_IMPLICIT_CONVERSIONS 1 #endif