swift
Loading...
Searching...
No Matches
propertyindex.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_PROPERTYINDEX_H
7#define SWIFT_MISC_PROPERTYINDEX_H
8
9#include <initializer_list>
10#include <type_traits>
11
12#include <QList>
13#include <QMetaType>
14#include <QString>
15
16#include "misc/metaclass.h"
26#include "misc/typetraits.h"
27
28namespace swift::misc
29{
35 public mixin::MetaType<CPropertyIndex>,
36 public mixin::HashByMetaClass<CPropertyIndex>,
37 public mixin::DBusByMetaClass<CPropertyIndex>,
38 public mixin::DataStreamByMetaClass<CPropertyIndex>,
39 public mixin::JsonOperators<CPropertyIndex>,
40 public mixin::EqualsByMetaClass<CPropertyIndex>,
41 public mixin::LessThanByMetaClass<CPropertyIndex>,
42 public mixin::CompareByMetaClass<CPropertyIndex>,
43 public mixin::String<CPropertyIndex>
44 {
45 // In the first trial I have used CSequence<int> as base class. This has created too much circular dependencies
46 // of the headers CIndexVariantMap is used in CValueObject, CPropertyIndex in CIndexVariantMap
47
48 public:
50 CPropertyIndex() = default;
51
53 CPropertyIndex(int singleProperty);
54
56 CPropertyIndex(std::initializer_list<int> il);
57
59 CPropertyIndex(const QList<int> &indexes);
60
62 CPropertyIndex(const QString &indexes);
63
65 operator CPropertyIndexRef() const;
66
68 Q_REQUIRED_RESULT CPropertyIndex copyFrontRemoved() const;
69
71 bool isNested() const;
72
74 bool isMyself() const;
75
77 bool isEmpty() const;
78
80 QList<int> indexList() const;
81
83 void prepend(int newLeftIndex);
84
86 bool contains(int index) const;
87
89 template <class EnumType>
90 bool contains(EnumType ev) const
91 {
92 static_assert(std::is_enum_v<EnumType>, "Argument must be an enum");
93 return this->contains(static_cast<int>(ev));
94 }
95
97 int frontToInt() const;
98
100 bool startsWith(int index) const;
101
103 template <class CastType>
104 CastType frontCasted() const
105 {
106 static_assert(std::is_enum_v<CastType> || std::is_integral_v<CastType>,
107 "CastType must be an enum or integer");
108 return static_cast<CastType>(frontToInt());
109 }
110
112 template <class EnumType>
113 bool startsWithPropertyIndexEnum(EnumType ev) const
114 {
115 static_assert(std::is_enum_v<EnumType>, "Argument must be an enum");
116 return this->startsWith(static_cast<int>(ev));
117 }
118
120 auto comparator() const
121 {
122 return [index = *this](const auto &a, const auto &b) {
123 using T = std::decay_t<decltype(a)>;
125 {
126 return a.comparePropertyByIndex(index, b);
127 }
128 else if constexpr (THasPropertyByIndex<T>::value)
129 {
130 return compare(a.propertyByIndex(index), b.propertyByIndex(index));
131 }
132 else
133 {
134 qFatal("Not implemented");
135 return 0;
136 }
137 };
138 }
139
141 QString convertToQString(bool i18n = false) const;
142
144 QJsonObject toJson() const;
145
147 void convertFromJson(const QJsonObject &json);
148
150 static const CPropertyIndex &empty()
151 {
152 static const CPropertyIndex pi;
153 return pi;
154 }
155
156 protected:
158 void parseFromString(const QString &indexes);
159
160 private:
161 QList<int> m_indexes;
162
165 SWIFT_METAMEMBER(indexes));
166 };
167} // namespace swift::misc
168
169Q_DECLARE_METATYPE(swift::misc::CPropertyIndex)
170
171#endif // SWIFT_MISC_PROPERTYINDEX_H
QList< int > indexList() const
Index list.
bool contains(EnumType ev) const
Compare with index given by enum.
bool startsWithPropertyIndexEnum(EnumType ev) const
Compare with index given by enum.
CPropertyIndex()=default
Default constructor.
void prepend(int newLeftIndex)
Shift existing indexes to right and insert given index at front.
bool isMyself() const
Myself index, used with nesting.
static const CPropertyIndex & empty()
an empty property index
bool startsWith(int index) const
Starts with given index?
auto comparator() const
Return a predicate function which can compare two objects based on this index.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
int frontToInt() const
Front to integer.
bool contains(int index) const
Contains index?
Q_REQUIRED_RESULT CPropertyIndex copyFrontRemoved() const
Copy with first element removed.
bool isNested() const
Is nested index?
Non-owning reference to a CPropertyIndex with a subset of its features.
CRTP class template from which a derived class can inherit non-member compare() implemented by metacl...
friend int compare(const CPropertyIndex &a, const CPropertyIndex &b)
CRTP class template from which a derived class can inherit common methods dealing with marshalling in...
Definition mixindbus.h:71
CRTP class template to generate QDataStream marshalling methods using CMetaClass.
CRTP class template from which a derived class can inherit operator== implemented by metaclass.
CRTP class template from which a derived class can inherit common methods dealing with hashing instan...
Definition mixinhash.h:48
CRTP class template which will generate marshalling operators for a derived class with its own marsha...
Definition mixinjson.h:37
CRTP class template from which a derived class can inherit operator< implemented by metaclass.
CRTP class template from which a derived class can inherit common methods dealing with the metatype o...
CRTP class template from which a derived class can inherit string streaming operations.
Definition mixinstring.h:29
#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.
Trait which is true if the expression a.compareByPropertyIndex(b, i) is valid when a and b are instan...
Definition typetraits.h:135
Trait which is true if the expression a.propertyByIndex(i) is valid with a is an instance of T and i ...
Definition typetraits.h:150
#define SWIFT_MISC_EXPORT
Export a class or function from the library.