6 #ifndef SWIFT_MISC_CONTAINERBASE_H
7 #define SWIFT_MISC_CONTAINERBASE_H
11 #include <QStringList>
32 return obj.toQString(i18n);
47 template <
class A,
class B>
58 template <
class Derived>
68 friend int compare(
const Derived &a,
const Derived &b)
70 for (
auto i = a.cbegin(), j = b.cbegin(); i != a.cend() && j != b.cend(); ++i, ++j)
72 if (*i < *j) {
return -1; }
73 if (*j < *i) {
return 1; }
75 if (a.size() < b.size()) {
return -1; }
76 if (b.size() < a.size()) {
return 1; }
82 template <
template <
class>
class Other>
85 return to(Other<typename Derived::value_type>());
92 template <
template <
class>
class Other,
class T>
93 Other<T>
to(Other<T> other)
const
95 for (
auto it = derived().cbegin(); it != derived().cend(); ++it) { other.push_back(*it); }
104 template <
class K0,
class V0,
class... KeysValues>
105 int removeIf(K0 k0, V0 v0, KeysValues... keysValues)
111 friend size_t qHash(
const Derived &) {
return 0; }
118 for (
auto it = derived().cbegin(); it != derived().cend(); ++it) { array << (*it); }
119 json.
insert(
"containerbase", array);
127 return jsonDoc.
toJson(format);
139 for (
auto ref : array)
143 typename Derived::value_type val;
145 derived().push_back(std::move(val));
152 if (jsonString.
isEmpty()) {
return; }
161 derived.convertFromJson(json);
169 if (jsonString.
isEmpty()) {
return obj; }
172 obj.convertFromJson(jsonObj);
183 if (jsonString.
isEmpty()) {
return obj; }
186 obj.convertFromJson(jsonObj);
191 errMsg = ex.
toString(
"JSON conversion");
208 for (
const auto &value : derived())
212 if (str.
isEmpty()) { str =
"{"; }
220 for (
const auto &obj : this->derived()) { sl.
append(obj.toQString(i18n)); }
232 argument.
beginArray(qMetaTypeId<typename Derived::value_type>());
233 std::for_each(derived().cbegin(), derived().cend(), [&](
const auto &value) { argument << value; });
242 while (!argument.
atEnd())
244 typename Derived::value_type value;
246 derived().push_back(value);
252 Derived &derived() {
return static_cast<Derived &
>(*this); }
253 const Derived &derived()
const {
return static_cast<const Derived &
>(*this); }
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...
Streamable status message, e.g.
CRTP class template which will generate marshalling operators for a derived class with its own marsha...
CRTP class template which will generate marshalling operators for a derived class with its own marsha...
CRTP class template from which a derived class can inherit string streaming operations.
QJsonObject jsonObjectFromString(const QString &json, bool acceptCacheFormat)
JSON Object from string.
auto MemberEqual(Ts... vs)
Predicate which tests whether some member functions return some values.
Free functions in swift::misc.
void beginArray(QMetaType id)
QByteArray toJson(QJsonDocument::JsonFormat format) const const
QJsonObject::iterator insert(QLatin1StringView key, const QJsonValue &value)
QJsonValue value(QLatin1StringView key) const const
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)