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