diff options
Diffstat (limited to 'src/corelib/global/qendian_p.h')
-rw-r--r-- | src/corelib/global/qendian_p.h | 82 |
1 files changed, 6 insertions, 76 deletions
diff --git a/src/corelib/global/qendian_p.h b/src/corelib/global/qendian_p.h index fd5a9dd992..8d96eba60c 100644 --- a/src/corelib/global/qendian_p.h +++ b/src/corelib/global/qendian_p.h @@ -20,81 +20,6 @@ QT_BEGIN_NAMESPACE -// 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 S, int pos, int width> -class QSpecialIntegerBitfield -{ -protected: - typedef typename S::StorageType T; - typedef typename std::make_unsigned<T>::type UT; - - static 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<T>::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<S, pos, width> i) const - { - return ((val ^ i.val) & S::toSpecial(mask())) == 0; - } - bool operator!=(QSpecialIntegerBitfield<S, pos, width> 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<typename T, int pos, int width> -using QLEIntegerBitfield = QSpecialIntegerBitfield<QLittleEndianStorageType<T>, pos, width>; - -template<typename T, int pos, int width> -using QBEIntegerBitfield = QSpecialIntegerBitfield<QBigEndianStorageType<T>, pos, width>; - -template<int pos, int width> -using qint32_le_bitfield = QLEIntegerBitfield<int, pos, width>; -template<int pos, int width> -using quint32_le_bitfield = QLEIntegerBitfield<uint, pos, width>; -template<int pos, int width> -using qint32_be_bitfield = QBEIntegerBitfield<int, pos, width>; -template<int pos, int width> -using quint32_be_bitfield = QBEIntegerBitfield<uint, pos, width>; - enum class QSpecialIntegerBitfieldInitializer {}; constexpr QSpecialIntegerBitfieldInitializer QSpecialIntegerBitfieldZero{}; @@ -139,7 +64,12 @@ public: static constexpr UnsignedType mask() noexcept { - return ((UnsignedType(1) << width) - 1) << pos; + if constexpr (width == sizeof(UnsignedType) * 8) { + static_assert(pos == 0); + return ~UnsignedType(0); + } else { + return ((UnsignedType(1) << width) - 1) << pos; + } } private: |