Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qjsonarray.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QJSONARRAY_H
5#define QJSONARRAY_H
6
7#include <QtCore/qjsonvalue.h>
8#include <QtCore/qiterator.h>
9#include <QtCore/qshareddata.h>
10#include <initializer_list>
11
13
14class QDebug;
16
17class Q_CORE_EXPORT QJsonArray
18{
19public:
21
22 QJsonArray(std::initializer_list<QJsonValue> args);
23
25
26 QJsonArray(const QJsonArray &other) noexcept;
27 QJsonArray &operator =(const QJsonArray &other) noexcept;
28
29 QJsonArray(QJsonArray &&other) noexcept;
30
31 QJsonArray &operator =(QJsonArray &&other) noexcept
32 {
33 swap(other);
34 return *this;
35 }
36
37 static QJsonArray fromStringList(const QStringList &list);
38 static QJsonArray fromVariantList(const QVariantList &list);
39 QVariantList toVariantList() const;
40
41 qsizetype size() const;
42 inline qsizetype count() const { return size(); }
43
44 bool isEmpty() const;
45 QJsonValue at(qsizetype i) const;
46 QJsonValue first() const;
47 QJsonValue last() const;
48
49 void prepend(const QJsonValue &value);
50 void append(const QJsonValue &value);
51 void removeAt(qsizetype i);
52 QJsonValue takeAt(qsizetype i);
53 inline void removeFirst() { removeAt(0); }
54 inline void removeLast() { removeAt(size() - 1); }
55
56 void insert(qsizetype i, const QJsonValue &value);
57 void replace(qsizetype i, const QJsonValue &value);
58
59 bool contains(const QJsonValue &element) const;
60 QJsonValueRef operator[](qsizetype i);
61 QJsonValue operator[](qsizetype i) const;
62
63 bool operator==(const QJsonArray &other) const;
64 bool operator!=(const QJsonArray &other) const;
65
66 void swap(QJsonArray &other) noexcept
67 {
68 a.swap(other.a);
69 }
70
71 class const_iterator;
72
73 class iterator {
74 public:
75 typedef std::random_access_iterator_tag iterator_category;
80
81 inline iterator() : item(static_cast<QJsonArray *>(nullptr), 0) { }
83
84 constexpr iterator(const iterator &other) = default;
86 {
87 item.rebind(other.item);
88 return *this;
89 }
90
91 inline QJsonValueRef operator*() const { return item; }
92 inline const QJsonValueConstRef *operator->() const { return &item; }
93 inline QJsonValueRef *operator->() { return &item; }
94 inline QJsonValueRef operator[](qsizetype j) const { return *(*this + j); }
95
96 inline bool operator==(const iterator &o) const
97 { return item.d == o.item.d && item.index == o.item.index; }
98 inline bool operator!=(const iterator &o) const { return !(*this == o); }
99 inline bool operator<(const iterator &other) const
100 { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
101 inline bool operator<=(const iterator &other) const
102 { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
103 inline bool operator>(const iterator &other) const { return !(*this <= other); }
104 inline bool operator>=(const iterator &other) const { return !(*this < other); }
105 inline bool operator==(const const_iterator &o) const
106 { return item.d == o.item.d && item.index == o.item.index; }
107 inline bool operator!=(const const_iterator &o) const { return !(*this == o); }
108 inline bool operator<(const const_iterator &other) const
109 { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
110 inline bool operator<=(const const_iterator &other) const
111 { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
112 inline bool operator>(const const_iterator &other) const { return !(*this <= other); }
113 inline bool operator>=(const const_iterator &other) const { return !(*this < other); }
114 inline iterator &operator++() { ++item.index; return *this; }
115 inline iterator operator++(int) { iterator n = *this; ++item.index; return n; }
116 inline iterator &operator--() { item.index--; return *this; }
117 inline iterator operator--(int) { iterator n = *this; item.index--; return n; }
118 inline iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; }
119 inline iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; }
120 inline iterator operator+(qsizetype j) const { iterator r = *this; return r += j; }
121 inline iterator operator-(qsizetype j) const { return operator+(-j); }
122 inline qsizetype operator-(iterator j) const { return item.index - j.item.index; }
123
124 private:
126 friend class QJsonArray;
127 };
128 friend class iterator;
129
131 public:
132 typedef std::random_access_iterator_tag iterator_category;
136 typedef const QJsonValueRef *pointer;
137
138 inline const_iterator() : item(static_cast<QJsonArray *>(nullptr), 0) { }
140 : item(const_cast<QJsonArray *>(array), index) { }
141 inline const_iterator(const iterator &o) : item(o.item) { }
142
143 constexpr const_iterator(const const_iterator &other) = default;
145 {
146 item.rebind(other.item);
147 return *this;
148 }
149
150 inline const QJsonValueConstRef operator*() const { return item; }
151 inline const QJsonValueConstRef *operator->() const { return &item; }
152
153 inline QJsonValueConstRef operator[](qsizetype j) const { return *(*this + j); }
154 inline bool operator==(const const_iterator &o) const
155 { return item.d == o.item.d && item.index == o.item.index; }
156 inline bool operator!=(const const_iterator &o) const { return !(*this == o); }
157 inline bool operator<(const const_iterator &other) const
158 { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
159 inline bool operator<=(const const_iterator &other) const
160 { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
161 inline bool operator>(const const_iterator &other) const { return !(*this <= other); }
162 inline bool operator>=(const const_iterator &other) const { return !(*this < other); }
163 inline const_iterator &operator++() { ++item.index; return *this; }
164 inline const_iterator operator++(int) { const_iterator n = *this; ++item.index; return n; }
165 inline const_iterator &operator--() { item.index--; return *this; }
166 inline const_iterator operator--(int) { const_iterator n = *this; item.index--; return n; }
167 inline const_iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; }
168 inline const_iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; }
169 inline const_iterator operator+(qsizetype j) const { const_iterator r = *this; return r += j; }
170 inline const_iterator operator-(qsizetype j) const { return operator+(-j); }
171 inline qsizetype operator-(const_iterator j) const { return item.index - j.item.index; }
172
173 private:
175 friend class QJsonArray;
176 };
177 friend class const_iterator;
178
179 // stl style
180 inline iterator begin() { detach(); return iterator(this, 0); }
181 inline const_iterator begin() const { return const_iterator(this, 0); }
182 inline const_iterator constBegin() const { return const_iterator(this, 0); }
183 inline const_iterator cbegin() const { return const_iterator(this, 0); }
184 inline iterator end() { detach(); return iterator(this, size()); }
185 inline const_iterator end() const { return const_iterator(this, size()); }
186 inline const_iterator constEnd() const { return const_iterator(this, size()); }
187 inline const_iterator cend() const { return const_iterator(this, size()); }
189 { insert(before.item.index, value); return before; }
191 { removeAt(it.item.index); return it; }
192
193 // more Qt
196
197 // convenience
198 inline QJsonArray operator+(const QJsonValue &v) const
199 { QJsonArray n = *this; n += v; return n; }
201 { append(v); return *this; }
203 { append(v); return *this; }
204
205 // stl compatibility
206 inline void push_back(const QJsonValue &t) { append(t); }
207 inline void push_front(const QJsonValue &t) { prepend(t); }
208 inline void pop_front() { removeFirst(); }
209 inline void pop_back() { removeLast(); }
210 inline bool empty() const { return isEmpty(); }
214 typedef const value_type *const_pointer;
218
219private:
220 friend class QJsonValue;
221 friend class QJsonValueConstRef;
222 friend class QJsonValueRef;
224 friend class QJsonDocument;
225 friend class QCborArray;
226 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &);
227
229 bool detach(qsizetype reserve = 0);
230
232};
233
234Q_DECLARE_SHARED(QJsonArray)
235
236#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
238 : d(a ? a->a.data() : nullptr), is_object(false), index(idx)
239{}
240#endif
241
242Q_CORE_EXPORT size_t qHash(const QJsonArray &array, size_t seed = 0);
243
244#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
245Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &);
246#endif
247
248#ifndef QT_NO_DATASTREAM
249Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonArray &);
250Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonArray &);
251#endif
252
254
255#endif // QJSONARRAY_H
\inmodule QtCore\reentrant
Definition qcborarray.h:20
\inmodule QtCore\reentrant
Definition qdatastream.h:30
\inmodule QtCore
QJsonValueConstRef operator[](qsizetype j) const
Returns the item at offset j from the item pointed to by this iterator (the item at position {*this +...
Definition qjsonarray.h:153
qsizetype operator-(const_iterator j) const
Returns the number of items between the item pointed to by other and the item pointed to by this iter...
Definition qjsonarray.h:171
const QJsonValueConstRef operator*() const
Returns the current item.
Definition qjsonarray.h:150
bool operator>=(const const_iterator &other) const
Returns true if the item pointed to by this iterator is greater than or equal to the item pointed to ...
Definition qjsonarray.h:162
bool operator>(const const_iterator &other) const
Returns true if the item pointed to by this iterator is greater than the item pointed to by the other...
Definition qjsonarray.h:161
const_iterator(const iterator &o)
Constructs a copy of other.
Definition qjsonarray.h:141
const_iterator & operator=(const const_iterator &other)
Definition qjsonarray.h:144
const_iterator & operator-=(qsizetype j)
Makes the iterator go back by j items.
Definition qjsonarray.h:168
std::random_access_iterator_tag iterator_category
A synonym for {std::random_access_iterator_tag} indicating this iterator is a random access iterator.
Definition qjsonarray.h:132
const_iterator & operator++()
The prefix {++} operator, {++it}, advances the iterator to the next item in the array and returns an ...
Definition qjsonarray.h:163
const_iterator & operator--()
The prefix {–} operator, {–it}, makes the preceding item current and returns an iterator to the new c...
Definition qjsonarray.h:165
const_iterator(const QJsonArray *array, qsizetype index)
Definition qjsonarray.h:139
bool operator<=(const const_iterator &other) const
Returns true if the item pointed to by this iterator is less than or equal to the item pointed to by ...
Definition qjsonarray.h:159
bool operator==(const const_iterator &o) const
Returns true if other points to the same item as this iterator; otherwise returns false.
Definition qjsonarray.h:154
const_iterator operator-(qsizetype j) const
Returns an iterator to the item at j positions backward from this iterator.
Definition qjsonarray.h:170
const QJsonValueConstRef * operator->() const
Returns a pointer to the current item.
Definition qjsonarray.h:151
const QJsonValueRef * pointer
Definition qjsonarray.h:136
constexpr const_iterator(const const_iterator &other)=default
const QJsonValueRef reference
Definition qjsonarray.h:135
bool operator<(const const_iterator &other) const
Returns true if the item pointed to by this iterator is less than the item pointed to by the other it...
Definition qjsonarray.h:157
bool operator!=(const const_iterator &o) const
Returns true if other points to a different item than this iterator; otherwise returns false.
Definition qjsonarray.h:156
const_iterator operator+(qsizetype j) const
Returns an iterator to the item at j positions forward from this iterator.
Definition qjsonarray.h:169
const_iterator operator++(int)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qjsonarray.h:164
const_iterator()
Constructs an uninitialized iterator.
Definition qjsonarray.h:138
const_iterator & operator+=(qsizetype j)
Advances the iterator by j items.
Definition qjsonarray.h:167
const_iterator operator--(int)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qjsonarray.h:166
\inmodule QtCore
Definition qjsonarray.h:73
iterator & operator+=(qsizetype j)
Advances the iterator by j items.
Definition qjsonarray.h:118
bool operator!=(const iterator &o) const
Definition qjsonarray.h:98
iterator & operator++()
The prefix {++} operator, {++it}, advances the iterator to the next item in the array and returns an ...
Definition qjsonarray.h:114
bool operator>=(const iterator &other) const
Definition qjsonarray.h:104
iterator & operator--()
The prefix {–} operator, {–it}, makes the preceding item current and returns an iterator to the new c...
Definition qjsonarray.h:116
qsizetype operator-(iterator j) const
Returns the number of items between the item pointed to by other and the item pointed to by this iter...
Definition qjsonarray.h:122
iterator & operator=(const iterator &other)
Definition qjsonarray.h:85
iterator operator+(qsizetype j) const
Returns an iterator to the item at j positions forward from this iterator.
Definition qjsonarray.h:120
bool operator!=(const const_iterator &o) const
Returns true if other points to a different item than this iterator; otherwise returns false.
Definition qjsonarray.h:107
QJsonValueRef * operator->()
Definition qjsonarray.h:93
bool operator>(const const_iterator &other) const
Returns true if the item pointed to by this iterator is greater than the item pointed to by the other...
Definition qjsonarray.h:112
const QJsonValueConstRef * operator->() const
Returns a pointer to a modifiable reference to the current item.
Definition qjsonarray.h:92
iterator(QJsonArray *array, qsizetype index)
Definition qjsonarray.h:82
QJsonValueRef operator*() const
Returns a modifiable reference to the current item.
Definition qjsonarray.h:91
iterator operator-(qsizetype j) const
Returns an iterator to the item at j positions backward from this iterator.
Definition qjsonarray.h:121
QJsonValueRef operator[](qsizetype j) const
Returns a modifiable reference to the item at offset j from the item pointed to by this iterator (the...
Definition qjsonarray.h:94
constexpr iterator(const iterator &other)=default
iterator()
Constructs an uninitialized iterator.
Definition qjsonarray.h:81
QJsonValueRef * pointer
Definition qjsonarray.h:79
bool operator<(const const_iterator &other) const
Returns true if the item pointed to by this iterator is less than the item pointed to by the other it...
Definition qjsonarray.h:108
std::random_access_iterator_tag iterator_category
A synonym for {std::random_access_iterator_tag} indicating this iterator is a random access iterator.
Definition qjsonarray.h:75
qsizetype difference_type
Definition qjsonarray.h:76
bool operator==(const const_iterator &o) const
Returns true if other points to the same item as this iterator; otherwise returns false.
Definition qjsonarray.h:105
iterator operator--(int)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qjsonarray.h:117
QJsonValue value_type
Definition qjsonarray.h:77
bool operator>(const iterator &other) const
Definition qjsonarray.h:103
iterator operator++(int)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qjsonarray.h:115
iterator & operator-=(qsizetype j)
Makes the iterator go back by j items.
Definition qjsonarray.h:119
bool operator==(const iterator &o) const
Definition qjsonarray.h:96
bool operator<=(const const_iterator &other) const
Returns true if the item pointed to by this iterator is less than or equal to the item pointed to by ...
Definition qjsonarray.h:110
QJsonValueRef reference
Definition qjsonarray.h:78
bool operator<=(const iterator &other) const
Definition qjsonarray.h:101
bool operator>=(const const_iterator &other) const
Returns true if the item pointed to by this iterator is greater than or equal to the item pointed to ...
Definition qjsonarray.h:113
bool operator<(const iterator &other) const
Definition qjsonarray.h:99
\inmodule QtCore\reentrant
Definition qjsonarray.h:18
void pop_front()
This function is provided for STL compatibility.
Definition qjsonarray.h:208
bool empty() const
This function is provided for STL compatibility.
Definition qjsonarray.h:210
~QJsonArray()
Deletes the array.
iterator end()
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last ...
Definition qjsonarray.h:184
qsizetype difference_type
Typedef for qsizetype.
Definition qjsonarray.h:217
QJsonArray(const QJsonArray &other) noexcept
Creates a copy of other.
qsizetype size_type
Typedef for qsizetype.
Definition qjsonarray.h:211
iterator Iterator
Qt-style synonym for QJsonArray::iterator.
Definition qjsonarray.h:194
const_iterator end() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qjsonarray.h:185
QJsonArray()
Creates an empty array.
QJsonValue const_reference
Typedef for const QJsonValue &.
Definition qjsonarray.h:216
QJsonArray operator+(const QJsonValue &v) const
Returns an array that contains all the items in this array followed by the provided value.
Definition qjsonarray.h:198
iterator insert(iterator before, const QJsonValue &value)
Inserts value before the position pointed to by before, and returns an iterator pointing to the newly...
Definition qjsonarray.h:188
value_type * pointer
Typedef for QJsonValue *.
Definition qjsonarray.h:213
const_iterator cend() const
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the ...
Definition qjsonarray.h:187
void pop_back()
This function is provided for STL compatibility.
Definition qjsonarray.h:209
void push_front(const QJsonValue &t)
This function is provided for STL compatibility.
Definition qjsonarray.h:207
const_iterator constBegin() const
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item in the array.
Definition qjsonarray.h:182
const_iterator constEnd() const
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the ...
Definition qjsonarray.h:186
QJsonValueRef reference
Typedef for QJsonValue &.
Definition qjsonarray.h:215
void removeFirst()
Removes the first item in the array.
Definition qjsonarray.h:53
iterator erase(iterator it)
Removes the item pointed to by it, and returns an iterator pointing to the next item.
Definition qjsonarray.h:190
const_iterator cbegin() const
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item in the array.
Definition qjsonarray.h:183
qsizetype count() const
Same as size().
Definition qjsonarray.h:42
QJsonArray & operator+=(const QJsonValue &v)
Appends value to the array, and returns a reference to the array itself.
Definition qjsonarray.h:200
const value_type * const_pointer
Typedef for const QJsonValue *.
Definition qjsonarray.h:214
void swap(QJsonArray &other) noexcept
Definition qjsonarray.h:66
const_iterator begin() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qjsonarray.h:181
iterator begin()
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in the array.
Definition qjsonarray.h:180
const_iterator ConstIterator
Qt-style synonym for QJsonArray::const_iterator.
Definition qjsonarray.h:195
QJsonValue value_type
Typedef for QJsonValue.
Definition qjsonarray.h:212
void push_back(const QJsonValue &t)
This function is provided for STL compatibility.
Definition qjsonarray.h:206
void removeLast()
Removes the last item in the array.
Definition qjsonarray.h:54
\inmodule QtCore\reentrant
QJsonValueConstRef(const QJsonValueConstRef &)=default
\inmodule QtCore \reentrant
\inmodule QtCore\reentrant
Definition qjsonvalue.h:24
Definition qlist.h:74
\inmodule QtCore
list append(new Employee("Blackpool", "Stephen"))
cache insert(employee->id(), employee)
QSet< QString >::iterator it
set reserve(20000)
Combined button and popup list for selecting options.
constexpr bool operator!=(const timespec &t1, const timespec &t2)
constexpr timespec operator+(const timespec &t1, const timespec &t2)
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
Q_CORE_EXPORT QDataStream & operator>>(QDataStream &, QJsonArray &)
Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &)
Q_CORE_EXPORT size_t qHash(const QJsonArray &array, size_t seed=0)
QList< QVariant > QVariantList
Definition qjsonarray.h:15
static bool contains(const QJsonArray &haystack, unsigned needle)
Definition qopengl.cpp:116
GLsizei const GLfloat * v
[13]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint index
[2]
GLboolean r
[2]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLint first
GLfloat n
GLenum array
GLdouble GLdouble t
Definition qopenglext.h:243
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1219
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
ptrdiff_t qptrdiff
Definition qtypes.h:69
unsigned long long quint64
Definition qtypes.h:56
ptrdiff_t qsizetype
Definition qtypes.h:70
QList< int > list
[14]
QDataStream & operator<<(QDataStream &out, const MyClass &myObj)
[4]
QObject::connect nullptr
list prepend("one")
QSharedPointer< T > other(t)
[5]
this swap(other)
QGraphicsItem * item
QAction * at
QJSValueList args