swift
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"
19 #include "misc/mixin/mixindbus.h"
20 #include "misc/mixin/mixinhash.h"
21 #include "misc/mixin/mixinjson.h"
23 #include "misc/mixin/mixinstring.h"
24 #include "misc/propertyindexref.h"
25 #include "misc/swiftmiscexport.h"
26 #include "misc/typetraits.h"
27 
28 namespace 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)>;
124  if constexpr (THasComparePropertyByIndex<T>::value) { return a.comparePropertyByIndex(index, b); }
125  else if constexpr (THasPropertyByIndex<T>::value)
126  {
127  return compare(a.propertyByIndex(index), b.propertyByIndex(index));
128  }
129  else
130  {
131  qFatal("Not implemented");
132  return 0;
133  }
134  };
135  }
136 
138  QString convertToQString(bool i18n = false) const;
139 
141  QJsonObject toJson() const;
142 
144  void convertFromJson(const QJsonObject &json);
145 
147  static const CPropertyIndex &empty()
148  {
149  static const CPropertyIndex pi;
150  return pi;
151  }
152 
153  protected:
155  void parseFromString(const QString &indexes);
156 
157  private:
158  QList<int> m_indexes;
159 
162  SWIFT_METAMEMBER(indexes));
163  };
164 } // namespace swift::misc
165 
166 Q_DECLARE_METATYPE(swift::misc::CPropertyIndex)
167 
168 #endif // SWIFT_MISC_PROPERTYINDEX_H
bool contains(EnumType ev) const
Compare with index given by enum.
Definition: propertyindex.h:90
bool startsWithPropertyIndexEnum(EnumType ev) const
Compare with index given by enum.
CPropertyIndex()=default
Default constructor.
static const CPropertyIndex & empty()
an empty property 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.
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...
Definition: mixincompare.h:216
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.
Definition: mixincompare.h:46
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.
Definition: mixincompare.h:136
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
#define SWIFT_METAMEMBER(MEMBER,...)
Macro to define an element within a metaclass.
Definition: metaclass.h:73
#define SWIFT_METACLASS(CLASS,...)
Macro to define a nested metaclass that describes the attributes of its enclosing class.
Definition: metaclass.h:53
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.