File indexing completed on 2025-01-05 05:23:43

0001 /*
0002     This file is part of the Okteta Kasten Framework, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 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_DATAINFORMATIONFACTORY_HPP
0010 #define KASTEN_DATAINFORMATIONFACTORY_HPP
0011 
0012 #include "../datatypes/primitive/bitfield/abstractbitfielddatainformation.hpp"
0013 #include "../datatypes/primitive/primitivedatainformation.hpp"
0014 #include "../datatypes/primitive/enumdefinition.hpp"
0015 #include "../datatypes/primitive/flagdatainformation.hpp"
0016 #include "../datatypes/primitive/enumdatainformation.hpp"
0017 #include "../datatypes/primitive/pointerdatainformation.hpp"
0018 #include "../datatypes/array/arraydatainformation.hpp"
0019 #include "../datatypes/strings/stringdatainformation.hpp"
0020 #include "../datatypes/taggeduniondatainformation.hpp"
0021 #include "../datatypes/uniondatainformation.hpp"
0022 #include "../datatypes/structuredatainformation.hpp"
0023 
0024 #include "../script/scriptlogger.hpp"
0025 #include "parserutils.hpp"
0026 // Qt
0027 #include <QScriptValue>
0028 #include <QSharedPointer>
0029 // Std
0030 #include <memory>
0031 
0032 struct CommonParsedData : public ParserInfo
0033 {
0034     inline explicit CommonParsedData(const ParserInfo& i)
0035         : ParserInfo(i)
0036         , endianess(DataInformation::DataInformationEndianess::EndianessInherit)
0037     {}
0038     CommonParsedData(const CommonParsedData&) = delete;
0039 
0040     ~CommonParsedData() = default;
0041 
0042     CommonParsedData& operator=(const CommonParsedData&) = delete;
0043 
0044     QScriptValue updateFunc;
0045     QScriptValue validationFunc;
0046     QScriptValue toStringFunc;
0047     QString customTypeName;
0048     DataInformation::DataInformationEndianess endianess;
0049 };
0050 
0051 struct BitfieldParsedData : public ParserInfo
0052 {
0053     inline explicit BitfieldParsedData(const ParserInfo& i) : ParserInfo(i) {}
0054     BitfieldParsedData(const BitfieldParsedData&) = delete;
0055 
0056     ~BitfieldParsedData() = default;
0057 
0058     BitfieldParsedData& operator=(const BitfieldParsedData&) = delete;
0059 
0060     QString type;
0061     ParsedNumber<int> width;
0062 };
0063 
0064 struct PrimitiveParsedData : public ParserInfo
0065 {
0066     inline explicit PrimitiveParsedData(const ParserInfo& i) : ParserInfo(i) {}
0067     PrimitiveParsedData(const PrimitiveParsedData&) = delete;
0068 
0069     ~PrimitiveParsedData() = default;
0070 
0071     PrimitiveParsedData& operator=(const PrimitiveParsedData&) = delete;
0072 
0073     QString type;
0074 };
0075 
0076 struct EnumParsedData : public ParserInfo
0077 {
0078     inline explicit EnumParsedData(const ParserInfo& i) : ParserInfo(i) {}
0079     EnumParsedData(const EnumParsedData&) = delete;
0080 
0081     ~EnumParsedData() = default;
0082 
0083     EnumParsedData& operator=(const EnumParsedData&) = delete;
0084 
0085     QString type;
0086     QString enumName;
0087     EnumDefinition::Ptr enumDef; // TODO QMap<QString, QScriptValue> instead
0088     /** only used if enumDef is null, to allow sharing (only possible in OSD) */
0089     QScriptValue enumValuesObject;
0090 };
0091 
0092 struct StringParsedData : public ParserInfo
0093 {
0094     inline explicit StringParsedData(const ParserInfo& i) : ParserInfo(i) {}
0095     StringParsedData(const StringParsedData&) = delete;
0096 
0097     ~StringParsedData() = default;
0098 
0099     StringParsedData& operator=(const StringParsedData&) = delete;
0100 
0101     QString encoding;
0102     ParsedNumber<quint32> termination;
0103     ParsedNumber<quint32> maxCharCount;
0104     ParsedNumber<quint32> maxByteCount;
0105 };
0106 
0107 struct ArrayParsedData : public ParserInfo
0108 {
0109     inline explicit ArrayParsedData(const ParserInfo& i)
0110         : ParserInfo(i)
0111         , arrayType(nullptr)
0112     {}
0113     ArrayParsedData(const ArrayParsedData&) = delete;
0114 
0115     ~ArrayParsedData() = default;
0116 
0117     ArrayParsedData& operator=(const ArrayParsedData&) = delete;
0118 
0119     QScriptValue length;
0120     DataInformation* arrayType;
0121 };
0122 
0123 struct PointerParsedData : public ParserInfo
0124 {
0125     inline explicit PointerParsedData(const ParserInfo& i)
0126         : ParserInfo(i)
0127         , valueType(nullptr)
0128         , pointerTarget(nullptr)
0129         , pointerScale(1)
0130     {}
0131     PointerParsedData(const PointerParsedData&) = delete;
0132 
0133     ~PointerParsedData() = default;
0134 
0135     PointerParsedData& operator=(const PointerParsedData&) = delete;
0136 
0137     DataInformation* valueType;
0138     DataInformation* pointerTarget;
0139     qint64           pointerScale;
0140     QScriptValue     interpretFunc;
0141 };
0142 
0143 struct TaggedUnionParsedData : public ParserInfo
0144 {
0145     struct Alternatives
0146     {
0147         QString name;
0148         QScriptValue selectIf;
0149         QSharedPointer<ChildrenParser> fields;
0150     };
0151     inline explicit TaggedUnionParsedData(const ParserInfo& i) : ParserInfo(i) {}
0152     TaggedUnionParsedData(const TaggedUnionParsedData&) = delete;
0153 
0154     ~TaggedUnionParsedData() = default;
0155 
0156     TaggedUnionParsedData& operator=(const TaggedUnionParsedData&) = delete;
0157 
0158     std::unique_ptr<ChildrenParser> children;
0159     QVector<Alternatives> alternatives;
0160     std::unique_ptr<ChildrenParser> defaultFields;
0161 };
0162 
0163 struct StructOrUnionParsedData : public ParserInfo
0164 {
0165     inline explicit StructOrUnionParsedData(const ParserInfo& i) : ParserInfo(i) {}
0166     StructOrUnionParsedData(const StructOrUnionParsedData&) = delete;
0167 
0168     ~StructOrUnionParsedData() = default;
0169 
0170     StructOrUnionParsedData& operator=(const StructOrUnionParsedData&) = delete;
0171 
0172     std::unique_ptr<ChildrenParser> children;
0173 };
0174 
0175 namespace DataInformationFactory {
0176 AbstractBitfieldDataInformation* newBitfield(const BitfieldParsedData& pd);
0177 PrimitiveDataInformation* newPrimitive(const PrimitiveParsedData& pd);
0178 EnumDataInformation* newEnum(const EnumParsedData& pd);
0179 FlagDataInformation* newFlags(const EnumParsedData& pd);
0180 ArrayDataInformation* newArray(const ArrayParsedData& pd);
0181 StringDataInformation* newString(const StringParsedData& pd);
0182 PointerDataInformation* newPointer(const PointerParsedData& pd);
0183 UnionDataInformation* newUnion(const StructOrUnionParsedData& pd);
0184 StructureDataInformation* newStruct(const StructOrUnionParsedData& pd);
0185 TaggedUnionDataInformation* newTaggedUnion(const TaggedUnionParsedData& pd);
0186 
0187 bool commonInitialization(DataInformation* data, const CommonParsedData& pd);
0188 }
0189 
0190 #endif /* KASTEN_DATAINFORMATIONFACTORY_HPP */