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 QJSONOBJECT_H |
41 | #define QJSONOBJECT_H |
42 | |
43 | #include <QtCore/qjsonvalue.h> |
44 | #include <QtCore/qiterator.h> |
45 | #include <QtCore/qpair.h> |
46 | #include <QtCore/qshareddata.h> |
47 | #include <initializer_list> |
48 | |
49 | QT_BEGIN_NAMESPACE |
50 | |
51 | class QDebug; |
52 | template <class Key, class T> class QMap; |
53 | typedef QMap<QString, QVariant> QVariantMap; |
54 | template <class Key, class T> class QHash; |
55 | typedef QHash<QString, QVariant> QVariantHash; |
56 | |
57 | class QCborContainerPrivate; |
58 | |
59 | class Q_CORE_EXPORT QJsonObject |
60 | { |
61 | public: |
62 | QJsonObject(); |
63 | |
64 | QJsonObject(std::initializer_list<QPair<QString, QJsonValue> > args); |
65 | |
66 | ~QJsonObject(); |
67 | |
68 | QJsonObject(const QJsonObject &other); |
69 | QJsonObject &operator =(const QJsonObject &other); |
70 | |
71 | QJsonObject(QJsonObject &&other) noexcept; |
72 | |
73 | QJsonObject &operator =(QJsonObject &&other) noexcept |
74 | { |
75 | swap(other); |
76 | return *this; |
77 | } |
78 | |
79 | void swap(QJsonObject &other) noexcept |
80 | { |
81 | qSwap(o, other.o); |
82 | } |
83 | |
84 | static QJsonObject fromVariantMap(const QVariantMap &map); |
85 | QVariantMap toVariantMap() const; |
86 | static QJsonObject fromVariantHash(const QVariantHash &map); |
87 | QVariantHash toVariantHash() const; |
88 | |
89 | QStringList keys() const; |
90 | int size() const; |
91 | inline int count() const { return size(); } |
92 | inline int length() const { return size(); } |
93 | bool isEmpty() const; |
94 | |
95 | #if QT_STRINGVIEW_LEVEL < 2 |
96 | QJsonValue value(const QString &key) const; |
97 | QJsonValue operator[] (const QString &key) const; |
98 | QJsonValueRef operator[] (const QString &key); |
99 | #endif |
100 | QJsonValue value(QStringView key) const; |
101 | QJsonValue value(QLatin1String key) const; |
102 | QJsonValue operator[] (QStringView key) const { return value(key); } |
103 | QJsonValue operator[] (QLatin1String key) const { return value(key); } |
104 | QJsonValueRef operator[] (QStringView key); |
105 | QJsonValueRef operator[] (QLatin1String key); |
106 | |
107 | #if QT_STRINGVIEW_LEVEL < 2 |
108 | void remove(const QString &key); |
109 | QJsonValue take(const QString &key); |
110 | bool contains(const QString &key) const; |
111 | #endif |
112 | void remove(QStringView key); |
113 | void remove(QLatin1String key); |
114 | QJsonValue take(QStringView key); |
115 | QJsonValue take(QLatin1String key); |
116 | bool contains(QStringView key) const; |
117 | bool contains(QLatin1String key) const; |
118 | |
119 | bool operator==(const QJsonObject &other) const; |
120 | bool operator!=(const QJsonObject &other) const; |
121 | |
122 | class const_iterator; |
123 | |
124 | class iterator |
125 | { |
126 | friend class const_iterator; |
127 | friend class QJsonObject; |
128 | QJsonObject *o; |
129 | int i; |
130 | |
131 | public: |
132 | typedef std::random_access_iterator_tag iterator_category; |
133 | typedef int difference_type; |
134 | typedef QJsonValue value_type; |
135 | typedef QJsonValueRef reference; |
136 | typedef QJsonValuePtr pointer; |
137 | |
138 | Q_DECL_CONSTEXPR inline iterator() : o(nullptr), i(0) {} |
139 | Q_DECL_CONSTEXPR inline iterator(QJsonObject *obj, int index) : o(obj), i(index) {} |
140 | |
141 | inline QString key() const { return o->keyAt(i); } |
142 | inline QJsonValueRef value() const { return QJsonValueRef(o, i); } |
143 | inline QJsonValueRef operator*() const { return QJsonValueRef(o, i); } |
144 | #ifdef Q_QDOC |
145 | inline QJsonValueRef* operator->() const; |
146 | #else |
147 | inline QJsonValueRefPtr operator->() const { return QJsonValueRefPtr(o, i); } |
148 | #endif |
149 | const QJsonValueRef operator[](int j) { return QJsonValueRef(o, i + j); } |
150 | |
151 | inline bool operator==(const iterator &other) const { return i == other.i; } |
152 | inline bool operator!=(const iterator &other) const { return i != other.i; } |
153 | bool operator<(const iterator& other) const { return i < other.i; } |
154 | bool operator<=(const iterator& other) const { return i <= other.i; } |
155 | bool operator>(const iterator& other) const { return i > other.i; } |
156 | bool operator>=(const iterator& other) const { return i >= other.i; } |
157 | |
158 | inline iterator &operator++() { ++i; return *this; } |
159 | inline iterator operator++(int) { iterator r = *this; ++i; return r; } |
160 | inline iterator &operator--() { --i; return *this; } |
161 | inline iterator operator--(int) { iterator r = *this; --i; return r; } |
162 | inline iterator operator+(int j) const |
163 | { iterator r = *this; r.i += j; return r; } |
164 | inline iterator operator-(int j) const { return operator+(-j); } |
165 | inline iterator &operator+=(int j) { i += j; return *this; } |
166 | inline iterator &operator-=(int j) { i -= j; return *this; } |
167 | int operator-(iterator j) const { return i - j.i; } |
168 | |
169 | public: |
170 | inline bool operator==(const const_iterator &other) const { return i == other.i; } |
171 | inline bool operator!=(const const_iterator &other) const { return i != other.i; } |
172 | bool operator<(const const_iterator& other) const { return i < other.i; } |
173 | bool operator<=(const const_iterator& other) const { return i <= other.i; } |
174 | bool operator>(const const_iterator& other) const { return i > other.i; } |
175 | bool operator>=(const const_iterator& other) const { return i >= other.i; } |
176 | }; |
177 | friend class iterator; |
178 | |
179 | class const_iterator |
180 | { |
181 | friend class iterator; |
182 | const QJsonObject *o; |
183 | int i; |
184 | |
185 | public: |
186 | typedef std::random_access_iterator_tag iterator_category; |
187 | typedef int difference_type; |
188 | typedef QJsonValue value_type; |
189 | typedef QJsonValue reference; |
190 | typedef QJsonValuePtr pointer; |
191 | |
192 | Q_DECL_CONSTEXPR inline const_iterator() : o(nullptr), i(0) {} |
193 | Q_DECL_CONSTEXPR inline const_iterator(const QJsonObject *obj, int index) |
194 | : o(obj), i(index) {} |
195 | inline const_iterator(const iterator &other) |
196 | : o(other.o), i(other.i) {} |
197 | |
198 | inline QString key() const { return o->keyAt(i); } |
199 | inline QJsonValue value() const { return o->valueAt(i); } |
200 | inline QJsonValue operator*() const { return o->valueAt(i); } |
201 | #ifdef Q_QDOC |
202 | inline QJsonValue* operator->() const; |
203 | #else |
204 | inline QJsonValuePtr operator->() const { return QJsonValuePtr(o->valueAt(i)); } |
205 | #endif |
206 | const QJsonValue operator[](int j) { return o->valueAt(i + j); } |
207 | |
208 | inline bool operator==(const const_iterator &other) const { return i == other.i; } |
209 | inline bool operator!=(const const_iterator &other) const { return i != other.i; } |
210 | bool operator<(const const_iterator& other) const { return i < other.i; } |
211 | bool operator<=(const const_iterator& other) const { return i <= other.i; } |
212 | bool operator>(const const_iterator& other) const { return i > other.i; } |
213 | bool operator>=(const const_iterator& other) const { return i >= other.i; } |
214 | |
215 | inline const_iterator &operator++() { ++i; return *this; } |
216 | inline const_iterator operator++(int) { const_iterator r = *this; ++i; return r; } |
217 | inline const_iterator &operator--() { --i; return *this; } |
218 | inline const_iterator operator--(int) { const_iterator r = *this; --i; return r; } |
219 | inline const_iterator operator+(int j) const |
220 | { const_iterator r = *this; r.i += j; return r; } |
221 | inline const_iterator operator-(int j) const { return operator+(-j); } |
222 | inline const_iterator &operator+=(int j) { i += j; return *this; } |
223 | inline const_iterator &operator-=(int j) { i -= j; return *this; } |
224 | int operator-(const_iterator j) const { return i - j.i; } |
225 | |
226 | inline bool operator==(const iterator &other) const { return i == other.i; } |
227 | inline bool operator!=(const iterator &other) const { return i != other.i; } |
228 | bool operator<(const iterator& other) const { return i < other.i; } |
229 | bool operator<=(const iterator& other) const { return i <= other.i; } |
230 | bool operator>(const iterator& other) const { return i > other.i; } |
231 | bool operator>=(const iterator& other) const { return i >= other.i; } |
232 | }; |
233 | friend class const_iterator; |
234 | |
235 | // STL style |
236 | inline iterator begin() { detach2(); return iterator(this, 0); } |
237 | inline const_iterator begin() const { return const_iterator(this, 0); } |
238 | inline const_iterator constBegin() const { return const_iterator(this, 0); } |
239 | inline iterator end() { detach2(); return iterator(this, size()); } |
240 | inline const_iterator end() const { return const_iterator(this, size()); } |
241 | inline const_iterator constEnd() const { return const_iterator(this, size()); } |
242 | iterator erase(iterator it); |
243 | |
244 | // more Qt |
245 | typedef iterator Iterator; |
246 | typedef const_iterator ConstIterator; |
247 | #if QT_STRINGVIEW_LEVEL < 2 |
248 | iterator find(const QString &key); |
249 | const_iterator find(const QString &key) const { return constFind(key); } |
250 | const_iterator constFind(const QString &key) const; |
251 | iterator insert(const QString &key, const QJsonValue &value); |
252 | #endif |
253 | iterator find(QStringView key); |
254 | iterator find(QLatin1String key); |
255 | const_iterator find(QStringView key) const { return constFind(key); } |
256 | const_iterator find(QLatin1String key) const { return constFind(key); } |
257 | const_iterator constFind(QStringView key) const; |
258 | const_iterator constFind(QLatin1String key) const; |
259 | iterator insert(QStringView key, const QJsonValue &value); |
260 | iterator insert(QLatin1String key, const QJsonValue &value); |
261 | |
262 | // STL compatibility |
263 | typedef QJsonValue mapped_type; |
264 | typedef QString key_type; |
265 | typedef int size_type; |
266 | |
267 | inline bool empty() const { return isEmpty(); } |
268 | |
269 | private: |
270 | friend class QJsonValue; |
271 | friend class QJsonDocument; |
272 | friend class QJsonValueRef; |
273 | friend class QCborMap; |
274 | friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &); |
275 | |
276 | QJsonObject(QCborContainerPrivate *object); |
277 | void initialize(); |
278 | // ### Qt 6: remove me and merge with detach2 |
279 | void detach(uint reserve = 0); |
280 | bool detach2(uint reserve = 0); |
281 | void compact(); |
282 | |
283 | template <typename T> QJsonValue valueImpl(T key) const; |
284 | template <typename T> QJsonValueRef atImpl(T key); |
285 | template <typename T> void removeImpl(T key); |
286 | template <typename T> QJsonValue takeImpl(T key); |
287 | template <typename T> bool containsImpl(T key) const; |
288 | template <typename T> iterator findImpl(T key); |
289 | template <typename T> const_iterator constFindImpl(T key) const; |
290 | template <typename T> iterator insertImpl(T key, const QJsonValue &value); |
291 | |
292 | QString keyAt(int i) const; |
293 | QJsonValue valueAt(int i) const; |
294 | void setValueAt(int i, const QJsonValue &val); |
295 | void removeAt(int i); |
296 | template <typename T> iterator insertAt(int i, T key, const QJsonValue &val, bool exists); |
297 | |
298 | // ### Qt 6: remove |
299 | void *dead = nullptr; |
300 | QExplicitlySharedDataPointer<QCborContainerPrivate> o; |
301 | }; |
302 | |
303 | Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QJsonObject) |
304 | |
305 | Q_CORE_EXPORT uint qHash(const QJsonObject &object, uint seed = 0); |
306 | |
307 | #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) |
308 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &); |
309 | #endif |
310 | |
311 | #ifndef QT_NO_DATASTREAM |
312 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonObject &); |
313 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonObject &); |
314 | #endif |
315 | |
316 | QT_END_NAMESPACE |
317 | |
318 | #endif // QJSONOBJECT_H |
319 | |