Long live DOM API for CBOR!

This is very similar to QJsonDocument, but there's no QCborDocument.
QCborValue is that.

[ChangeLog][QtCore] Added QCborValue, QCborArray and QCborMap, classes
that permit DOM-like access to CBOR data. The API is similar to
QJsonValue, QJsonArray and QJsonObject, respectively.

Change-Id: I9741f017961b410c910dfffd14ffca50dd8ef3ba
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
bb10
Thiago Macieira 2017-12-12 23:34:08 -08:00 committed by Liang Qi
parent 95e6ab3329
commit 92e472302a
12 changed files with 8141 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,272 @@
/****************************************************************************
**
** Copyright (C) 2018 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QCBORARRAY_H
#define QCBORARRAY_H
#include <QtCore/qcborvalue.h>
#include <initializer_list>
QT_BEGIN_NAMESPACE
class QJsonArray;
class QCborContainerPrivate;
class Q_CORE_EXPORT QCborArray
{
public:
class ConstIterator;
class Iterator {
mutable QCborValueRef item;
friend class ConstIterator;
friend class QCborArray;
Iterator(QCborContainerPrivate *dd, qsizetype ii) : item(dd, ii) {}
public:
typedef std::random_access_iterator_tag iterator_category;
typedef qsizetype difference_type;
typedef QCborValue value_type;
typedef QCborValueRef reference;
typedef QCborValueRef *pointer;
Q_DECL_CONSTEXPR Iterator() = default;
Q_DECL_CONSTEXPR Iterator(const Iterator &) = default;
Iterator &operator=(const Iterator &other)
{
// rebind the reference
item.d = other.item.d;
item.i = other.item.i;
return *this;
}
QCborValueRef operator*() const { return item; }
QCborValueRef *operator->() const { return &item; }
QCborValueRef operator[](qsizetype j) { return { item.d, item.i + j }; }
bool operator==(const Iterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
bool operator!=(const Iterator &o) const { return !(*this == o); }
bool operator<(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
bool operator<=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
bool operator>(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
bool operator>=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
bool operator==(const ConstIterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
bool operator!=(const ConstIterator &o) const { return !(*this == o); }
bool operator<(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
bool operator<=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
bool operator>(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
bool operator>=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
Iterator &operator++() { ++item.i; return *this; }
Iterator operator++(int) { Iterator n = *this; ++item.i; return n; }
Iterator &operator--() { item.i--; return *this; }
Iterator operator--(int) { Iterator n = *this; item.i--; return n; }
Iterator &operator+=(qsizetype j) { item.i += j; return *this; }
Iterator &operator-=(qsizetype j) { item.i -= j; return *this; }
Iterator operator+(qsizetype j) const { return Iterator({ item.d, item.i + j }); }
Iterator operator-(qsizetype j) const { return Iterator({ item.d, item.i - j }); }
qsizetype operator-(Iterator j) const { return item.i - j.item.i; }
};
class ConstIterator {
QCborValueRef item;
friend class Iterator;
friend class QCborArray;
ConstIterator(QCborContainerPrivate *dd, qsizetype ii) : item(dd, ii) {}
public:
typedef std::random_access_iterator_tag iterator_category;
typedef qsizetype difference_type;
typedef const QCborValue value_type;
typedef const QCborValueRef reference;
typedef const QCborValueRef *pointer;
Q_DECL_CONSTEXPR ConstIterator() = default;
Q_DECL_CONSTEXPR ConstIterator(const ConstIterator &) = default;
ConstIterator &operator=(const ConstIterator &other)
{
// rebind the reference
item.d = other.item.d;
item.i = other.item.i;
return *this;
}
const QCborValueRef operator*() const { return item; }
const QCborValueRef *operator->() const { return &item; }
const QCborValueRef operator[](qsizetype j) { return { item.d, item.i + j }; }
bool operator==(const Iterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
bool operator!=(const Iterator &o) const { return !(*this == o); }
bool operator<(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
bool operator<=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
bool operator>(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
bool operator>=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
bool operator==(const ConstIterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
bool operator!=(const ConstIterator &o) const { return !(*this == o); }
bool operator<(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
bool operator<=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
bool operator>(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
bool operator>=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
ConstIterator &operator++() { ++item.i; return *this; }
ConstIterator operator++(int) { ConstIterator n = *this; ++item.i; return n; }
ConstIterator &operator--() { item.i--; return *this; }
ConstIterator operator--(int) { ConstIterator n = *this; item.i--; return n; }
ConstIterator &operator+=(qsizetype j) { item.i += j; return *this; }
ConstIterator &operator-=(qsizetype j) { item.i -= j; return *this; }
ConstIterator operator+(qsizetype j) const { return ConstIterator({ item.d, item.i + j }); }
ConstIterator operator-(qsizetype j) const { return ConstIterator({ item.d, item.i - j }); }
qsizetype operator-(ConstIterator j) const { return item.i - j.item.i; }
};
typedef qsizetype size_type;
typedef QCborValue value_type;
typedef value_type *pointer;
typedef const value_type *const_pointer;
typedef QCborValue &reference;
typedef const QCborValue &const_reference;
typedef qsizetype difference_type;
QCborArray() Q_DECL_NOTHROW;
QCborArray(const QCborArray &other) Q_DECL_NOTHROW;
QCborArray &operator=(const QCborArray &other) Q_DECL_NOTHROW;
QCborArray(std::initializer_list<QCborValue> args)
: QCborArray()
{
detach(qsizetype(args.size()));
for (const QCborValue &v : args)
append(v);
}
~QCborArray();
void swap(QCborArray &other) Q_DECL_NOTHROW
{
qSwap(d, other.d);
}
QCborValue toCborValue() const { return *this; }
qsizetype size() const Q_DECL_NOTHROW;
bool isEmpty() const { return size() == 0; }
QCborValue at(qsizetype i) const;
QCborValue first() const { return at(0); }
QCborValue last() const { return at(size() - 1); }
QCborValue operator[](qsizetype i) const { return at(i); }
QCborValueRef first() { Q_ASSERT(!isEmpty()); return begin()[0]; }
QCborValueRef last() { Q_ASSERT(!isEmpty()); return begin()[size() - 1]; }
QCborValueRef operator[](qsizetype i) { Q_ASSERT(i < size()); return begin()[i]; }
void insert(qsizetype i, const QCborValue &value);
void prepend(const QCborValue &value) { insert(0, value); }
void append(const QCborValue &value) { insert(-1, value); }
void removeAt(qsizetype i);
QCborValue takeAt(qsizetype i) { QCborValue v = at(i); removeAt(i); return v; }
void removeFirst() { removeAt(0); }
void removeLast() { removeAt(size() - 1); }
QCborValue takeFirst() { return takeAt(0); }
QCborValue takeLast() { return takeAt(size() - 1); }
bool contains(const QCborValue &value) const;
int compare(const QCborArray &other) const Q_DECL_NOTHROW Q_DECL_PURE_FUNCTION;
#if QT_HAS_INCLUDE(<compare>)
std::strong_ordering operator<=>(const QCborArray &other) const
{
int c = compare(other);
if (c > 0) return std::strong_ordering::greater;
if (c == 0) return std::strong_ordering::equivalent;
return std::strong_ordering::less;
}
#else
bool operator==(const QCborArray &other) const Q_DECL_NOTHROW
{ return compare(other) == 0; }
bool operator!=(const QCborArray &other) const Q_DECL_NOTHROW
{ return !(*this == other); }
bool operator<(const QCborArray &other) const
{ return compare(other) < 0; }
#endif
typedef Iterator iterator;
typedef ConstIterator const_iterator;
iterator begin() { detach(); return iterator{d.data(), 0}; }
const_iterator constBegin() const { return const_iterator{d.data(), 0}; }
const_iterator begin() const { return constBegin(); }
const_iterator cbegin() const { return constBegin(); }
iterator end() { detach(); return iterator{d.data(), size()}; }
const_iterator constEnd() const { return const_iterator{d.data(), size()}; }
const_iterator end() const { return constEnd(); }
const_iterator cend() const { return constEnd(); }
iterator insert(iterator before, const QCborValue &value)
{ insert(before.item.i, value); return iterator{d.data(), before.item.i}; }
iterator erase(iterator it) { removeAt(it.item.i); return iterator{d.data(), it.item.i}; }
void push_back(const QCborValue &t) { append(t); }
void push_front(const QCborValue &t) { prepend(t); }
void pop_front() { removeFirst(); }
void pop_back() { removeLast(); }
bool empty() const { return isEmpty(); }
// convenience
QCborArray operator+(const QCborValue &v) const
{ QCborArray n = *this; n += v; return n; }
QCborArray &operator+=(const QCborValue &v)
{ append(v); return *this; }
QCborArray &operator<<(const QCborValue &v)
{ append(v); return *this; }
private:
void detach(qsizetype reserve = 0);
friend QCborValue;
explicit QCborArray(QCborContainerPrivate &dd) Q_DECL_NOTHROW;
QExplicitlySharedDataPointer<QCborContainerPrivate> d;
};
Q_DECLARE_SHARED(QCborArray)
inline QCborArray QCborValueRef::toArray() const
{
return concrete().toArray();
}
inline QCborArray QCborValueRef::toArray(const QCborArray &a) const
{
return concrete().toArray(a);
}
QT_END_NAMESPACE
#endif // QCBORARRAY_H

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,320 @@
/****************************************************************************
**
** Copyright (C) 2018 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QCBORMAP_H
#define QCBORMAP_H
#include <QtCore/qcborvalue.h>
#include <QtCore/qpair.h>
#include <initializer_list>
QT_BEGIN_NAMESPACE
template <class Key, class T> class QMap;
typedef QMap<QString, QVariant> QVariantMap;
template <class Key, class T> class QHash;
typedef QHash<QString, QVariant> QVariantHash;
class QJsonObject;
class QCborContainerPrivate;
class Q_CORE_EXPORT QCborMap
{
public:
typedef QPair<QCborValue, QCborValue> value_type;
typedef QCborValue key_type;
typedef QCborValue mapped_type;
typedef qsizetype size_type;
class ConstIterator;
class Iterator {
mutable QCborValueRef item; // points to the value
friend class ConstIterator;
friend class QCborMap;
Iterator(QCborContainerPrivate *dd, qsizetype ii) : item(dd, ii) {}
public:
typedef std::random_access_iterator_tag iterator_category;
typedef qsizetype difference_type;
typedef QPair<const QCborValueRef, QCborValueRef> value_type;
typedef QPair<const QCborValueRef, QCborValueRef> reference;
typedef QPair<const QCborValueRef, QCborValueRef> pointer;
Q_DECL_CONSTEXPR Iterator() = default;
Q_DECL_CONSTEXPR Iterator(const Iterator &) = default;
Iterator &operator=(const Iterator &other)
{
// rebind the reference
item.d = other.item.d;
item.i = other.item.i;
return *this;
}
value_type operator*() const { return { {item.d, item.i - 1}, item }; }
QCborValueRef *operator->() const { return &item; }
QCborValue key() const { return QCborValueRef(item.d, item.i - 1); }
QCborValueRef value() const { return item; }
bool operator==(const Iterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
bool operator!=(const Iterator &o) const { return !(*this == o); }
bool operator<(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
bool operator<=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
bool operator>(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
bool operator>=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
bool operator==(const ConstIterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
bool operator!=(const ConstIterator &o) const { return !(*this == o); }
bool operator<(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
bool operator<=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
bool operator>(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
bool operator>=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
Iterator &operator++() { item.i += 2; return *this; }
Iterator operator++(int) { Iterator n = *this; item.i += 2; return n; }
Iterator &operator--() { item.i -= 2; return *this; }
Iterator operator--(int) { Iterator n = *this; item.i -= 2; return n; }
Iterator &operator+=(qsizetype j) { item.i += 2 * j; return *this; }
Iterator &operator-=(qsizetype j) { item.i -= 2 * j; return *this; }
Iterator operator+(qsizetype j) const { return Iterator({ item.d, item.i + 2 * j }); }
Iterator operator-(qsizetype j) const { return Iterator({ item.d, item.i - 2 * j }); }
qsizetype operator-(Iterator j) const { return (item.i - j.item.i) / 2; }
};
class ConstIterator {
QCborValueRef item; // points to the value
friend class Iterator;
friend class QCborMap;
ConstIterator(QCborContainerPrivate *dd, qsizetype ii) : item(dd, ii) {}
public:
typedef std::random_access_iterator_tag iterator_category;
typedef qsizetype difference_type;
typedef QPair<const QCborValueRef, const QCborValueRef> value_type;
typedef QPair<const QCborValueRef, const QCborValueRef> reference;
typedef QPair<const QCborValueRef, const QCborValueRef> pointer;
Q_DECL_CONSTEXPR ConstIterator() = default;
Q_DECL_CONSTEXPR ConstIterator(const ConstIterator &) = default;
ConstIterator &operator=(const ConstIterator &other)
{
// rebind the reference
item.d = other.item.d;
item.i = other.item.i;
return *this;
}
value_type operator*() const { return { {item.d, item.i - 1}, item }; }
const QCborValueRef *operator->() const { return &item; }
QCborValue key() const { return QCborValueRef(item.d, item.i - 1); }
QCborValueRef value() const { return item; }
bool operator==(const Iterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
bool operator!=(const Iterator &o) const { return !(*this == o); }
bool operator<(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
bool operator<=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
bool operator>(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
bool operator>=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
bool operator==(const ConstIterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
bool operator!=(const ConstIterator &o) const { return !(*this == o); }
bool operator<(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
bool operator<=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
bool operator>(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
bool operator>=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
ConstIterator &operator++() { item.i += 2; return *this; }
ConstIterator operator++(int) { ConstIterator n = *this; item.i += 2; return n; }
ConstIterator &operator--() { item.i -= 2; return *this; }
ConstIterator operator--(int) { ConstIterator n = *this; item.i -= 2; return n; }
ConstIterator &operator+=(qsizetype j) { item.i += 2 * j; return *this; }
ConstIterator &operator-=(qsizetype j) { item.i -= 2 * j; return *this; }
ConstIterator operator+(qsizetype j) const { return ConstIterator({ item.d, item.i + 2 * j }); }
ConstIterator operator-(qsizetype j) const { return ConstIterator({ item.d, item.i - 2 * j }); }
qsizetype operator-(ConstIterator j) const { return (item.i - j.item.i) / 2; }
};
QCborMap() Q_DECL_NOTHROW;
QCborMap(const QCborMap &other) Q_DECL_NOTHROW;
QCborMap &operator=(const QCborMap &other) Q_DECL_NOTHROW;
QCborMap(std::initializer_list<value_type> args)
: QCborMap()
{
detach(args.size());
for (auto pair : args)
insert(pair.first, pair.second);
}
~QCborMap();
void swap(QCborMap &other) Q_DECL_NOTHROW
{
qSwap(d, other.d);
}
QCborValue toCborValue() const { return *this; }
qsizetype size() const Q_DECL_NOTHROW Q_DECL_PURE_FUNCTION;
bool isEmpty() const { return size() == 0; }
QVector<QCborValue> keys() const;
QCborValue value(qint64 key) const
{ const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
QCborValue value(QLatin1String key) const
{ const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
QCborValue value(const QString & key) const
{ const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
QCborValue value(const QCborValue &key) const
{ const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
QCborValue operator[](qint64 key) const
{ const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
QCborValue operator[](QLatin1String key) const
{ const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
QCborValue operator[](const QString & key) const
{ const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
QCborValue operator[](const QCborValue &key) const
{ const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
QCborValueRef operator[](qint64 key);
QCborValueRef operator[](QLatin1String key);
QCborValueRef operator[](const QString & key);
QCborValueRef operator[](const QCborValue &key);
void remove(qint64 key)
{ iterator it = find(key); if (it != end()) erase(it); }
void remove(QLatin1String key)
{ iterator it = find(key); if (it != end()) erase(it); }
void remove(const QString & key)
{ iterator it = find(key); if (it != end()) erase(it); }
void remove(const QCborValue &key)
{ iterator it = find(key); if (it != end()) erase(it); }
bool contains(qint64 key) const
{ const_iterator it = find(key); return it != end(); }
bool contains(QLatin1String key) const
{ const_iterator it = find(key); return it != end(); }
bool contains(const QString & key) const
{ const_iterator it = find(key); return it != end(); }
bool contains(const QCborValue &key) const
{ const_iterator it = find(key); return it != end(); }
int compare(const QCborMap &other) const Q_DECL_NOTHROW Q_DECL_PURE_FUNCTION;
#if QT_HAS_INCLUDE(<compare>)
std::strong_ordering operator<=>(const QCborMap &other) const
{
int c = compare(other);
if (c > 0) return std::strong_ordering::greater;
if (c == 0) return std::strong_ordering::equivalent;
return std::strong_ordering::less;
}
#else
bool operator==(const QCborMap &other) const Q_DECL_NOTHROW
{ return compare(other) == 0; }
bool operator!=(const QCborMap &other) const Q_DECL_NOTHROW
{ return !(*this == other); }
bool operator<(const QCborMap &other) const
{ return compare(other) < 0; }
#endif
typedef Iterator iterator;
typedef ConstIterator const_iterator;
iterator begin() { detach(); return iterator{d.data(), 1}; }
const_iterator constBegin() const { return const_iterator{d.data(), 1}; }
const_iterator begin() const { return constBegin(); }
const_iterator cbegin() const { return constBegin(); }
iterator end() { detach(); return iterator{d.data(), 2 * size() + 1}; }
const_iterator constEnd() const { return const_iterator{d.data(), 2 * size() + 1}; }
const_iterator end() const { return constEnd(); }
const_iterator cend() const { return constEnd(); }
iterator erase(iterator it);
iterator erase(const_iterator it) { return erase(iterator{ it.item.d, it.item.i }); }
bool empty() const { return isEmpty(); }
iterator find(qint64 key);
iterator find(QLatin1String key);
iterator find(const QString & key);
iterator find(const QCborValue &key);
const_iterator constFind(qint64 key) const;
const_iterator constFind(QLatin1String key) const;
const_iterator constFind(const QString & key) const;
const_iterator constFind(const QCborValue &key) const;
const_iterator find(qint64 key) const { return constFind(key); }
const_iterator find(QLatin1String key) const { return constFind(key); }
const_iterator find(const QString & key) const { return constFind(key); }
const_iterator find(const QCborValue &key) const { return constFind(key); }
iterator insert(qint64 key, const QCborValue &value)
{
QCborValueRef v = operator[](key); // detaches
v = value;
return { d.data(), v.i };
}
iterator insert(QLatin1String key, const QCborValue &value)
{
QCborValueRef v = operator[](key); // detaches
v = value;
return { d.data(), v.i };
}
iterator insert(const QString &key, const QCborValue &value)
{
QCborValueRef v = operator[](key); // detaches
v = value;
return { d.data(), v.i };
}
iterator insert(const QCborValue &key, const QCborValue &value)
{
QCborValueRef v = operator[](key); // detaches
v = value;
return { d.data(), v.i };
}
iterator insert(value_type v) { return insert(v.first, v.second); }
private:
void detach(qsizetype reserve = 0);
friend QCborValue;
explicit QCborMap(QCborContainerPrivate &dd) Q_DECL_NOTHROW;
QExplicitlySharedDataPointer<QCborContainerPrivate> d;
};
Q_DECLARE_SHARED(QCborMap)
inline QCborMap QCborValueRef::toMap() const
{
return concrete().toMap();
}
inline QCborMap QCborValueRef::toMap(const QCborMap &m) const
{
return concrete().toMap(m);
}
QT_END_NAMESPACE
#endif // QCBORMAP_H

View File

@ -244,6 +244,7 @@ private:
}
friend QCborStreamReaderPrivate;
friend class QCborContainerPrivate;
quint64 value64;
QScopedPointer<QCborStreamReaderPrivate> d;
quint8 type_;

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,427 @@
/****************************************************************************
**
** Copyright (C) 2018 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QCBORVALUE_H
#define QCBORVALUE_H
#include <QtCore/qbytearray.h>
#include <QtCore/qdatetime.h>
#include <QtCore/qcborcommon.h>
#include <QtCore/qregularexpression.h>
#include <QtCore/qstring.h>
#include <QtCore/qstringview.h>
#include <QtCore/qurl.h>
#include <QtCore/quuid.h>
#include <QtCore/qvariant.h>
#include <QtCore/qvector.h>
#if QT_HAS_INCLUDE(<compare>)
# include <compare>
#endif
QT_BEGIN_NAMESPACE
class QCborArray;
class QCborMap;
class QCborStreamReader;
class QCborStreamWriter;
struct QCborParserError
{
qint64 offset = 0;
QCborError error = {};
QString errorString() const { return error.toString(); }
};
class QCborContainerPrivate;
class Q_CORE_EXPORT QCborValue
{
Q_GADGET
public:
enum EncodingOption {
SortKeysInMaps = 0x01,
UseFloat = 0x02,
UseFloat16 = UseFloat | 0x04,
UseIntegers = 0x08,
NoTransformation = 0
};
Q_DECLARE_FLAGS(EncodingOptions, EncodingOption)
enum DiagnosticNotationOption {
Compact = 0x00,
LineWrapped = 0x01,
ExtendedFormat = 0x02
};
Q_DECLARE_FLAGS(DiagnosticNotationOptions, DiagnosticNotationOption)
// different from QCborStreamReader::Type because we have more types
enum Type : int {
Integer = 0x00,
ByteArray = 0x40,
String = 0x60,
Array = 0x80,
Map = 0xa0,
Tag = 0xc0,
// range 0x100 - 0x1ff for Simple Types
SimpleType = 0x100,
False = SimpleType + int(QCborSimpleType::False),
True = SimpleType + int(QCborSimpleType::True),
Null = SimpleType + int(QCborSimpleType::Null),
Undefined = SimpleType + int(QCborSimpleType::Undefined),
Double = 0x202,
// extended (tagged) types
DateTime = 0x10000,
Url = 0x10020,
RegularExpression = 0x10023,
Uuid = 0x10025,
Invalid = -1
};
Q_ENUM(Type)
QCborValue() {}
QCborValue(Type t_) : t(t_) {}
QCborValue(std::nullptr_t) : t(Null) {}
QCborValue(bool b_) : t(b_ ? True : False) {}
#ifndef Q_QDOC
QCborValue(int i) : QCborValue(qint64(i)) {}
QCborValue(unsigned u) : QCborValue(qint64(u)) {}
#endif
QCborValue(qint64 i) : n(i), t(Integer) {}
QCborValue(double v) : t(Double) { memcpy(&n, &v, sizeof(n)); }
QCborValue(QCborSimpleType st) : t(type_helper(st)) {}
QCborValue(const QByteArray &ba);
QCborValue(const QString &s);
QCborValue(QLatin1String s);
#ifndef QT_NO_CAST_FROM_ASCII
QT_ASCII_CAST_WARN QCborValue(const char *s) : QCborValue(QString::fromUtf8(s)) {}
#endif
QCborValue(const QCborArray &a);
QCborValue(const QCborMap &m);
QCborValue(QCborTag tag, const QCborValue &taggedValue = QCborValue());
QCborValue(QCborKnownTags tag, const QCborValue &taggedValue = QCborValue())
: QCborValue(QCborTag(tag), taggedValue)
{}
explicit QCborValue(const QDateTime &dt);
explicit QCborValue(const QUrl &url);
explicit QCborValue(const QRegularExpression &rx);
explicit QCborValue(const QUuid &uuid);
~QCborValue() { if (container) dispose(); }
// make sure const char* doesn't go call the bool constructor
QCborValue(const void *) = delete;
QCborValue(const QCborValue &other);
QCborValue(QCborValue &&other) Q_DECL_NOTHROW
: n(other.n), container(other.container), t(other.t)
{
other.t = Undefined;
other.container = nullptr;
}
QCborValue &operator=(const QCborValue &other);
QCborValue &operator=(QCborValue &&other) Q_DECL_NOTHROW
{
QCborValue tmp;
qSwap(*this, tmp);
qSwap(other, *this);
return *this;
}
void swap(QCborValue &other) Q_DECL_NOTHROW
{
qSwap(n, other.n);
qSwap(container, other.container);
qSwap(t, other.t);
}
Type type() const { return t; }
bool isInteger() const { return type() == Integer; }
bool isByteArray() const { return type() == ByteArray; }
bool isString() const { return type() == String; }
bool isArray() const { return type() == Array; }
bool isMap() const { return type() == Map; }
bool isTag() const { return type() == Tag; }
bool isFalse() const { return type() == False; }
bool isTrue() const { return type() == True; }
bool isBool() const { return isFalse() || isTrue(); }
bool isNull() const { return type() == Null; }
bool isUndefined() const { return type() == Undefined; }
bool isDouble() const { return type() == Double; }
bool isDateTime() const { return type() == DateTime; }
bool isUrl() const { return type() == Url; }
bool isRegularExpression() const { return type() == RegularExpression; }
bool isUuid() const { return type() == Uuid; }
bool isInvalid() const { return type() == Invalid; }
bool isContainer() const { return isMap() || isArray(); }
bool isSimpleType() const
{
return int(type()) >> 8 == int(SimpleType) >> 8;
}
bool isSimpleType(QCborSimpleType st) const
{
return type() == type_helper(st);
}
QCborSimpleType toSimpleType(QCborSimpleType defaultValue = QCborSimpleType::Undefined) const
{
return isSimpleType() ? QCborSimpleType(type() & 0xff) : defaultValue;
}
qint64 toInteger(qint64 defaultValue = 0) const
{ return isInteger() ? value_helper() : isDouble() ? qint64(fp_helper()) : defaultValue; }
bool toBool(bool defaultValue = false) const
{ return isBool() ? isTrue() : defaultValue; }
double toDouble(double defaultValue = 0) const
{ return isDouble() ? fp_helper() : isInteger() ? double(value_helper()) : defaultValue; }
QCborTag tag(QCborTag defaultValue = QCborTag(-1)) const;
QCborValue taggedValue(const QCborValue &defaultValue = QCborValue()) const;
QCborValue reinterpretAsTag() const;
QByteArray toByteArray(const QByteArray &defaultValue = {}) const;
QString toString(const QString &defaultValue = {}) const;
QDateTime toDateTime(const QDateTime &defaultValue = {}) const;
QUrl toUrl(const QUrl &defaultValue = {}) const;
QRegularExpression toRegularExpression(const QRegularExpression &defaultValue = {}) const;
QUuid toUuid(const QUuid &defaultValue = {}) const;
#ifdef Q_QDOC
QCborArray toArray(const QCborArray &a = {}) const;
QCborMap toMap(const QCborMap &m = {}) const;
#else
// only forward-declared, need split functions
QCborArray toArray() const;
QCborArray toArray(const QCborArray &defaultValue) const;
QCborMap toMap() const;
QCborMap toMap(const QCborMap &defaultValue) const;
#endif
const QCborValue operator[](const QString &key) const;
const QCborValue operator[](QLatin1String key) const;
const QCborValue operator[](qint64 key) const;
int compare(const QCborValue &other) const;
#if QT_HAS_INCLUDE(<compare>)
std::strong_ordering operator<=>(const QCborValue &other) const
{
int c = compare(other);
if (c > 0) return std::partial_ordering::greater;
if (c == 0) return std::partial_ordering::equivalent;
return std::partial_ordering::less;
}
#else
bool operator==(const QCborValue &other) const Q_DECL_NOTHROW
{ return compare(other) == 0; }
bool operator!=(const QCborValue &other) const Q_DECL_NOTHROW
{ return !(*this == other); }
bool operator<(const QCborValue &other) const
{ return compare(other) < 0; }
#endif
static QCborValue fromCbor(QCborStreamReader &reader);
static QCborValue fromCbor(const QByteArray &ba, QCborParserError *error = nullptr);
static QCborValue fromCbor(const char *data, qsizetype len, QCborParserError *error = nullptr)
{ return fromCbor(QByteArray(data, int(len)), error); }
static QCborValue fromCbor(const quint8 *data, qsizetype len, QCborParserError *error = nullptr)
{ return fromCbor(QByteArray(reinterpret_cast<const char *>(data), int(len)), error); }
QByteArray toCbor(EncodingOptions opt = NoTransformation);
void toCbor(QCborStreamWriter &writer, EncodingOptions opt = NoTransformation);
QString toDiagnosticNotation(DiagnosticNotationOptions opts = Compact) const;
private:
friend class QCborValueRef;
friend class QCborContainerPrivate;
qint64 n = 0;
QCborContainerPrivate *container = nullptr;
Type t = Undefined;
void dispose();
qint64 value_helper() const
{
return n;
}
double fp_helper() const
{
Q_STATIC_ASSERT(sizeof(double) == sizeof(n));
double d;
memcpy(&d, &n, sizeof(d));
return d;
}
Q_DECL_CONSTEXPR static Type type_helper(QCborSimpleType st)
{
return Type(quint8(st) | SimpleType);
}
};
Q_DECLARE_SHARED(QCborValue)
class Q_CORE_EXPORT QCborValueRef
{
public:
operator QCborValue() const { return concrete(); }
QCborValueRef &operator=(const QCborValue &other);
QCborValueRef &operator=(const QCborValueRef &other);
QCborValue::Type type() const { return concreteType(); }
bool isInteger() const { return type() == QCborValue::Integer; }
bool isByteArray() const { return type() == QCborValue::ByteArray; }
bool isString() const { return type() == QCborValue::String; }
bool isArray() const { return type() == QCborValue::Array; }
bool isMap() const { return type() == QCborValue::Map; }
bool isTag() const { return type() == QCborValue::Tag; }
bool isFalse() const { return type() == QCborValue::False; }
bool isTrue() const { return type() == QCborValue::True; }
bool isBool() const { return isFalse() || isTrue(); }
bool isNull() const { return type() == QCborValue::Null; }
bool isUndefined() const { return type() == QCborValue::Undefined; }
bool isDouble() const { return type() == QCborValue::Double; }
bool isDateTime() const { return type() == QCborValue::DateTime; }
bool isUrl() const { return type() == QCborValue::Url; }
bool isRegularExpression() const { return type() == QCborValue::RegularExpression; }
bool isUuid() const { return type() == QCborValue::Uuid; }
bool isInvalid() const { return type() == QCborValue::Invalid; }
bool isContainer() const { return isMap() || isArray(); }
bool isSimpleType() const
{
return type() >= QCborValue::SimpleType && type() < QCborValue::SimpleType + 0x100;
}
bool isSimpleType(QCborSimpleType st) const
{
return type() == QCborValue::type_helper(st);
}
QCborTag tag(QCborTag tag = QCborTag(-1)) const
{ return concrete().tag(tag); }
QCborValue taggedValue(const QCborValue &defaultValue = QCborValue()) const
{ return concrete().taggedValue(defaultValue); }
qint64 toInteger(qint64 defaultValue = 0) const
{ return concrete().toInteger(defaultValue); }
bool toBool(bool defaultValue = false) const
{ return concrete().toBool(defaultValue); }
double toDouble(double defaultValue = 0) const
{ return concrete().toDouble(defaultValue); }
QByteArray toByteArray(const QByteArray &defaultValue = {}) const
{ return concrete().toByteArray(defaultValue); }
QString toString(const QString &defaultValue = {}) const
{ return concrete().toString(defaultValue); }
QDateTime toDateTime(const QDateTime &defaultValue = {}) const
{ return concrete().toDateTime(defaultValue); }
QUrl toUrl(const QUrl &defaultValue = {}) const
{ return concrete().toUrl(defaultValue); }
QRegularExpression toRegularExpression(const QRegularExpression &defaultValue = {}) const
{ return concrete().toRegularExpression(defaultValue); }
QUuid toUuid(const QUuid &defaultValue = {}) const
{ return concrete().toUuid(defaultValue); }
#ifdef Q_QDOC
QCborArray toArray(const QCborArray &a = {}) const;
QCborMap toMap(const QCborMap &m = {}) const;
#else
// only forward-declared, need split functions. Implemented in qcbor{array,map}.h
QCborArray toArray() const;
QCborArray toArray(const QCborArray &a) const;
QCborMap toMap() const;
QCborMap toMap(const QCborMap &m) const;
#endif
int compare(const QCborValue &other) const
{ return concrete().compare(other); }
#if QT_HAS_INCLUDE(<compare>)
std::strong_ordering operator<=>(const QCborValue &other) const
{
int c = compare(other);
if (c > 0) return std::strong_ordering::greater;
if (c == 0) return std::strong_ordering::equivalent;
return std::strong_ordering::less;
}
#else
bool operator==(const QCborValue &other) const
{ return compare(other) == 0; }
bool operator!=(const QCborValue &other) const
{ return !(*this == other); }
bool operator<(const QCborValue &other) const
{ return compare(other) < 0; }
#endif
QByteArray toCbor(QCborValue::EncodingOptions opt = QCborValue::NoTransformation)
{ return concrete().toCbor(opt); }
void toCbor(QCborStreamWriter &writer, QCborValue::EncodingOptions opt = QCborValue::NoTransformation);
QString toDiagnosticNotation(QCborValue::DiagnosticNotationOptions opt = QCborValue::Compact)
{ return concrete().toDiagnosticNotation(opt); }
private:
friend class QCborArray;
friend class QCborMap;
friend class QCborContainerPrivate;
friend class QCborValueRefPtr;
// static so we can pass this by value
static QCborValue concrete(QCborValueRef that) Q_DECL_NOTHROW;
QCborValue concrete() const Q_DECL_NOTHROW { return concrete(*this); }
static QCborValue::Type concreteType(QCborValueRef self) Q_DECL_NOTHROW Q_DECL_PURE_FUNCTION;
QCborValue::Type concreteType() const Q_DECL_NOTHROW { return concreteType(*this); }
// this will actually be invalid...
Q_DECL_CONSTEXPR QCborValueRef() : d(nullptr), i(0) {}
QCborValueRef(QCborContainerPrivate *dd, qsizetype ii)
: d(dd), i(ii)
{}
QCborContainerPrivate *d;
qsizetype i;
};
QT_END_NAMESPACE
#endif // QCBORVALUE_H

View File

@ -0,0 +1,366 @@
/****************************************************************************
**
** Copyright (C) 2018 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QCBORVALUE_P_H
#define QCBORVALUE_P_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API.
// This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#include "qcborvalue.h"
#include <private/qglobal_p.h>
#include <private/qutfcodec_p.h>
QT_BEGIN_NAMESPACE
namespace QtCbor {
struct Undefined {};
struct Element
{
enum ValueFlag : quint32 {
IsContainer = 0x0001,
HasByteData = 0x0002,
StringIsUtf16 = 0x0004,
StringIsAscii = 0x0008
};
Q_DECLARE_FLAGS(ValueFlags, ValueFlag)
union {
qint64 value;
QCborContainerPrivate *container;
};
QCborValue::Type type;
ValueFlags flags = {};
Element(qint64 v = 0, QCborValue::Type t = QCborValue::Undefined, ValueFlags f = {})
: value(v), type(t), flags(f)
{}
Element(QCborContainerPrivate *d, QCborValue::Type t, ValueFlags f = {})
: container(d), type(t), flags(f | IsContainer)
{}
double fpvalue() const
{
double d;
memcpy(&d, &value, sizeof(d));
return d;
}
};
Q_DECLARE_OPERATORS_FOR_FLAGS(Element::ValueFlags)
struct ByteData
{
QByteArray::size_type len;
const char *byte() const { return reinterpret_cast<const char *>(this + 1); }
char *byte() { return reinterpret_cast<char *>(this + 1); }
const QChar *utf16() const { return reinterpret_cast<const QChar *>(this + 1); }
QChar *utf16() { return reinterpret_cast<QChar *>(this + 1); }
QByteArray toByteArray() const { return QByteArray(byte(), len); }
QString toString() const { return QString(utf16(), len / 2); }
QString toUtf8String() const { return QString::fromUtf8(byte(), len); }
QByteArray asByteArrayView() const { return QByteArray::fromRawData(byte(), len); }
QLatin1String asLatin1() const { return QLatin1String(byte(), len); }
QStringView asStringView() const{ return QStringView(utf16(), len / 2); }
QString asQStringRaw() const { return QString::fromRawData(utf16(), len / 2); }
};
Q_STATIC_ASSERT(sizeof(Element) == 16);
Q_STATIC_ASSERT(std::is_pod<ByteData>::value);
} // namespace QtCbor
Q_DECLARE_TYPEINFO(QtCbor::Element, Q_PRIMITIVE_TYPE);
class QCborContainerPrivate : public QSharedData
{
friend class QExplicitlySharedDataPointer<QCborContainerPrivate>;
~QCborContainerPrivate();
public:
QByteArray::size_type usedData = 0;
QByteArray data;
QVector<QtCbor::Element> elements;
void deref() { if (!ref.deref()) delete this; }
void compact(qsizetype reserved);
static QCborContainerPrivate *clone(QCborContainerPrivate *d, qsizetype reserved = -1);
static QCborContainerPrivate *detach(QCborContainerPrivate *d, qsizetype reserved);
qptrdiff addByteData(const char *block, qsizetype len)
{
// This function does not do overflow checking, since the len parameter
// is expected to be trusted. There's another version of this function
// in decodeStringFromCbor(), which checks.
qptrdiff offset = data.size();
// align offset
offset += Q_ALIGNOF(QtCbor::ByteData) - 1;
offset &= ~(Q_ALIGNOF(QtCbor::ByteData) - 1);
qptrdiff increment = qptrdiff(sizeof(QtCbor::ByteData)) + len;
usedData += increment;
data.resize(offset + increment);
char *ptr = data.begin() + offset;
auto b = new (ptr) QtCbor::ByteData;
b->len = len;
memcpy(b->byte(), block, len);
return offset;
}
const QtCbor::ByteData *byteData(QtCbor::Element e) const
{
if ((e.flags & QtCbor::Element::HasByteData) == 0)
return nullptr;
size_t offset = size_t(e.value);
Q_ASSERT((offset % Q_ALIGNOF(QtCbor::ByteData)) == 0);
Q_ASSERT(offset + sizeof(QtCbor::ByteData) <= size_t(data.size()));
auto b = reinterpret_cast<const QtCbor::ByteData *>(data.constData() + offset);
Q_ASSERT(offset + sizeof(*b) + size_t(b->len) <= size_t(data.size()));
return b;
}
const QtCbor::ByteData *byteData(qsizetype idx) const
{
return byteData(elements.at(idx));
}
QCborContainerPrivate *containerAt(qsizetype idx, QCborValue::Type type) const
{
const QtCbor::Element &e = elements.at(idx);
if (e.type != type || (e.flags & QtCbor::Element::IsContainer) == 0)
return nullptr;
return e.container;
}
void replaceAt_complex(QtCbor::Element &e, const QCborValue &value);
void replaceAt_internal(QtCbor::Element &e, const QCborValue &value)
{
if (value.container)
return replaceAt_complex(e, value);
e.value = value.value_helper();
e.type = value.type();
if (value.isContainer())
e.container = nullptr;
}
void replaceAt(qsizetype idx, const QCborValue &value)
{
QtCbor::Element &e = elements[idx];
if (e.flags & QtCbor::Element::IsContainer) {
e.container->deref();
e.container = nullptr;
e.flags = {};
} else if (e.flags & QtCbor::Element::HasByteData) {
usedData -= byteData(idx)->len + sizeof(QtCbor::ByteData);
}
replaceAt_internal(e, value);
}
void insertAt(qsizetype idx, const QCborValue &value)
{
replaceAt_internal(*elements.insert(elements.begin() + idx, {}), value);
}
void append(QtCbor::Undefined)
{
elements.append(QtCbor::Element());
}
void append(qint64 value)
{
elements.append(QtCbor::Element(value , QCborValue::Integer));
}
void append(QCborTag tag)
{
elements.append(QtCbor::Element(qint64(tag), QCborValue::Tag));
}
void appendByteData(const char *data, qsizetype len, QCborValue::Type type,
QtCbor::Element::ValueFlags extraFlags = {})
{
elements.append(QtCbor::Element(addByteData(data, len), type,
QtCbor::Element::HasByteData | extraFlags));
}
void append(QLatin1String s)
{
if (!QtPrivate::isAscii(s))
return append(QString(s));
// US-ASCII is a subset of UTF-8, so we can keep in 8-bit
appendByteData(s.latin1(), s.size(), QCborValue::String,
QtCbor::Element::StringIsAscii);
}
void append(const QString &s)
{
appendByteData(reinterpret_cast<const char *>(s.constData()), s.size() * 2,
QCborValue::String, QtCbor::Element::StringIsUtf16);
}
void append(const QCborValue &v)
{
insertAt(elements.size(), v);
}
QByteArray byteArrayAt(qsizetype idx) const
{
const auto &e = elements.at(idx);
const auto data = byteData(e);
if (!data)
return QByteArray();
return data->toByteArray();
}
QString stringAt(qsizetype idx) const
{
const auto &e = elements.at(idx);
const auto data = byteData(e);
if (!data)
return QString();
if (e.flags & QtCbor::Element::StringIsUtf16)
return data->toString();
if (e.flags & QtCbor::Element::StringIsAscii)
return data->asLatin1();
return data->toUtf8String();
}
static QCborValue makeValue(QCborValue::Type type, qint64 n, QCborContainerPrivate *d = nullptr)
{
QCborValue result(type);
result.n = n;
result.container = d;
if (d)
d->ref.ref();
return result;
}
QCborValue valueAt(qsizetype idx) const
{
const auto &e = elements.at(idx);
if (e.flags & QtCbor::Element::IsContainer) {
if (e.type == QCborValue::Tag && e.container->elements.size() != 2) {
// invalid tags can be created due to incomplete parsing
return makeValue(QCborValue::Invalid, 0, nullptr);
}
return makeValue(e.type, -1, e.container);
} else if (e.flags & QtCbor::Element::HasByteData) {
return makeValue(e.type, idx, const_cast<QCborContainerPrivate *>(this));
}
return makeValue(e.type, e.value);
}
static QtCbor::Element elementFromValue(const QCborValue &value)
{
if (value.n >= 0 && value.container)
return value.container->elements.at(value.n);
QtCbor::Element e;
e.value = value.n;
e.type = value.t;
if (value.container) {
e.container = value.container;
e.flags = QtCbor::Element::IsContainer;
}
return e;
}
bool stringEqualsElement(qsizetype idx, QLatin1String s) const
{
const auto &e = elements.at(idx);
if (e.type != QCborValue::String)
return false;
const QtCbor::ByteData *b = byteData(idx);
if (!b)
return s.isEmpty();
if (e.flags & QtCbor::Element::StringIsUtf16)
return QtPrivate::compareStrings(b->asStringView(), s) == 0;
return QUtf8::compareUtf8(b->byte(), b->len, s) == 0;
}
bool stringEqualsElement(qsizetype idx, const QString &s) const
{
const auto &e = elements.at(idx);
if (e.type != QCborValue::String)
return false;
const QtCbor::ByteData *b = byteData(idx);
if (!b)
return s.isEmpty();
if (e.flags & QtCbor::Element::StringIsUtf16)
return QtPrivate::compareStrings(b->asStringView(), s) == 0;
return QUtf8::compareUtf8(b->byte(), b->len, s.data(), s.size()) == 0;
}
static int compareElement_helper(const QCborContainerPrivate *c1, QtCbor::Element e1,
const QCborContainerPrivate *c2, QtCbor::Element e2);
int compareElement(qsizetype idx, const QCborValue &value) const
{
auto &e1 = elements.at(idx);
auto e2 = elementFromValue(value);
return compareElement_helper(this, e1, value.container, e2);
}
void removeAt(qsizetype idx)
{
replaceAt(idx, {});
elements.remove(idx);
}
void decodeValueFromCbor(QCborStreamReader &reader);
void decodeFromCbor(QCborStreamReader &reader);
void decodeStringFromCbor(QCborStreamReader &reader);
};
QT_END_NAMESPACE
#endif // QCBORVALUE_P_H

View File

@ -1,7 +1,11 @@
# Qt data formats core module
HEADERS += \
serialization/qcborarray.h \
serialization/qcborcommon.h \
serialization/qcbormap.h \
serialization/qcborvalue.h \
serialization/qcborvalue_p.h \
serialization/qcborstream.h \
serialization/qdatastream.h \
serialization/qdatastream_p.h \
@ -20,6 +24,7 @@ HEADERS += \
SOURCES += \
serialization/qcborstream.cpp \
serialization/qcborvalue.cpp \
serialization/qdatastream.cpp \
serialization/qjson.cpp \
serialization/qjsondocument.cpp \
@ -32,4 +37,8 @@ SOURCES += \
serialization/qxmlstream.cpp \
serialization/qxmlutils.cpp
false: SOURCES += \
serialization/qcborarray.cpp \
serialization/qcbormap.cpp
INCLUDEPATH += ../3rdparty/tinycbor/src

View File

@ -0,0 +1,11 @@
QT = core testlib
TARGET = tst_qcborvalue
CONFIG += testcase
SOURCES += \
tst_qcborvalue.cpp
INCLUDEPATH += \
../../../../../src/3rdparty/tinycbor/src \
../../../../../src/3rdparty/tinycbor/tests/parser
DEFINES += SRCDIR=\\\"$$PWD/\\\"

File diff suppressed because it is too large Load Diff

View File

@ -3,6 +3,7 @@ SUBDIRS = \
json \
qcborstreamreader \
qcborstreamwriter \
qcborvalue \
qdatastream \
qtextstream \
qxmlstream