QtBase  v6.3.1
qjsonarray.h
Go to the documentation of this file.
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 
49 
50 class QDebug;
52 
53 class Q_CORE_EXPORT QJsonArray
54 {
55 public:
57 
58  QJsonArray(std::initializer_list<QJsonValue> args);
59 
61 
62  QJsonArray(const QJsonArray &other);
63  QJsonArray &operator =(const QJsonArray &other);
64 
65  QJsonArray(QJsonArray &&other) noexcept;
66 
67  QJsonArray &operator =(QJsonArray &&other) noexcept
68  {
69  swap(other);
70  return *this;
71  }
72 
73  static QJsonArray fromStringList(const QStringList &list);
74  static QJsonArray fromVariantList(const QVariantList &list);
75  QVariantList toVariantList() const;
76 
77  qsizetype size() const;
78  inline qsizetype count() const { return size(); }
79 
80  bool isEmpty() const;
81  QJsonValue at(qsizetype i) const;
82  QJsonValue first() const;
83  QJsonValue last() const;
84 
85  void prepend(const QJsonValue &value);
86  void append(const QJsonValue &value);
87  void removeAt(qsizetype i);
88  QJsonValue takeAt(qsizetype i);
89  inline void removeFirst() { removeAt(0); }
90  inline void removeLast() { removeAt(size() - 1); }
91 
92  void insert(qsizetype i, const QJsonValue &value);
93  void replace(qsizetype i, const QJsonValue &value);
94 
95  bool contains(const QJsonValue &element) const;
96  QJsonValueRef operator[](qsizetype i);
97  QJsonValue operator[](qsizetype i) const;
98 
99  bool operator==(const QJsonArray &other) const;
100  bool operator!=(const QJsonArray &other) const;
101 
102  void swap(QJsonArray &other) noexcept
103  {
104  a.swap(other.a);
105  }
106 
107  class const_iterator;
108 
109  class iterator {
110  public:
111  typedef std::random_access_iterator_tag iterator_category;
116 
117  inline iterator() : item(static_cast<QJsonArray *>(nullptr), 0) { }
118  explicit inline iterator(QJsonArray *array, qsizetype index) : item(array, index) { }
119 
120  constexpr iterator(const iterator &other) = default;
122  {
123  item.a = other.item.a;
124  item.index = other.item.index;
125  return *this;
126  }
127 
128  inline QJsonValueRef operator*() const { return item; }
129  inline QJsonValueRef *operator->() const { return &item; }
131  { return { item.a, qsizetype(item.index) + j }; }
132 
133  inline bool operator==(const iterator &o) const
134  { return item.a == o.item.a && item.index == o.item.index; }
135  inline bool operator!=(const iterator &o) const { return !(*this == o); }
136  inline bool operator<(const iterator &other) const
137  { Q_ASSERT(item.a == other.item.a); return item.index < other.item.index; }
138  inline bool operator<=(const iterator &other) const
139  { Q_ASSERT(item.a == other.item.a); return item.index <= other.item.index; }
140  inline bool operator>(const iterator &other) const { return !(*this <= other); }
141  inline bool operator>=(const iterator &other) const { return !(*this < other); }
142  inline bool operator==(const const_iterator &o) const
143  { return item.a == o.item.a && item.index == o.item.index; }
144  inline bool operator!=(const const_iterator &o) const { return !(*this == o); }
145  inline bool operator<(const const_iterator &other) const
146  { Q_ASSERT(item.a == other.item.a); return item.index < other.item.index; }
147  inline bool operator<=(const const_iterator &other) const
148  { Q_ASSERT(item.a == other.item.a); return item.index <= other.item.index; }
149  inline bool operator>(const const_iterator &other) const { return !(*this <= other); }
150  inline bool operator>=(const const_iterator &other) const { return !(*this < other); }
151  inline iterator &operator++() { ++item.index; return *this; }
152  inline iterator operator++(int) { iterator n = *this; ++item.index; return n; }
153  inline iterator &operator--() { item.index--; return *this; }
154  inline iterator operator--(int) { iterator n = *this; item.index--; return n; }
155  inline iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; }
156  inline iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; }
158  { return iterator(item.a, qsizetype(item.index) + j); }
160  { return iterator(item.a, qsizetype(item.index) - j); }
161  inline qsizetype operator-(iterator j) const { return item.index - j.item.index; }
162 
163  private:
164  mutable QJsonValueRef item;
165  friend class QJsonArray;
166  };
167  friend class iterator;
168 
170  public:
171  typedef std::random_access_iterator_tag iterator_category;
174  typedef const QJsonValueRef reference;
175  typedef const QJsonValueRef *pointer;
176 
177  inline const_iterator() : item(static_cast<QJsonArray *>(nullptr), 0) { }
178  explicit inline const_iterator(const QJsonArray *array, qsizetype index)
179  : item(const_cast<QJsonArray *>(array), index) { }
180  inline const_iterator(const iterator &o) : item(o.item) { }
181 
182  constexpr const_iterator(const const_iterator &other) = default;
184  {
185  item.a = other.item.a;
186  item.index = other.item.index;
187  return *this;
188  }
189 
190  inline const QJsonValueRef operator*() const { return item; }
191  inline const QJsonValueRef *operator->() const { return &item; }
192 
194  { return { item.a, qsizetype(item.index) + j }; }
195  inline bool operator==(const const_iterator &o) const
196  { return item.a == o.item.a && item.index == o.item.index; }
197  inline bool operator!=(const const_iterator &o) const { return !(*this == o); }
198  inline bool operator<(const const_iterator &other) const
199  { Q_ASSERT(item.a == other.item.a); return item.index < other.item.index; }
200  inline bool operator<=(const const_iterator &other) const
201  { Q_ASSERT(item.a == other.item.a); return item.index <= other.item.index; }
202  inline bool operator>(const const_iterator &other) const { return !(*this <= other); }
203  inline bool operator>=(const const_iterator &other) const { return !(*this < other); }
204  inline const_iterator &operator++() { ++item.index; return *this; }
205  inline const_iterator operator++(int) { const_iterator n = *this; ++item.index; return n; }
206  inline const_iterator &operator--() { item.index--; return *this; }
207  inline const_iterator operator--(int) { const_iterator n = *this; item.index--; return n; }
208  inline const_iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; }
209  inline const_iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; }
211  { return const_iterator(item.a, qsizetype(item.index) + j); }
213  { return const_iterator(item.a, qsizetype(item.index) - j); }
214  inline qsizetype operator-(const_iterator j) const { return item.index - j.item.index; }
215 
216  private:
218  friend class QJsonArray;
219  };
220  friend class const_iterator;
221 
222  // stl style
223  inline iterator begin() { detach(); return iterator(this, 0); }
224  inline const_iterator begin() const { return const_iterator(this, 0); }
225  inline const_iterator constBegin() const { return const_iterator(this, 0); }
226  inline const_iterator cbegin() const { return const_iterator(this, 0); }
227  inline iterator end() { detach(); return iterator(this, size()); }
228  inline const_iterator end() const { return const_iterator(this, size()); }
229  inline const_iterator constEnd() const { return const_iterator(this, size()); }
230  inline const_iterator cend() const { return const_iterator(this, size()); }
232  { insert(before.item.index, value); return before; }
234  { removeAt(it.item.index); return it; }
235 
236  // more Qt
239 
240  // convenience
241  inline QJsonArray operator+(const QJsonValue &v) const
242  { QJsonArray n = *this; n += v; return n; }
244  { append(v); return *this; }
246  { append(v); return *this; }
247 
248  // stl compatibility
249  inline void push_back(const QJsonValue &t) { append(t); }
250  inline void push_front(const QJsonValue &t) { prepend(t); }
251  inline void pop_front() { removeFirst(); }
252  inline void pop_back() { removeLast(); }
253  inline bool empty() const { return isEmpty(); }
256  typedef value_type *pointer;
257  typedef const value_type *const_pointer;
261 
262 private:
263  friend class QJsonValue;
264  friend class QJsonDocument;
265  friend class QCborArray;
266  friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &);
267 
269  bool detach(qsizetype reserve = 0);
270 
272 };
273 
275 
276 Q_CORE_EXPORT size_t qHash(const QJsonArray &array, size_t seed = 0);
277 
278 #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
279 Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &);
280 #endif
281 
282 #ifndef QT_NO_DATASTREAM
283 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonArray &);
284 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonArray &);
285 #endif
286 
288 
289 #endif // QJSONARRAY_H
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
The QCborArray class is used to hold an array of CBOR elements.
Definition: qcborarray.h:56
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:66
operator>>(QDataStream &ds, qfloat16 &f)
Definition: qfloat16.cpp:344
operator<<(QDataStream &ds, qfloat16 f)
Definition: qfloat16.cpp:327
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:65
template< typename Enum > size_t qHash(QFlags< Enum > flags, size_t seed=0) noexcept
The QJsonArray::const_iterator class provides an STL-style const iterator for QJsonArray.
Definition: qjsonarray.h:169
QJsonValueRef operator[](qsizetype j) const
Definition: qjsonarray.h:193
qsizetype operator-(const_iterator j) const
Definition: qjsonarray.h:214
const QJsonValueRef operator*() const
Definition: qjsonarray.h:190
bool operator>=(const const_iterator &other) const
Definition: qjsonarray.h:203
bool operator>(const const_iterator &other) const
Definition: qjsonarray.h:202
const_iterator(const iterator &o)
Definition: qjsonarray.h:180
const_iterator & operator-=(qsizetype j)
Definition: qjsonarray.h:209
std::random_access_iterator_tag iterator_category
Definition: qjsonarray.h:171
const_iterator & operator=(const const_iterator &other)
Definition: qjsonarray.h:183
const_iterator & operator++()
Definition: qjsonarray.h:204
const_iterator & operator--()
Definition: qjsonarray.h:206
const_iterator(const QJsonArray *array, qsizetype index)
Definition: qjsonarray.h:178
bool operator<=(const const_iterator &other) const
Definition: qjsonarray.h:200
bool operator==(const const_iterator &o) const
Definition: qjsonarray.h:195
const_iterator operator-(qsizetype j) const
Definition: qjsonarray.h:212
const QJsonValueRef * operator->() const
Definition: qjsonarray.h:191
const QJsonValueRef * pointer
Definition: qjsonarray.h:175
constexpr const_iterator(const const_iterator &other)=default
const QJsonValueRef reference
Definition: qjsonarray.h:174
bool operator<(const const_iterator &other) const
Definition: qjsonarray.h:198
bool operator!=(const const_iterator &o) const
Definition: qjsonarray.h:197
const_iterator operator+(qsizetype j) const
Definition: qjsonarray.h:210
const_iterator operator++(int)
Definition: qjsonarray.h:205
const_iterator & operator+=(qsizetype j)
Definition: qjsonarray.h:208
const_iterator operator--(int)
Definition: qjsonarray.h:207
The QJsonArray::iterator class provides an STL-style non-const iterator for QJsonArray.
Definition: qjsonarray.h:109
iterator & operator+=(qsizetype j)
Definition: qjsonarray.h:155
bool operator!=(const iterator &o) const
Definition: qjsonarray.h:135
iterator & operator++()
Definition: qjsonarray.h:151
bool operator>=(const iterator &other) const
Definition: qjsonarray.h:141
iterator & operator--()
Definition: qjsonarray.h:153
qsizetype operator-(iterator j) const
Definition: qjsonarray.h:161
iterator operator+(qsizetype j) const
Definition: qjsonarray.h:157
bool operator!=(const const_iterator &o) const
Definition: qjsonarray.h:144
bool operator>(const const_iterator &other) const
Definition: qjsonarray.h:149
QJsonValueRef * operator->() const
Definition: qjsonarray.h:129
iterator(QJsonArray *array, qsizetype index)
Definition: qjsonarray.h:118
QJsonValueRef operator*() const
Definition: qjsonarray.h:128
iterator operator-(qsizetype j) const
Definition: qjsonarray.h:159
QJsonValueRef operator[](qsizetype j) const
Definition: qjsonarray.h:130
constexpr iterator(const iterator &other)=default
QJsonValueRef * pointer
Definition: qjsonarray.h:115
bool operator<(const const_iterator &other) const
Definition: qjsonarray.h:145
std::random_access_iterator_tag iterator_category
Definition: qjsonarray.h:111
qsizetype difference_type
Definition: qjsonarray.h:112
bool operator==(const const_iterator &o) const
Definition: qjsonarray.h:142
iterator & operator=(const iterator &other)
Definition: qjsonarray.h:121
iterator operator--(int)
Definition: qjsonarray.h:154
QJsonValue value_type
Definition: qjsonarray.h:113
bool operator>(const iterator &other) const
Definition: qjsonarray.h:140
iterator operator++(int)
Definition: qjsonarray.h:152
iterator & operator-=(qsizetype j)
Definition: qjsonarray.h:156
bool operator==(const iterator &o) const
Definition: qjsonarray.h:133
bool operator<=(const const_iterator &other) const
Definition: qjsonarray.h:147
QJsonValueRef reference
Definition: qjsonarray.h:114
bool operator<=(const iterator &other) const
Definition: qjsonarray.h:138
bool operator>=(const const_iterator &other) const
Definition: qjsonarray.h:150
bool operator<(const iterator &other) const
Definition: qjsonarray.h:136
The QJsonArray class encapsulates a JSON array.
Definition: qjsonarray.h:54
void pop_front()
Definition: qjsonarray.h:251
bool empty() const
Definition: qjsonarray.h:253
iterator end()
Definition: qjsonarray.h:227
qsizetype difference_type
Definition: qjsonarray.h:260
qsizetype size_type
Definition: qjsonarray.h:254
iterator Iterator
Definition: qjsonarray.h:237
const_iterator end() const
Definition: qjsonarray.h:228
QJsonValue const_reference
Definition: qjsonarray.h:259
QJsonArray operator+(const QJsonValue &v) const
Definition: qjsonarray.h:241
iterator insert(iterator before, const QJsonValue &value)
Definition: qjsonarray.h:231
value_type * pointer
Definition: qjsonarray.h:256
const_iterator cend() const
Definition: qjsonarray.h:230
void pop_back()
Definition: qjsonarray.h:252
void push_front(const QJsonValue &t)
Definition: qjsonarray.h:250
const_iterator constBegin() const
Definition: qjsonarray.h:225
const_iterator constEnd() const
Definition: qjsonarray.h:229
QJsonValueRef reference
Definition: qjsonarray.h:258
void removeFirst()
Definition: qjsonarray.h:89
iterator erase(iterator it)
Definition: qjsonarray.h:233
const_iterator cbegin() const
Definition: qjsonarray.h:226
qsizetype count() const
Definition: qjsonarray.h:78
QJsonArray & operator+=(const QJsonValue &v)
Definition: qjsonarray.h:243
const value_type * const_pointer
Definition: qjsonarray.h:257
void swap(QJsonArray &other) noexcept
Definition: qjsonarray.h:102
const_iterator begin() const
Definition: qjsonarray.h:224
iterator begin()
Definition: qjsonarray.h:223
const_iterator ConstIterator
Definition: qjsonarray.h:238
QJsonValue value_type
Definition: qjsonarray.h:255
void push_back(const QJsonValue &t)
Definition: qjsonarray.h:249
void removeLast()
Definition: qjsonarray.h:90
The QJsonDocument class provides a way to read and write JSON documents.
Definition: qjsondocument.h:83
The QJsonValue class encapsulates a value in JSON.
Definition: qjsonvalue.h:60
The QJsonValueRef class is a helper class for QJsonValue.
Definition: qjsonvalue.h:154
The QStringList class provides a list of strings.
map insert("Paris", "France")
list append(new Employee("Blackpool", "Stephen"))
set reserve(20000)
set contains("Julia")
typename C::const_iterator const_iterator
typename C::iterator iterator
constexpr bool operator!=(const timespec &t1, const timespec &t2)
Definition: qcore_unix_p.h:124
EGLOutputLayerEXT EGLint EGLAttrib value
ptrdiff_t qptrdiff
Definition: qglobal.h:307
unsigned long long quint64
Definition: qglobal.h:299
ptrdiff_t qsizetype
Definition: qglobal.h:308
Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &)
bool operator==(const QMakeBaseKey &one, const QMakeBaseKey &two)
GLsizei const GLfloat * v
[13]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint index
[2]
GLint first
GLfloat n
GLenum array
Definition: qopenglext.h:7028
GLdouble GLdouble t
[9]
Definition: qopenglext.h:243
#define Q_ASSERT(cond)
Definition: qrandom.cpp:84
#define Q_DECLARE_SHARED(TYPE)
Definition: qtypeinfo.h:197
QObject::connect nullptr
list prepend("one")
QSharedPointer< T > other(t)
[5]
this swap(other)
QGraphicsItem * item
QAction * at
QStringList::Iterator it
QStringList list
[0]