swift
Loading...
Searching...
No Matches
testvalueobject.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
4#ifndef SWIFT_MISC_TESTVALUEOBJECT_H
5#define SWIFT_MISC_TESTVALUEOBJECT_H
6
10
11#include <type_traits>
12
13#include <QDBusArgument>
14#include <QHash>
15#include <QMap>
16#include <QMetaType>
17#include <QString>
18
19#include "misc/collection.h"
20#include "misc/dictionary.h"
21#include "misc/metaclass.h"
22#include "misc/propertyindex.h"
23#include "misc/sequence.h"
24#include "misc/valueobject.h"
25#include "misc/variant.h"
26
27class QJsonArray;
28class QJsonValueRef;
29
30namespace swift::misc
31{
33 class CTestValueObject : public swift::misc::CValueObject<CTestValueObject>
34 {
35 public:
38 {
39 IndexName = 10000,
40 IndexDescription,
41 };
42
44 CTestValueObject() = default;
45
47 CTestValueObject(const QString &name, const QString &description) : m_name(name), m_description(description) {}
48
50 const QString &getName() const { return m_name; }
51
53 void setName(const QString &name) { m_name = name; }
54
56 const QString &getDescription() const { return m_description; }
57
59 void setDescription(const QString &description) { m_description = description; }
60
63 {
64 if (index.isMyself())
65 {
66 return CVariant::fromValue(*this);
67 }
68 const auto i = index.frontCasted<ColumnIndex>();
69 switch (i)
70 {
71 case IndexDescription: return CVariant::fromValue(this->m_description);
72 case IndexName: return CVariant::fromValue(this->m_name);
73 default: return CValueObject::propertyByIndex(index);
74 }
75 }
76
78 void setPropertyByIndex(const swift::misc::CPropertyIndex &index, const CVariant &variant)
79 {
80 if (index.isMyself())
81 {
82 (*this) = variant.value<CTestValueObject>();
83 return;
84 }
85 const auto i = index.frontCasted<ColumnIndex>();
86 switch (i)
87 {
88 case IndexDescription: this->setDescription(variant.value<QString>()); break;
89 case IndexName: this->setName(variant.value<QString>()); break;
90 default: CValueObject::setPropertyByIndex(index, variant); break;
91 }
92 }
93
95 QString convertToQString(bool i18n = false) const
96 {
97 Q_UNUSED(i18n);
98 QString s(this->m_name);
99 s.append(" ").append(this->m_description);
100 return s;
101 }
102
103 private:
104 QString m_name;
105 QString m_description;
106
109 SWIFT_METAMEMBER(name),
110 SWIFT_METAMEMBER(description));
111 };
112
114 struct CNotHashable
115 {
116 int n;
117 bool operator<(const CNotHashable &other) const { return n < other.n; }
118 QString toQString(bool = false) const { return {}; } // cppcheck-suppress functionStatic
119 bool operator==(const CNotHashable &other) const { return n == other.n; }
120 };
121 inline QJsonArray &operator<<(QJsonArray &a, const CNotHashable &) { return a; }
122 inline const QJsonValueRef &operator>>(const QJsonValueRef &v, CNotHashable &) { return v; }
123 inline QDBusArgument &operator<<(QDBusArgument &a, const CNotHashable &) { return a; }
124 inline const QDBusArgument &operator>>(const QDBusArgument &a, const CNotHashable &) { return a; }
125 inline QDataStream &operator<<(QDataStream &a, const CNotHashable &) { return a; }
126 inline QDataStream &operator>>(QDataStream &a, const CNotHashable &) { return a; }
128
129} // namespace swift::misc
130
131Q_DECLARE_METATYPE(swift::misc::CTestValueObject)
134
135Q_DECLARE_METATYPE(swift::misc::CNotHashable)
136
137// We need to typedef because 'commas' confuse the Q_DECLARE_METATYPE macro
138// https://bugreports.qt-project.org/browse/QTBUG-11485
139
140
141using CValueObjectDictionary = swift::misc::CDictionary<swift::misc::CTestValueObject, swift::misc::CTestValueObject>;
142
145 swift::misc::CDictionary<swift::misc::CTestValueObject, swift::misc::CTestValueObject, QHash>;
146
148using CNotHashableDictionary = swift::misc::CDictionary<swift::misc::CNotHashable, QString>;
149
151using CNotHashableMapDictionary = swift::misc::CDictionary<swift::misc::CNotHashable, QString, QMap>;
152Q_DECLARE_METATYPE(CValueObjectDictionary)
153Q_DECLARE_METATYPE(CNotHashableDictionary)
154
155static_assert(std::is_same<CValueObjectDictionary::impl_type, CValueObjectHashDictionary::impl_type>::value,
156 "Expected CValueObjectDictionary to use QHash");
157static_assert(std::is_same<CNotHashableDictionary::impl_type, CNotHashableMapDictionary::impl_type>::value,
158 "Expected CDictionary<CNotHashableDictionary, Value> to use QMap");
159
161
162#endif // SWIFT_MISC_TESTVALUEOBJECT_H
Generic ordered container with value semantics.
Definition collection.h:113
bool isMyself() const
Myself index, used with nesting.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
Generic sequential container with value semantics.
Definition sequence.h:86
void setPropertyByIndex(const swift::misc::CPropertyIndex &index, const CVariant &variant)
Set property by index.
ColumnIndex
Properties by index.
const QString & getName() const
Get name.
QString convertToQString(bool i18n=false) const
Cast as QString.
void setDescription(const QString &description)
Set description.
CTestValueObject(const QString &name, const QString &description)
Constructor.
void setName(const QString &name)
Set name.
CTestValueObject()=default
Default constructor.
CVariant propertyByIndex(const swift::misc::CPropertyIndex &index) const
Property by index.
const QString & getDescription() const
Get description.
Mix of the most commonly used mixin classes.
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition variant.h:69
static CVariant fromValue(T &&value)
Construct a variant from a value.
Definition variant.h:139
T value() const
Return the value converted to the type T.
Definition variant.h:154
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition mixinindex.h:167
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition mixinindex.h:180
#define SWIFT_METAMEMBER(MEMBER,...)
Macro to define an element within a metaclass.
Definition metaclass.h:72
#define SWIFT_METACLASS(CLASS,...)
Macro to define a nested metaclass that describes the attributes of its enclosing class.
Definition metaclass.h:52
Free functions in swift::misc.
QDebug operator<<(QDebug d, const CRange< I > &range)
Streaming operators for CRange to qDebug.
Definition range.h:307
QString & append(QChar ch)
swift::misc::CDictionary< swift::misc::CNotHashable, QString > CNotHashableDictionary
Test value object dictionary using ordered container with not hashable key.
swift::misc::CDictionary< swift::misc::CTestValueObject, swift::misc::CTestValueObject > CValueObjectDictionary
Test value object dictionary using ordered container.
swift::misc::CDictionary< swift::misc::CTestValueObject, swift::misc::CTestValueObject, QHash > CValueObjectHashDictionary
Test value object dictionary using unordered container.
swift::misc::CDictionary< swift::misc::CNotHashable, QString, QMap > CNotHashableMapDictionary
Test value object dictionary using unordered container with not hashable key.