swift
Loading...
Searching...
No Matches
containerbase.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#ifndef SWIFT_MISC_CONTAINERBASE_H
7#define SWIFT_MISC_CONTAINERBASE_H
8
9#include <algorithm>
10
11#include <QStringList>
12
13#include "misc/json.h"
14#include "misc/jsonexception.h"
19#include "misc/predicates.h"
20#include "misc/range.h"
21
22namespace swift::misc
23{
26 {
27 public:
29 template <class U>
30 static QString stringify(const U &obj, bool i18n)
31 {
32 return obj.toQString(i18n);
33 }
34
35 static QString stringify(int n, bool i18n) { return i18n ? QLocale().toString(n) : QString::number(n); }
37 static QString stringify(uint n, bool i18n) { return i18n ? QLocale().toString(n) : QString::number(n); }
39 static QString stringify(qlonglong n, bool i18n) { return i18n ? QLocale().toString(n) : QString::number(n); }
41 static QString stringify(qulonglong n, bool i18n) { return i18n ? QLocale().toString(n) : QString::number(n); }
43 static QString stringify(double n, bool i18n) { return i18n ? QLocale().toString(n) : QString::number(n); }
45 static QString stringify(QString str, bool /*i18n*/) { return str; }
47 template <class A, class B>
48 static QString stringify(const std::pair<A, B> &pair, bool i18n)
49 {
50 return stringify(pair.first, i18n) + ":" + stringify(pair.second, i18n);
51 }
52 };
53
58 template <class Derived>
60 public CRangeBase<Derived>,
61 public mixin::MetaType<Derived>,
62 public mixin::DBusOperators<Derived>,
63 public mixin::JsonOperators<Derived>,
64 public mixin::String<Derived>
65 {
66 public:
68 friend int compare(const Derived &a, const Derived &b)
69 {
70 for (auto i = a.cbegin(), j = b.cbegin(); i != a.cend() && j != b.cend(); ++i, ++j)
71 {
72 if (*i < *j)
73 {
74 return -1;
75 }
76 if (*j < *i)
77 {
78 return 1;
79 }
80 }
81 if (a.size() < b.size())
82 {
83 return -1;
84 }
85 if (b.size() < a.size())
86 {
87 return 1;
88 }
89 return 0;
90 }
91
94 template <template <class> class Other>
95 auto to() const
96 {
97 return to(Other<typename Derived::value_type>());
98 }
99
104 template <template <class> class Other, class T>
105 Other<T> to(Other<T> other) const
106 {
107 for (auto it = derived().cbegin(); it != derived().cend(); ++it)
108 {
109 other.push_back(*it);
110 }
111 return other;
112 }
113
119 template <class K0, class V0, class... KeysValues>
120 int removeIf(K0 k0, V0 v0, KeysValues... keysValues)
121 {
122 return derived().removeIf(swift::misc::predicates::MemberEqual(k0, v0, keysValues...));
123 }
124
126 friend size_t qHash(const Derived &) { return 0; } // clazy:exclude=qhash-namespace
127
130 {
131 QJsonArray array;
132 QJsonObject json;
133 for (auto it = derived().cbegin(); it != derived().cend(); ++it)
134 {
135 array << (*it);
136 }
137 json.insert("containerbase", array);
138 return json;
139 }
140
143 {
144 QJsonDocument jsonDoc(toJson());
145 return jsonDoc.toJson(format);
146 }
147
150 void convertFromJson(const QJsonObject &json)
151 {
152 derived().clear();
153 QJsonValue value = json.value("containerbase");
154 if (value.isUndefined())
155 {
156 throw CJsonException("Missing 'containerbase'");
157 }
158 QJsonArray array = value.toArray();
159 int index = 0;
160 for (auto ref : array)
161 {
162 CJsonScope scope("containerbase", index++);
163 Q_UNUSED(scope);
164 typename Derived::value_type val;
165 ref >> val;
166 derived().push_back(std::move(val));
167 }
168 }
169
171 void convertFromJson(const QString &jsonString, bool acceptCacheFormat = false)
172 {
173 if (jsonString.isEmpty())
174 {
175 return;
176 }
177 const QJsonObject jsonObject = json::jsonObjectFromString(jsonString, acceptCacheFormat);
178 this->convertFromJson(jsonObject);
179 }
180
182 static Derived fromJson(const QJsonObject &json)
183 {
184 Derived derived;
185 derived.convertFromJson(json);
186 return derived;
187 }
188
190 static Derived fromJson(const QString &jsonString, bool acceptCacheJson = false)
191 {
192 Derived obj;
193 if (jsonString.isEmpty())
194 {
195 return obj;
196 }
197 const QJsonObject jsonObj =
198 acceptCacheJson ? json::swiftDataObjectValue(jsonString) : json::jsonObjectFromString(jsonString);
199 obj.convertFromJson(jsonObj);
200 return obj;
201 }
202
204 static Derived fromJsonNoThrow(const QString &jsonString, bool acceptCacheJson, bool &success, QString &errMsg)
205 {
206 success = false;
207 Derived obj;
208 try
209 {
210 if (jsonString.isEmpty())
211 {
212 return obj;
213 }
214 const QJsonObject jsonObj =
215 acceptCacheJson ? json::swiftDataObjectValue(jsonString) : json::jsonObjectFromString(jsonString);
216 obj.convertFromJson(jsonObj);
217 success = true;
218 }
219 catch (const CJsonException &ex)
220 {
221 errMsg = ex.toString("JSON conversion");
222 }
223 return obj;
224 }
225
228 const QString &prefix); // implemented in statusmessage.h
229
231 CStatusMessage convertFromJsonNoThrow(const QString &jsonString, const CLogCategoryList &categories,
232 const QString &prefix); // implemented in statusmessage.h
233
235 QString convertToQString(bool i18n = false) const
236 {
237 QString str;
238 for (const auto &value : derived())
239 {
240 str += (str.isEmpty() ? "{" : ", ") + CContainerHelper::stringify(value, i18n);
241 }
242 if (str.isEmpty())
243 {
244 str = "{";
245 }
246 return str += "}";
247 }
248
250 QStringList toStringList(bool i18n = false) const
251 {
252 QStringList sl;
253 for (const auto &obj : this->derived())
254 {
255 sl.append(obj.toQString(i18n));
256 }
257 return sl;
258 }
259
260 protected:
262 int getMetaTypeId() const { return qMetaTypeId<Derived>(); }
263
264 public:
266 void marshallToDbus(QDBusArgument &argument) const
267 {
268 argument.beginArray(qMetaTypeId<typename Derived::value_type>());
269 std::for_each(derived().cbegin(), derived().cend(), [&](const auto &value) { argument << value; });
270 argument.endArray();
271 }
272
274 void unmarshallFromDbus(const QDBusArgument &argument)
275 {
276 derived().clear();
277 argument.beginArray();
278 while (!argument.atEnd())
279 {
280 typename Derived::value_type value;
281 argument >> value;
282 derived().push_back(value);
283 }
284 argument.endArray();
285 }
286
287 private:
288 Derived &derived() { return static_cast<Derived &>(*this); }
289 const Derived &derived() const { return static_cast<const Derived &>(*this); }
290 };
291} // namespace swift::misc
292
293#endif // SWIFT_MISC_CONTAINERBASE_H
Base class for CCollection and CSequence adding mutating operations and CValueObject facility on top ...
QStringList toStringList(bool i18n=false) const
To string list.
QJsonObject toJson() const
Cast to JSON object.
static Derived fromJsonNoThrow(const QString &jsonString, bool acceptCacheJson, bool &success, QString &errMsg)
Static version of convertFromJson.
auto to() const
Return a new container of a different type, containing the same elements as this one.
void convertFromJson(const QString &jsonString, bool acceptCacheFormat=false)
Assign from JSON object string.
QString convertToQString(bool i18n=false) const
Cast as QString.
Other< T > to(Other< T > other) const
Return a new container of a different type, containing the same elements as this one.
QString toJsonString(QJsonDocument::JsonFormat format=QJsonDocument::Indented) const
Convenience function JSON as string.
static Derived fromJson(const QString &jsonString, bool acceptCacheJson=false)
Static version of convertFromJson.
void unmarshallFromDbus(const QDBusArgument &argument)
Unmarshall without begin/endStructure, for when composed within another object.
int removeIf(K0 k0, V0 v0, KeysValues... keysValues)
Remove elements matching some particular key/value pair(s).
void convertFromJson(const QJsonObject &json)
Assign from JSON object.
CStatusMessage convertFromJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix)
Call convertFromJson, catch any CJsonException that is thrown and return it as CStatusMessage.
static Derived fromJson(const QJsonObject &json)
Static version of convertFromJson.
friend size_t qHash(const Derived &)
Simplifies composition, returns 0 for performance.
friend int compare(const Derived &a, const Derived &b)
Return negative, zero, or positive if a is less than, equal to, or greater than b.
void marshallToDbus(QDBusArgument &argument) const
Marshall without begin/endStructure, for when composed within another object.
int getMetaTypeId() const
Returns the Qt meta type ID of this object.
Class providing static helper methods for different containers.
static QString stringify(const U &obj, bool i18n)
Stringify value object.
static QString stringify(qlonglong n, bool i18n)
Stringify qlonglong.
static QString stringify(const std::pair< A, B > &pair, bool i18n)
Stringify pair.
static QString stringify(double n, bool i18n)
Stringify double.
static QString stringify(QString str, bool)
Stringify QString.
static QString stringify(uint n, bool i18n)
Stringify uint.
static QString stringify(qulonglong n, bool i18n)
Stringify qulonglong.
static QString stringify(int n, bool i18n)
Stringify int.
Thrown when a convertFromJson method encounters an unrecoverable error in JSON data.
QString toString(const QString &prefix) const
As string info.
Pseudo-RAII pattern that tracks the current JSON value being converted.
A sequence of log categories.
Any container class with begin and end iterators can inherit from this CRTP class to gain some useful...
Definition range.h:33
Streamable status message, e.g.
CRTP class template which will generate marshalling operators for a derived class with its own marsha...
Definition mixindbus.h:39
CRTP class template which will generate marshalling operators for a derived class with its own marsha...
Definition mixinjson.h:37
CRTP class template from which a derived class can inherit common methods dealing with the metatype o...
CRTP class template from which a derived class can inherit string streaming operations.
Definition mixinstring.h:29
QJsonObject jsonObjectFromString(const QString &json, bool acceptCacheFormat)
JSON Object from string.
Definition json.cpp:419
auto MemberEqual(Ts... vs)
Predicate which tests whether some member functions return some values.
Definition predicates.h:26
Free functions in swift::misc.
bool atEnd() const const
void beginArray(QMetaType id)
QByteArray toJson(QJsonDocument::JsonFormat format) const const
QJsonObject::iterator insert(QLatin1StringView key, const QJsonValue &value)
bool isUndefined() const const
QJsonArray toArray() const const
void append(QList< T > &&value)
QString toString(QDate date, QLocale::FormatType format) const const
bool isEmpty() const const
QString number(double n, char format, int precision)