1/****************************************************************************
2**
3** Copyright (C) 2018 Intel Corporation.
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 QCBORARRAY_H
41#define QCBORARRAY_H
42
43#include <QtCore/qcborvalue.h>
44
45#include <initializer_list>
46
47QT_BEGIN_NAMESPACE
48
49class QJsonArray;
50class QDataStream;
51
52class QCborContainerPrivate;
53class Q_CORE_EXPORT QCborArray
54{
55public:
56 class ConstIterator;
57 class Iterator {
58 mutable QCborValueRef item;
59 friend class ConstIterator;
60 friend class QCborArray;
61 Iterator(QCborContainerPrivate *dd, qsizetype ii) : item(dd, ii) {}
62 public:
63 typedef std::random_access_iterator_tag iterator_category;
64 typedef qsizetype difference_type;
65 typedef QCborValue value_type;
66 typedef QCborValueRef reference;
67 typedef QCborValueRef *pointer;
68
69 Q_DECL_CONSTEXPR Iterator() = default;
70 Q_DECL_CONSTEXPR Iterator(const Iterator &) = default;
71 Iterator &operator=(const Iterator &other)
72 {
73 // rebind the reference
74 item.d = other.item.d;
75 item.i = other.item.i;
76 return *this;
77 }
78
79 QCborValueRef operator*() const { return item; }
80 QCborValueRef *operator->() const { return &item; }
81 QCborValueRef operator[](qsizetype j) { return { item.d, item.i + j }; }
82
83 bool operator==(const Iterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
84 bool operator!=(const Iterator &o) const { return !(*this == o); }
85 bool operator<(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
86 bool operator<=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
87 bool operator>(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
88 bool operator>=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
89 bool operator==(const ConstIterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
90 bool operator!=(const ConstIterator &o) const { return !(*this == o); }
91 bool operator<(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
92 bool operator<=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
93 bool operator>(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
94 bool operator>=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
95 Iterator &operator++() { ++item.i; return *this; }
96 Iterator operator++(int) { Iterator n = *this; ++item.i; return n; }
97 Iterator &operator--() { item.i--; return *this; }
98 Iterator operator--(int) { Iterator n = *this; item.i--; return n; }
99 Iterator &operator+=(qsizetype j) { item.i += j; return *this; }
100 Iterator &operator-=(qsizetype j) { item.i -= j; return *this; }
101 Iterator operator+(qsizetype j) const { return Iterator({ item.d, item.i + j }); }
102 Iterator operator-(qsizetype j) const { return Iterator({ item.d, item.i - j }); }
103 qsizetype operator-(Iterator j) const { return item.i - j.item.i; }
104 };
105
106 class ConstIterator {
107 QCborValueRef item;
108 friend class Iterator;
109 friend class QCborArray;
110 ConstIterator(QCborContainerPrivate *dd, qsizetype ii) : item(dd, ii) {}
111 public:
112 typedef std::random_access_iterator_tag iterator_category;
113 typedef qsizetype difference_type;
114 typedef const QCborValue value_type;
115 typedef const QCborValueRef reference;
116 typedef const QCborValueRef *pointer;
117
118 Q_DECL_CONSTEXPR ConstIterator() = default;
119 Q_DECL_CONSTEXPR ConstIterator(const ConstIterator &) = default;
120 ConstIterator &operator=(const ConstIterator &other)
121 {
122 // rebind the reference
123 item.d = other.item.d;
124 item.i = other.item.i;
125 return *this;
126 }
127
128 const QCborValueRef operator*() const { return item; }
129 const QCborValueRef *operator->() const { return &item; }
130 const QCborValueRef operator[](qsizetype j) { return { item.d, item.i + j }; }
131
132 bool operator==(const Iterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
133 bool operator!=(const Iterator &o) const { return !(*this == o); }
134 bool operator<(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
135 bool operator<=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
136 bool operator>(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
137 bool operator>=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
138 bool operator==(const ConstIterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
139 bool operator!=(const ConstIterator &o) const { return !(*this == o); }
140 bool operator<(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
141 bool operator<=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
142 bool operator>(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
143 bool operator>=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
144 ConstIterator &operator++() { ++item.i; return *this; }
145 ConstIterator operator++(int) { ConstIterator n = *this; ++item.i; return n; }
146 ConstIterator &operator--() { item.i--; return *this; }
147 ConstIterator operator--(int) { ConstIterator n = *this; item.i--; return n; }
148 ConstIterator &operator+=(qsizetype j) { item.i += j; return *this; }
149 ConstIterator &operator-=(qsizetype j) { item.i -= j; return *this; }
150 ConstIterator operator+(qsizetype j) const { return ConstIterator({ item.d, item.i + j }); }
151 ConstIterator operator-(qsizetype j) const { return ConstIterator({ item.d, item.i - j }); }
152 qsizetype operator-(ConstIterator j) const { return item.i - j.item.i; }
153 };
154
155 typedef qsizetype size_type;
156 typedef QCborValue value_type;
157 typedef value_type *pointer;
158 typedef const value_type *const_pointer;
159 typedef QCborValue &reference;
160 typedef const QCborValue &const_reference;
161 typedef qsizetype difference_type;
162
163 QCborArray() noexcept;
164 QCborArray(const QCborArray &other) noexcept;
165 QCborArray &operator=(const QCborArray &other) noexcept;
166 QCborArray(std::initializer_list<QCborValue> args)
167 : QCborArray()
168 {
169 detach(qsizetype(args.size()));
170 for (const QCborValue &v : args)
171 append(v);
172 }
173 ~QCborArray();
174
175 void swap(QCborArray &other) noexcept
176 {
177 qSwap(d, other.d);
178 }
179
180 QCborValue toCborValue() const { return *this; }
181
182 qsizetype size() const noexcept;
183 bool isEmpty() const { return size() == 0; }
184 void clear();
185
186 QCborValue at(qsizetype i) const;
187 QCborValue first() const { return at(0); }
188 QCborValue last() const { return at(size() - 1); }
189 const QCborValue operator[](qsizetype i) const { return at(i); }
190 QCborValueRef first() { Q_ASSERT(!isEmpty()); return begin()[0]; }
191 QCborValueRef last() { Q_ASSERT(!isEmpty()); return begin()[size() - 1]; }
192 QCborValueRef operator[](qsizetype i)
193 {
194 if (i >= size())
195 insert(i, QCborValue());
196 return begin()[i];
197 }
198
199 void insert(qsizetype i, const QCborValue &value);
200 void insert(qsizetype i, QCborValue &&value);
201 void prepend(const QCborValue &value) { insert(0, value); }
202 void prepend(QCborValue &&value) { insert(0, std::move(value)); }
203 void append(const QCborValue &value) { insert(-1, value); }
204 void append(QCborValue &&value) { insert(-1, std::move(value)); }
205 QCborValue extract(ConstIterator it) { return extract(Iterator{ it.item.d, it.item.i }); }
206 QCborValue extract(Iterator it);
207 void removeAt(qsizetype i);
208 QCborValue takeAt(qsizetype i) { Q_ASSERT(i < size()); return extract(begin() + i); }
209 void removeFirst() { removeAt(0); }
210 void removeLast() { removeAt(size() - 1); }
211 QCborValue takeFirst() { return takeAt(0); }
212 QCborValue takeLast() { return takeAt(size() - 1); }
213
214 bool contains(const QCborValue &value) const;
215
216 int compare(const QCborArray &other) const noexcept Q_DECL_PURE_FUNCTION;
217#if 0 && QT_HAS_INCLUDE(<compare>)
218 std::strong_ordering operator<=>(const QCborArray &other) const
219 {
220 int c = compare(other);
221 if (c > 0) return std::strong_ordering::greater;
222 if (c == 0) return std::strong_ordering::equivalent;
223 return std::strong_ordering::less;
224 }
225#else
226 bool operator==(const QCborArray &other) const noexcept
227 { return compare(other) == 0; }
228 bool operator!=(const QCborArray &other) const noexcept
229 { return !(*this == other); }
230 bool operator<(const QCborArray &other) const
231 { return compare(other) < 0; }
232#endif
233
234 typedef Iterator iterator;
235 typedef ConstIterator const_iterator;
236 iterator begin() { detach(); return iterator{d.data(), 0}; }
237 const_iterator constBegin() const { return const_iterator{d.data(), 0}; }
238 const_iterator begin() const { return constBegin(); }
239 const_iterator cbegin() const { return constBegin(); }
240 iterator end() { detach(); return iterator{d.data(), size()}; }
241 const_iterator constEnd() const { return const_iterator{d.data(), size()}; }
242 const_iterator end() const { return constEnd(); }
243 const_iterator cend() const { return constEnd(); }
244 iterator insert(iterator before, const QCborValue &value)
245 { insert(before.item.i, value); return iterator{d.data(), before.item.i}; }
246 iterator insert(const_iterator before, const QCborValue &value)
247 { insert(before.item.i, value); return iterator{d.data(), before.item.i}; }
248 iterator erase(iterator it) { removeAt(it.item.i); return iterator{d.data(), it.item.i}; }
249 iterator erase(const_iterator it) { removeAt(it.item.i); return iterator{d.data(), it.item.i}; }
250
251 void push_back(const QCborValue &t) { append(t); }
252 void push_front(const QCborValue &t) { prepend(t); }
253 void pop_front() { removeFirst(); }
254 void pop_back() { removeLast(); }
255 bool empty() const { return isEmpty(); }
256
257 // convenience
258 QCborArray operator+(const QCborValue &v) const
259 { QCborArray n = *this; n += v; return n; }
260 QCborArray &operator+=(const QCborValue &v)
261 { append(v); return *this; }
262 QCborArray &operator<<(const QCborValue &v)
263 { append(v); return *this; }
264
265 static QCborArray fromStringList(const QStringList &list);
266 static QCborArray fromVariantList(const QVariantList &list);
267 static QCborArray fromJsonArray(const QJsonArray &array);
268 QVariantList toVariantList() const;
269 QJsonArray toJsonArray() const;
270
271private:
272 void detach(qsizetype reserve = 0);
273
274 friend QCborValue;
275 friend QCborValueRef;
276 explicit QCborArray(QCborContainerPrivate &dd) noexcept;
277 QExplicitlySharedDataPointer<QCborContainerPrivate> d;
278};
279
280Q_DECLARE_SHARED(QCborArray)
281
282inline QCborValue::QCborValue(QCborArray &&a)
283 : n(-1), container(a.d.take()), t(Array)
284{
285}
286
287inline QCborArray QCborValueRef::toArray() const
288{
289 return concrete().toArray();
290}
291
292inline QCborArray QCborValueRef::toArray(const QCborArray &a) const
293{
294 return concrete().toArray(a);
295}
296
297Q_CORE_EXPORT uint qHash(const QCborArray &array, uint seed = 0);
298
299#if !defined(QT_NO_DEBUG_STREAM)
300Q_CORE_EXPORT QDebug operator<<(QDebug, const QCborArray &a);
301#endif
302
303#ifndef QT_NO_DATASTREAM
304Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QCborArray &);
305Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QCborArray &);
306#endif
307
308QT_END_NAMESPACE
309
310#endif // QCBORARRAY_H
311