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

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