Skip to content

Commit

Permalink
Merge pull request #117 from oblivioncth/bugfix/json_member_override
Browse files Browse the repository at this point in the history
Make QX_JSON_MEMBER_OVERRIDE() struct external
  • Loading branch information
oblivioncth authored Jul 27, 2023
2 parents d931683 + 77c1b45 commit 620ca72
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
20 changes: 9 additions & 11 deletions lib/core/doc/res/snippets/qx-json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,16 @@ struct MySpecialStruct
int number;
QString name;

QX_JSON_STRUCT(number, name);

QX_JSON_DECLARE_MEMBER_OVERRIDES();

QX_JSON_MEMBER_OVERRIDE(name,
static Qx::JsonError fromJson(QString& member, const QJsonValue& jv)
{
member = "OverridenName";
return Qx::JsonError();
}
)
QX_JSON_STRUCT(number, name);
}

QX_JSON_MEMBER_OVERRIDE(MySpecialStruct, name,
static Qx::JsonError fromJson(QString& member, const QJsonValue& jv)
{
member = "OverridenName";
return Qx::JsonError();
}
)
//! [5]

//! [6]
Expand Down
32 changes: 20 additions & 12 deletions lib/core/include/qx/core/qx-json.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,15 @@ namespace QxJson \
#define QX_JSON_STRUCT_OUTSIDE(Struct, ...) __QX_JSON_META_STRUCT_OUTSIDE(Struct, std::make_tuple(QX_FOR_EACH_DELIM(QX_JSON_MEMBER, __VA_ARGS__)))
#define QX_JSON_STRUCT_OUTSIDE_X(Struct, ...) __QX_JSON_META_STRUCT_OUTSIDE(Struct, std::make_tuple(__VA_ARGS__))

#define QX_JSON_DECLARE_MEMBER_OVERRIDES() \
template<Qx::StringLiteral MemberN> \
struct QxJsonConversionOverride

#define QX_JSON_MEMBER_OVERRIDE(member, ...) \
#define QX_JSON_MEMBER_OVERRIDE(Struct, member, ...) \
namespace QxJson \
{ \
template<> \
struct QxJsonConversionOverride<#member> \
struct MemberOverrideCoverter<Struct, #member> \
{ \
__VA_ARGS__\
};
}; \
}

namespace Qx
{
Expand Down Expand Up @@ -174,6 +173,9 @@ namespace QxJson
template<typename T>
struct Converter;

template<class Struct, Qx::StringLiteral member>
struct MemberOverrideCoverter;

template<typename SelfType, typename DelayedSelfType>
struct QxJsonMetaStructOutside;

Expand Down Expand Up @@ -202,7 +204,7 @@ concept json_convertible = requires(T& tValue) {

template<class K, typename T, Qx::StringLiteral N>
concept json_override_convertible = requires(T& tValue) {
{ K::template QxJsonConversionOverride<N>::fromJson(tValue, QJsonValue()) } -> std::same_as<Qx::JsonError>;
{ MemberOverrideCoverter<K, N>::fromJson(tValue, QJsonValue()) } -> std::same_as<Qx::JsonError>;
};

template<typename Key, class Value>
Expand Down Expand Up @@ -242,8 +244,7 @@ namespace QxJsonPrivate
* false branches are taken. Different compilers seem to discard the untaken
* branch of the value dependent statement at different stages as it compiled
* fine with MSVC and a newer version of GCC, but not clang or older GCC versions.
* To clarify, compi
* lation would fail due to the type in the not-yet-discarded
* To clarify, compilation would fail due to the type in the not-yet-discarded
* branch not being declared.
*
* Putting the reference of the potentially undeclared types behind these functions
Expand All @@ -267,7 +268,14 @@ template<class K, typename T, Qx::StringLiteral N>
requires QxJson::json_override_convertible<K, T, N>
Qx::JsonError performOverrideConversion(T& value, const QJsonValue& jv)
{
return K::template QxJsonConversionOverride<N>::fromJson(value, jv);
return QxJson::MemberOverrideCoverter<K, N>::fromJson(value, jv);
}

template<typename T>
requires QxJson::json_convertible<T>
Qx::JsonError performRegularConversion(T& value, const QJsonValue& jv)
{
return QxJson::Converter<T>::fromJson(value, jv);
}
/*! @endcond */

Expand Down Expand Up @@ -335,7 +343,7 @@ struct Converter<T>
if constexpr(json_override_convertible<T, mType, mName>)
cnvError = QxJsonPrivate::performOverrideConversion<T, mType, mName>(value.*mPtr, mValue);
else
cnvError = Converter<mType>::fromJson(value.*mPtr, mValue);
cnvError = QxJsonPrivate::performRegularConversion<mType>(value.*mPtr, mValue);

return !cnvError.isValid();
}() && ...);
Expand Down
11 changes: 2 additions & 9 deletions lib/core/src/qx-json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,6 @@
* @sa QX_JSON_STRUCT_OUTSIDE()
*/

/*!
* @def QX_JSON_DECLARE_MEMBER_OVERRIDES()
*
* Declares that a JSON-tried struct has member/key specific value parsing overrides.
*
* This macro must be used before any member specific overrides can be defined.
*
* @sa QX_JSON_MEMBER_OVERRIDE()
*/

/*!
* @def QX_JSON_MEMBER_OVERRIDE()
Expand All @@ -106,6 +97,8 @@
* The specified member will be parsed using the provided function instead of a
* potentially available generic one for that type.
*
* Must be used in namespace scope, outside of the struct definition.
*
* @snippet qx-json.cpp 5
*/

Expand Down

0 comments on commit 620ca72

Please sign in to comment.