diff --git a/TranslateUI/SequenceView.cpp b/TranslateUI/SequenceView.cpp index 41e73a7..ae7589b 100644 --- a/TranslateUI/SequenceView.cpp +++ b/TranslateUI/SequenceView.cpp @@ -36,7 +36,7 @@ void IntDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewIt void RuleSetCustom::targetRuleRefresh(const QModelIndex& idx) { - auto rule_insv = _view._ruleset->_bind[idx.row()]; + auto rule_insv = (*_view._ruleset)[idx.row()]; auto offset_index = idx.sibling(idx.row(), 1); auto offset_number = offset_index.data(Qt::DisplayRole).toInt(); @@ -65,7 +65,7 @@ void RuleSetCustom::peersRuleChanged(const QModelIndex& idx) auto rule_nm = _view.base->extractUnitList()[rule_name]; auto new_inst = std::dynamic_pointer_cast(rule_nm->newDefault()); - _view._ruleset->_bind.replace(idx.row(), std::make_pair(field_name, new_inst)); + _view._ruleset->replace(idx.row(), std::make_pair(field_name, new_inst)); // 对ListUnit执行特殊初始化步骤 if (typeid(*new_inst.get()) == typeid(BytesAsList)) { @@ -117,7 +117,7 @@ RuleSetCustom::RuleSetCustom(std::shared_ptr base, if (!curr.isValid()) return; - emit this->currentRuleChanged(_view._ruleset->_bind[curr.row()].second, curr); + emit this->currentRuleChanged(_view._ruleset->operator[](curr.row()).second, curr); }); } @@ -133,8 +133,8 @@ void RuleSetCustom::customTranslateRuleEdit(const QPoint& pos) void RuleSetCustom::addTranslateUnit() { auto curr_rule = _view.base->defaultExtractUnit()->newDefault(); - this->_view._ruleset->_bind.append( - QString(u8"rule_%1").arg(_view._ruleset->_bind.sub_units.size()), + this->_view._ruleset->append( + QString(u8"rule_%1").arg(_view._ruleset->memberCount()), std::static_pointer_cast(curr_rule)); auto hex_rule = std::static_pointer_cast(curr_rule); @@ -149,7 +149,7 @@ void RuleSetCustom::removeTranslateUnit() if (!idx_curr.isValid()) return; - _view._ruleset->_bind.removeAt(idx_curr.row()); + _view._ruleset->removeAt(idx_curr.row()); _view._seqs_model->removeRow(idx_curr.row()); } @@ -157,9 +157,8 @@ void RuleSetCustom::membersPresent(std::shared_ptr rule { model->removeRows(0, model->rowCount()); - auto units = ruleset->_bind.sub_units; - for (auto uidx = 0; uidx < units.size(); ++uidx) { - auto u_pair = units.at(uidx); + for (auto uidx = 0; uidx < ruleset->memberCount(); ++uidx) { + auto u_pair = ruleset->operator[](uidx); QList new_row; new_row << new QStandardItem(u_pair.first); diff --git a/TranslateUI/StructuralRuleView.cpp b/TranslateUI/StructuralRuleView.cpp index 42995cc..5106e9b 100644 --- a/TranslateUI/StructuralRuleView.cpp +++ b/TranslateUI/StructuralRuleView.cpp @@ -86,7 +86,7 @@ void StructuralRuleView::cacheRefresh(const QModelIndex& curr, ListUnitConfigura auto field_idx = curr.sibling(curr.row(), 0); this->_current_fields_cache->bindCurrent(field_idx.data(Qt::DisplayRole).toString()); - auto ins = this->_rule_base->_bind[curr.row()]; + auto ins = this->_rule_base->operator[](curr.row()); auto conv = std::dynamic_pointer_cast(ins.second); if (conv) { t->currentRuleAccept(conv, curr, this->_current_fields_cache); diff --git a/TranslateUI/TranslateBasic.cpp b/TranslateUI/TranslateBasic.cpp index 1b76fd8..2ec4fe9 100644 --- a/TranslateUI/TranslateBasic.cpp +++ b/TranslateUI/TranslateBasic.cpp @@ -195,7 +195,7 @@ std::shared_ptr InterpretedNumberPrivider::newDefault() const return std::make_shared(); } -void ValueAccess::init(const QString& field, std::shared_ptr parent) +void ValueAccessContext::init(const QString& field, std::shared_ptr parent) { this->_cascade._current_field = field; @@ -203,17 +203,17 @@ void ValueAccess::init(const QString& field, std::shared_ptr parent->setChild(field, this->shared_from_this()); } -void ValueAccess::setChild(const QString& field, std::shared_ptr inst) +void ValueAccessContext::setChild(const QString& field, std::shared_ptr inst) { this->_cascade._children_context[field] = inst; } -void ValueAccess::append(const QVariant& value) +void ValueAccessContext::append(const QVariant& value) { this->_cascade._current_value = value; } -QVariant ValueAccess::get(const QString& unique_key) const +QVariant ValueAccessContext::get(const QString& unique_key) const { if (_cascade._current_field == unique_key) return _cascade._current_value; diff --git a/TranslateUI/TranslateBasic.h b/TranslateUI/TranslateBasic.h index 6c170fb..3e7ca46 100644 --- a/TranslateUI/TranslateBasic.h +++ b/TranslateUI/TranslateBasic.h @@ -184,7 +184,8 @@ namespace size_provider { }; } -class ValueAccess : public DataAccessContext, std::enable_shared_from_this { +class ValueAccessContext : public DataAccessContext, + public std::enable_shared_from_this { public: struct __Private { QString _current_field = ""; diff --git a/TranslateUI/extract_basic.cpp b/TranslateUI/extract_basic.cpp index fdc1e12..9f0af99 100644 --- a/TranslateUI/extract_basic.cpp +++ b/TranslateUI/extract_basic.cpp @@ -421,7 +421,7 @@ void BytesAsList::parse(const QByteArray& bytes, std::shared_ptr(); + auto slice_context = std::make_shared(); slice_context->init(QString("ls<%1>").arg(idx), out); this->_list._bind_unit->parse(secn, slice_context); } @@ -463,7 +463,7 @@ void BytesAsUnion::parse(const QByteArray& bytes, std::shared_ptrelementRules()) if (u->checkpass(bytes)) { - auto enum_context = std::make_shared(); + auto enum_context = std::make_shared(); enum_context->init(u->name(), out); u->bindRule()->parse(bytes, enum_context); } @@ -559,8 +559,8 @@ void BytesAsUnion::registSubField(std::shared_ptr inst) void BytesAsRuleSet::registSubField(std::shared_ptr inst) { - for (auto rl_key : this->_bind.fieldNames()){ - auto rlinst = this->_bind[rl_key]; + for (auto rl_key : this->fieldNames()){ + auto rlinst = (*this)[rl_key]; inst->setField(rl_key, rlinst->outType()); auto layer = std::make_shared(rl_key, inst); rlinst->registSubField(layer); @@ -570,11 +570,11 @@ void BytesAsRuleSet::registSubField(std::shared_ptr inst) void BytesAsRuleSet::parse(const QByteArray& bytes, std::shared_ptr out) const { auto bufx = bytes; - for (auto keym : this->_bind.fieldNames()) { - auto rule_context = std::make_shared(); + for (auto keym : this->fieldNames()) { + auto rule_context = std::make_shared(); rule_context->init(keym, out); - auto rule = this->_bind[keym]; + auto rule = (*this)[keym]; bufx = bufx.mid(rule->offsetSpan()); auto count = rule->countWithin(); auto secx = bufx.mid(0, count); @@ -658,39 +658,39 @@ std::shared_ptr BytesAsRuleSet::newDefault() const return std::make_shared(); } -std::pair> BytesAsRuleSet::__Private::operator[](int index) const +std::pair> BytesAsRuleSet::operator[](int index) const { - return this->sub_units[index]; + return this->_bind.sub_units[index]; } -void BytesAsRuleSet::__Private::replace(int index, std::pair> inst) +void BytesAsRuleSet::replace(int index, std::pair> inst) { - this->sub_units.replace(index, inst); + this->_bind.sub_units.replace(index, inst); } -extract::BytesAsRuleSet::__Private& BytesAsRuleSet::__Private::append(const QString& nm, std::shared_ptr u) +extract::BytesAsRuleSet& BytesAsRuleSet::append(const QString& nm, std::shared_ptr u) { - this->sub_units.append(std::make_pair(nm, u)); + this->_bind.sub_units.append(std::make_pair(nm, u)); return *this; } -void BytesAsRuleSet::__Private::removeAt(int index) +void BytesAsRuleSet::removeAt(int index) { - this->sub_units.removeAt(index); + this->_bind.sub_units.removeAt(index); } -QList BytesAsRuleSet::__Private::fieldNames() const +QList BytesAsRuleSet::fieldNames() const { QList fields_store; - for (auto fpair : this->sub_units) { + for (auto fpair : this->_bind.sub_units) { fields_store << fpair.first; } return fields_store;; } -std::shared_ptr BytesAsRuleSet::__Private::operator[](const QString& field) const +std::shared_ptr BytesAsRuleSet::operator[](const QString& field) const { - for (auto pair : this->sub_units) { + for (auto pair : this->_bind.sub_units) { if(pair.first == field) return pair.second; } @@ -707,3 +707,8 @@ QString BytesAsRuleSet::customName() const { return _bind._name_store; } + +int BytesAsRuleSet::memberCount() const +{ + return _bind.sub_units.size(); +} diff --git a/TranslateUI/extract_basic.h b/TranslateUI/extract_basic.h index a0e7cde..8fcf4f5 100644 --- a/TranslateUI/extract_basic.h +++ b/TranslateUI/extract_basic.h @@ -69,7 +69,7 @@ namespace extract { virtual bool setCountWithin(int bytes); // ExtractUnit =========================== - QString name() const override; + QString name() const override; void registSubField(std::shared_ptr inst) override; virtual DataType outType() const; /// @@ -252,25 +252,27 @@ namespace extract { class BytesAsRuleSet : public ExtractUnit { - public: + private: struct __Private { int _byte_offset = 0, _byte_count = 1; QString _name_store; QList>> sub_units; - QList fieldNames() const; - std::shared_ptr operator[](const QString &field) const; - - - std::pair> operator[](int index) const; - void replace(int index, std::pair> inst); - void removeAt(int index); - __Private& append(const QString& nm, std::shared_ptr u); }_bind; + public: void setCustomName(const QString& name); QString customName() const; + QList fieldNames() const; + std::shared_ptr operator[](const QString& field) const; + + int memberCount() const; + 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); + public: QString name() const override; void registSubField(std::shared_ptr inst) override;