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

0001 /*
0002     This file is part of the Okteta Kasten Framework, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2009, 2010, 2011 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_PRIMITIVEDATAINFORMATION_HPP
0010 #define KASTEN_PRIMITIVEDATAINFORMATION_HPP
0011 
0012 #include "../datainformation.hpp"
0013 #include "../primitivedatatype.hpp"
0014 #include "../../allprimitivetypes.hpp"
0015 // Std
0016 #include <memory>
0017 
0018 union AllPrimitiveTypes;
0019 
0020 /**
0021  * A base class for all primitive data elements (e.g. unsigned integers or floating-point numbers)
0022  */
0023 class PrimitiveDataInformation : public DataInformation
0024 {
0025     friend class PrimitiveDataInformationTest; // that unit test needs to change mWasAbleToRead
0026 
0027 public:
0028     explicit PrimitiveDataInformation(const QString& name, DataInformation* parent = nullptr);
0029     ~PrimitiveDataInformation() override;
0030     PrimitiveDataInformation* clone() const override = 0;
0031 
0032     Qt::ItemFlags flags(int column, bool fileLoaded = true) const override;
0033 
0034     bool isPrimitive() const override;
0035     virtual AllPrimitiveTypes value() const = 0;
0036     virtual void setValue(AllPrimitiveTypes newValue) = 0;
0037     virtual QVariant valueToQVariant() const = 0;
0038     virtual QScriptValue valueAsQScriptValue() const = 0;
0039     virtual PrimitiveDataType type() const = 0;
0040     virtual QString valueToQString(AllPrimitiveTypes value) const = 0;
0041     virtual QVariant valueToQVariant(AllPrimitiveTypes value) const = 0;
0042 
0043     unsigned int childCount() const override;
0044     DataInformation* childAt(unsigned int) const override;
0045     BitCount64 childPosition(const DataInformation*, Okteta::Address) const override;
0046     int indexOf(const DataInformation* const) const override;
0047 
0048     /** @return the matching prefix for the base (nothing, '0x', '0b' or '0o') */
0049     static QString basePrefix(int base);
0050 
0051 protected:
0052     virtual BitCount32 offset(unsigned int index) const;
0053     PrimitiveDataInformation(const PrimitiveDataInformation& d);
0054 };
0055 
0056 /**
0057  * A base class for data types which just wrap an underlying primitive data type.
0058  * The basic operations (reading/writing/size/value) are simply delegated to the wrapped element.
0059  *
0060  * This is used for pointers (wrapping differently sized integers) and enumerations/bitflags
0061  * Due to the use of a PrimitiveDataInformation pointer as a member any primitive type can be wrapped.
0062  * For example pointers using a bitfield value are possible and theoretically floating point enums
0063  * (although these were not added due to float comparison issues - use the binary representation of an
0064  * equally sized integer instead).
0065  */
0066 class PrimitiveDataInformationWrapper : public PrimitiveDataInformation
0067 {
0068 protected:
0069     PrimitiveDataInformationWrapper(const PrimitiveDataInformationWrapper& d);
0070 
0071 public:
0072     /** takes ownership of @p valueType */
0073     PrimitiveDataInformationWrapper(const QString& name, PrimitiveDataInformation* valueType,
0074                                     DataInformation* parent = nullptr);
0075     ~PrimitiveDataInformationWrapper() override = default;
0076 
0077     // delegate all these to the underlying object:
0078 
0079     bool setData(const QVariant& value, Okteta::AbstractByteArrayModel* out,
0080                  Okteta::Address address, BitCount64 bitsRemaining, quint8 bitOffset) override;
0081 
0082     BitCount32 size() const override;
0083 
0084     void setWidgetData(QWidget* w) const override;
0085 
0086     QVariant dataFromWidget(const QWidget* w) const override;
0087 
0088     QWidget* createEditWidget(QWidget* parent) const override;
0089 
0090     AllPrimitiveTypes value() const override;
0091 
0092     void setValue(AllPrimitiveTypes newValue) override;
0093 
0094     PrimitiveDataType type() const override;
0095 
0096     QVariant valueToQVariant() const override;
0097 
0098     QScriptValue valueAsQScriptValue() const override;
0099 
0100     QString valueToQString(AllPrimitiveTypes value) const override;
0101 
0102     QVariant valueToQVariant(AllPrimitiveTypes value) const override;
0103 
0104     // we have to do slightly more than just forward with this method
0105     qint64 readData(const Okteta::AbstractByteArrayModel* input, Okteta::Address address,
0106                     BitCount64 bitsRemaining, quint8* bitOffset) override;
0107 
0108 protected:
0109     std::unique_ptr<PrimitiveDataInformation> mValue;
0110 };
0111 
0112 inline PrimitiveDataInformation::PrimitiveDataInformation(const QString& name,
0113                                                           DataInformation* parent)
0114     : DataInformation(name, parent)
0115 {
0116 }
0117 
0118 inline PrimitiveDataInformation::PrimitiveDataInformation(const PrimitiveDataInformation& d) = default;
0119 
0120 inline QString PrimitiveDataInformation::basePrefix(int base)
0121 {
0122     switch (base) {
0123     case 10: return {};
0124     case 2: return QStringLiteral("0b");
0125     case 8: return QStringLiteral("0o");
0126     case 16: return QStringLiteral("0x");
0127     default:
0128         Q_ASSERT_X(false, "PrimitiveDataInformation::basePrefix()", "Invalid argument!");
0129         return {};
0130     }
0131 }
0132 
0133 #endif /* KASTEN_PRIMITIVEDATAINFORMATION_HPP */