QtBase  v6.3.1
qassociativeiterable.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2020 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QASSOCIATIVEITERABLE_H
41 #define QASSOCIATIVEITERABLE_H
42 
43 #include <QtCore/qiterable.h>
44 #include <QtCore/qvariant.h>
45 
47 
48 class Q_CORE_EXPORT QAssociativeIterator : public QIterator<QMetaAssociation>
49 {
50 public:
51  using key_type = QVariant;
55 
57  : QIterator(std::move(it))
58  {}
59 
60  QVariant key() const;
62 
64  QVariantPointer<QAssociativeIterator> operator->() const;
65 };
66 
67 class Q_CORE_EXPORT QAssociativeConstIterator : public QConstIterator<QMetaAssociation>
68 {
69 public:
70  using key_type = QVariant;
72  using reference = const QVariant &;
74 
76  : QConstIterator(std::move(it))
77  {}
78 
79  QVariant key() const;
80  QVariant value() const;
81 
82  QVariant operator*() const;
83  QVariantConstPointer operator->() const;
84 };
85 
86 class Q_CORE_EXPORT QAssociativeIterable : public QIterable<QMetaAssociation>
87 {
88 public:
91 
96 
101 
102  template<class T>
104  : QIterable(QMetaAssociation::fromContainer<T>(), p)
105  {
106  }
107 
108  template<class T>
110  : QIterable(QMetaAssociation::fromContainer<T>(), p)
111  {
112  }
113 
116  {
117  }
118 
119  template<typename Pointer>
120  QAssociativeIterable(const QMetaAssociation &metaAssociation, Pointer iterable)
121  : QIterable(metaAssociation, iterable)
122  {
123  }
124 
125  // ### Qt7: Pass QMetaType as value rather than const ref.
126  QAssociativeIterable(const QMetaAssociation &metaAssociation, const QMetaType &metaType,
127  void *iterable)
128  : QIterable(metaAssociation, metaType.alignOf(), iterable)
129  {
130  }
131 
132  // ### Qt7: Pass QMetaType as value rather than const ref.
133  QAssociativeIterable(const QMetaAssociation &metaAssociation, const QMetaType &metaType,
134  const void *iterable)
135  : QIterable(metaAssociation, metaType.alignOf(), iterable)
136  {
137  }
138 
140 
142  {
143  QIterable::operator=(std::move(other));
144  return *this;
145  }
146 
147  const_iterator begin() const { return constBegin(); }
148  const_iterator end() const { return constEnd(); }
149 
152 
155 
156  const_iterator find(const QVariant &key) const;
157  const_iterator constFind(const QVariant &key) const { return find(key); }
158  iterator mutableFind(const QVariant &key);
159 
160  bool containsKey(const QVariant &key);
161  void insertKey(const QVariant &key);
162  void removeKey(const QVariant &key);
163 
164  QVariant value(const QVariant &key) const;
165  void setValue(const QVariant &key, const QVariant &mapped);
166 };
167 
168 template<>
170 {
171  if (m_pointer == nullptr)
172  return QVariant();
173 
174  const auto metaAssociation = m_pointer->metaContainer();
175  const QMetaType metaType(metaAssociation.mappedMetaType());
176  if (!metaType.isValid())
177  return m_pointer->key();
178 
179  QVariant v(metaType);
180  metaAssociation.mappedAtIterator(m_pointer->constIterator(),
181  metaType == QMetaType::fromType<QVariant>() ? &v : v.data());
182  return v;
183 }
184 
185 template<>
187  const QVariant &value)
188 {
189  if (m_pointer == nullptr)
190  return *this;
191 
192  const auto metaAssociation = m_pointer->metaContainer();
193  const QMetaType metaType(metaAssociation.mappedMetaType());
194  if (metaType.isValid()) {
196  metaAssociation.setMappedAtIterator(m_pointer->constIterator(),
197  coercer.coerce(value, metaType));
198  }
199 
200  return *this;
201 }
202 
206 
208 
209 #endif // QASSOCIATIVEITERABLE_H
#define value
[5]
QAssociativeConstIterator(QConstIterator &&it)
The QAssociativeIterable class is an iterable interface for an associative container in a QVariant.
const_iterator begin() const
QAssociativeIterable(const QMetaAssociation &metaAssociation, const QMetaType &metaType, const void *iterable)
const_iterator constBegin() const
const_iterator constFind(const QVariant &key) const
QAssociativeIterable(QIterable< QMetaAssociation > &&other)
const_iterator constEnd() const
const_iterator end() const
QAssociativeIterable & operator=(QIterable< QMetaAssociation > &&other)
QAssociativeIterable(const QMetaAssociation &metaAssociation, const QMetaType &metaType, void *iterable)
QAssociativeIterable(const QMetaAssociation &metaAssociation, Pointer iterable)
QAssociativeIterator(QIterator &&it)
QIterable is a template class that is the base class for QSequentialIterable and QAssociativeIterable...
Definition: qiterable.h:444
QConstIterator< Container > constBegin() const
Definition: qiterable.h:504
QIterator< Container > mutableEnd()
Definition: qiterable.h:519
QIterator< Container > mutableBegin()
Definition: qiterable.h:514
QConstIterator< Container > constEnd() const
Definition: qiterable.h:509
The QMetaType class manages named types in the meta-object system.
Definition: qmetatype.h:328
bool isValid() const
Definition: qmetatype.cpp:509
QTaggedIterator is a template class that wraps an iterator and exposes standard iterator traits.
Definition: qiterable.h:104
Emulated const pointer to QVariant based on a pointer.
Definition: qvariant.h:695
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:95
QVariantPointer is a template class that emulates a pointer to QVariant based on a pointer.
Definition: qvariant.h:708
The QVariantRef acts as a non-const reference to a QVariant.
Definition: qvariant.h:671
QVariantRef & operator=(const QVariant &value)
const void * coerce(const QVariant &value, const QMetaType &type)
Definition: qvariant.cpp:2784
typename C::const_iterator const_iterator
typename C::iterator iterator
QFuture< QtPrivate::MapResultType< Sequence, MapFunctor > > mapped(QThreadPool *pool, Sequence &&sequence, MapFunctor &&map)
Definition: qfloat16.h:381
Q_DECLARE_TYPEINFO(QAssociativeIterable, Q_RELOCATABLE_TYPE)
constexpr timespec operator*(const timespec &t1, int mul)
Definition: qcore_unix_p.h:146
EGLOutputLayerEXT EGLint EGLAttrib value
GLsizei const GLfloat * v
[13]
GLuint64 key
GLfloat GLfloat p
[1]
Definition: qopenglext.h:12698
@ Q_RELOCATABLE_TYPE
Definition: qtypeinfo.h:156
QObject::connect nullptr
QSharedPointer< T > other(t)
[5]
header setValue("Host", "qt-project.org")
QStringList::Iterator it
The QConstIterator allows iteration over a container in a QVariant.
Definition: qiterable.h:345
The QIterator is a template class that allows iteration over a container in a QVariant.
Definition: qiterable.h:251
Definition: main.cpp:38
QDomElement find(const QString &tagName, const QDomElement &e)
Definition: main.cpp:39