File indexing completed on 2024-06-30 05:51:24

0001 /*
0002     This file is part of the Okteta Kasten Framework, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2010 Alex Richardson <alex.richardson@gmx.de>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #ifndef KASTEN_ABSTRACTBITFIELDDATAINFORMATION_HPP
0010 #define KASTEN_ABSTRACTBITFIELDDATAINFORMATION_HPP
0011 
0012 #include "../primitivedatainformation.hpp"
0013 #include "../../../allprimitivetypes.hpp"
0014 
0015 class AbstractBitfieldDataInformation : public PrimitiveDataInformation
0016 {
0017 public:
0018     enum class Type {
0019         Signed,
0020         Unsigned,
0021         Boolean
0022     };
0023 
0024     AbstractBitfieldDataInformation(const QString& name, BitCount32 width, DataInformation* parent = nullptr);
0025     ~AbstractBitfieldDataInformation() override;
0026 
0027     BitCount32 width() const;
0028     void setWidth(BitCount32 newWidth);
0029     BitCount32 size() const override;
0030     quint64 mask() const;
0031     AllPrimitiveTypes value() const override;
0032     void setValue(AllPrimitiveTypes newVal) override;
0033     PrimitiveDataType type() const override;
0034     QString sizeString() const override;
0035     bool isBitfield() const override;
0036     virtual Type bitfieldType() const = 0;
0037     Qt::ItemFlags flags(int column, bool fileLoaded) const override;
0038     qint64 readData(const Okteta::AbstractByteArrayModel* input,
0039                     Okteta::Address address, BitCount64 bitsRemaining, quint8* bitOffset) override;
0040     bool setData(const QVariant& valueVariant, Okteta::AbstractByteArrayModel* out,
0041                  Okteta::Address address, BitCount64 bitsRemaining, quint8 bitOffset) override;
0042 
0043 protected:
0044     AbstractBitfieldDataInformation(const AbstractBitfieldDataInformation& d);
0045     virtual AllPrimitiveTypes fromVariant(const QVariant& variant, bool* ok) const;
0046 
0047 private:
0048     QScriptClass* scriptClass(ScriptHandlerInfo* handlerInfo) const override;
0049 
0050 protected:
0051     AllPrimitiveTypes mValue = 0;
0052     quint8 mWidth; // cannot be more than 64 since a quint64 is used for storage
0053 };
0054 
0055 inline quint64 AbstractBitfieldDataInformation::mask() const
0056 {
0057     /* same as:
0058      *
0059      * quint64 ret = 0;
0060      * for (int i = 0; i < width(); i++)
0061      *     ret |= 1 << i;
0062      * return ret;
0063      */
0064     if (Q_UNLIKELY(mWidth == 64U)) {
0065         return ~quint64(0);
0066     }
0067     return (quint64(1) << mWidth) - 1;
0068 }
0069 
0070 inline BitCount32 AbstractBitfieldDataInformation::width() const
0071 {
0072     return mWidth;
0073 }
0074 
0075 inline void AbstractBitfieldDataInformation::setWidth(BitCount32 newWidth)
0076 {
0077     Q_ASSERT(newWidth > 0 && newWidth <= 64);
0078     mWidth = qMin(newWidth, BitCount32(64U)); // maximum width is 64 bits
0079 }
0080 
0081 #endif /* KASTEN_ABSTRACTBITFIELDDATAINFORMATION_HPP */