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
12QT_BEGIN_NAMESPACE
13
14class QDebug;
15typedef QList<QVariant> QVariantList;
16
17class Q_CORE_EXPORT QJsonArray
18{
19public:
20 QJsonArray();
21
22 QJsonArray(std::initializer_list<QJsonValue> args);
23
24 ~QJsonArray();
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(i: 0); }
54 inline void removeLast() { removeAt(i: 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#if QT_CORE_REMOVED_SINCE(6, 8)
64 bool operator==(const QJsonArray &other) const;
65 bool operator!=(const QJsonArray &other) const;
66#endif
67 void swap(QJsonArray &other) noexcept
68 {
69 a.swap(other&: other.a);
70 }
71
72 class const_iterator;
73
74 class iterator {
75 public:
76 typedef std::random_access_iterator_tag iterator_category;
77 typedef qsizetype difference_type;
78 typedef QJsonValue value_type;
79 typedef QJsonValueRef reference;
80 typedef QJsonValueRef *pointer;
81
82 inline iterator() : item(static_cast<QJsonArray *>(nullptr), 0) { }
83 explicit inline iterator(QJsonArray *array, qsizetype index) : item(array, index) { }
84
85 constexpr iterator(const iterator &other) = default;
86 iterator &operator=(const iterator &other)
87 {
88 item.rebind(other: other.item);
89 return *this;
90 }
91
92 inline QJsonValueRef operator*() const { return item; }
93 inline const QJsonValueConstRef *operator->() const { return &item; }
94 inline QJsonValueRef *operator->() { return &item; }
95 inline QJsonValueRef operator[](qsizetype j) const { return *(*this + j); }
96
97#if QT_CORE_REMOVED_SINCE(6, 8)
98 inline bool operator==(const iterator &o) const
99 { return item.d == o.item.d && item.index == o.item.index; }
100 inline bool operator!=(const iterator &o) const { return !operator==(o); }
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
104 { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
105 inline bool operator>(const iterator &other) const { return !operator<=(other); }
106 inline bool operator>=(const iterator &other) const { return !operator<(other); }
107 inline bool operator==(const const_iterator &o) const
108 { return item.d == o.item.d && item.index == o.item.index; }
109 inline bool operator!=(const const_iterator &o) const { return !operator==(o); }
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
113 { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
114 inline bool operator>(const const_iterator &other) const { return !operator<=(other); }
115 inline bool operator>=(const const_iterator &other) const { return !operator<(other); }
116#endif
117 inline iterator &operator++() { ++item.index; return *this; }
118 inline iterator operator++(int) { iterator n = *this; ++item.index; return n; }
119 inline iterator &operator--() { item.index--; return *this; }
120 inline iterator operator--(int) { iterator n = *this; item.index--; return n; }
121 inline iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; }
122 inline iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; }
123 inline iterator operator+(qsizetype j) const { iterator r = *this; return r += j; }
124 inline iterator operator-(qsizetype j) const { return operator+(j: -j); }
125 inline qsizetype operator-(iterator j) const { return item.index - j.item.index; }
126
127 private:
128 // Helper functions
129 static bool comparesEqual_helper(const iterator &lhs, const iterator &rhs) noexcept
130 {
131 return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index;
132 }
133
134 static bool comparesEqual_helper(const iterator &lhs, const const_iterator &rhs) noexcept
135 {
136 return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index;
137 }
138
139 static Qt::strong_ordering compareThreeWay_helper(const iterator &lhs,
140 const iterator &rhs)
141 {
142 Q_ASSERT(lhs.item.d == rhs.item.d);
143 return Qt::compareThreeWay(lhs: lhs.item.index, rhs: rhs.item.index);
144 }
145
146 static Qt::strong_ordering compareThreeWay_helper(const iterator &lhs,
147 const const_iterator &rhs)
148 {
149 Q_ASSERT(lhs.item.d == rhs.item.d);
150 return Qt::compareThreeWay(lhs: lhs.item.index, rhs: rhs.item.index);
151 }
152
153 // Compare friends
154 friend bool comparesEqual(const iterator &lhs, const iterator &rhs) noexcept
155 {
156 return comparesEqual_helper(lhs, rhs);
157 }
158 friend Qt::strong_ordering compareThreeWay(const iterator &lhs,
159 const iterator &rhs)
160 {
161 return compareThreeWay_helper(lhs, rhs);
162 }
163 Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(iterator)
164 friend bool comparesEqual(const iterator &lhs, const const_iterator &rhs) noexcept
165 {
166 return comparesEqual_helper(lhs, rhs);
167 }
168 friend Qt::strong_ordering compareThreeWay(const iterator &lhs,
169 const const_iterator &rhs)
170 {
171 return compareThreeWay_helper(lhs, rhs);
172 }
173 Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(iterator, const_iterator)
174
175 QJsonValueRef item;
176 friend class QJsonArray;
177 };
178 friend class iterator;
179
180 class const_iterator {
181 public:
182 typedef std::random_access_iterator_tag iterator_category;
183 typedef qptrdiff difference_type;
184 typedef QJsonValue value_type;
185 typedef const QJsonValueRef reference;
186 typedef const QJsonValueRef *pointer;
187
188 inline const_iterator() : item(static_cast<QJsonArray *>(nullptr), 0) { }
189 explicit inline const_iterator(const QJsonArray *array, qsizetype index)
190 : item(const_cast<QJsonArray *>(array), index) { }
191 inline const_iterator(const iterator &o) : item(o.item) { }
192
193 constexpr const_iterator(const const_iterator &other) = default;
194 const_iterator &operator=(const const_iterator &other)
195 {
196 item.rebind(other: other.item);
197 return *this;
198 }
199
200 inline const QJsonValueConstRef operator*() const { return item; }
201 inline const QJsonValueConstRef *operator->() const { return &item; }
202
203 inline QJsonValueConstRef operator[](qsizetype j) const { return *(*this + j); }
204#if QT_CORE_REMOVED_SINCE(6, 8)
205 inline bool operator==(const const_iterator &o) const
206 { return item.d == o.item.d && item.index == o.item.index; }
207 inline bool operator!=(const const_iterator &o) const { return !operator==(o); }
208 inline bool operator<(const const_iterator &other) const
209 { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
210 inline bool operator<=(const const_iterator &other) const
211 { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
212 inline bool operator>(const const_iterator &other) const { return !operator<=(other); }
213 inline bool operator>=(const const_iterator &other) const { return !operator<(other); }
214#endif
215 inline const_iterator &operator++() { ++item.index; return *this; }
216 inline const_iterator operator++(int) { const_iterator n = *this; ++item.index; return n; }
217 inline const_iterator &operator--() { item.index--; return *this; }
218 inline const_iterator operator--(int) { const_iterator n = *this; item.index--; return n; }
219 inline const_iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; }
220 inline const_iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; }
221 inline const_iterator operator+(qsizetype j) const { const_iterator r = *this; return r += j; }
222 inline const_iterator operator-(qsizetype j) const { return operator+(j: -j); }
223 inline qsizetype operator-(const_iterator j) const { return item.index - j.item.index; }
224
225 private:
226 // Helper functions
227 static bool comparesEqual_helper(const const_iterator &lhs,
228 const const_iterator &rhs) noexcept
229 {
230 return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index;
231 }
232 static Qt::strong_ordering compareThreeWay_helper(const const_iterator &lhs,
233 const const_iterator &rhs)
234 {
235 Q_ASSERT(lhs.item.d == rhs.item.d);
236 return Qt::compareThreeWay(lhs: lhs.item.index, rhs: rhs.item.index);
237 }
238
239 // Compare friends
240 friend bool comparesEqual(const const_iterator &lhs, const const_iterator &rhs) noexcept
241 {
242 return comparesEqual_helper(lhs, rhs);
243 }
244 friend Qt::strong_ordering compareThreeWay(const const_iterator &lhs,
245 const const_iterator &rhs)
246 {
247 return compareThreeWay_helper(lhs, rhs);
248 }
249 Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(const_iterator)
250 QJsonValueConstRef item;
251 friend class QJsonArray;
252 };
253 friend class const_iterator;
254
255 // stl style
256 inline iterator begin() { detach(); return iterator(this, 0); }
257 inline const_iterator begin() const { return const_iterator(this, 0); }
258 inline const_iterator constBegin() const { return const_iterator(this, 0); }
259 inline const_iterator cbegin() const { return const_iterator(this, 0); }
260 inline iterator end() { detach(); return iterator(this, size()); }
261 inline const_iterator end() const { return const_iterator(this, size()); }
262 inline const_iterator constEnd() const { return const_iterator(this, size()); }
263 inline const_iterator cend() const { return const_iterator(this, size()); }
264 iterator insert(iterator before, const QJsonValue &value)
265 { insert(i: before.item.index, value); return before; }
266 iterator erase(iterator it)
267 { removeAt(i: it.item.index); return it; }
268
269 // more Qt
270 typedef iterator Iterator;
271 typedef const_iterator ConstIterator;
272
273 // convenience
274 inline QJsonArray operator+(const QJsonValue &v) const
275 { QJsonArray n = *this; n += v; return n; }
276 inline QJsonArray &operator+=(const QJsonValue &v)
277 { append(value: v); return *this; }
278 inline QJsonArray &operator<< (const QJsonValue &v)
279 { append(value: v); return *this; }
280
281 // stl compatibility
282 inline void push_back(const QJsonValue &t) { append(value: t); }
283 inline void push_front(const QJsonValue &t) { prepend(value: t); }
284 inline void pop_front() { removeFirst(); }
285 inline void pop_back() { removeLast(); }
286 inline bool empty() const { return isEmpty(); }
287 typedef qsizetype size_type;
288 typedef QJsonValue value_type;
289 typedef value_type *pointer;
290 typedef const value_type *const_pointer;
291 typedef QJsonValueRef reference;
292 typedef QJsonValue const_reference;
293 typedef qsizetype difference_type;
294
295private:
296 friend class QJsonValue;
297 friend class QJsonValueConstRef;
298 friend class QJsonValueRef;
299 friend class QJsonPrivate::Value;
300 friend class QJsonDocument;
301 friend class QCborArray;
302 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &);
303
304 friend Q_CORE_EXPORT bool comparesEqual(const QJsonArray &lhs,
305 const QJsonArray &rhs);
306
307 friend Q_CORE_EXPORT bool comparesEqual(const QJsonArray &lhs,
308 const QJsonValue &rhs);
309 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonArray)
310 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonArray, QJsonValue)
311
312 QJsonArray(QCborContainerPrivate *array);
313 bool detach(qsizetype reserve = 0);
314
315 QExplicitlySharedDataPointer<QCborContainerPrivate> a;
316};
317
318Q_DECLARE_SHARED(QJsonArray)
319
320#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
321inline QJsonValueConstRef::QJsonValueConstRef(QJsonArray *a, qsizetype idx)
322 : d(a ? a->a.data() : nullptr), is_object(false), index(idx)
323{}
324#endif
325
326Q_CORE_EXPORT size_t qHash(const QJsonArray &array, size_t seed = 0);
327
328#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
329Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &);
330#endif
331
332#ifndef QT_NO_DATASTREAM
333Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonArray &);
334Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonArray &);
335#endif
336
337QT_END_NAMESPACE
338
339#endif // QJSONARRAY_H
340

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of qtbase/src/corelib/serialization/qjsonarray.h