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

0001 /*
0002     This file is part of the Okteta Kasten Framework, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2009, 2010, 2011, 2012 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_ARRAYDATAINFORMATION_HPP
0010 #define KASTEN_ARRAYDATAINFORMATION_HPP
0011 
0012 #include "../dummydatainformation.hpp"
0013 #include "abstractarraydata.hpp"
0014 // Qt
0015 #include <QVariant>
0016 #include <QScriptValue>
0017 // Std
0018 #include <memory>
0019 
0020 class AbstractArrayData;
0021 
0022 class ArrayDataInformation : public DataInformationWithDummyChildren
0023 {
0024     friend class PrimitiveArrayTest;
0025     DATAINFORMATION_CLONE_DECL(ArrayDataInformation, DataInformationWithDummyChildren);
0026 
0027 public:
0028     /** creates a new array with initial length @p length.
0029      *  takes ownership over @p childType
0030      *  length should be > 0
0031      */
0032     ArrayDataInformation(const QString& name, uint length, DataInformation* childType,
0033                          DataInformation* parent = nullptr, const QScriptValue& lengthFunction = QScriptValue());
0034     ~ArrayDataInformation() override;
0035 
0036 public:
0037     uint length() const;
0038     QWidget* createEditWidget(QWidget* parent) const override;
0039     QVariant dataFromWidget(const QWidget* w) const override;
0040     void setWidgetData(QWidget* w) const override;
0041 
0042     QWidget* createChildEditWidget(uint index, QWidget* parent) const override;
0043     QVariant dataFromChildWidget(uint index, const QWidget* w) const override;
0044     void setChildWidgetData(uint index, QWidget* w) const override;
0045 
0046     BitCount32 size() const override;
0047 
0048     qint64 readData(const Okteta::AbstractByteArrayModel* input, Okteta::Address address,
0049                     BitCount64 bitsRemaining, quint8* bitOffset) override;
0050     bool setData(const QVariant& value, Okteta::AbstractByteArrayModel* out,
0051                  Okteta::Address address, BitCount64 bitsRemaining, quint8 bitOffset) override;
0052     bool setChildData(uint row, const QVariant& value, Okteta::AbstractByteArrayModel* out,
0053                       Okteta::Address address, BitCount64 bitsRemaining, quint8 bitOffset) override;
0054 
0055     Qt::ItemFlags childFlags(int row, int column, bool fileLoaded = true) const override;
0056     QVariant childData(int row, int column, int role) const override;
0057     DataInformation* childAt(unsigned int idx) const override;
0058     unsigned int childCount() const override;
0059     QString childTypeName(uint index) const override;
0060     QString childString(uint index) const override;
0061     int indexOf(const DataInformation* const data) const override;
0062     BitCount32 childSize(uint index) const override;
0063 
0064     bool isArray() const override;
0065 
0066     bool setArrayLength(uint newLength);
0067     /** Sets the new array type
0068      * @param newChildtype the new type (ownership is always taken, do not use anymore after this call!)
0069      */
0070     void setArrayType(DataInformation* newChildtype);
0071     DataInformation* arrayType() const;
0072 
0073     QScriptValue childType() const;
0074     QScriptValue lengthFunction() const;
0075     void setLengthFunction(const QScriptValue& newFunc);
0076     QScriptValue childToScriptValue(uint index, QScriptEngine* engine, ScriptHandlerInfo* handlerInfo) const override;
0077     BitCount64 childPosition(const DataInformation* child, Okteta::Address start) const override;
0078 
0079 private:
0080     QScriptClass* scriptClass(ScriptHandlerInfo* handlerInfo) const override;
0081     QString typeNameImpl() const override;
0082     QString valueStringImpl() const override;
0083 
0084 private:
0085     std::unique_ptr<AbstractArrayData> mData;
0086     // Limit the length to 64K to avoid excessive memory consumption
0087     static constexpr uint MAX_LEN = 65535;
0088 };
0089 
0090 inline uint ArrayDataInformation::length() const
0091 {
0092     return mData->length();
0093 }
0094 
0095 inline DataInformation* ArrayDataInformation::arrayType() const
0096 {
0097     return mData->childType();
0098 }
0099 
0100 inline QScriptValue ArrayDataInformation::lengthFunction() const
0101 {
0102     return mAdditionalData.get(AdditionalData::AdditionalDataType::ArrayLengthFunction).value<QScriptValue>();
0103 }
0104 
0105 inline void ArrayDataInformation::setLengthFunction(const QScriptValue& newFunc)
0106 {
0107     setAdditionalFunction(AdditionalData::AdditionalDataType::ArrayLengthFunction, newFunc, "array length function");
0108 }
0109 
0110 #endif /* KASTEN_ARRAYDATAINFORMATION_HPP */