swift
propertyindexvariantmap.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_PROPERTYINDEXVARIANTMAP_H
7 #define SWIFT_MISC_PROPERTYINDEXVARIANTMAP_H
8 
9 #include <type_traits>
10 
11 #include <QDBusArgument>
12 #include <QList>
13 #include <QMap>
14 #include <QMetaType>
15 #include <QString>
16 #include <QVariant>
17 #include <QtGlobal>
18 
19 #include "misc/mixin/mixindbus.h"
20 #include "misc/mixin/mixinindex.h"
22 #include "misc/mixin/mixinstring.h"
23 #include "misc/propertyindex.h"
24 #include "misc/propertyindexlist.h"
25 #include "misc/swiftmiscexport.h"
26 #include "misc/variant.h"
27 
28 namespace swift::misc
29 {
35  public mixin::MetaType<CPropertyIndexVariantMap>,
36  public mixin::DBusOperators<CPropertyIndexVariantMap>,
37  public mixin::DataStreamOperators<CPropertyIndexVariantMap>,
38  public mixin::String<CPropertyIndexVariantMap>
39  {
40  public:
43  CPropertyIndexVariantMap(bool wildcard = false);
44 
46  CPropertyIndexVariantMap(const CPropertyIndex &index, const CVariant &value);
47 
49  void addValue(const CPropertyIndex &index, const CVariant &value);
50 
52  void addValue(const CPropertyIndex &index, const QVariant &value) { this->addValue(index, CVariant(value)); }
53 
55  void addValue(const CPropertyIndex &index, const char *str);
56 
58  template <class T>
59  void addValue(const CPropertyIndex &index, const T &value)
60  {
61  m_values.insert(index, CVariant::fromValue(value));
62  }
63 
65  void prependIndex(int index);
66 
68  bool isEmpty() const { return m_values.isEmpty(); }
69 
71  CVariant value(const CPropertyIndex &index) const { return m_values.value(index); }
72 
74  void value(const CPropertyIndex &index, const CVariant &value) { m_values.value(index, value); }
75 
78 
80  bool contains(const CPropertyIndex &index) const { return m_values.contains(index); }
81 
83  QList<CVariant> values() const { return m_values.values(); }
84 
86  bool isWildcard() const { return m_wildcard; }
87 
89  void setWildcard(bool wildcard) { m_wildcard = wildcard; }
90 
92  void clear() { m_values.clear(); }
93 
95  int size() const;
96 
99 
102 
104  bool matchesVariant(const CVariant &value) const;
105 
107  template <typename T, typename = std::enable_if_t<!std::is_same_v<T, CVariant>>>
108  bool matches(const T &value) const
109  {
110  return matchesVariant(CVariant::from(value));
111  }
112 
114  const QMap<CPropertyIndex, CVariant> &map() const { return m_values; }
115 
117  size_t getValueHash() const;
118 
120  friend size_t qHash(const CPropertyIndexVariantMap &vm) { return vm.getValueHash(); }
121 
123  QString convertToQString(bool i18n = false) const;
124 
125  protected:
127  bool m_wildcard;
128 
129  public:
131  void marshallToDbus(QDBusArgument &argument) const;
132 
134  void unmarshallFromDbus(const QDBusArgument &argument);
135 
137  void marshalToDataStream(QDataStream &stream) const;
138 
140  void unmarshalFromDataStream(QDataStream &stream);
141  };
142 
143  namespace mixin
144  {
145  template <class Derived>
147  bool skipEqualValues)
148  {
149  if (indexMap.isEmpty()) return {};
150 
151  CPropertyIndexList changed;
152  const auto &map = indexMap.map();
153  for (auto it = map.begin(); it != map.end(); ++it)
154  {
155  const CVariant value = it.value();
156  const CPropertyIndex index = it.key();
157  if (skipEqualValues)
158  {
159  const bool equal = derived()->equalsPropertyByIndex(value, index);
160  if (equal) { continue; }
161  }
162  derived()->setPropertyByIndex(index, value);
163  changed.push_back(index);
164  }
165  return changed;
166  }
167  } // namespace mixin
168 } // namespace swift::misc
169 
170 Q_DECLARE_METATYPE(swift::misc::CPropertyIndexVariantMap)
171 
172 #endif // SWIFT_MISC_PROPERTYINDEXVARIANTMAP_H
Value object encapsulating a list of property indexes.
Specialized value object compliant map for variants, based on indexes.
void unmarshalFromDataStream(QDataStream &stream)
Unmarshal a value from a QDataStream.
void addValue(const CPropertyIndex &index, const T &value)
Add a value as non CVariant.
SWIFT_MISC_EXPORT friend bool operator!=(const CPropertyIndexVariantMap &a, const CPropertyIndexVariantMap &b)
Equal operator, required if maps are directly compared, not with CValueObject.
bool matches(const T &value) const
True if this map matches the value.
void unmarshallFromDbus(const QDBusArgument &argument)
Unmarshall without begin/endStructure, for when composed within another object.
bool matchesVariant(const CVariant &value) const
True if this map matches the value contained in the variant.
void addValue(const CPropertyIndex &index, const char *str)
Add QString as literal, disambiguate as I want to add QString.
void prependIndex(int index)
Prepend index to all property indexes.
CVariant value(const CPropertyIndex &index) const
Value.
const QMap< CPropertyIndex, CVariant > & map() const
Map.
QMap< CPropertyIndex, CVariant > m_values
values
CPropertyIndexVariantMap(const CPropertyIndex &index, const CVariant &value)
Single value constructor.
CPropertyIndexVariantMap(bool wildcard=false)
Constructor.
int size() const
Number of elements.
void setWildcard(bool wildcard)
Wildcard, only relevant when used in search.
void value(const CPropertyIndex &index, const CVariant &value)
Set value.
void addValue(const CPropertyIndex &index, const CVariant &value)
Add a value.
QString convertToQString(bool i18n=false) const
Cast as QString.
QList< CVariant > values() const
values
friend size_t qHash(const CPropertyIndexVariantMap &vm)
qHash overload, needed for storing value in a QSet.
bool contains(const CPropertyIndex &index) const
Contains index?
SWIFT_MISC_EXPORT friend bool operator==(const CPropertyIndexVariantMap &a, const CPropertyIndexVariantMap &b)
Equal operator, required if maps are directly compared, not with CValueObject.
void marshalToDataStream(QDataStream &stream) const
Marshal a value to a QDataStream.
size_t getValueHash() const
Hash value.
void addValue(const CPropertyIndex &index, const QVariant &value)
Add a value.
void marshallToDbus(QDBusArgument &argument) const
Marshall without begin/endStructure, for when composed within another object.
bool isWildcard() const
Wildcard, only relevant when used in search.
CPropertyIndexList indexes() const
Indexes.
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition: sequence.h:305
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition: variant.h:66
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
CRTP class template which will generate marshalling operators for a derived class with its own marsha...
Definition: mixindbus.h:39
CRTP class template to generate non-member QDataStream streaming operators.
CPropertyIndexList apply(const CPropertyIndexVariantMap &indexMap, bool skipEqualValues=false)
Update by variant map.
CRTP class template from which a derived class can inherit common methods dealing with the metatype o...
Definition: mixinmetatype.h:29
CRTP class template from which a derived class can inherit string streaming operations.
Definition: mixinstring.h:31
Free functions in swift::misc.
#define SWIFT_MISC_EXPORT
Export a class or function from the library.