305 lines
8.3 KiB
C++
305 lines
8.3 KiB
C++
#pragma once
|
||
|
||
#include "common.h"
|
||
#include <QHash>
|
||
|
||
class SizeProvider;
|
||
class RuleMatch;
|
||
class ScopeFieldsSetter;
|
||
class DataAccessContext;
|
||
|
||
|
||
/// <summary>
|
||
/// 翻译单元抽象接口
|
||
/// </summary>
|
||
class ExtractUnit : public Serializable {
|
||
public:
|
||
|
||
/// <summary>
|
||
/// 单元名称
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
virtual QString name() const = 0;
|
||
|
||
/// <summary>
|
||
/// 注册自身
|
||
/// </summary>
|
||
/// <param name="inst"></param>
|
||
virtual void registSubField(std::shared_ptr<ScopeFieldsSetter> inst) = 0;
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
virtual DataType outType() const = 0;
|
||
/// <summary>
|
||
/// 设置偏移字节数量
|
||
/// </summary>
|
||
/// <param name="bytes"></param>
|
||
virtual bool setOffsetSpan(int bytes) = 0;
|
||
/// <summary>
|
||
/// 从上一个单元字节偏移字节数量
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
virtual int offsetSpan() const = 0;
|
||
/// <summary>
|
||
/// 解析所用的字节数量
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
virtual int countWithin() const = 0;
|
||
|
||
/// <summary>
|
||
/// 执行解析过程
|
||
/// </summary>
|
||
/// <param name="bytes"></param>
|
||
/// <returns></returns>
|
||
virtual void parse(const QByteArray& bytes, std::shared_ptr<DataAccessContext> out) const = 0;
|
||
};
|
||
|
||
namespace extract {
|
||
class AbstractExtractor : public ExtractUnit {
|
||
public:
|
||
struct __Private {
|
||
QString name_store = "";
|
||
DataType type_store = DataType::Integer;
|
||
int byte_offset = 0, byte_count = 1;
|
||
} _abs_data;
|
||
|
||
AbstractExtractor(const QString& name, DataType data);
|
||
virtual bool setCountWithin(int bytes);
|
||
|
||
// ExtractUnit ===========================
|
||
QString name() const override;
|
||
void registSubField(std::shared_ptr<ScopeFieldsSetter> inst) override;
|
||
virtual DataType outType() const;
|
||
/// <summary>
|
||
/// 设置偏移字节数量
|
||
/// </summary>
|
||
/// <param name="bytes"></param>
|
||
virtual bool setOffsetSpan(int bytes);
|
||
int offsetSpan() const override;
|
||
int countWithin() const override;
|
||
|
||
// Serializable ==============================
|
||
void loadFrom(std::shared_ptr<TranslateBasic> core, const QJsonObject& obj) override;
|
||
void saveTo(QJsonObject& obj) const override;
|
||
};
|
||
|
||
/// <summary>
|
||
/// 转换源数值未16进制数值,默认分割方式
|
||
/// </summary>
|
||
class AsHex : public AbstractExtractor {
|
||
public:
|
||
static QString topic();
|
||
AsHex();
|
||
|
||
void parse(const QByteArray& bytes, std::shared_ptr<DataAccessContext> out) const override;
|
||
std::shared_ptr<Serializable> newDefault() const override;
|
||
};
|
||
|
||
/// <summary>
|
||
/// 转换源数值为位联合
|
||
/// </summary>
|
||
class AsBitCombine : public AbstractExtractor {
|
||
public:
|
||
struct __Private {
|
||
QHash<int, QString> _switch_options;
|
||
} _combine;
|
||
|
||
static QString topic();
|
||
AsBitCombine();
|
||
|
||
bool setSwitchOption(int bit_index, const QString& keyword);
|
||
QHash<int, QString> switchOptions() const;
|
||
virtual void clearOptions();
|
||
|
||
// ExtractUnit ============================
|
||
void parse(const QByteArray& bytes, std::shared_ptr<DataAccessContext> out) const override;
|
||
|
||
// Serializable ==============================
|
||
void loadFrom(std::shared_ptr<TranslateBasic> core, const QJsonObject& obj) override;
|
||
void saveTo(QJsonObject& obj) const override;
|
||
std::shared_ptr<Serializable> newDefault() const override;
|
||
};
|
||
|
||
/// <summary>
|
||
/// 转换源数据为整形
|
||
/// </summary>
|
||
class AsInteger : public AbstractExtractor {
|
||
public:
|
||
static QString topic();
|
||
AsInteger();
|
||
|
||
// ExtractUnit ============================
|
||
bool setCountWithin(int bytes) override;
|
||
void parse(const QByteArray& bytes, std::shared_ptr<DataAccessContext> out) const override;
|
||
|
||
// Serializable ==============================
|
||
std::shared_ptr<Serializable> newDefault() const override;
|
||
};
|
||
|
||
/// <summary>
|
||
/// 转换源数据为整形
|
||
/// </summary>
|
||
class AsUnsigned : public AbstractExtractor {
|
||
public:
|
||
static QString topic();
|
||
AsUnsigned();
|
||
|
||
// ExtractUnit ============================
|
||
bool setCountWithin(int bytes) override;
|
||
void parse(const QByteArray& bytes, std::shared_ptr<DataAccessContext> out) const override;
|
||
|
||
// Serializable ==============================
|
||
std::shared_ptr<Serializable> newDefault() const override;
|
||
};
|
||
|
||
/// <summary>
|
||
/// 转换源数据为字符串
|
||
/// </summary>
|
||
class AsString : public AbstractExtractor {
|
||
public:
|
||
struct __Private {
|
||
QTextCodec* _conv_with = nullptr;
|
||
} _strings;
|
||
|
||
public:
|
||
static QString topic();
|
||
AsString();
|
||
|
||
void setStrCodec(QTextCodec* ins);
|
||
QString codecName() const;
|
||
|
||
// ExtractUnit ============================
|
||
void parse(const QByteArray& bytes, std::shared_ptr<DataAccessContext> out) const override;
|
||
|
||
// Serializable ==============================
|
||
void loadFrom(std::shared_ptr<TranslateBasic> core, const QJsonObject& obj) override;
|
||
void saveTo(QJsonObject& obj) const override;
|
||
std::shared_ptr<Serializable> newDefault() const override;
|
||
};
|
||
|
||
class AsFloat : public AbstractExtractor {
|
||
public:
|
||
static QString topic();
|
||
AsFloat();
|
||
|
||
void parse(const QByteArray& bytes, std::shared_ptr<DataAccessContext> out) const override;
|
||
std::shared_ptr<Serializable> newDefault() const override;
|
||
};
|
||
|
||
class AsDouble : public AbstractExtractor {
|
||
public:
|
||
static QString topic();
|
||
AsDouble();
|
||
|
||
void parse(const QByteArray& bytes, std::shared_ptr<DataAccessContext> out) const override;
|
||
std::shared_ptr<Serializable> newDefault() const override;
|
||
};
|
||
|
||
|
||
class AsList : public ExtractUnit {
|
||
public:
|
||
struct __Private {
|
||
std::shared_ptr<ExtractUnit> _bind_unit = nullptr;
|
||
int _bytes_offset = 0;
|
||
std::shared_ptr<SizeProvider> _bind_size_v = nullptr;
|
||
} _list;
|
||
|
||
public:
|
||
static QString topic();
|
||
bool setOffsetSpan(int value);
|
||
std::shared_ptr<ExtractUnit> elementRule() const;
|
||
bool setElementRule(std::shared_ptr<ExtractUnit> u);
|
||
std::shared_ptr<SizeProvider> sizeProvider() const;
|
||
bool setSizeProvider(std::shared_ptr<SizeProvider> ins);
|
||
|
||
public:
|
||
QString name() const override;
|
||
void registSubField(std::shared_ptr<ScopeFieldsSetter> inst) override;
|
||
DataType outType() const override;
|
||
int offsetSpan() const override;
|
||
int countWithin() const override;
|
||
|
||
void loadFrom(std::shared_ptr<TranslateBasic> core, const QJsonObject& obj) override;
|
||
void saveTo(QJsonObject& obj) const override;
|
||
|
||
void parse(const QByteArray& bytes, std::shared_ptr<DataAccessContext> out) const override;
|
||
std::shared_ptr<Serializable> newDefault() const override;
|
||
|
||
};
|
||
|
||
class AsUnion : public ExtractUnit {
|
||
public:
|
||
struct __Private {
|
||
int byte_offset = 0, byte_count = 1;
|
||
QList<std::shared_ptr<RuleMatch>> _rule_list;
|
||
} _union;
|
||
|
||
public:
|
||
static QString topic();
|
||
bool setCountWithin(int bytes);
|
||
|
||
void clearRules();
|
||
QList<std::shared_ptr<RuleMatch>> elementRules() const;
|
||
bool appendElementRule(std::shared_ptr<RuleMatch> u);
|
||
|
||
public:
|
||
QString name() const override;
|
||
void registSubField(std::shared_ptr<ScopeFieldsSetter> inst) override;
|
||
DataType outType() const override;
|
||
bool setOffsetSpan(int value);
|
||
int offsetSpan() const override;
|
||
int countWithin() const override;
|
||
|
||
void parse(const QByteArray& bytes, std::shared_ptr<DataAccessContext> out) const override;
|
||
void loadFrom(std::shared_ptr<TranslateBasic> core, const QJsonObject& obj) override;
|
||
void saveTo(QJsonObject& obj) const override;
|
||
|
||
std::shared_ptr<Serializable> newDefault() const override;
|
||
};
|
||
|
||
|
||
class AsRuleSet : public ExtractUnit {
|
||
private:
|
||
struct __Private {
|
||
int _byte_offset = 0, _byte_count = 1;
|
||
QString _name_store;
|
||
QList<std::pair<QString, std::shared_ptr<ExtractUnit>>> sub_units;
|
||
|
||
}_bind;
|
||
|
||
public:
|
||
static QString topic();
|
||
|
||
void setCustomName(const QString& name);
|
||
QString customName() const;
|
||
|
||
QList<QString> fieldNames() const;
|
||
std::shared_ptr<ExtractUnit> operator[](const QString& field) const;
|
||
|
||
int memberCount() const;
|
||
std::pair<QString, std::shared_ptr<ExtractUnit>> operator[](int index) const;
|
||
void replace(int index, std::pair<QString, std::shared_ptr<ExtractUnit>> inst);
|
||
void removeAt(int index);
|
||
AsRuleSet& append(const QString& nm, std::shared_ptr<ExtractUnit> u);
|
||
|
||
public:
|
||
QString name() const override;
|
||
void registSubField(std::shared_ptr<ScopeFieldsSetter> inst) override;
|
||
DataType outType() const override;
|
||
bool setOffsetSpan(int bytes) override;
|
||
int offsetSpan() const override;
|
||
int countWithin() const override;
|
||
std::shared_ptr<Serializable> newDefault() const override;
|
||
|
||
void parse(const QByteArray& bytes, std::shared_ptr<DataAccessContext> out) const override;
|
||
|
||
void loadFrom(std::shared_ptr<TranslateBasic> core, const QJsonObject& obj) override;
|
||
void saveTo(QJsonObject& obj) const override;
|
||
|
||
};
|
||
}
|
||
|
||
|