diff --git a/TranslateUI/SequenceView.cpp b/TranslateUI/SequenceView.cpp index 045b4bf..04bda91 100644 --- a/TranslateUI/SequenceView.cpp +++ b/TranslateUI/SequenceView.cpp @@ -71,10 +71,10 @@ void RuleSetCustom::peersRuleChanged(const QModelIndex& idx) _view._ruleset->replace(idx.row(), std::make_pair(field_name, new_inst)); // 对ListUnit执行特殊初始化步骤 - if (typeid(*new_inst.get()) == typeid(BytesAsList)) { - auto conv = std::dynamic_pointer_cast(new_inst); + if (typeid(*new_inst.get()) == typeid(AsList)) { + auto conv = std::dynamic_pointer_cast(new_inst); if (!conv->elementRule()) { - conv->appendElementRule(this->_view.base->defaultExtractUnit()); + conv->setElementRule(this->_view.base->defaultExtractUnit()); } if (!conv->sizeProvider()) { @@ -93,7 +93,7 @@ void RuleSetCustom::showEvent(QShowEvent* e) } RuleSetCustom::RuleSetCustom(std::shared_ptr base, - std::shared_ptr rule_set, QWidget* p /*= nullptr*/) + std::shared_ptr rule_set, QWidget* p /*= nullptr*/) :QTableView(p) { _view._ruleset = rule_set; @@ -138,7 +138,7 @@ void RuleSetCustom::addTranslateUnit() QString(u8"rule_%1").arg(_view._ruleset->memberCount()), std::static_pointer_cast(curr_rule)); - auto hex_rule = std::static_pointer_cast(curr_rule); + auto hex_rule = std::static_pointer_cast(curr_rule); hex_rule->setCountWithin(1); membersPresent(this->_view._ruleset, this->_view._seqs_model); @@ -154,7 +154,7 @@ void RuleSetCustom::removeTranslateUnit() _view._seqs_model->removeRow(idx_curr.row()); } -void RuleSetCustom::membersPresent(std::shared_ptr ruleset, QStandardItemModel* model) +void RuleSetCustom::membersPresent(std::shared_ptr ruleset, QStandardItemModel* model) { model->removeRows(0, model->rowCount()); diff --git a/TranslateUI/SequenceView.h b/TranslateUI/SequenceView.h index f66464f..825ed68 100644 --- a/TranslateUI/SequenceView.h +++ b/TranslateUI/SequenceView.h @@ -58,7 +58,7 @@ private: struct __Private { QStandardItemModel* _seqs_model; std::shared_ptr base = nullptr; - std::shared_ptr _ruleset = nullptr; + std::shared_ptr _ruleset = nullptr; } _view; signals: @@ -73,12 +73,12 @@ protected: public: RuleSetCustom(std::shared_ptr base, - std::shared_ptr rule_set, QWidget* p = nullptr); + std::shared_ptr rule_set, QWidget* p = nullptr); void customTranslateRuleEdit(const QPoint& pos); void addTranslateUnit(); void removeTranslateUnit(); - void membersPresent(std::shared_ptr _bind, QStandardItemModel* model); + void membersPresent(std::shared_ptr _bind, QStandardItemModel* model); }; diff --git a/TranslateUI/StructuralRuleView.cpp b/TranslateUI/StructuralRuleView.cpp index 1923488..08e5536 100644 --- a/TranslateUI/StructuralRuleView.cpp +++ b/TranslateUI/StructuralRuleView.cpp @@ -13,7 +13,7 @@ using namespace size_provider; ExtractRuleView::ExtractRuleView(std::shared_ptr base, - std::shared_ptr inst_r, QWidget* p /*= nullptr*/) + std::shared_ptr inst_r, QWidget* p /*= nullptr*/) : QWidget(p), _rule_base(inst_r), _sequence_view(new RuleSetCustom(base, inst_r, this)), _configs_stack(new QStackedWidget(this)) @@ -43,17 +43,19 @@ ExtractRuleView::ExtractRuleView(std::shared_ptr base, _sequence_view, &RuleSetCustom::targetRuleRefresh); connect(combine_config, &BitCombineConfiguration::currentRuleChanged, _sequence_view, &RuleSetCustom::targetRuleRefresh); + connect(list_config, &ListUnitConfiguration::currentRuleChanged, + _sequence_view, &RuleSetCustom::targetRuleRefresh); connect(_sequence_view, &RuleSetCustom::currentRuleChanged, [=](std::shared_ptr u, const QModelIndex& i) { switch (u->outType()) { case DataType::TextString: - if (typeid(*u.get()) == typeid(extract::BytesAsString)) { + if (typeid(*u.get()) == typeid(extract::AsString)) { _configs_stack->setCurrentIndex(2); encode_config->currentRuleAccept(u, i); break; } - else if (typeid(*u.get()) == typeid(extract::BytesAsBitCombine)) { + else if (typeid(*u.get()) == typeid(extract::AsBitCombine)) { _configs_stack->setCurrentIndex(3); combine_config->currentRuleAccept(u, i); break; @@ -86,7 +88,7 @@ void ExtractRuleView::cacheRefresh(const QModelIndex& curr, ListUnitConfiguratio this->_current_fields_cache->bindCurrent(field_idx.data(Qt::DisplayRole).toString()); auto ins = this->_rule_base->operator[](curr.row()); - auto conv = std::dynamic_pointer_cast(ins.second); + auto conv = std::dynamic_pointer_cast(ins.second); if (conv) { t->currentRuleAccept(conv, curr, this->_current_fields_cache); } @@ -160,7 +162,7 @@ EncodingConfiguration::EncodingConfiguration(QWidget* p /*= nullptr*/) connect(_encoding_set, QOverload::of(&QComboBox::currentIndexChanged), [=](const QString& value) { if (this->_bind_u) { - auto ptr = std::dynamic_pointer_cast(this->_bind_u); + auto ptr = std::dynamic_pointer_cast(this->_bind_u); if (ptr) { auto insx = QTextCodec::codecForName(value.toUtf8()); ptr->setStrCodec(insx); @@ -176,7 +178,7 @@ void EncodingConfiguration::currentRuleAccept(std::shared_ptr u, co this->_bind_index = i; _count_input->setValue(u->countWithin()); - auto codec_name = std::dynamic_pointer_cast(u)->codecName(); + auto codec_name = std::dynamic_pointer_cast(u)->codecName(); QSignalBlocker x(this->_encoding_set); this->_encoding_set->setCurrentText(codec_name); @@ -246,13 +248,13 @@ BitCombineConfiguration::BitCombineConfiguration(QWidget* p /*= nullptr*/) void BitCombineConfiguration::currentRuleAccept(std::shared_ptr u, const QModelIndex& i) { - this->_bind_u = std::dynamic_pointer_cast(u);; + this->_bind_u = std::dynamic_pointer_cast(u);; this->_bind_index = i; this->reloadContent(this->_bind_u); } -void BitCombineConfiguration::reloadContent(std::shared_ptr u) +void BitCombineConfiguration::reloadContent(std::shared_ptr u) { this->_count_input->setValue(u->countWithin()); this->_index_appoint->setRange(0, u->countWithin() * 8); @@ -306,7 +308,8 @@ ListUnitConfiguration::ListUnitConfiguration( for (auto key : this->_bind_core->customRules().keys()) _rule_select->addItem(key, CUSTOM_RULE); for (auto key : this->_bind_core->extractUnitList().keys()) - _rule_select->addItem(key, BASIC_RULE); + if (!QList{"AsList", "AsUnion", "AsRuleSet"}.contains(key)) + _rule_select->addItem(key, BASIC_RULE); auto keys = this->_bind_core->sizeProviderList().keys(); std::sort(keys.begin(), keys.end()); @@ -316,41 +319,59 @@ ListUnitConfiguration::ListUnitConfiguration( [=](int idx) { this->_configs_stack->setCurrentIndex(this->_size_layout_select->currentIndex()); }); + connect(this->_rule_select, &QComboBox::currentTextChanged, + this, &ListUnitConfiguration::elmRuleSet); } -void ListUnitConfiguration::currentRuleAccept( - std::shared_ptr u, const QModelIndex& i, - std::shared_ptr getter) +void ListUnitConfiguration::currentRuleAccept(std::shared_ptr u, + const QModelIndex& i, std::shared_ptr getter) { - this->_bind_u = std::dynamic_pointer_cast(u); + this->_bind_u = std::dynamic_pointer_cast(u); this->_bind_index = i; reloadContent(this->_bind_u, getter); } -void ListUnitConfiguration::reloadContent( - std::shared_ptr u, +void ListUnitConfiguration::reloadContent(std::shared_ptr u, std::shared_ptr getter) { QSignalBlocker v(_rule_select); _rule_select->setCurrentText(u->elementRule()->name()); QSignalBlocker m(_size_layout_select); _size_layout_select->setCurrentText(u->sizeProvider()->name()); - QSignalBlocker vll(_prev_field_refer); auto item_list = getter->prevFields(); this->_prev_field_refer->clear(); this->_prev_field_refer->addItems(item_list.keys()); - if (typeid(*u->sizeProvider().get()) == typeid(ConstNumberProvider)) { + auto const_conv = std::dynamic_pointer_cast(u->sizeProvider()); + if (const_conv) { _configs_stack->setCurrentIndex(0); + QSignalBlocker v(_const_number_input); - auto conv = std::dynamic_pointer_cast(u->sizeProvider()); - _const_number_input->setValue(conv->value(conv->expression())); + _const_number_input->setValue(const_conv->value(const_conv->expression())); } - else if (typeid(*u->sizeProvider().get()) == typeid(InterpretedNumberPrivider)) { + + auto runtime_conv = std::dynamic_pointer_cast(u->sizeProvider()); + if (runtime_conv) { _configs_stack->setCurrentIndex(1); - auto conv = std::dynamic_pointer_cast(u->sizeProvider()); - this->_prev_field_refer->setCurrentText(conv->name()); + this->_prev_field_refer->setCurrentText(runtime_conv->name()); } } + +void ListUnitConfiguration::elmRuleSet(const QString& name) +{ + auto type_mark = this->_rule_select->currentData().toInt(); + switch (type_mark) { + case CUSTOM_RULE: { + auto target_rule = this->_bind_core->customRules()[name]; + _bind_u->setElementRule(target_rule); + }break; + default: { + auto target_unit = this->_bind_core->extractUnitList()[name]; + _bind_u->setElementRule(target_unit); + }break; + } + + emit this->currentRuleChanged(this->_bind_index); +} diff --git a/TranslateUI/StructuralRuleView.h b/TranslateUI/StructuralRuleView.h index 084c58d..cffff74 100644 --- a/TranslateUI/StructuralRuleView.h +++ b/TranslateUI/StructuralRuleView.h @@ -64,7 +64,7 @@ namespace configuration_panel { class BitCombineConfiguration : public QWidget { Q_OBJECT private: - std::shared_ptr _bind_u; + std::shared_ptr _bind_u; QModelIndex _bind_index; QSpinBox* const _count_input; @@ -77,7 +77,7 @@ namespace configuration_panel { BitCombineConfiguration(QWidget* p = nullptr); void currentRuleAccept(std::shared_ptr u, const QModelIndex& i); - void reloadContent(std::shared_ptr u); + void reloadContent(std::shared_ptr u); signals: void currentRuleChanged(const QModelIndex& idx); @@ -90,7 +90,7 @@ namespace configuration_panel { Q_OBJECT private: std::shared_ptr _bind_core; - std::shared_ptr _bind_u; + std::shared_ptr _bind_u; QModelIndex _bind_index; QComboBox* const _rule_select; @@ -100,6 +100,8 @@ namespace configuration_panel { QSpinBox* const _const_number_input; QComboBox* const _prev_field_refer; + void elmRuleSet(const QString &name); + public: ListUnitConfiguration(std::shared_ptr core, QWidget* p = nullptr); @@ -107,7 +109,7 @@ namespace configuration_panel { std::shared_ptr u, const QModelIndex& i, std::shared_ptr getter); - void reloadContent(std::shared_ptr u, + void reloadContent(std::shared_ptr u, std::shared_ptr getter); signals: @@ -121,12 +123,12 @@ private: RuleSetCustom* const _sequence_view; QStackedWidget* const _configs_stack; - std::shared_ptr _rule_base; + std::shared_ptr _rule_base; std::shared_ptr _current_fields_cache = nullptr; public: ExtractRuleView(std::shared_ptr base, - std::shared_ptr inst_r, QWidget* p = nullptr); + std::shared_ptr inst_r, QWidget* p = nullptr); void cacheRefresh(const QModelIndex& curr, configuration_panel::ListUnitConfiguration* t); }; diff --git a/TranslateUI/TranslateBasic.cpp b/TranslateUI/TranslateBasic.cpp index 2ec4fe9..44ae866 100644 --- a/TranslateUI/TranslateBasic.cpp +++ b/TranslateUI/TranslateBasic.cpp @@ -6,35 +6,35 @@ using namespace size_provider; TranslateBasic::TranslateBasic() { // extractor type-list - std::shared_ptr u_ptr = std::make_shared(); + std::shared_ptr u_ptr = std::make_shared(); _extractor_types[u_ptr->name()] = u_ptr; - u_ptr = std::make_shared(); + u_ptr = std::make_shared(); _extractor_types[u_ptr->name()] = u_ptr; - u_ptr = std::make_shared(); + u_ptr = std::make_shared(); _extractor_types[u_ptr->name()] = u_ptr; - u_ptr = std::make_shared(); + u_ptr = std::make_shared(); _extractor_types[u_ptr->name()] = u_ptr; - u_ptr = std::make_shared(); + u_ptr = std::make_shared(); _extractor_types[u_ptr->name()] = u_ptr; this->_default_translate_rule = u_ptr; - u_ptr = std::make_shared(); + u_ptr = std::make_shared(); _extractor_types[u_ptr->name()] = u_ptr; - u_ptr = std::make_shared(); + u_ptr = std::make_shared(); _extractor_types[u_ptr->name()] = u_ptr; - u_ptr = std::make_shared(); + u_ptr = std::make_shared(); _extractor_types[u_ptr->name()] = u_ptr; - u_ptr = std::make_shared(); + u_ptr = std::make_shared(); _extractor_types[u_ptr->name()] = u_ptr; - u_ptr = std::make_shared(); + u_ptr = std::make_shared(); _extractor_types[u_ptr->name()] = u_ptr; // size-provider type-list @@ -66,11 +66,11 @@ QHash> TranslateBasic::extractUnitList() c return _extractor_types; } -void TranslateBasic::setCustomRule(const QString& name, std::shared_ptr inst) +void TranslateBasic::setCustomRule(const QString& name, std::shared_ptr inst) { auto exist = std::count_if( this->_custom_rule_types.begin(), this->_custom_rule_types.end(), - [=](std::shared_ptr v) { + [=](std::shared_ptr v) { return v->name() == name; }); assert("RuleName不能重复" && !exist); @@ -92,9 +92,9 @@ void TranslateBasic::removeCustomRule(const QString& name) assert("指定RuleName不合法" && 0); } -QHash> TranslateBasic::customRules() const +QHash> TranslateBasic::customRules() const { - QHash> value_set; + QHash> value_set; for (auto instv : this->_custom_rule_types) value_set[instv->customName()] = instv; return value_set; diff --git a/TranslateUI/TranslateBasic.h b/TranslateUI/TranslateBasic.h index 3e7ca46..b5865ca 100644 --- a/TranslateUI/TranslateBasic.h +++ b/TranslateUI/TranslateBasic.h @@ -113,7 +113,7 @@ private: QHash> _rule_match_types; std::shared_ptr _default_rule_match = nullptr; - QList> _custom_rule_types; + QList> _custom_rule_types; public: TranslateBasic(); @@ -126,9 +126,9 @@ public: std::shared_ptr defaultExtractUnit() const; QHash> extractUnitList() const; - void setCustomRule(const QString& name, std::shared_ptr inst); + void setCustomRule(const QString& name, std::shared_ptr inst); void removeCustomRule(const QString& name); - QHash> customRules() const; + QHash> customRules() const; }; namespace size_provider { diff --git a/TranslateUI/TranslateUI.cpp b/TranslateUI/TranslateUI.cpp index d8c2dd5..1f4da9b 100644 --- a/TranslateUI/TranslateUI.cpp +++ b/TranslateUI/TranslateUI.cpp @@ -62,7 +62,7 @@ void TranslateUI::addCustomRule() return; } - auto new_rule = std::make_shared(); + auto new_rule = std::make_shared(); new_rule->setCustomName(name); _core._translate_core->setCustomRule(name, new_rule); diff --git a/TranslateUI/TranslateUI.h b/TranslateUI/TranslateUI.h index 448c4a0..0c2c2c8 100644 --- a/TranslateUI/TranslateUI.h +++ b/TranslateUI/TranslateUI.h @@ -18,7 +18,7 @@ class ViewManager : public QObject { Q_OBJECT private: std::shared_ptr _translate_core = nullptr; - QList, QWidget*, ViewType>> content_pages; + QList, QWidget*, ViewType>> content_pages; public: ViewManager(std::shared_ptr base, QObject* p = nullptr); diff --git a/TranslateUI/extract_basic.cpp b/TranslateUI/extract_basic.cpp index 9f0af99..4e8e8f9 100644 --- a/TranslateUI/extract_basic.cpp +++ b/TranslateUI/extract_basic.cpp @@ -60,13 +60,12 @@ void AbstractExtractor::registSubField(std::shared_ptr inst) } -BytesAsHex::BytesAsHex() - : AbstractExtractor(NAME(BytesAsHex), DataType::TextString) { +AsHex::AsHex(): AbstractExtractor(topic(), DataType::TextString) { setCountWithin(1); } #include "TranslateBasic.h" -void BytesAsHex::parse(const QByteArray& bytes, std::shared_ptr out) const +void AsHex::parse(const QByteArray& bytes, std::shared_ptr out) const { QString result; for (auto char_v : bytes) { @@ -75,18 +74,23 @@ void BytesAsHex::parse(const QByteArray& bytes, std::shared_ptrappend(result); } -std::shared_ptr BytesAsHex::newDefault() const +std::shared_ptr AsHex::newDefault() const { - return std::make_shared(); + return std::make_shared(); } -BytesAsBitCombine::BytesAsBitCombine() - : AbstractExtractor(NAME(BytesAsBitCombine), DataType::TextString) +QString AsHex::topic() +{ + return NAME(AsHex); +} + +AsBitCombine::AsBitCombine() + : AbstractExtractor(topic(), DataType::TextString) { } -bool BytesAsBitCombine::setSwitchOption(int bit_index, const QString& keyword) +bool AsBitCombine::setSwitchOption(int bit_index, const QString& keyword) { if (bit_index >= 0 && bit_index <= this->countWithin() * 8) { _combine._switch_options[bit_index] = keyword; @@ -95,17 +99,17 @@ bool BytesAsBitCombine::setSwitchOption(int bit_index, const QString& keyword) return false; } -QHash BytesAsBitCombine::switchOptions() const +QHash AsBitCombine::switchOptions() const { return _combine._switch_options; } -void BytesAsBitCombine::clearOptions() +void AsBitCombine::clearOptions() { _combine._switch_options.clear(); } -void BytesAsBitCombine::parse(const QByteArray& bytes, std::shared_ptr out) const +void AsBitCombine::parse(const QByteArray& bytes, std::shared_ptr out) const { auto keys = _combine._switch_options.keys(); std::sort(keys.begin(), keys.end()); @@ -124,7 +128,7 @@ void BytesAsBitCombine::parse(const QByteArray& bytes, std::shared_ptr -void BytesAsBitCombine::loadFrom( +void AsBitCombine::loadFrom( std::shared_ptr core, const QJsonObject& obj) { AbstractExtractor::loadFrom(core, obj); @@ -140,7 +144,7 @@ void BytesAsBitCombine::loadFrom( } } -void BytesAsBitCombine::saveTo(QJsonObject& obj) const +void AsBitCombine::saveTo(QJsonObject& obj) const { AbstractExtractor::saveTo(obj); @@ -154,27 +158,32 @@ void BytesAsBitCombine::saveTo(QJsonObject& obj) const ARRAY_SAVE(arr, obj); } -std::shared_ptr BytesAsBitCombine::newDefault() const +std::shared_ptr AsBitCombine::newDefault() const { - return std::make_shared(); + return std::make_shared(); } -std::shared_ptr BytesAsInteger::newDefault() const +QString AsBitCombine::topic() { - return std::make_shared(); + return NAME(AsBitCombine); } -BytesAsInteger::BytesAsInteger() - :AbstractExtractor(NAME(BytesAsInteger), DataType::Integer) { +std::shared_ptr AsInteger::newDefault() const +{ + return std::make_shared(); } -bool BytesAsInteger::setCountWithin(int bytes) +AsInteger::AsInteger() + :AbstractExtractor(topic(), DataType::Integer) { +} + +bool AsInteger::setCountWithin(int bytes) { bytes = std::min(8, std::max(bytes, 0)); return AbstractExtractor::setCountWithin(bytes); } -void BytesAsInteger::parse(const QByteArray& bytes, std::shared_ptr out) const +void AsInteger::parse(const QByteArray& bytes, std::shared_ptr out) const { auto last = bytes[bytes.size() - 1]; auto mark = last == 0 ? 0 : last / std::abs(last); @@ -197,30 +206,35 @@ void BytesAsInteger::parse(const QByteArray& bytes, std::shared_ptrappend(*((long long*)(&value))); } +QString AsInteger::topic() +{ + return NAME(AsInteger); +} + #include -BytesAsString::BytesAsString() - :AbstractExtractor(NAME(BytesAsString), DataType::TextString) { +AsString::AsString() + :AbstractExtractor(topic(), DataType::TextString) { _strings._conv_with = QTextCodec::codecForName("GBK"); } -void BytesAsString::setStrCodec(QTextCodec* ins) +void AsString::setStrCodec(QTextCodec* ins) { this->_strings._conv_with = ins; } -QString BytesAsString::codecName() const +QString AsString::codecName() const { return this->_strings._conv_with->name(); } -void BytesAsString::parse(const QByteArray& bytes, std::shared_ptr out) const +void AsString::parse(const QByteArray& bytes, std::shared_ptr out) const { auto v = _strings._conv_with->toUnicode(bytes); out->append(v); } -void BytesAsString::loadFrom(std::shared_ptr core, const QJsonObject& obj) +void AsString::loadFrom(std::shared_ptr core, const QJsonObject& obj) { AbstractExtractor::loadFrom(core, obj); @@ -229,7 +243,7 @@ void BytesAsString::loadFrom(std::shared_ptr core, const QJsonOb this->_strings._conv_with = QTextCodec::codecForName(codec_name.toLatin1()); } -void BytesAsString::saveTo(QJsonObject& obj) const +void AsString::saveTo(QJsonObject& obj) const { AbstractExtractor::saveTo(obj); @@ -237,56 +251,71 @@ void BytesAsString::saveTo(QJsonObject& obj) const STRING_SAVE(codec_name, obj); } -std::shared_ptr BytesAsString::newDefault() const +std::shared_ptr AsString::newDefault() const { - return std::make_shared(); + return std::make_shared(); } -void BytesAsFloat::parse(const QByteArray& bytes, std::shared_ptr out) const +QString AsString::topic() +{ + return NAME(AsString); +} + +void AsFloat::parse(const QByteArray& bytes, std::shared_ptr out) const { out->append(*((float*)bytes.data())); } -std::shared_ptr BytesAsFloat::newDefault() const +std::shared_ptr AsFloat::newDefault() const { - return std::make_shared(); + return std::make_shared(); } -BytesAsFloat::BytesAsFloat() - :AbstractExtractor(NAME(BytesAsFloat), DataType::Flt32) +AsFloat::AsFloat() + :AbstractExtractor(topic(), DataType::Flt32) { this->setCountWithin(4); } -std::shared_ptr BytesAsDouble::newDefault() const +QString AsFloat::topic() { - return std::make_shared(); + return NAME(AsFloat); } -void BytesAsDouble::parse(const QByteArray& bytes, std::shared_ptr out) const +std::shared_ptr AsDouble::newDefault() const +{ + return std::make_shared(); +} + +void AsDouble::parse(const QByteArray& bytes, std::shared_ptr out) const { out->append(*((double*)bytes.data())); } -BytesAsDouble::BytesAsDouble() - :AbstractExtractor(NAME(BytesAsDouble), DataType::Dbl64) +AsDouble::AsDouble() + :AbstractExtractor(topic(), DataType::Dbl64) { this->setCountWithin(8); } -BytesAsUnsigned::BytesAsUnsigned() - :AbstractExtractor(NAME(BytesAsUnsigned), DataType::Unsigned) +QString AsDouble::topic() +{ + return NAME(AsDouble); +} + +AsUnsigned::AsUnsigned() + :AbstractExtractor(topic(), DataType::Unsigned) { } -bool BytesAsUnsigned::setCountWithin(int bytes) +bool AsUnsigned::setCountWithin(int bytes) { auto count = std::max(1, std::min(8, bytes)); return AbstractExtractor::setCountWithin(count); } -void BytesAsUnsigned::parse(const QByteArray& bytes, std::shared_ptr out) const +void AsUnsigned::parse(const QByteArray& bytes, std::shared_ptr out) const { unsigned long long value = 0; for (auto vidx = bytes.size() - 1; vidx >= 0; vidx--) { @@ -297,57 +326,62 @@ void BytesAsUnsigned::parse(const QByteArray& bytes, std::shared_ptrappend(value); } -std::shared_ptr BytesAsUnsigned::newDefault() const +std::shared_ptr AsUnsigned::newDefault() const { - return std::make_shared(); + return std::make_shared(); +} + +QString AsUnsigned::topic() +{ + return NAME(AsUnsigned); } #include -bool BytesAsList::setOffsetSpan(int value) +bool AsList::setOffsetSpan(int value) { this->_list._bytes_offset = value; return true; } -std::shared_ptr BytesAsList::elementRule() const +std::shared_ptr AsList::elementRule() const { return this->_list._bind_unit; } -bool BytesAsList::appendElementRule(std::shared_ptr u) +bool AsList::setElementRule(std::shared_ptr u) { this->_list._bind_unit = u; return true; } -std::shared_ptr BytesAsList::sizeProvider() const +std::shared_ptr AsList::sizeProvider() const { return this->_list._bind_size_v; } -bool BytesAsList::setSizeProvider(std::shared_ptr ins) +bool AsList::setSizeProvider(std::shared_ptr ins) { this->_list._bind_size_v = ins; return true; } -QString BytesAsList::name() const +QString AsList::name() const { - return NAME(BytesAsList); + return topic(); } -DataType BytesAsList::outType() const +DataType AsList::outType() const { return DataType::LIST_COLLECTION; } -int BytesAsList::offsetSpan() const +int AsList::offsetSpan() const { return _list._bytes_offset; } -int BytesAsList::countWithin() const +int AsList::countWithin() const { auto expr = this->_list._bind_size_v->expression(); if (this->_list._bind_size_v->value(expr) < 0) { @@ -359,7 +393,7 @@ int BytesAsList::countWithin() const } #include "TranslateBasic.h" -void BytesAsList::loadFrom(std::shared_ptr core, const QJsonObject& obj) +void AsList::loadFrom(std::shared_ptr core, const QJsonObject& obj) { INT32_PEAK(_list._bytes_offset, obj); @@ -386,7 +420,7 @@ void BytesAsList::loadFrom(std::shared_ptr core, const QJsonObje } #include -void BytesAsList::saveTo(QJsonObject& obj) const +void AsList::saveTo(QJsonObject& obj) const { INT32_SAVE(_list._bytes_offset, obj); @@ -412,7 +446,7 @@ void BytesAsList::saveTo(QJsonObject& obj) const OBJECT_SAVE(size_provider_obj, obj); } -void BytesAsList::parse(const QByteArray& bytes, std::shared_ptr out) const +void AsList::parse(const QByteArray& bytes, std::shared_ptr out) const { auto expr = this->_list._bind_size_v->expression(); auto size_value = this->_list._bind_size_v->value(expr); @@ -427,39 +461,44 @@ void BytesAsList::parse(const QByteArray& bytes, std::shared_ptr BytesAsList::newDefault() const +std::shared_ptr AsList::newDefault() const { - return std::make_shared(); + return std::make_shared(); } -void BytesAsList::registSubField(std::shared_ptr inst) +void AsList::registSubField(std::shared_ptr inst) { inst->setField("ls", this->_list._bind_unit->outType()); auto layer = std::make_shared("ls", inst); this->_list._bind_unit->registSubField(layer); } -QString BytesAsUnion::name() const +QString AsList::topic() { - return NAME(BytesAsUnion); + return NAME(AsList); } -DataType BytesAsUnion::outType() const +QString AsUnion::name() const +{ + return topic(); +} + +DataType AsUnion::outType() const { return DataType::UNION_COMBINATE; } -int BytesAsUnion::offsetSpan() const +int AsUnion::offsetSpan() const { return _union.byte_offset; } -int BytesAsUnion::countWithin() const +int AsUnion::countWithin() const { return _union.byte_count; } -void BytesAsUnion::parse(const QByteArray& bytes, std::shared_ptr out) const +void AsUnion::parse(const QByteArray& bytes, std::shared_ptr out) const { for (auto u : this->elementRules()) if (u->checkpass(bytes)) { @@ -469,7 +508,7 @@ void BytesAsUnion::parse(const QByteArray& bytes, std::shared_ptr core, const QJsonObject& obj) { INT32_PEAK(_union.byte_count, obj); @@ -495,7 +534,7 @@ void BytesAsUnion::loadFrom( } } -void BytesAsUnion::saveTo(QJsonObject& obj) const +void AsUnion::saveTo(QJsonObject& obj) const { INT32_SAVE(_union.byte_count, obj); INT32_SAVE(_union.byte_offset, obj); @@ -515,40 +554,40 @@ void BytesAsUnion::saveTo(QJsonObject& obj) const ARRAY_SAVE(array, obj); } -std::shared_ptr BytesAsUnion::newDefault() const +std::shared_ptr AsUnion::newDefault() const { - return std::make_shared(); + return std::make_shared(); } -QList> BytesAsUnion::elementRules() const +QList> AsUnion::elementRules() const { return _union._rule_list; } -bool BytesAsUnion::appendElementRule(std::shared_ptr u) +bool AsUnion::appendElementRule(std::shared_ptr u) { this->_union._rule_list << u; return true; } -void BytesAsUnion::clearRules() +void AsUnion::clearRules() { this->_union._rule_list.clear(); } -bool BytesAsUnion::setCountWithin(int bytes) +bool AsUnion::setCountWithin(int bytes) { this->_union.byte_count = bytes; return true; } -bool BytesAsUnion::setOffsetSpan(int value) +bool AsUnion::setOffsetSpan(int value) { this->_union.byte_offset = value; return true; } -void BytesAsUnion::registSubField(std::shared_ptr inst) +void AsUnion::registSubField(std::shared_ptr inst) { for (auto rl : this->_union._rule_list) { inst->setField(rl->name(), rl->bindRule()->outType()); @@ -557,7 +596,12 @@ void BytesAsUnion::registSubField(std::shared_ptr inst) } } -void BytesAsRuleSet::registSubField(std::shared_ptr inst) +QString AsUnion::topic() +{ + return NAME(AsUnion); +} + +void AsRuleSet::registSubField(std::shared_ptr inst) { for (auto rl_key : this->fieldNames()){ auto rlinst = (*this)[rl_key]; @@ -567,7 +611,7 @@ void BytesAsRuleSet::registSubField(std::shared_ptr inst) } } -void BytesAsRuleSet::parse(const QByteArray& bytes, std::shared_ptr out) const +void AsRuleSet::parse(const QByteArray& bytes, std::shared_ptr out) const { auto bufx = bytes; for (auto keym : this->fieldNames()) { @@ -583,33 +627,33 @@ void BytesAsRuleSet::parse(const QByteArray& bytes, std::shared_ptr_bind._byte_offset = bytes; return true; } -int BytesAsRuleSet::offsetSpan() const +int AsRuleSet::offsetSpan() const { return this->_bind._byte_offset; } -int BytesAsRuleSet::countWithin() const +int AsRuleSet::countWithin() const { return this->_bind._byte_count; } -void BytesAsRuleSet::loadFrom(std::shared_ptr core, const QJsonObject& obj) +void AsRuleSet::loadFrom(std::shared_ptr core, const QJsonObject& obj) { INT32_PEAK(_bind._byte_count, obj); INT32_PEAK(_bind._byte_offset, obj); @@ -631,7 +675,7 @@ void BytesAsRuleSet::loadFrom(std::shared_ptr core, const QJsonO } } -void BytesAsRuleSet::saveTo(QJsonObject& obj) const +void AsRuleSet::saveTo(QJsonObject& obj) const { INT32_SAVE(_bind._byte_count, obj); INT32_SAVE(_bind._byte_offset, obj); @@ -653,33 +697,33 @@ void BytesAsRuleSet::saveTo(QJsonObject& obj) const ARRAY_SAVE(array, obj); } -std::shared_ptr BytesAsRuleSet::newDefault() const +std::shared_ptr AsRuleSet::newDefault() const { - return std::make_shared(); + return std::make_shared(); } -std::pair> BytesAsRuleSet::operator[](int index) const +std::pair> AsRuleSet::operator[](int index) const { return this->_bind.sub_units[index]; } -void BytesAsRuleSet::replace(int index, std::pair> inst) +void AsRuleSet::replace(int index, std::pair> inst) { this->_bind.sub_units.replace(index, inst); } -extract::BytesAsRuleSet& BytesAsRuleSet::append(const QString& nm, std::shared_ptr u) +extract::AsRuleSet& AsRuleSet::append(const QString& nm, std::shared_ptr u) { this->_bind.sub_units.append(std::make_pair(nm, u)); return *this; } -void BytesAsRuleSet::removeAt(int index) +void AsRuleSet::removeAt(int index) { this->_bind.sub_units.removeAt(index); } -QList BytesAsRuleSet::fieldNames() const +QList AsRuleSet::fieldNames() const { QList fields_store; for (auto fpair : this->_bind.sub_units) { @@ -688,7 +732,7 @@ QList BytesAsRuleSet::fieldNames() const return fields_store;; } -std::shared_ptr BytesAsRuleSet::operator[](const QString& field) const +std::shared_ptr AsRuleSet::operator[](const QString& field) const { for (auto pair : this->_bind.sub_units) { if(pair.first == field) @@ -698,17 +742,22 @@ std::shared_ptr BytesAsRuleSet::operator[](const QString& field) co return std::shared_ptr(); } -void BytesAsRuleSet::setCustomName(const QString& name) +void AsRuleSet::setCustomName(const QString& name) { _bind._name_store = name; } -QString BytesAsRuleSet::customName() const +QString AsRuleSet::customName() const { return _bind._name_store; } -int BytesAsRuleSet::memberCount() const +int AsRuleSet::memberCount() const { return _bind.sub_units.size(); } + +QString AsRuleSet::topic() +{ + return NAME(AsRuleSet); +} diff --git a/TranslateUI/extract_basic.h b/TranslateUI/extract_basic.h index 8fcf4f5..56ae4f4 100644 --- a/TranslateUI/extract_basic.h +++ b/TranslateUI/extract_basic.h @@ -88,9 +88,10 @@ namespace extract { /// /// 转换源数值未16进制数值,默认分割方式 /// - class BytesAsHex : public AbstractExtractor { + class AsHex : public AbstractExtractor { public: - BytesAsHex(); + static QString topic(); + AsHex(); void parse(const QByteArray& bytes, std::shared_ptr out) const override; std::shared_ptr newDefault() const override; @@ -99,13 +100,14 @@ namespace extract { /// /// 转换源数值为位联合 /// - class BytesAsBitCombine : public AbstractExtractor { + class AsBitCombine : public AbstractExtractor { public: struct __Private { QHash _switch_options; } _combine; - BytesAsBitCombine(); + static QString topic(); + AsBitCombine(); bool setSwitchOption(int bit_index, const QString& keyword); QHash switchOptions() const; @@ -123,9 +125,10 @@ namespace extract { /// /// 转换源数据为整形 /// - class BytesAsInteger : public AbstractExtractor { + class AsInteger : public AbstractExtractor { public: - BytesAsInteger(); + static QString topic(); + AsInteger(); // ExtractUnit ============================ bool setCountWithin(int bytes) override; @@ -138,9 +141,10 @@ namespace extract { /// /// 转换源数据为整形 /// - class BytesAsUnsigned : public AbstractExtractor { + class AsUnsigned : public AbstractExtractor { public: - BytesAsUnsigned(); + static QString topic(); + AsUnsigned(); // ExtractUnit ============================ bool setCountWithin(int bytes) override; @@ -153,14 +157,15 @@ namespace extract { /// /// 转换源数据为字符串 /// - class BytesAsString : public AbstractExtractor { + class AsString : public AbstractExtractor { public: struct __Private { QTextCodec* _conv_with = nullptr; } _strings; public: - BytesAsString(); + static QString topic(); + AsString(); void setStrCodec(QTextCodec* ins); QString codecName() const; @@ -174,24 +179,26 @@ namespace extract { std::shared_ptr newDefault() const override; }; - class BytesAsFloat : public AbstractExtractor { + class AsFloat : public AbstractExtractor { public: - BytesAsFloat(); + static QString topic(); + AsFloat(); void parse(const QByteArray& bytes, std::shared_ptr out) const override; std::shared_ptr newDefault() const override; }; - class BytesAsDouble : public AbstractExtractor { + class AsDouble : public AbstractExtractor { public: - BytesAsDouble(); + static QString topic(); + AsDouble(); void parse(const QByteArray& bytes, std::shared_ptr out) const override; std::shared_ptr newDefault() const override; }; - class BytesAsList : public ExtractUnit { + class AsList : public ExtractUnit { public: struct __Private { std::shared_ptr _bind_unit = nullptr; @@ -200,9 +207,10 @@ namespace extract { } _list; public: + static QString topic(); bool setOffsetSpan(int value); std::shared_ptr elementRule() const; - bool appendElementRule(std::shared_ptr u); + bool setElementRule(std::shared_ptr u); std::shared_ptr sizeProvider() const; bool setSizeProvider(std::shared_ptr ins); @@ -221,7 +229,7 @@ namespace extract { }; - class BytesAsUnion : public ExtractUnit { + class AsUnion : public ExtractUnit { public: struct __Private { int byte_offset = 0, byte_count = 1; @@ -229,6 +237,7 @@ namespace extract { } _union; public: + static QString topic(); bool setCountWithin(int bytes); void clearRules(); @@ -251,7 +260,7 @@ namespace extract { }; - class BytesAsRuleSet : public ExtractUnit { + class AsRuleSet : public ExtractUnit { private: struct __Private { int _byte_offset = 0, _byte_count = 1; @@ -261,6 +270,8 @@ namespace extract { }_bind; public: + static QString topic(); + void setCustomName(const QString& name); QString customName() const; @@ -271,7 +282,7 @@ namespace extract { std::pair> operator[](int index) const; void replace(int index, std::pair> inst); void removeAt(int index); - BytesAsRuleSet& append(const QString& nm, std::shared_ptr u); + AsRuleSet& append(const QString& nm, std::shared_ptr u); public: QString name() const override;