| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Copyright (C) 2016 Intel Corporation. |
| 5 | ** Contact: https://www.qt.io/licensing/ |
| 6 | ** |
| 7 | ** This file is part of the QtCore module of the Qt Toolkit. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** GNU Lesser General Public License Usage |
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 20 | ** General Public License version 3 as published by the Free Software |
| 21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 22 | ** packaging of this file. Please review the following information to |
| 23 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 25 | ** |
| 26 | ** GNU General Public License Usage |
| 27 | ** Alternatively, this file may be used under the terms of the GNU |
| 28 | ** General Public License version 2.0 or (at your option) the GNU General |
| 29 | ** Public license version 3 or any later version approved by the KDE Free |
| 30 | ** Qt Foundation. The licenses are as published by the Free Software |
| 31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 32 | ** included in the packaging of this file. Please review the following |
| 33 | ** information to ensure the GNU General Public License requirements will |
| 34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 35 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 36 | ** |
| 37 | ** $QT_END_LICENSE$ |
| 38 | ** |
| 39 | ****************************************************************************/ |
| 40 | |
| 41 | #ifndef QJSON_P_H |
| 42 | #define QJSON_P_H |
| 43 | |
| 44 | // |
| 45 | // W A R N I N G |
| 46 | // ------------- |
| 47 | // |
| 48 | // This file is not part of the Qt API. It exists purely as an |
| 49 | // implementation detail. This header file may change from version to |
| 50 | // version without notice, or even be removed. |
| 51 | // |
| 52 | // We mean it. |
| 53 | // |
| 54 | |
| 55 | #include <qjsonvalue.h> |
| 56 | #include <qcborvalue.h> |
| 57 | #include <private/qcborvalue_p.h> |
| 58 | |
| 59 | QT_BEGIN_NAMESPACE |
| 60 | |
| 61 | namespace QJsonPrivate { |
| 62 | |
| 63 | template<typename Element, typename ElementsIterator> |
| 64 | struct ObjectIterator |
| 65 | { |
| 66 | using pointer = Element *; |
| 67 | |
| 68 | struct value_type; |
| 69 | struct reference { |
| 70 | reference(Element &ref) : m_key(&ref) {} |
| 71 | |
| 72 | reference() = delete; |
| 73 | ~reference() = default; |
| 74 | |
| 75 | reference(const reference &other) = default; |
| 76 | reference(reference &&other) = default; |
| 77 | |
| 78 | reference &operator=(const value_type &value); |
| 79 | reference &operator=(const reference &other) |
| 80 | { |
| 81 | if (m_key != other.m_key) { |
| 82 | key() = other.key(); |
| 83 | value() = other.value(); |
| 84 | } |
| 85 | return *this; |
| 86 | } |
| 87 | |
| 88 | reference &operator=(reference &&other) |
| 89 | { |
| 90 | key() = other.key(); |
| 91 | value() = other.value(); |
| 92 | return *this; |
| 93 | } |
| 94 | |
| 95 | Element &key() { return *m_key; } |
| 96 | Element &value() { return *(m_key + 1); } |
| 97 | |
| 98 | const Element &key() const { return *m_key; } |
| 99 | const Element &value() const { return *(m_key + 1); } |
| 100 | |
| 101 | |
| 102 | private: |
| 103 | Element *m_key; |
| 104 | }; |
| 105 | |
| 106 | struct value_type { |
| 107 | value_type(reference ref) : m_key(ref.key()), m_value(ref.value()) {} |
| 108 | |
| 109 | Element key() const { return m_key; } |
| 110 | Element value() const { return m_value; } |
| 111 | private: |
| 112 | Element m_key; |
| 113 | Element m_value; |
| 114 | }; |
| 115 | |
| 116 | using difference_type = typename QList<Element>::difference_type; |
| 117 | using iterator_category = std::random_access_iterator_tag; |
| 118 | |
| 119 | ObjectIterator() = default; |
| 120 | ObjectIterator(ElementsIterator it) : it(it) {} |
| 121 | ElementsIterator elementsIterator() { return it; } |
| 122 | |
| 123 | ObjectIterator operator++(int) { ObjectIterator ret(it); it += 2; return ret; } |
| 124 | ObjectIterator &operator++() { it += 2; return *this; } |
| 125 | ObjectIterator &operator+=(difference_type n) { it += 2 * n; return *this; } |
| 126 | |
| 127 | ObjectIterator operator--(int) { ObjectIterator ret(it); it -= 2; return ret; } |
| 128 | ObjectIterator &operator--() { it -= 2; return *this; } |
| 129 | ObjectIterator &operator-=(difference_type n) { it -= 2 * n; return *this; } |
| 130 | |
| 131 | reference operator*() const { return *it; } |
| 132 | reference operator[](qsizetype n) const { return it[n * 2]; } |
| 133 | |
| 134 | bool operator<(ObjectIterator other) const { return it < other.it; } |
| 135 | bool operator>(ObjectIterator other) const { return it > other.it; } |
| 136 | bool operator<=(ObjectIterator other) const { return it <= other.it; } |
| 137 | bool operator>=(ObjectIterator other) const { return it >= other.it; } |
| 138 | |
| 139 | private: |
| 140 | ElementsIterator it; |
| 141 | }; |
| 142 | |
| 143 | template<typename Element, typename ElementsIterator> |
| 144 | inline ObjectIterator<Element, ElementsIterator> operator+( |
| 145 | ObjectIterator<Element, ElementsIterator> a, |
| 146 | typename ObjectIterator<Element, ElementsIterator>::difference_type n) |
| 147 | { |
| 148 | return {a.elementsIterator() + 2 * n}; |
| 149 | } |
| 150 | template<typename Element, typename ElementsIterator> |
| 151 | inline ObjectIterator<Element, ElementsIterator> operator+( |
| 152 | qsizetype n, ObjectIterator<Element, ElementsIterator> a) |
| 153 | { |
| 154 | return {a.elementsIterator() + 2 * n}; |
| 155 | } |
| 156 | template<typename Element, typename ElementsIterator> |
| 157 | inline ObjectIterator<Element, ElementsIterator> operator-( |
| 158 | ObjectIterator<Element, ElementsIterator> a, |
| 159 | typename ObjectIterator<Element, ElementsIterator>::difference_type n) |
| 160 | { |
| 161 | return {a.elementsIterator() - 2 * n}; |
| 162 | } |
| 163 | template<typename Element, typename ElementsIterator> |
| 164 | inline qsizetype operator-( |
| 165 | ObjectIterator<Element, ElementsIterator> a, |
| 166 | ObjectIterator<Element, ElementsIterator> b) |
| 167 | { |
| 168 | return (a.elementsIterator() - b.elementsIterator()) / 2; |
| 169 | } |
| 170 | template<typename Element, typename ElementsIterator> |
| 171 | inline bool operator!=( |
| 172 | ObjectIterator<Element, ElementsIterator> a, |
| 173 | ObjectIterator<Element, ElementsIterator> b) |
| 174 | { |
| 175 | return a.elementsIterator() != b.elementsIterator(); |
| 176 | } |
| 177 | template<typename Element, typename ElementsIterator> |
| 178 | inline bool operator==( |
| 179 | ObjectIterator<Element, ElementsIterator> a, |
| 180 | ObjectIterator<Element, ElementsIterator> b) |
| 181 | { |
| 182 | return a.elementsIterator() == b.elementsIterator(); |
| 183 | } |
| 184 | |
| 185 | using KeyIterator = ObjectIterator<QtCbor::Element, QList<QtCbor::Element>::iterator>; |
| 186 | using ConstKeyIterator = ObjectIterator<const QtCbor::Element, QList<QtCbor::Element>::const_iterator>; |
| 187 | |
| 188 | template<> |
| 189 | inline KeyIterator::reference &KeyIterator::reference::operator=(const KeyIterator::value_type &value) |
| 190 | { |
| 191 | *m_key = value.key(); |
| 192 | *(m_key + 1) = value.value(); |
| 193 | return *this; |
| 194 | } |
| 195 | |
| 196 | inline void swap(KeyIterator::reference a, KeyIterator::reference b) |
| 197 | { |
| 198 | KeyIterator::value_type t = a; |
| 199 | a = b; |
| 200 | b = t; |
| 201 | } |
| 202 | |
| 203 | class Value |
| 204 | { |
| 205 | public: |
| 206 | static QCborContainerPrivate *container(const QCborValue &v) { return v.container; } |
| 207 | static qint64 valueHelper(const QCborValue &v) { return v.n; } |
| 208 | |
| 209 | static QJsonValue fromTrustedCbor(const QCborValue &v) |
| 210 | { |
| 211 | QJsonValue result; |
| 212 | result.value = v; |
| 213 | return result; |
| 214 | } |
| 215 | }; |
| 216 | |
| 217 | class Variant |
| 218 | { |
| 219 | public: |
| 220 | static QJsonObject toJsonObject(const QVariantMap &map); |
| 221 | static QJsonArray toJsonArray(const QVariantList &list); |
| 222 | }; |
| 223 | |
| 224 | } // namespace QJsonPrivate |
| 225 | |
| 226 | QT_END_NAMESPACE |
| 227 | |
| 228 | #endif // QJSON_P_H |
| 229 | |