From 6ca65dd97d252d1618dd9870bf119b2d482e7eaf Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Thu, 22 Jun 2017 12:37:04 +0200 Subject: Move qle_bitfield to qendian_p.h Makes the qle_bitfield template more generic and moves it to qendian_p.h It is also hardened to be more reliable. Change-Id: I53214ec99cceee4f5e8934ae688c99e555a5fb42 Reviewed-by: Ville Voutilainen Reviewed-by: Thiago Macieira --- src/corelib/global/qendian_p.h | 84 +++++++++++++++++++++++++++++++++++++- src/corelib/json/qjson_p.h | 91 +----------------------------------------- 2 files changed, 85 insertions(+), 90 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/global/qendian_p.h b/src/corelib/global/qendian_p.h index b279701a31..42d91cf735 100644 --- a/src/corelib/global/qendian_p.h +++ b/src/corelib/global/qendian_p.h @@ -51,7 +51,7 @@ // We mean it. // -#include "qendian.h" +#include QT_BEGIN_NAMESPACE @@ -136,6 +136,88 @@ typedef QBEInteger quint16_be; typedef QBEInteger quint32_be; typedef QBEInteger quint64_be; +// Note if using multiple of these bitfields in a union; the underlying storage type must +// match. Since we always use an unsigned storage type, unsigned and signed versions may +// be used together, but different bit-widths may not. +template +class QSpecialIntegerBitfield +{ +protected: + typedef typename S::StorageType T; + typedef typename std::make_unsigned::type UT; + + static Q_DECL_CONSTEXPR UT mask() + { + return ((UT(1) << width) - 1) << pos; + } +public: + // FIXME: val is public until qtdeclarative is fixed to not access it directly. + UT val; + + QSpecialIntegerBitfield &operator =(T t) + { + UT i = S::fromSpecial(val); + i &= ~mask(); + i |= (UT(t) << pos) & mask(); + val = S::toSpecial(i); + return *this; + } + operator T() const + { + if (std::is_signed::value) { + UT i = S::fromSpecial(val); + i <<= (sizeof(T) * 8) - width - pos; + T t = T(i); + t >>= (sizeof(T) * 8) - width; + return t; + } + return (S::fromSpecial(val) & mask()) >> pos; + } + + bool operator !() const { return !(val & S::toSpecial(mask())); } + bool operator ==(QSpecialIntegerBitfield i) const + { return ((val ^ i.val) & S::toSpecial(mask())) == 0; } + bool operator !=(QSpecialIntegerBitfield i) const + { return ((val ^ i.val) & S::toSpecial(mask())) != 0; } + + QSpecialIntegerBitfield &operator +=(T i) + { return (*this = (T(*this) + i)); } + QSpecialIntegerBitfield &operator -=(T i) + { return (*this = (T(*this) - i)); } + QSpecialIntegerBitfield &operator *=(T i) + { return (*this = (T(*this) * i)); } + QSpecialIntegerBitfield &operator /=(T i) + { return (*this = (T(*this) / i)); } + QSpecialIntegerBitfield &operator %=(T i) + { return (*this = (T(*this) % i)); } + QSpecialIntegerBitfield &operator |=(T i) + { return (*this = (T(*this) | i)); } + QSpecialIntegerBitfield &operator &=(T i) + { return (*this = (T(*this) & i)); } + QSpecialIntegerBitfield &operator ^=(T i) + { return (*this = (T(*this) ^ i)); } + QSpecialIntegerBitfield &operator >>=(T i) + { return (*this = (T(*this) >> i)); } + QSpecialIntegerBitfield &operator <<=(T i) + { return (*this = (T(*this) << i)); } +}; + +template +using QLEIntegerBitfield = QSpecialIntegerBitfield, pos, width>; + +template +using QBEIntegerBitfield = QSpecialIntegerBitfield, pos, width>; + +template +using qint32_le_bitfield = QLEIntegerBitfield; +template +using quint32_le_bitfield = QLEIntegerBitfield; +template +using qint32_be_bitfield = QBEIntegerBitfield; +template +using quint32_be_bitfield = QBEIntegerBitfield; + + QT_END_NAMESPACE #endif // QENDIAN_P_H diff --git a/src/corelib/json/qjson_p.h b/src/corelib/json/qjson_p.h index 5d600843aa..131528376b 100644 --- a/src/corelib/json/qjson_p.h +++ b/src/corelib/json/qjson_p.h @@ -140,97 +140,10 @@ typedef q_littleendian qle_int; typedef q_littleendian qle_uint; template -class qle_bitfield -{ -public: - uint val; - - enum { - mask = ((1u << width) - 1) << pos - }; - - void operator =(uint t) { - uint i = qFromLittleEndian(val); - i &= ~mask; - i |= t << pos; - val = qToLittleEndian(i); - } - operator uint() const { - uint t = qFromLittleEndian(val); - t &= mask; - t >>= pos; - return t; - } - bool operator !() const { - return !operator uint(); - } - - bool operator ==(uint t) { return uint(*this) == t; } - bool operator !=(uint t) { return uint(*this) != t; } - bool operator <(uint t) { return uint(*this) < t; } - bool operator >(uint t) { return uint(*this) > t; } - bool operator <=(uint t) { return uint(*this) <= t; } - bool operator >=(uint t) { return uint(*this) >= t; } - qle_bitfield &operator +=(uint i) { - *this = (uint(*this) + i); - return *this; - } - qle_bitfield &operator -=(uint i) { - *this = (uint(*this) - i); - return *this; - } - qle_bitfield &operator |=(uint i) { - *this = (uint(*this) | i); - return *this; - } - qle_bitfield &operator &=(uint i) { - *this = (uint(*this) & i); - return *this; - } -}; +using qle_bitfield = QLEIntegerBitfield; template -class qle_signedbitfield -{ -public: - uint val; - - enum { - mask = ((1u << width) - 1) << pos - }; - - void operator =(int t) { - uint i = qFromLittleEndian(val); - i &= ~mask; - i |= t << pos; - val = qToLittleEndian(i); - } - operator int() const { - uint i = qFromLittleEndian(val); - i <<= 32 - width - pos; - int t = (int) i; - t >>= 32 - width; - return t; - } - bool operator !() const { - return !operator int(); - } - - bool operator ==(int t) { return int(*this) == t; } - bool operator !=(int t) { return int(*this) != t; } - bool operator <(int t) { return int(*this) < t; } - bool operator >(int t) { return int(*this) > t; } - bool operator <=(int t) { return int(*this) <= t; } - bool operator >=(int t) { return int(*this) >= t; } - qle_signedbitfield &operator +=(int i) { - *this = (int(*this) + i); - return *this; - } - qle_signedbitfield &operator -=(int i) { - *this = (int(*this) - i); - return *this; - } -}; +using qle_signedbitfield = QLEIntegerBitfield; typedef qle_uint offset; -- cgit v1.2.3