swift
Loading...
Searching...
No Matches
mixinindex.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_MIXIN_MIXININDEX_H
7#define SWIFT_MISC_MIXIN_MIXININDEX_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 <QtDebug>
18#include <QtGlobal>
19
20#include "misc/icon.h"
22#include "misc/predicates.h"
24
25namespace swift::misc
26{
29
30 namespace mixin
31 {
38 template <class Derived>
39 class Index
40 {
41 public:
44 {
45 IndexPixmap = 10, // manually set to avoid circular dependencies
46 IndexIcon,
47 IndexString
48 };
49
53 bool skipEqualValues = false); // impl in propertyindexvariantmap.h
54
56 void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant);
57
60
62 int comparePropertyByIndex(CPropertyIndexRef index, const Derived &compareValue) const;
63
65 bool equalsPropertyByIndex(const QVariant &compareValue, CPropertyIndexRef index) const;
66
67 private:
68 const Derived *derived() const;
69 Derived *derived();
70
71 template <typename T>
72 QVariant myself() const;
73 template <typename T>
74 void myself(const QVariant &variant);
75
76 template <typename T>
77 QVariant basePropertyByIndex(const T *base, CPropertyIndexRef index) const;
78 template <typename T>
79 void baseSetPropertyByIndex(T *base, const QVariant &var, CPropertyIndexRef index);
80
81 QVariant basePropertyByIndex(const void *, CPropertyIndexRef) const;
82 void baseSetPropertyByIndex(void *, const QVariant &, CPropertyIndexRef);
83 };
84
85 template <class Derived>
86 const Derived *Index<Derived>::derived() const
87 {
88 return static_cast<const Derived *>(this);
89 }
90
91 template <class Derived>
92 Derived *Index<Derived>::derived()
93 {
94 return static_cast<Derived *>(this);
95 }
96
97 template <class Derived>
98 template <typename T>
99 QVariant Index<Derived>::myself() const
100 {
101 if constexpr (std::is_default_constructible_v<T>)
102 {
103 return QVariant::fromValue(*derived());
104 }
105 else
106 {
107 qFatal("isMyself should have been handled before reaching here");
108 return {};
109 }
110 }
111
112 template <class Derived>
113 template <typename T>
114 void Index<Derived>::myself(const QVariant &variant)
115 {
116 if constexpr (std::is_default_constructible_v<T>)
117 {
118 *derived() = variant.value<T>();
119 }
120 else
121 {
122 qFatal("isMyself should have been handled before reaching here");
123 }
124 }
125
126 template <class Derived>
127 template <typename T>
128 QVariant Index<Derived>::basePropertyByIndex(const T *base, CPropertyIndexRef index) const
129 {
130 return base->propertyByIndex(index);
131 }
132
133 template <class Derived>
134 template <typename T>
135 void Index<Derived>::baseSetPropertyByIndex(T *base, const QVariant &var, CPropertyIndexRef index)
136 {
137 base->setPropertyByIndex(index, var);
138 }
139
140 template <class Derived>
141 QVariant Index<Derived>::basePropertyByIndex(const void *, CPropertyIndexRef) const
142 {
143 // qFatal("%s", qPrintable("Property by index not found, index: " + index.toQString())); return {};
144 qFatal("Property by index not found");
145 return {};
146 }
147
148 template <class Derived>
149 void Index<Derived>::baseSetPropertyByIndex(void *, const QVariant &, CPropertyIndexRef)
150 {
151 // qFatal("%s", qPrintable("Property by index not found (setter), index: " + index.toQString()));
152 qFatal("Property by index not found");
153 }
154
159#define SWIFT_MISC_DECLARE_USING_MIXIN_INDEX(DERIVED) \
160 using ::swift::misc::mixin::Index<DERIVED>::apply; \
161 using ::swift::misc::mixin::Index<DERIVED>::setPropertyByIndex; \
162 using ::swift::misc::mixin::Index<DERIVED>::propertyByIndex; \
163 using ::swift::misc::mixin::Index<DERIVED>::comparePropertyByIndex; \
164 using ::swift::misc::mixin::Index<DERIVED>::equalsPropertyByIndex;
165
166 template <class Derived>
168 {
169 if (index.isMyself())
170 {
171 myself<Derived>(variant);
172 }
173 else
174 {
175 baseSetPropertyByIndex(static_cast<TIndexBaseOfT<Derived> *>(derived()), variant, index);
176 }
177 }
178
179 template <class Derived>
181 {
182 if (index.isMyself())
183 {
184 return myself<Derived>();
185 }
186 const auto i = index.frontCasted<ColumnIndex>(); // keep that "auto", otherwise I won's compile
187 switch (i)
188 {
189 case IndexIcon: return CIcons::toVariant(derived()->toIcon());
190 case IndexPixmap: return CIcons::toVariantPixmap(derived()->toIcon());
191 case IndexString: return QVariant(derived()->toQString());
192 default: return basePropertyByIndex(static_cast<const TIndexBaseOfT<Derived> *>(derived()), index);
193 }
194 }
195
196 template <class Derived>
198 {
199 return derived()->propertyByIndex(index) == compareValue;
200 }
201
202 template <class Derived>
203 int Index<Derived>::comparePropertyByIndex(CPropertyIndexRef index, const Derived &compareValue) const
204 {
205 if (this == &compareValue)
206 {
207 return 0;
208 }
209 if (index.isMyself())
210 {
211 // slow, only last resort
212 return derived()->toQString().compare(compareValue.toQString());
213 }
214
215 const auto i = index.frontCasted<ColumnIndex>();
216 switch (i)
217 {
218 case IndexIcon:
219 case IndexPixmap:
220 case IndexString:
221 default: break; // also covers
222 }
223
224 // slow, only last resort
225 return derived()->toQString().compare(compareValue.toQString());
226 }
227 } // namespace mixin
228} // namespace swift::misc
229
230#endif // SWIFT_MISC_MIXIN_MIXININDEX_H
static QVariant toVariant(IconIndex icon)
Variant containing CIcon.
Definition icon.cpp:251
static QVariant toVariantPixmap(IconIndex icon)
Variant containing QPixmap.
Definition icon.cpp:252
Value object encapsulating a list of property indexes.
Non-owning reference to a CPropertyIndex with a subset of its features.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
Specialized value object compliant map for variants, based on indexes.
CRTP class template from which a derived class can inherit property indexing functions.
Definition mixinindex.h:40
ColumnIndex
Base class enums.
Definition mixinindex.h:44
int comparePropertyByIndex(CPropertyIndexRef index, const Derived &compareValue) const
Compare for index.
Definition mixinindex.h:203
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
CPropertyIndexList apply(const CPropertyIndexVariantMap &indexMap, bool skipEqualValues=false)
Update by variant map.
bool equalsPropertyByIndex(const QVariant &compareValue, CPropertyIndexRef index) const
Is given variant equal to value of property index?
Definition mixinindex.h:197
Mixin classes which implement common operations for value classes.
Free functions in swift::misc.
typename TIndexBaseOf< T >::type TIndexBaseOfT
Alias for typename TIndexBaseOf<T>::type.
QVariant fromValue(T &&value)
T value() const &const