@@ -19,23 +19,20 @@ template <typename T, typename=std::enable_if_t<TJO_JSON_COMPATIBLE>>
1919class TaggedJSONObject
2020{
2121public:
22+ // ! Default constructor, which is useful if the parameters planned to be filled later
23+ explicit TaggedJSONObject () : m_value() {}
24+
2225 /* !
2326 * \brief TaggedJSONObject Constructor that takes the JSON data and stores it according to the template argument.
2427 * \param val Target JSON value to be stored.
2528 * \param checkValue If set to true, invalid conversions (missing value, wrong type etc.) will throw a runtime error.
2629 */
27- explicit TaggedJSONObject (QJsonValue val, const bool checkValue=true )
28- {
29- // Check if there is a valid data if it's intended
30- if (checkValue && val.isUndefined ())
31- throw (std::runtime_error (" Invalid data has been encountered while parsing the json data for TaggedJSONObject" ));
32-
33- dispatchValue (std::move (val));
34- };
30+ explicit TaggedJSONObject (const QJsonValue& val, const bool checkValue=true ) : m_value(dispatchValue(val, checkValue))
31+ {};
3532
3633 // ! Implicit value constructor for the tagged object constructor
3734 template <typename V, typename = std::enable_if_t <std::is_convertible_v<V, T>>>
38- TaggedJSONObject (V&& val) { m_value = std::forward<V>(val); };
35+ TaggedJSONObject (V&& val) : m_value( std::forward<V>(val)) { };
3936
4037 /* !
4138 * \brief l-value reference getter for the contained object
@@ -86,7 +83,7 @@ class TaggedJSONObject
8683 * \brief operator -> Shortcut for object values (QString, QVariant etc.) methods.
8784 * \return The contained object
8885 */
89- const T* const operator ->() const {return &m_value;};
86+ const T* operator ->() const {return &m_value;};
9087
9188 // ! Shortcut for value operations (for QJsonValue and QJsonObjects only)
9289 template <typename S=T, typename =std::enable_if_t <std::is_same_v<S, QJsonValue> || std::is_same_v<S, QJsonObject>>>
@@ -106,28 +103,34 @@ class TaggedJSONObject
106103 return QString (m_value);
107104 };
108105
106+ QJsonValue toJsonValue () const {return QJsonValue{m_value};}
107+
109108private:
110109 T m_value;
111110
112111 template <class TO >
113112 friend std::ostream& operator << (std::ostream& stream, const TaggedJSONObject<TO>& obj);
114113
115114 // Determine the function to be called at compile time based on the current template parameter
116- void dispatchValue (QJsonValue&& val){
115+ T dispatchValue (const QJsonValue& val, const bool checkValue){
116+ // Check if there is a valid data if it's intended
117+ if (checkValue && val.isUndefined ())
118+ throw (std::runtime_error (" Invalid data has been encountered while parsing the json data for TaggedJSONObject" ));
119+
117120 if constexpr (std::is_same_v<T, bool >)
118- m_value = val.toBool ();
121+ return val.toBool ();
119122 else if constexpr (std::is_integral_v<T>)
120- m_value = val.toInt ();
123+ return val.toInt ();
121124 else if constexpr (std::is_floating_point_v<T>)
122- m_value = val.toDouble ();
125+ return val.toDouble ();
123126 else if constexpr (std::is_same_v<T, QJsonValue>)
124- m_value = std::move ( val) ;
127+ return val;
125128 else if constexpr (std::is_same_v<T, QJsonObject>)
126- m_value = val.toObject ();
129+ return val.toObject ();
127130 else if constexpr (std::is_same_v<T, QString>)
128- m_value = val.toString ();
131+ return val.toString ();
129132 else if constexpr (std::is_same_v<T, QVariant>)
130- m_value = val.toVariant ();
133+ return val.toVariant ();
131134 else
132135 throw (std::invalid_argument (" Template argument for the TaggedJSONObject is not valid" ));
133136 }
0 commit comments