swift
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 
27 class QJsonArray;
28 class QJsonValueRef;
29 
30 namespace swift::misc
31 {
33  class CTestValueObject : public swift::misc::CValueObject<CTestValueObject>
34  {
35  public:
38  {
39  IndexName = 10000,
40  IndexDescription,
41  };
42 
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()) { return CVariant::from(*this); }
65  ColumnIndex i = index.frontCasted<ColumnIndex>();
66  switch (i)
67  {
68  case IndexDescription: return CVariant::fromValue(this->m_description);
69  case IndexName: return CVariant::fromValue(this->m_name);
70  default: return CValueObject::propertyByIndex(index);
71  }
72  }
73 
75  void setPropertyByIndex(const swift::misc::CPropertyIndex &index, const CVariant &variant)
76  {
77  if (index.isMyself())
78  {
79  (*this) = variant.to<CTestValueObject>();
80  return;
81  }
82  ColumnIndex i = index.frontCasted<ColumnIndex>();
83  switch (i)
84  {
85  case IndexDescription: this->setDescription(variant.value<QString>()); break;
86  case IndexName: this->setName(variant.value<QString>()); break;
87  default: CValueObject::setPropertyByIndex(index, variant); break;
88  }
89  }
90 
92  QString convertToQString(bool i18n = false) const
93  {
94  Q_UNUSED(i18n);
95  QString s(this->m_name);
96  s.append(" ").append(this->m_description);
97  return s;
98  }
99 
100  private:
101  QString m_name;
102  QString m_description;
103 
104  SWIFT_METACLASS(
106  SWIFT_METAMEMBER(name),
107  SWIFT_METAMEMBER(description));
108  };
109 
111  struct CNotHashable
112  {
113  int n;
114  bool operator<(const CNotHashable &other) const { return n < other.n; }
115  QString toQString(bool = false) const { return {}; }
116  bool operator==(const CNotHashable &other) const { return n == other.n; }
117  };
118  inline QJsonArray &operator<<(QJsonArray &a, const CNotHashable &) { return a; }
119  inline const QJsonValueRef &operator>>(const QJsonValueRef &v, CNotHashable &) { return v; }
120  inline QDBusArgument &operator<<(QDBusArgument &a, const CNotHashable &) { return a; }
121  inline const QDBusArgument &operator>>(const QDBusArgument &a, const CNotHashable &) { return a; }
122  inline QDataStream &operator<<(QDataStream &a, const CNotHashable &) { return a; }
123  inline QDataStream &operator>>(QDataStream &a, const CNotHashable &) { return a; }
125 
126 } // namespace swift::misc
127 
128 Q_DECLARE_METATYPE(swift::misc::CTestValueObject)
131 
132 Q_DECLARE_METATYPE(swift::misc::CNotHashable)
133 
134 // We need to typedef because 'commas' confuse the Q_DECLARE_METATYPE macro
135 // https://bugreports.qt-project.org/browse/QTBUG-11485
136 
139 
143 
146 
149 Q_DECLARE_METATYPE(CValueObjectDictionary)
150 Q_DECLARE_METATYPE(CNotHashableDictionary)
151 
152 static_assert(std::is_same<CValueObjectDictionary::impl_type, CValueObjectHashDictionary::impl_type>::value,
153  "Expected CValueObjectDictionary to use QHash");
154 static_assert(std::is_same<CNotHashableDictionary::impl_type, CNotHashableMapDictionary::impl_type>::value,
155  "Expected CDictionary<CNotHashableDictionary, Value> to use QMap");
156 
158 
159 #endif // SWIFT_MISC_TESTVALUEOBJECT_H
Generic ordered container with value semantics.
Definition: collection.h:107
Associative container with value semantics, chooses a sensible default implementation container type.
Definition: dictionary.h:83
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.
QString convertToQString(bool i18n=false) const
Cast as QString.
void setDescription(const QString &description)
Set description.
CTestValueObject(const QString &name, const QString &description)
Constructor.
CTestValueObject()
Default constructor.
const QString & getName() const
Get name.
void setName(const QString &name)
Set name.
const QString & getDescription() const
Get description.
CVariant propertyByIndex(const swift::misc::CPropertyIndex &index) const
Property by index.
Mix of the most commonly used mixin classes.
Definition: valueobject.h:114
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition: variant.h:66
T to() const
Synonym for value().
Definition: variant.h:176
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:169
static CVariant from(T &&value)
Synonym for fromValue().
Definition: variant.h:147
ColumnIndex
Base class enums.
Definition: mixinindex.h:44
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: mixinindex.h:160
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: mixinindex.h:167
const QDBusArgument & operator>>(const QDBusArgument &arg, std::string &s)
Operator for std::string from QDBusArgument.
Definition: dbus.cpp:14
#define SWIFT_METAMEMBER(MEMBER,...)
Macro to define an element within a metaclass.
Definition: metaclass.h:73
Free functions in swift::misc.
QDebug operator<<(QDebug d, const CRange< I > &range)
Streaming operators for CRange to qDebug.
Definition: range.h:298