1/****************************************************************************
2**
3** Copyright (C) 2016 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 QJSONARRAY_H
41#define QJSONARRAY_H
42
43#include <QtCore/qjsonvalue.h>
44#include <QtCore/qiterator.h>
45#include <initializer_list>
46
47QT_BEGIN_NAMESPACE
48
49class QDebug;
50class QStringList;
51template <typename T> class QList;
52typedef QList<QVariant> QVariantList;
53
54class Q_CORE_EXPORT QJsonArray
55{
56public:
57 QJsonArray();
58
59 QJsonArray(std::initializer_list<QJsonValue> args)
60 {
61 initialize();
62 for (std::initializer_list<QJsonValue>::const_iterator i = args.begin(); i != args.end(); ++i)
63 append(*i);
64 }
65
66 ~QJsonArray();
67
68 QJsonArray(const QJsonArray &other);
69 QJsonArray &operator =(const QJsonArray &other);
70
71 QJsonArray(QJsonArray &&other) noexcept
72 : d(other.d),
73 a(other.a)
74 {
75 other.d = nullptr;
76 other.a = nullptr;
77 }
78
79 QJsonArray &operator =(QJsonArray &&other) noexcept
80 {
81 swap(other);
82 return *this;
83 }
84
85 static QJsonArray fromStringList(const QStringList &list);
86 static QJsonArray fromVariantList(const QVariantList &list);
87 QVariantList toVariantList() const;
88
89 int size() const;
90 inline int count() const { return size(); }
91
92 bool isEmpty() const;
93 QJsonValue at(int i) const;
94 QJsonValue first() const;
95 QJsonValue last() const;
96
97 void prepend(const QJsonValue &value);
98 void append(const QJsonValue &value);
99 void removeAt(int i);
100 QJsonValue takeAt(int i);
101 inline void removeFirst() { removeAt(0); }
102 inline void removeLast() { removeAt(size() - 1); }
103
104 void insert(int i, const QJsonValue &value);
105 void replace(int i, const QJsonValue &value);
106
107 bool contains(const QJsonValue &element) const;
108 QJsonValueRef operator[](int i);
109 QJsonValue operator[](int i) const;
110
111 bool operator==(const QJsonArray &other) const;
112 bool operator!=(const QJsonArray &other) const;
113
114 void swap(QJsonArray &other) noexcept
115 {
116 qSwap(d, other.d);
117 qSwap(a, other.a);
118 }
119
120 class const_iterator;
121
122 class iterator {
123 public:
124 QJsonArray *a;
125 int i;
126 typedef std::random_access_iterator_tag iterator_category;
127 typedef int difference_type;
128 typedef QJsonValue value_type;
129 typedef QJsonValueRef reference;
130 typedef QJsonValueRefPtr pointer;
131
132 inline iterator() : a(nullptr), i(0) { }
133 explicit inline iterator(QJsonArray *array, int index) : a(array), i(index) { }
134
135 inline QJsonValueRef operator*() const { return QJsonValueRef(a, i); }
136#ifdef Q_QDOC
137 inline QJsonValueRef* operator->() const;
138#else
139 inline QJsonValueRefPtr operator->() const { return QJsonValueRefPtr(a, i); }
140#endif
141 inline QJsonValueRef operator[](int j) const { return QJsonValueRef(a, i + j); }
142
143 inline bool operator==(const iterator &o) const { return i == o.i; }
144 inline bool operator!=(const iterator &o) const { return i != o.i; }
145 inline bool operator<(const iterator& other) const { return i < other.i; }
146 inline bool operator<=(const iterator& other) const { return i <= other.i; }
147 inline bool operator>(const iterator& other) const { return i > other.i; }
148 inline bool operator>=(const iterator& other) const { return i >= other.i; }
149 inline bool operator==(const const_iterator &o) const { return i == o.i; }
150 inline bool operator!=(const const_iterator &o) const { return i != o.i; }
151 inline bool operator<(const const_iterator& other) const { return i < other.i; }
152 inline bool operator<=(const const_iterator& other) const { return i <= other.i; }
153 inline bool operator>(const const_iterator& other) const { return i > other.i; }
154 inline bool operator>=(const const_iterator& other) const { return i >= other.i; }
155 inline iterator &operator++() { ++i; return *this; }
156 inline iterator operator++(int) { iterator n = *this; ++i; return n; }
157 inline iterator &operator--() { i--; return *this; }
158 inline iterator operator--(int) { iterator n = *this; i--; return n; }
159 inline iterator &operator+=(int j) { i+=j; return *this; }
160 inline iterator &operator-=(int j) { i-=j; return *this; }
161 inline iterator operator+(int j) const { return iterator(a, i+j); }
162 inline iterator operator-(int j) const { return iterator(a, i-j); }
163 inline int operator-(iterator j) const { return i - j.i; }
164 };
165 friend class iterator;
166
167 class const_iterator {
168 public:
169 const QJsonArray *a;
170 int i;
171 typedef std::random_access_iterator_tag iterator_category;
172 typedef qptrdiff difference_type;
173 typedef QJsonValue value_type;
174 typedef QJsonValue reference;
175 typedef QJsonValuePtr pointer;
176
177 inline const_iterator() : a(nullptr), i(0) { }
178 explicit inline const_iterator(const QJsonArray *array, int index) : a(array), i(index) { }
179#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
180 inline const_iterator(const const_iterator &o) : a(o.a), i(o.i) {} // ### Qt 6: Removed so class can be trivially-copyable
181#endif
182 inline const_iterator(const iterator &o) : a(o.a), i(o.i) {}
183
184 inline QJsonValue operator*() const { return a->at(i); }
185#ifdef Q_QDOC
186 inline QJsonValue* operator->() const;
187#else
188 inline QJsonValuePtr operator->() const { return QJsonValuePtr(a->at(i)); }
189#endif
190 inline QJsonValue operator[](int j) const { return a->at(i+j); }
191 inline bool operator==(const const_iterator &o) const { return i == o.i; }
192 inline bool operator!=(const const_iterator &o) const { return i != o.i; }
193 inline bool operator<(const const_iterator& other) const { return i < other.i; }
194 inline bool operator<=(const const_iterator& other) const { return i <= other.i; }
195 inline bool operator>(const const_iterator& other) const { return i > other.i; }
196 inline bool operator>=(const const_iterator& other) const { return i >= other.i; }
197 inline const_iterator &operator++() { ++i; return *this; }
198 inline const_iterator operator++(int) { const_iterator n = *this; ++i; return n; }
199 inline const_iterator &operator--() { i--; return *this; }
200 inline const_iterator operator--(int) { const_iterator n = *this; i--; return n; }
201 inline const_iterator &operator+=(int j) { i+=j; return *this; }
202 inline const_iterator &operator-=(int j) { i-=j; return *this; }
203 inline const_iterator operator+(int j) const { return const_iterator(a, i+j); }
204 inline const_iterator operator-(int j) const { return const_iterator(a, i-j); }
205 inline int operator-(const_iterator j) const { return i - j.i; }
206 };
207 friend class const_iterator;
208
209 // stl style
210 inline iterator begin() { detach2(); return iterator(this, 0); }
211 inline const_iterator begin() const { return const_iterator(this, 0); }
212 inline const_iterator constBegin() const { return const_iterator(this, 0); }
213 inline const_iterator cbegin() const { return const_iterator(this, 0); }
214 inline iterator end() { detach2(); return iterator(this, size()); }
215 inline const_iterator end() const { return const_iterator(this, size()); }
216 inline const_iterator constEnd() const { return const_iterator(this, size()); }
217 inline const_iterator cend() const { return const_iterator(this, size()); }
218 iterator insert(iterator before, const QJsonValue &value) { insert(before.i, value); return before; }
219 iterator erase(iterator it) { removeAt(it.i); return it; }
220
221 // more Qt
222 typedef iterator Iterator;
223 typedef const_iterator ConstIterator;
224
225 // convenience
226 inline QJsonArray operator+(const QJsonValue &v) const
227 { QJsonArray n = *this; n += v; return n; }
228 inline QJsonArray &operator+=(const QJsonValue &v)
229 { append(v); return *this; }
230 inline QJsonArray &operator<< (const QJsonValue &v)
231 { append(v); return *this; }
232
233 // stl compatibility
234 inline void push_back(const QJsonValue &t) { append(t); }
235 inline void push_front(const QJsonValue &t) { prepend(t); }
236 inline void pop_front() { removeFirst(); }
237 inline void pop_back() { removeLast(); }
238 inline bool empty() const { return isEmpty(); }
239 typedef int size_type;
240 typedef QJsonValue value_type;
241 typedef value_type *pointer;
242 typedef const value_type *const_pointer;
243 typedef QJsonValueRef reference;
244 typedef QJsonValue const_reference;
245 typedef int difference_type;
246
247private:
248 friend class QJsonPrivate::Data;
249 friend class QJsonValue;
250 friend class QJsonDocument;
251 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &);
252
253 QJsonArray(QJsonPrivate::Data *data, QJsonPrivate::Array *array);
254 void initialize();
255 void compact();
256 // ### Qt 6: remove me and merge with detach2
257 void detach(uint reserve = 0);
258 bool detach2(uint reserve = 0);
259
260 QJsonPrivate::Data *d;
261 QJsonPrivate::Array *a;
262};
263
264Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QJsonArray)
265
266Q_CORE_EXPORT uint qHash(const QJsonArray &array, uint seed = 0);
267
268#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
269Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &);
270#endif
271
272#ifndef QT_NO_DATASTREAM
273Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonArray &);
274Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonArray &);
275#endif
276
277QT_END_NAMESPACE
278
279#endif // QJSONARRAY_H
280