Skip to content

Commit 931fbf8

Browse files
committed
Member construction is added for the tagged object
1 parent 1d41396 commit 931fbf8

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

Tests/taggedjsonobject_test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ TEST(ConstructorTests, ConstructorFromQJsonObjectNonStrictTestPass)
120120
ASSERT_NO_THROW(MissingValuedClass(jsonObject, false));
121121
}
122122

123+
// Tagged object can be constructed with values of its members
124+
TEST(ConstructorTests, ConstructorFromMemberInitialization)
125+
{
126+
OuterClass testObj{ EXPECTED_INT_RESULT, EXPECTED_STRING_RESULT, EXPECTED_DOUBLE_RESULT, InnerClass{QJsonObject{}, false}, QJsonObject{}, QJsonValue{}, QJsonValue{}};
127+
128+
ASSERT_EQ(EXPECTED_INT_RESULT, *testObj.example_int);
129+
}
130+
123131

124132
// Asterisk operator can be used for obtaining the target data
125133
TEST_F(TaggedObjectFixture, IntegerGetFromAsterisk)

inc/taggedjsonarray.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ class TaggedJSONArray <T, typename std::enable_if_t<TJO_JSON_COMPATIBLE>>
6464
throw(std::runtime_error("Invalid data has been encountered while parsing the json data for TaggedJSONArray"));
6565
}
6666

67+
//! Implicit value constructor for the tagged object constructor
68+
template<typename V, typename = std::enable_if_t<std::is_convertible_v<V, QJsonArray>>>
69+
TaggedJSONArray(V&& val) { m_arr = std::forward<V>(val); };
70+
6771
//! Assignment operator setter
6872
template<typename V, typename = std::enable_if_t<std::is_convertible_v<V, QJsonArray>>>
6973
void operator=(V&& val) { m_arr = std::forward<V>(val); };
@@ -228,6 +232,10 @@ class TaggedJSONArray<T, typename std::enable_if_t<TJO_IS_TAGGED_OBJECT>>
228232
throw(std::runtime_error("Invalid data has been encountered while parsing the json data for TaggedJSONArray"));
229233
}
230234

235+
//! Implicit value constructor for the tagged object constructor
236+
template<typename V, typename = std::enable_if_t<std::is_convertible_v<V, std::vector<T>>>>
237+
TaggedJSONArray(V&& val) { m_arr = std::forward<V>(val); };
238+
231239
//!Assignment operator to get the whole container data
232240
template<typename V, typename = std::enable_if_t<std::is_convertible_v<V, std::vector<T>>>>
233241
void operator=(V&& val) { m_arr = std::forward<V>(val); };

inc/taggedjsonobject.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ class TaggedJSONObject
3333
dispatchValue(std::move(val));
3434
};
3535

36+
//! Implicit value constructor for the tagged object constructor
37+
template<typename V, typename = std::enable_if_t<std::is_convertible_v<V, T>>>
38+
TaggedJSONObject(V&& val) { m_value = std::forward<V>(val); };
39+
3640
/*!
3741
* \brief l-value reference getter for the contained object
3842
* \return The l-value reference of the contained object

inc/taggedjsonobjectmacros.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
#define TAGGEDOBJECTMACRO_INITIALIZE_MEMBER_VALUE(type, name) name(val[#name], checkValues),
1717
#define TAGGEDOBJECTMACRO_INITIALIZE_MEMBER_VALUE_UNPACK(pair) TAGGEDOBJECTMACRO_INITIALIZE_MEMBER_VALUE pair
1818

19+
#define TAGGEDOBJECTMACRO_LIST_MEMBERS(type, name) type name,
20+
#define TAGGEDOBJECTMACRO_LIST_MEMBERS_UNPACK(pair) TAGGEDOBJECTMACRO_LIST_MEMBERS pair
21+
22+
#define TAGGEDOBJECTMACRO_MOVE_PARAMETERS(type, name) name(std::move(name)),
23+
#define TAGGEDOBJECTMACRO_MOVE_PARAMETERS_UNPACK(pair) TAGGEDOBJECTMACRO_MOVE_PARAMETERS pair
24+
1925

2026
namespace TaggedObject {
2127
inline QJsonObject getJSONObjectFromJSONText(const QByteArray& json)
@@ -47,6 +53,7 @@ public:\
4753
explicit CLASS_NAME(QJsonValue val, const bool checkValues=true) : MAP(TAGGEDOBJECTMACRO_INITIALIZE_MEMBER_VALUE_UNPACK, __VA_ARGS__) dummyVar(false) {}; \
4854
explicit CLASS_NAME(const QByteArray& json, const bool checkValues=true) : CLASS_NAME(TaggedObject::getJSONObjectFromJSONText(json), checkValues) {};\
4955
explicit CLASS_NAME(const QString& filePath, const bool checkValues=true) : CLASS_NAME(TaggedObject::getJSONObjectFromFile(filePath), checkValues) {};\
56+
explicit CLASS_NAME(MAP(TAGGEDOBJECTMACRO_LIST_MEMBERS_UNPACK, __VA_ARGS__) const bool checkValues=true) : MAP(TAGGEDOBJECTMACRO_MOVE_PARAMETERS_UNPACK, __VA_ARGS__) dummyVar(false) {};\
5057
MAP(TAGGEDOBJECTMACRO_DECLARE_MEMBER_UNPACK, __VA_ARGS__)\
5158
private:\
5259
bool dummyVar;\

0 commit comments

Comments
 (0)