| 1 | // Copyright (C) 2019 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QBINARYJSONVALUE_P_H |
| 5 | #define QBINARYJSONVALUE_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtCore5Compat/qcore5global.h> |
| 19 | |
| 20 | #include <QtCore/qstring.h> |
| 21 | #include <QtCore/qjsonvalue.h> |
| 22 | #include <QtCore/private/qglobal_p.h> |
| 23 | |
| 24 | QT_BEGIN_NAMESPACE |
| 25 | |
| 26 | class QBinaryJsonArray; |
| 27 | class QBinaryJsonObject; |
| 28 | |
| 29 | namespace QBinaryJsonPrivate { |
| 30 | class ConstData; |
| 31 | class MutableData; |
| 32 | class Base; |
| 33 | class Value; |
| 34 | class Object; |
| 35 | class Array; |
| 36 | } |
| 37 | |
| 38 | class Q_CORE5COMPAT_EXPORT QBinaryJsonValue |
| 39 | { |
| 40 | Q_DISABLE_COPY(QBinaryJsonValue) |
| 41 | public: |
| 42 | explicit QBinaryJsonValue(QJsonValue::Type type) : ui(0), t(type) {} |
| 43 | explicit QBinaryJsonValue(bool b) : b(b), t(QJsonValue::Bool) {} |
| 44 | explicit QBinaryJsonValue(double n) : dbl(n), t(QJsonValue::Double) {} |
| 45 | explicit QBinaryJsonValue(QString s); |
| 46 | QBinaryJsonValue(const QBinaryJsonArray &a); |
| 47 | QBinaryJsonValue(const QBinaryJsonObject &o); |
| 48 | |
| 49 | ~QBinaryJsonValue(); |
| 50 | |
| 51 | QBinaryJsonValue(QBinaryJsonValue &&other) noexcept |
| 52 | : ui(other.ui), |
| 53 | stringData(std::move(other.stringData)), |
| 54 | d(other.d), |
| 55 | t(other.t) |
| 56 | { |
| 57 | other.ui = 0; |
| 58 | other.d = nullptr; |
| 59 | other.t = QJsonValue::Null; |
| 60 | } |
| 61 | |
| 62 | QBinaryJsonValue &operator =(QBinaryJsonValue &&other) noexcept |
| 63 | { |
| 64 | stringData.swap(other&: other.stringData); |
| 65 | std::swap(a&: ui, b&: other.ui); |
| 66 | qt_ptr_swap(lhs&: d, rhs&: other.d); |
| 67 | std::swap(a&: t, b&: other.t); |
| 68 | return *this; |
| 69 | } |
| 70 | |
| 71 | static QBinaryJsonValue fromJsonValue(const QJsonValue &json); |
| 72 | QJsonValue::Type type() const { return t; } |
| 73 | bool toBool() const { return (t == QJsonValue::Bool) && b; } |
| 74 | double toDouble() const { return (t == QJsonValue::Double) ? dbl : 0; } |
| 75 | QString toString() const; |
| 76 | |
| 77 | private: |
| 78 | friend class QBinaryJsonPrivate::Value; |
| 79 | friend class QBinaryJsonArray; |
| 80 | friend class QBinaryJsonObject; |
| 81 | |
| 82 | QBinaryJsonValue(QBinaryJsonPrivate::MutableData *d, QBinaryJsonPrivate::Base *parent, |
| 83 | const QBinaryJsonPrivate::Value &v); |
| 84 | |
| 85 | void detach(); |
| 86 | |
| 87 | union { |
| 88 | quint64 ui; |
| 89 | bool b; |
| 90 | double dbl; |
| 91 | const QBinaryJsonPrivate::Base *base; |
| 92 | }; |
| 93 | QString stringData; |
| 94 | QBinaryJsonPrivate::MutableData *d = nullptr; // needed for Objects and Arrays |
| 95 | QJsonValue::Type t = QJsonValue::Null; |
| 96 | }; |
| 97 | |
| 98 | QT_END_NAMESPACE |
| 99 | |
| 100 | #endif // QBINARYJSONVALUE_P_H |
| 101 | |