diff options
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/global/global.pri | 1 | ||||
-rw-r--r-- | src/corelib/global/qendian_p.h | 141 | ||||
-rw-r--r-- | src/corelib/json/qjson.cpp | 13 | ||||
-rw-r--r-- | src/corelib/json/qjson_p.h | 38 |
4 files changed, 147 insertions, 46 deletions
diff --git a/src/corelib/global/global.pri b/src/corelib/global/global.pri index a087452d06..efc7f3cd22 100644 --- a/src/corelib/global/global.pri +++ b/src/corelib/global/global.pri @@ -9,6 +9,7 @@ HEADERS += \ global/qprocessordetection.h \ global/qnamespace.h \ global/qendian.h \ + global/qendian_p.h \ global/qnumeric_p.h \ global/qnumeric.h \ global/qfloat16_p.h \ diff --git a/src/corelib/global/qendian_p.h b/src/corelib/global/qendian_p.h new file mode 100644 index 0000000000..b279701a31 --- /dev/null +++ b/src/corelib/global/qendian_p.h @@ -0,0 +1,141 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** 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 QENDIAN_P_H +#define QENDIAN_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qendian.h" + +QT_BEGIN_NAMESPACE + +template<class S> +class QSpecialInteger +{ + typedef typename S::StorageType T; + T val; +public: + QSpecialInteger() = default; + explicit Q_DECL_CONSTEXPR QSpecialInteger(T i) : val(S::toSpecial(i)) {} + + QSpecialInteger &operator =(T i) { val = S::toSpecial(i); return *this; } + operator T() const { return S::fromSpecial(val); } + + bool operator ==(QSpecialInteger<S> i) const { return val == i.val; } + bool operator !=(QSpecialInteger<S> i) const { return val != i.val; } + + QSpecialInteger &operator +=(T i) + { return (*this = S::fromSpecial(val) + i); } + QSpecialInteger &operator -=(T i) + { return (*this = S::fromSpecial(val) - i); } + QSpecialInteger &operator *=(T i) + { return (*this = S::fromSpecial(val) * i); } + QSpecialInteger &operator >>=(T i) + { return (*this = S::fromSpecial(val) >> i); } + QSpecialInteger &operator <<=(T i) + { return (*this = S::fromSpecial(val) << i); } + QSpecialInteger &operator /=(T i) + { return (*this = S::fromSpecial(val) / i); } + QSpecialInteger &operator %=(T i) + { return (*this = S::fromSpecial(val) % i); } + QSpecialInteger &operator |=(T i) + { return (*this = S::fromSpecial(val) | i); } + QSpecialInteger &operator &=(T i) + { return (*this = S::fromSpecial(val) & i); } + QSpecialInteger &operator ^=(T i) + { return (*this = S::fromSpecial(val) ^ i); } +}; + +template<typename T> +class QLittleEndianStorageType { +public: + typedef T StorageType; + static Q_DECL_CONSTEXPR T toSpecial(T source) { return qToLittleEndian(source); } + static Q_DECL_CONSTEXPR T fromSpecial(T source) { return qFromLittleEndian(source); } +}; + +template<typename T> +class QBigEndianStorageType { +public: + typedef T StorageType; + static Q_DECL_CONSTEXPR T toSpecial(T source) { return qToBigEndian(source); } + static Q_DECL_CONSTEXPR T fromSpecial(T source) { return qFromBigEndian(source); } +}; + +template<typename T> +using QLEInteger = QSpecialInteger<QLittleEndianStorageType<T>>; + +template<typename T> +using QBEInteger = QSpecialInteger<QBigEndianStorageType<T>>; + +template <typename T> +class QTypeInfo<QLEInteger<T> > + : public QTypeInfoMerger<QLEInteger<T>, T> {}; + +template <typename T> +class QTypeInfo<QBEInteger<T> > + : public QTypeInfoMerger<QBEInteger<T>, T> {}; + +typedef QLEInteger<qint16> qint16_le; +typedef QLEInteger<qint32> qint32_le; +typedef QLEInteger<qint64> qint64_le; +typedef QLEInteger<quint16> quint16_le; +typedef QLEInteger<quint32> quint32_le; +typedef QLEInteger<quint64> quint64_le; + +typedef QBEInteger<qint16> qint16_be; +typedef QBEInteger<qint32> qint32_be; +typedef QBEInteger<qint64> qint64_be; +typedef QBEInteger<quint16> quint16_be; +typedef QBEInteger<quint32> quint32_be; +typedef QBEInteger<quint64> quint64_be; + +QT_END_NAMESPACE + +#endif // QENDIAN_P_H diff --git a/src/corelib/json/qjson.cpp b/src/corelib/json/qjson.cpp index d509349a51..e4bca3bcd0 100644 --- a/src/corelib/json/qjson.cpp +++ b/src/corelib/json/qjson.cpp @@ -45,15 +45,8 @@ QT_BEGIN_NAMESPACE namespace QJsonPrivate { -#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN -#define Q_TO_LITTLE_ENDIAN(x) (x) -#else -#define Q_TO_LITTLE_ENDIAN(x) ( ((x & 0xff) << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | ((x & 0xff000000) >> 24) ) -#endif - -static const Base emptyArray = { { Q_TO_LITTLE_ENDIAN(sizeof(Base)) }, { 0 }, { 0 } }; -static const Base emptyObject = { { Q_TO_LITTLE_ENDIAN(sizeof(Base)) }, { 0 }, { 0 } }; - +static Q_CONSTEXPR Base emptyArray = { { qle_uint(sizeof(Base)) }, { 0 }, { qle_uint(0) } }; +static Q_CONSTEXPR Base emptyObject = { { qle_uint(sizeof(Base)) }, { 0 }, { qle_uint(0) } }; void Data::compact() { @@ -394,7 +387,7 @@ int Value::requiredStorage(QJsonValue &v, bool *compressed) v.d->compact(); v.base = static_cast<QJsonPrivate::Base *>(v.d->header->root()); } - return v.base ? v.base->size : sizeof(QJsonPrivate::Base); + return v.base ? uint(v.base->size) : sizeof(QJsonPrivate::Base); case QJsonValue::Undefined: case QJsonValue::Null: case QJsonValue::Bool: diff --git a/src/corelib/json/qjson_p.h b/src/corelib/json/qjson_p.h index c012ec2662..5d600843aa 100644 --- a/src/corelib/json/qjson_p.h +++ b/src/corelib/json/qjson_p.h @@ -61,6 +61,7 @@ #include <qendian.h> #include <qnumeric.h> +#include "private/qendian_p.h" #include "private/qsimd_p.h" #include <limits.h> @@ -131,42 +132,7 @@ class Value; class Entry; template<typename T> -class q_littleendian -{ -public: - T val; - - q_littleendian &operator =(T i) { val = qToLittleEndian(i); return *this; } - operator T() const { return qFromLittleEndian(val); } - - bool operator ==(T i) { return qFromLittleEndian(val) == i; } - bool operator !=(T i) { return qFromLittleEndian(val) != i; } - bool operator ==(q_littleendian<T> i) { return val == i.val; } - bool operator !=(q_littleendian<T> i) { return val != i.val; } - bool operator <(T i) { return qFromLittleEndian(val) < i; } - bool operator >(T i) { return qFromLittleEndian(val) > i; } - bool operator <=(T i) { return qFromLittleEndian(val) <= i; } - bool operator >=(T i) { return qFromLittleEndian(val) >= i; } - q_littleendian &operator +=(T i) { - val = qToLittleEndian(qFromLittleEndian(val) + i); - return *this; - } - q_littleendian &operator |=(T i) { - val = qToLittleEndian(qFromLittleEndian(val) | i); - return *this; - } - q_littleendian &operator &=(T i) { - val = qToLittleEndian(qFromLittleEndian(val) & i); - return *this; - } -}; -} // namespace QJsonPrivate - -template <typename T> -class QTypeInfo<QJsonPrivate::q_littleendian<T> > - : public QTypeInfoMerger<QJsonPrivate::q_littleendian<T>, T> {}; - -namespace QJsonPrivate { +using q_littleendian = QLEInteger<T>; typedef q_littleendian<short> qle_short; typedef q_littleendian<unsigned short> qle_ushort; |