// Copyright (C) 2020 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #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 #include QT_BEGIN_NAMESPACE enum class QSpecialIntegerBitfieldInitializer {}; constexpr QSpecialIntegerBitfieldInitializer QSpecialIntegerBitfieldZero{}; template class QSpecialIntegerStorage { public: using UnsignedStorageType = std::make_unsigned_t; constexpr QSpecialIntegerStorage() = default; constexpr QSpecialIntegerStorage(QSpecialIntegerBitfieldInitializer) : val(0) {} constexpr QSpecialIntegerStorage(UnsignedStorageType initial) : val(initial) {} UnsignedStorageType val; }; template class QSpecialIntegerAccessor; template class QSpecialIntegerConstAccessor { Q_DISABLE_COPY_MOVE(QSpecialIntegerConstAccessor) public: using Storage = const QSpecialIntegerStorage; using Type = T; using UnsignedType = std::make_unsigned_t; operator Type() const noexcept { if constexpr (std::is_signed_v) { UnsignedType i = S::fromSpecial(storage->val); i <<= (sizeof(Type) * 8) - width - pos; Type t = Type(i); t >>= (sizeof(Type) * 8) - width; return t; } return (S::fromSpecial(storage->val) & mask()) >> pos; } bool operator!() const noexcept { return !(storage->val & S::toSpecial(mask())); } static constexpr UnsignedType mask() noexcept { if constexpr (width == sizeof(UnsignedType) * 8) { static_assert(pos == 0); return ~UnsignedType(0); } else { return ((UnsignedType(1) << width) - 1) << pos; } } private: template friend class QSpecialIntegerBitfieldUnion; friend class QSpecialIntegerAccessor; explicit QSpecialIntegerConstAccessor(Storage *storage) : storage(storage) {} friend bool operator==(const QSpecialIntegerConstAccessor &i, const QSpecialIntegerConstAccessor &j) noexcept { return ((i.storage->val ^ j.storage->val) & S::toSpecial(mask())) == 0; } friend bool operator!=(const QSpecialIntegerConstAccessor &i, const QSpecialIntegerConstAccessor &j) noexcept { return ((i.storage->val ^ j.storage->val) & S::toSpecial(mask())) != 0; } Storage *storage; }; template class QSpecialIntegerAccessor { Q_DISABLE_COPY_MOVE(QSpecialIntegerAccessor) public: using Const = QSpecialIntegerConstAccessor; using Storage = QSpecialIntegerStorage; using Type = T; using UnsignedType = std::make_unsigned_t; QSpecialIntegerAccessor &operator=(Type t) { UnsignedType i = S::fromSpecial(storage->val); i &= ~Const::mask(); i |= (UnsignedType(t) << pos) & Const::mask(); storage->val = S::toSpecial(i); return *this; } operator Const() { return Const(storage); } private: template friend class QSpecialIntegerBitfieldUnion; explicit QSpecialIntegerAccessor(Storage *storage) : storage(storage) {} Storage *storage; }; template class QSpecialIntegerBitfieldUnion { public: constexpr QSpecialIntegerBitfieldUnion() = default; constexpr QSpecialIntegerBitfieldUnion(QSpecialIntegerBitfieldInitializer initial) : storage(initial) {} constexpr QSpecialIntegerBitfieldUnion( typename QSpecialIntegerStorage::UnsignedStorageType initial) : storage(initial) {} template void set(typename A::Type value) { member() = value; } template typename A::Type get() const { return member(); } typename QSpecialIntegerStorage::UnsignedStorageType data() const { return storage.val; } private: template static constexpr bool isAccessor = std::disjunction_v...>; template A member() { static_assert(isAccessor); return A(&storage); } template typename A::Const member() const { static_assert(isAccessor); return typename A::Const(&storage); } QSpecialIntegerStorage storage; }; template using QLEIntegerBitfieldUnion = QSpecialIntegerBitfieldUnion, Accessors...>; template using QBEIntegerBitfieldUnion = QSpecialIntegerBitfieldUnion, Accessors...>; template using qint32_le_bitfield_union = QLEIntegerBitfieldUnion; template using quint32_le_bitfield_union = QLEIntegerBitfieldUnion; template using qint32_be_bitfield_union = QBEIntegerBitfieldUnion; template using quint32_be_bitfield_union = QBEIntegerBitfieldUnion; template using qint32_le_bitfield_member = QSpecialIntegerAccessor, pos, width, T>; template using quint32_le_bitfield_member = QSpecialIntegerAccessor, pos, width, T>; template using qint32_be_bitfield_member = QSpecialIntegerAccessor, pos, width, T>; template using quint32_be_bitfield_member = QSpecialIntegerAccessor, pos, width, T>; QT_END_NAMESPACE #endif // QENDIAN_P_H