332 lines
8.2 KiB
C++
332 lines
8.2 KiB
C++
#include "TranslateBasic.h"
|
||
|
||
using namespace extract;
|
||
using namespace size_provider;
|
||
|
||
TranslateBasic::TranslateBasic()
|
||
{
|
||
// extractor type-list
|
||
std::shared_ptr<ExtractDelegate> u_ptr = std::make_shared<AsInteger>();
|
||
_basic_extractor_types[u_ptr->unitType()] = u_ptr;
|
||
|
||
u_ptr = std::make_shared<AsUnsigned>();
|
||
_basic_extractor_types[u_ptr->unitType()] = u_ptr;
|
||
|
||
u_ptr = std::make_shared<AsFloat>();
|
||
_basic_extractor_types[u_ptr->unitType()] = u_ptr;
|
||
|
||
u_ptr = std::make_shared<AsDouble>();
|
||
_basic_extractor_types[u_ptr->unitType()] = u_ptr;
|
||
|
||
u_ptr = std::make_shared<AsBitCombine>();
|
||
_basic_extractor_types[u_ptr->unitType()] = u_ptr;
|
||
|
||
u_ptr = std::make_shared<AsString>();
|
||
_basic_extractor_types[u_ptr->unitType()] = u_ptr;
|
||
|
||
u_ptr = std::make_shared<AsHex>();
|
||
_basic_extractor_types[u_ptr->unitType()] = u_ptr;
|
||
this->_default_translate_rule = u_ptr;
|
||
|
||
// size-provider type-list
|
||
std::shared_ptr<SizeProvider> sz_ptr = std::make_shared<ConstNumberProvider>();
|
||
this->_default_size_provider = sz_ptr;
|
||
_size_provider_types[sz_ptr->name()] = sz_ptr;
|
||
|
||
sz_ptr = std::make_shared<InterpretedNumberPrivider>();
|
||
_size_provider_types[sz_ptr->name()] = sz_ptr;
|
||
|
||
// extract-unit type-list
|
||
std::shared_ptr<ExtractUnit> x_ptr = std::make_shared<SingleBasedUnit>();
|
||
_process_unit_types[x_ptr->baseType()] = x_ptr;
|
||
_default_process_unit = x_ptr;
|
||
|
||
x_ptr = std::make_shared<ListBasedUnit>();
|
||
_process_unit_types[x_ptr->baseType()] = x_ptr;
|
||
|
||
x_ptr = std::make_shared<UnionBasedUnit>();
|
||
_process_unit_types[x_ptr->baseType()] = x_ptr;
|
||
|
||
}
|
||
|
||
std::shared_ptr<ExtractDelegate> TranslateBasic::defaultExtractDelegate() const
|
||
{
|
||
return _default_translate_rule;
|
||
}
|
||
|
||
QHash<QString, std::shared_ptr<ExtractDelegate>> TranslateBasic::extractorMap() const
|
||
{
|
||
return _basic_extractor_types;
|
||
}
|
||
|
||
std::shared_ptr<SizeProvider> TranslateBasic::defaultSizeProvider() const
|
||
{
|
||
return _default_size_provider;
|
||
}
|
||
|
||
std::shared_ptr<RuleMatch> TranslateBasic::defaultRuleMatch() const
|
||
{
|
||
return _default_rule_match;
|
||
}
|
||
|
||
void TranslateBasic::addDelegate(std::shared_ptr<ExtractDelegate> inst)
|
||
{
|
||
auto repeat_name_count = std::count_if(
|
||
this->_custom_rule_types.begin(), this->_custom_rule_types.end(),
|
||
[=](std::shared_ptr<ExtractDelegate> v) {
|
||
return v->aliasName() == inst->aliasName();
|
||
});
|
||
|
||
if(repeat_name_count)
|
||
throw new BaseException(u8"RuleName<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD>");
|
||
|
||
switch (inst->outType()) {
|
||
case DataType::LIST_COLLECTION:
|
||
case DataType::UNION_COMBINATE:
|
||
throw new BaseException(u8"<EFBFBD><EFBFBD><EFBFBD>ܽ<EFBFBD><EFBFBD>ܵ<EFBFBD>ί<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD>");
|
||
default:
|
||
break;
|
||
}
|
||
|
||
this->_custom_rule_types << inst;
|
||
}
|
||
|
||
void TranslateBasic::removeDelegate(const QString& typeAlias)
|
||
{
|
||
for (auto idxp = 0; idxp< this->_custom_rule_types.size(); idxp++) {
|
||
auto rule = this->_custom_rule_types.at(idxp);
|
||
if (rule->aliasName() == typeAlias) {
|
||
this->_custom_rule_types.removeAt(idxp--);
|
||
return;
|
||
}
|
||
}
|
||
|
||
assert("ָ<EFBFBD><EFBFBD>RuleName<EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD>" && 0);
|
||
}
|
||
|
||
QList<QString> TranslateBasic::delegateAlias() const
|
||
{
|
||
QList<QString> values;
|
||
std::transform(_custom_rule_types.begin(), _custom_rule_types.end(),
|
||
std::back_inserter(values), [](std::shared_ptr<ExtractDelegate> impl){
|
||
return impl->aliasName();
|
||
});
|
||
return values;
|
||
}
|
||
|
||
std::shared_ptr<ExtractDelegate> TranslateBasic::operator[](const QString& name) const
|
||
{
|
||
for(auto ins : _custom_rule_types)
|
||
if(ins->aliasName() == name)
|
||
return ins;
|
||
|
||
assert("ָ<EFBFBD><EFBFBD>Name<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" && 0);
|
||
}
|
||
|
||
void TranslateBasic::replaceDelegate(const QString& name, std::shared_ptr<ExtractDelegate> inst)
|
||
{
|
||
for (auto idx = 0; _custom_rule_types.size(); ++idx) {
|
||
auto rule_inst = _custom_rule_types.at(idx);
|
||
if (rule_inst->aliasName() == name) {
|
||
_custom_rule_types.replace(idx, inst);
|
||
return;
|
||
}
|
||
}
|
||
|
||
assert("ָ<EFBFBD><EFBFBD>Name<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" && 0);
|
||
}
|
||
|
||
QHash<QString, std::shared_ptr<SizeProvider>> TranslateBasic::sizeProviderMap() const
|
||
{
|
||
return _size_provider_types;
|
||
}
|
||
|
||
QHash<QString, std::shared_ptr<RuleMatch>> TranslateBasic::ruleMatchMap() const
|
||
{
|
||
return _rule_match_types;
|
||
}
|
||
|
||
std::shared_ptr<ExtractUnit> TranslateBasic::defaultExtractUnit() const
|
||
{
|
||
return _default_process_unit;
|
||
}
|
||
|
||
QHash<QString, std::shared_ptr<ExtractUnit>> TranslateBasic::extractUnitMap() const
|
||
{
|
||
return _process_unit_types;
|
||
}
|
||
|
||
void ConstNumberProvider::bindInput(std::shared_ptr<DataContext> inst)
|
||
{
|
||
|
||
}
|
||
|
||
QString ConstNumberProvider::name() const
|
||
{
|
||
return NAME(ConstNumberProvider);
|
||
}
|
||
|
||
int32_t ConstNumberProvider::value(const QString& expr) const
|
||
{
|
||
return expr.toInt();
|
||
}
|
||
|
||
void ConstNumberProvider::setExpression(const QString& expr)
|
||
{
|
||
this->_number._value_string = expr;
|
||
}
|
||
|
||
QString ConstNumberProvider::expression() const
|
||
{
|
||
return this->_number._value_string;
|
||
}
|
||
|
||
void ConstNumberProvider::loadFrom(std::shared_ptr<TranslateBasic> core, const QJsonObject& obj)
|
||
{
|
||
STRING_PEAK(_number._value_string, obj);
|
||
}
|
||
|
||
void ConstNumberProvider::saveTo(QJsonObject& obj) const
|
||
{
|
||
STRING_SAVE(_number._value_string, obj);
|
||
}
|
||
|
||
std::shared_ptr<Serializable> ConstNumberProvider::newDefault() const
|
||
{
|
||
return std::make_shared<ConstNumberProvider>();
|
||
}
|
||
|
||
void InterpretedNumberPrivider::bindInput(std::shared_ptr<DataContext> inst)
|
||
{
|
||
this->_refer._context_inst = inst;
|
||
}
|
||
|
||
QString InterpretedNumberPrivider::name() const
|
||
{
|
||
return NAME(InterpretedNumberPrivider);
|
||
}
|
||
|
||
#include <QMetaType>
|
||
int32_t InterpretedNumberPrivider::value(const QString& expr) const
|
||
{
|
||
if (this->_refer._context_inst) {
|
||
auto value = this->_refer._context_inst->get(expr);
|
||
if (value.userType() == QMetaType::Int)
|
||
return value.toInt();
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
void InterpretedNumberPrivider::setExpression(const QString& expr)
|
||
{
|
||
this->_refer._field_refer = expr;
|
||
}
|
||
|
||
QString InterpretedNumberPrivider::expression() const
|
||
{
|
||
return this->_refer._field_refer;
|
||
}
|
||
|
||
void InterpretedNumberPrivider::loadFrom(std::shared_ptr<TranslateBasic> core, const QJsonObject& obj)
|
||
{
|
||
STRING_PEAK(this->_refer._field_refer, obj);
|
||
}
|
||
|
||
void InterpretedNumberPrivider::saveTo(QJsonObject& obj) const
|
||
{
|
||
STRING_SAVE(this->_refer._field_refer, obj);
|
||
}
|
||
|
||
std::shared_ptr<Serializable> InterpretedNumberPrivider::newDefault() const
|
||
{
|
||
return std::make_shared<InterpretedNumberPrivider>();
|
||
}
|
||
|
||
void ValueAccessContext::init(const QString& field, std::shared_ptr<DataContext> parent)
|
||
{
|
||
this->_cascade._current_field = field;
|
||
|
||
if (parent)
|
||
parent->setChild(field, this->shared_from_this());
|
||
}
|
||
|
||
void ValueAccessContext::setChild(const QString& field, std::shared_ptr<DataContext> inst)
|
||
{
|
||
this->_cascade._children_context << std::make_pair(field, inst);
|
||
}
|
||
|
||
void ValueAccessContext::append(const QVariant& value)
|
||
{
|
||
this->_cascade._current_value = value;
|
||
}
|
||
|
||
QList<std::pair<QString, QVariant>> ValueAccessContext::dataSeqs() const
|
||
{
|
||
QList<std::pair<QString, QVariant>> values;
|
||
values << std::make_pair(_cascade._current_field, _cascade._current_value);
|
||
for (auto pair : _cascade._children_context) {
|
||
values << pair.second->dataSeqs();
|
||
}
|
||
return values;
|
||
}
|
||
|
||
QVariant ValueAccessContext::get(const QString& unique_key) const
|
||
{
|
||
if (_cascade._current_field == unique_key)
|
||
return _cascade._current_value;
|
||
|
||
for (auto unit : _cascade._children_context) {
|
||
auto value = unit.second->get(unique_key);
|
||
if (value.isValid())
|
||
return value;
|
||
}
|
||
return QVariant();
|
||
}
|
||
|
||
bool FieldManagerLayer::bindCurrent(const QString& unique_key)
|
||
{
|
||
for (auto index = 0; index < __Private::_fields_map.size(); ++index) {
|
||
auto pair = __Private::_fields_map[index];
|
||
if (pair.first == unique_key) {
|
||
__Private::_fields_map = __Private::_fields_map.mid(0, index);
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
QHash<QString, DataType> FieldManagerLayer::prevFields() const
|
||
{
|
||
QHash<QString, DataType> fields;
|
||
for (auto pair : __Private::_fields_map)
|
||
fields[pair.first] = pair.second;
|
||
|
||
return fields;
|
||
}
|
||
|
||
void FieldManagerLayer::clear()
|
||
{
|
||
__Private::_fields_map.clear();
|
||
}
|
||
|
||
FieldManagerLayer::FieldManagerLayer(const QString& self, std::shared_ptr<ScopeFieldsSetter> instp)
|
||
{
|
||
_setter._rule_name = self;
|
||
_setter._parent_setter = instp;
|
||
}
|
||
|
||
#include <stdexcept>
|
||
void FieldManagerLayer::setField(const QString& fieldName, DataType type)
|
||
{
|
||
if (_setter._parent_setter)
|
||
_setter._parent_setter->setField(QString("%1.%2").arg(_setter._rule_name, fieldName), type);
|
||
else {
|
||
for (auto pair : __Private::_fields_map) {
|
||
if (pair.first == fieldName)
|
||
throw new std::invalid_argument("<EFBFBD>ֶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD>");
|
||
}
|
||
__Private::_fields_map << std::make_pair(fieldName, type);
|
||
}
|
||
}
|
||
QList<std::pair<QString, DataType>> FieldManagerLayer::__Private::_fields_map; |