From b8f071b935f892c07c73cdfee2489e765836e08e Mon Sep 17 00:00:00 2001 From: codeboss <2422523675@qq.com> Date: Tue, 10 Jun 2025 12:52:59 +0800 Subject: [PATCH] update --- ComponentBasic/EntityDocks.h | 2 +- ComponentBasic/componentbasic.h | 2 +- MessageBasic/messagebasic.h | 2 + SimsBasic/SimsBasic.vcxproj | 2 + SimsBasic/SimsBasic.vcxproj.filters | 8 + SimsBasic/internal_impl.cpp | 222 ++++++++++++++++++++++++++++ SimsBasic/internal_impl.h | 149 +++++++++++++++++++ SimsBasic/simsbasic.cpp | 220 --------------------------- SimsBasic/simsbasic.h | 141 ------------------ 9 files changed, 385 insertions(+), 363 deletions(-) create mode 100644 SimsBasic/internal_impl.cpp create mode 100644 SimsBasic/internal_impl.h diff --git a/ComponentBasic/EntityDocks.h b/ComponentBasic/EntityDocks.h index abdd8cb..cc2a95b 100644 --- a/ComponentBasic/EntityDocks.h +++ b/ComponentBasic/EntityDocks.h @@ -1,5 +1,5 @@ #pragma once -#include +#include #include #include "componentbasic.h" #include diff --git a/ComponentBasic/componentbasic.h b/ComponentBasic/componentbasic.h index dc83731..a22c3dd 100644 --- a/ComponentBasic/componentbasic.h +++ b/ComponentBasic/componentbasic.h @@ -2,7 +2,7 @@ #include "componentbasic_global.h" #include - +#include /// /// 消息处理单元基类 diff --git a/MessageBasic/messagebasic.h b/MessageBasic/messagebasic.h index 7be079a..220ffad 100644 --- a/MessageBasic/messagebasic.h +++ b/MessageBasic/messagebasic.h @@ -3,6 +3,8 @@ #include "messagebasic_global.h" #include #include +#include +#include #define NAME(v) #v #define DOUBLE_SAVE(u) obj[NAME(u)] = u diff --git a/SimsBasic/SimsBasic.vcxproj b/SimsBasic/SimsBasic.vcxproj index 0edb828..7abe405 100644 --- a/SimsBasic/SimsBasic.vcxproj +++ b/SimsBasic/SimsBasic.vcxproj @@ -96,7 +96,9 @@ + + diff --git a/SimsBasic/SimsBasic.vcxproj.filters b/SimsBasic/SimsBasic.vcxproj.filters index 9d48920..2186e2f 100644 --- a/SimsBasic/SimsBasic.vcxproj.filters +++ b/SimsBasic/SimsBasic.vcxproj.filters @@ -32,5 +32,13 @@ Header Files + + Header Files + + + + + Source Files + \ No newline at end of file diff --git a/SimsBasic/internal_impl.cpp b/SimsBasic/internal_impl.cpp new file mode 100644 index 0000000..76b8823 --- /dev/null +++ b/SimsBasic/internal_impl.cpp @@ -0,0 +1,222 @@ +#include "internal_impl.h" + + +RtWsEntity::RtWsEntity() {} + +void RtWsEntity::resetTemplet(const QString& name) { + this->_templet_name = name; +} + +QString RtWsEntity::templetName() const { + return this->_templet_name; +} + +void RtWsEntity::resetID(uint64_t id) { + this->_entity_id = id; +} + +uint64_t RtWsEntity::entityID() const { + return this->_entity_id; +} + +void RtWsEntity::resetName(const QString& name) { + this->_runtime_name = name; +} + +QString RtWsEntity::name() const { + return this->_runtime_name; +} + +void RtWsEntity::append(std::shared_ptr ins) { + if (this->_comps_list.contains(ins->name())) + return; + + ins->bindEntity(this->shared_from_this()); + this->_comps_list[ins->name()] = ins; +} + +void RtWsEntity::remove(const QString& component_type) { + this->_comps_list.remove(component_type); +} + +QList> RtWsEntity::components() const { + return _comps_list.values(); +} + +std::shared_ptr RtWsEntity::defaultNew() const { + auto newx = std::make_shared(); + newx->_entity_id = this->_entity_id; + newx->_templet_name = this->_templet_name; + newx->_runtime_name = this->_runtime_name; + + for (auto c : this->_comps_list) + newx->append(c->defaultNew()); + return newx; +} + +#include +QList RtWsEntity::inputTypes() const +{ + QList types; + for (auto c : this->_comps_list) + types.append(c->inputTypes()); + return types.toSet().toList(); +} + +#include +#include +QList RtWsEntity::getEntryWithSignature(const WsRespondSignatureType& t) const +{ + QList list; + for (auto c : this->_comps_list) + list.append(c->getEntryWithSignature(t)); + return list; +} + +QList RtWsEntity::getEntryWithInType(const QString& msg_type) const +{ + QList list; + for (auto c : this->_comps_list) + list.append(c->getEntrysWithInType(msg_type)); + return list; +} +void RtWsEntity::recoveryFrom(const QJsonObject& obj) +{ + this->_entity_id = obj["entity_id"].toVariant().toULongLong(); + this->_runtime_name = obj["entity_name"].toString(); + this->_templet_name = obj["entity_templet"].toString(); + + auto array = obj["component_array"].toArray(); + for (auto idx = 0; idx < array.size(); ++idx) { + auto value = array.at(idx).toObject(); + auto type = value["component_type"].toString(); + + auto compins = this->_comps_list[type]; + if (!compins) + throw new UniException(type + "类型插件不存在!"); + + compins->recoveryFrom(value); + } +} + +void RtWsEntity::saveTo(QJsonObject& obj) const +{ + obj["entity_id"] = QJsonValue::fromVariant(QVariant::fromValue(this->_entity_id)); + obj["entity_name"] = this->_runtime_name; + obj["entity_templet"] = this->_templet_name; + + QJsonArray array; + for (auto c : this->_comps_list) + { + QJsonObject comp_obj; + comp_obj["component_type"] = c->name(); + c->saveTo(comp_obj); + array.append(comp_obj); + } + obj["component_array"] = array; +} + +QString RtEntityManager::templetName() const +{ + return "实体管理器"; +} + +uint64_t RtEntityManager::entityID() const +{ + return const_id; +} + +QString RtEntityManager::name() const +{ + return "实体管理器唯一实例"; +} + +void RtEntityManager::append(std::shared_ptr ins) { + _comps_list[ins->name()] = ins; +} + +std::shared_ptr RtEntityManager::defaultNew() const +{ + return nullptr; +} + +QList RtEntityManager::getEntryWithSignature(const WsRespondSignatureType& t) const +{ + QList list; + for (auto c : this->_comps_list) + list.append(c->getEntryWithSignature(t)); + return list; +} + +QList RtEntityManager::getEntryWithInType(const QString& msg_type) const +{ + QList list; + for (auto c : this->_comps_list) + list.append(c->getEntrysWithInType(msg_type)); + return list; +} + +void RtEntityManager::recoveryFrom(const QJsonObject& obj) +{ + auto array = obj["component_array"].toArray(); + for (auto idx = 0; idx < array.size(); ++idx) { + auto value = array.at(idx).toObject(); + auto type = value["component_type"].toString(); + + auto compins = this->_comps_list[type]; + if (!compins) + throw new UniException(type + "类型插件不存在!"); + + compins->recoveryFrom(value); + } +} + +void RtEntityManager::saveTo(QJsonObject& obj) const +{ + QJsonArray array; + for (auto c : this->_comps_list) + { + QJsonObject comp_obj; + comp_obj["component_type"] = c->name(); + c->saveTo(comp_obj); + array.append(comp_obj); + } + obj["component_array"] = array; +} + +void RtEntityManager::remove(const QString& component_type) +{ + this->_comps_list.remove(component_type); +} + +QList> RtEntityManager::components() const +{ + return this->_comps_list.values(); +} + +ImmediateKernel::ImmediateKernel(std::shared_ptr bind) :_bind_entity(bind) {} + +uint64_t ImmediateKernel::entityManagerID() const +{ + return RtEntityManager::const_id; +} + +QList> ImmediateKernel::execute(const WsRespondSignatureType& resp_signature, std::shared_ptr in) +{ + QList> rets; + auto list = this->_bind_entity->getEntryWithSignature(resp_signature); + for (auto func : list) { + func(shared_from_this(), in, rets); + } + return rets; +} + +QList> ImmediateKernel::execute(std::shared_ptr in) +{ + QList> rets; + auto list = this->_bind_entity->getEntryWithInType(in->topicString()); + for (auto func : list) { + func(shared_from_this(), in, rets); + } + return rets; +} diff --git a/SimsBasic/internal_impl.h b/SimsBasic/internal_impl.h new file mode 100644 index 0000000..1b2e983 --- /dev/null +++ b/SimsBasic/internal_impl.h @@ -0,0 +1,149 @@ +#pragma once +#include "simsbasic.h" +#include + + +/// +/// 内存实体实例类型 +/// +class SIMSBASIC_EXPORT RtWsEntity : public WsEntity, public ComponentSet, public std::enable_shared_from_this { +private: + uint64_t _entity_id = 0; + QString _templet_name, _runtime_name; + QHash> _comps_list; + +public: + explicit RtWsEntity(); + virtual ~RtWsEntity() = default; + + /// + /// 重置本实例模板名 + /// + /// 模板名称 + virtual void resetTemplet(const QString& name); + /// + /// 获取本实例的模板名 + /// + /// 模板名称 + virtual QString templetName() const; + + /// + /// 重置本实例的运行ID + /// + /// + virtual void resetID(uint64_t id); + /// + /// 获取本实例的运行ID + /// + /// + virtual uint64_t entityID() const; + /// + /// 重置本实例的运行名称 + /// + /// + virtual void resetName(const QString& name); + /// + /// 获取本实例的运行名称 + /// + /// 运行名称 + virtual QString name() const; + + /// + /// 为本实例添加指定类型的插件 + /// + /// + virtual void append(std::shared_ptr ins) override; + /// + /// 移除指定类型的插件实例 + /// + /// + virtual void remove(const QString& component_type) override; + /// + /// 获取本实例内包含的所有插件实例 + /// + /// + virtual QList> components() const override; + + /// + /// 深度克隆本实例,插件和数据一致 + /// + /// + virtual std::shared_ptr defaultNew() const; + + /// + /// 允许输入的消息类型 + /// + /// 消息类型集合 + virtual QList inputTypes() const; + /// + /// 通过指定的签名获取处理入口 + /// + /// 签名 + /// 处理接口 + virtual QList getEntryWithSignature(const WsRespondSignatureType& t) const; + /// + /// 通过输入消息类型获取处理入口 + /// + /// 输入消息类型 + /// 处理接口列表 + virtual QList getEntryWithInType(const QString& msg_type) const; + + // 通过 Serializable 继承 + void recoveryFrom(const QJsonObject& obj) override; + void saveTo(QJsonObject& obj) const override; +}; + +/// +/// 运行时实体管理器 +/// +class SIMSBASIC_EXPORT RtEntityManager : public WsEntity, public ComponentSet, + public std::enable_shared_from_this +{ +public: + static const uint64_t const_id = 0x01ff; + QHash> _comps_list; + + QString templetName() const override; + uint64_t entityID() const override; + QString name() const override; + void append(std::shared_ptr ins) override; + void remove(const QString& component_type) override; + QList> components() const override; + + std::shared_ptr defaultNew() const override; + QList getEntryWithSignature(const WsRespondSignatureType& t) const override; + QList getEntryWithInType(const QString& msg_type) const override; + + void recoveryFrom(const QJsonObject& obj) override; + void saveTo(QJsonObject& obj) const override; +}; + +/// +/// 实体内迅捷调用核心 +/// +class SIMSBASIC_EXPORT ImmediateKernel : public Immediate, + public std::enable_shared_from_this { +private: + std::shared_ptr _bind_entity = nullptr; + +public: + ImmediateKernel(std::shared_ptr bind); + /// + /// 获取实体管理器id + /// + /// + virtual uint64_t entityManagerID() const override; + /// + /// 实体内指定类型的调用立即执行 + /// + /// + /// + virtual QList> execute(const WsRespondSignatureType& resp_signature, std::shared_ptr in) override; + /// + /// 实体内兼容调用立即执行 + /// + /// + /// + virtual QList> execute(std::shared_ptr in) override; + +}; \ No newline at end of file diff --git a/SimsBasic/simsbasic.cpp b/SimsBasic/simsbasic.cpp index cece96c..5df5d10 100644 --- a/SimsBasic/simsbasic.cpp +++ b/SimsBasic/simsbasic.cpp @@ -9,223 +9,3 @@ QString UniException::content() const noexcept { return _error_store; } - -RtWsEntity::RtWsEntity() {} - -void RtWsEntity::resetTemplet(const QString& name) { - this->_templet_name = name; -} - -QString RtWsEntity::templetName() const { - return this->_templet_name; -} - -void RtWsEntity::resetID(uint64_t id) { - this->_entity_id = id; -} - -uint64_t RtWsEntity::entityID() const { - return this->_entity_id; -} - -void RtWsEntity::resetName(const QString& name) { - this->_runtime_name = name; -} - -QString RtWsEntity::name() const { - return this->_runtime_name; -} - -void RtWsEntity::append(std::shared_ptr ins) { - if (this->_comps_list.contains(ins->name())) - return; - - ins->bindEntity(this->shared_from_this()); - this->_comps_list[ins->name()] = ins; -} - -void RtWsEntity::remove(const QString& component_type) { - this->_comps_list.remove(component_type); -} - -QList> RtWsEntity::components() const { - return _comps_list.values(); -} - -std::shared_ptr RtWsEntity::defaultNew() const { - auto newx = std::make_shared(); - newx->_entity_id = this->_entity_id; - newx->_templet_name = this->_templet_name; - newx->_runtime_name = this->_runtime_name; - - for (auto c : this->_comps_list) - newx->append(c->defaultNew()); - return newx; -} - -#include -QList RtWsEntity::inputTypes() const -{ - QList types; - for (auto c : this->_comps_list) - types.append(c->inputTypes()); - return types.toSet().toList(); -} - -#include -#include -QList RtWsEntity::getEntryWithSignature(const WsRespondSignatureType& t) const -{ - QList list; - for (auto c : this->_comps_list) - list.append(c->getEntryWithSignature(t)); - return list; -} - -QList RtWsEntity::getEntryWithInType(const QString& msg_type) const -{ - QList list; - for (auto c : this->_comps_list) - list.append(c->getEntrysWithInType(msg_type)); - return list; -} -void RtWsEntity::recoveryFrom(const QJsonObject& obj) -{ - this->_entity_id = obj["entity_id"].toVariant().toULongLong(); - this->_runtime_name = obj["entity_name"].toString(); - this->_templet_name = obj["entity_templet"].toString(); - - auto array = obj["component_array"].toArray(); - for (auto idx = 0; idx < array.size(); ++idx) { - auto value = array.at(idx).toObject(); - auto type = value["component_type"].toString(); - - auto compins = this->_comps_list[type]; - if (!compins) - throw new UniException(type + "类型插件不存在!"); - - compins->recoveryFrom(value); - } -} - -void RtWsEntity::saveTo(QJsonObject& obj) const -{ - obj["entity_id"] = QJsonValue::fromVariant(QVariant::fromValue(this->_entity_id)); - obj["entity_name"] = this->_runtime_name; - obj["entity_templet"] = this->_templet_name; - - QJsonArray array; - for (auto c : this->_comps_list) - { - QJsonObject comp_obj; - comp_obj["component_type"] = c->name(); - c->saveTo(comp_obj); - array.append(comp_obj); - } - obj["component_array"] = array; -} - -QString RtEntityManager::templetName() const -{ - return "实体管理器"; -} - -uint64_t RtEntityManager::entityID() const -{ - return const_id; -} - -QString RtEntityManager::name() const -{ - return "实体管理器唯一实例"; -} - -void RtEntityManager::append(std::shared_ptr ins) { - _comps_list[ins->name()] = ins; -} - -std::shared_ptr RtEntityManager::defaultNew() const -{ - return nullptr; -} - -QList RtEntityManager::getEntryWithSignature(const WsRespondSignatureType& t) const -{ - QList list; - for (auto c : this->_comps_list) - list.append(c->getEntryWithSignature(t)); - return list; -} - -QList RtEntityManager::getEntryWithInType(const QString& msg_type) const -{ - QList list; - for (auto c : this->_comps_list) - list.append(c->getEntrysWithInType(msg_type)); - return list; -} - -void RtEntityManager::recoveryFrom(const QJsonObject& obj) -{ - auto array = obj["component_array"].toArray(); - for (auto idx = 0; idx < array.size(); ++idx) { - auto value = array.at(idx).toObject(); - auto type = value["component_type"].toString(); - - auto compins = this->_comps_list[type]; - if (!compins) - throw new UniException(type + "类型插件不存在!"); - - compins->recoveryFrom(value); - } -} - -void RtEntityManager::saveTo(QJsonObject& obj) const -{ - QJsonArray array; - for (auto c : this->_comps_list) - { - QJsonObject comp_obj; - comp_obj["component_type"] = c->name(); - c->saveTo(comp_obj); - array.append(comp_obj); - } - obj["component_array"] = array; -} - -void RtEntityManager::remove(const QString& component_type) -{ - this->_comps_list.remove(component_type); -} - -QList> RtEntityManager::components() const -{ - return this->_comps_list.values(); -} - -ImmediateKernel::ImmediateKernel(std::shared_ptr bind) :_bind_entity(bind) {} - -uint64_t ImmediateKernel::entityManagerID() const -{ - return RtEntityManager::const_id; -} - -QList> ImmediateKernel::execute(const WsRespondSignatureType& resp_signature, std::shared_ptr in) -{ - QList> rets; - auto list = this->_bind_entity->getEntryWithSignature(resp_signature); - for (auto func : list) { - func(shared_from_this(), in, rets); - } - return rets; -} - -QList> ImmediateKernel::execute(std::shared_ptr in) -{ - QList> rets; - auto list = this->_bind_entity->getEntryWithInType(in->topicString()); - for (auto func : list) { - func(shared_from_this(), in, rets); - } - return rets; -} diff --git a/SimsBasic/simsbasic.h b/SimsBasic/simsbasic.h index c43b244..0c3c165 100644 --- a/SimsBasic/simsbasic.h +++ b/SimsBasic/simsbasic.h @@ -203,144 +203,3 @@ public: /// virtual QList> components() const = 0; }; - -#include -/// -/// 内存实体实例类型 -/// -class SIMSBASIC_EXPORT RtWsEntity : public WsEntity, public ComponentSet, public std::enable_shared_from_this { -private: - uint64_t _entity_id = 0; - QString _templet_name, _runtime_name; - QHash> _comps_list; - -public: - explicit RtWsEntity(); - virtual ~RtWsEntity() = default; - - /// - /// 重置本实例模板名 - /// - /// 模板名称 - virtual void resetTemplet(const QString& name); - /// - /// 获取本实例的模板名 - /// - /// 模板名称 - virtual QString templetName() const; - - /// - /// 重置本实例的运行ID - /// - /// - virtual void resetID(uint64_t id); - /// - /// 获取本实例的运行ID - /// - /// - virtual uint64_t entityID() const; - /// - /// 重置本实例的运行名称 - /// - /// - virtual void resetName(const QString& name); - /// - /// 获取本实例的运行名称 - /// - /// 运行名称 - virtual QString name() const; - - /// - /// 为本实例添加指定类型的插件 - /// - /// - virtual void append(std::shared_ptr ins) override; - /// - /// 移除指定类型的插件实例 - /// - /// - virtual void remove(const QString& component_type) override; - /// - /// 获取本实例内包含的所有插件实例 - /// - /// - virtual QList> components() const override; - - /// - /// 深度克隆本实例,插件和数据一致 - /// - /// - virtual std::shared_ptr defaultNew() const; - - /// - /// 允许输入的消息类型 - /// - /// 消息类型集合 - virtual QList inputTypes() const; - /// - /// 通过指定的签名获取处理入口 - /// - /// 签名 - /// 处理接口 - virtual QList getEntryWithSignature(const WsRespondSignatureType& t) const; - /// - /// 通过输入消息类型获取处理入口 - /// - /// 输入消息类型 - /// 处理接口列表 - virtual QList getEntryWithInType(const QString& msg_type) const; - - // 通过 Serializable 继承 - void recoveryFrom(const QJsonObject& obj) override; - void saveTo(QJsonObject& obj) const override; -}; - - -class SIMSBASIC_EXPORT RtEntityManager : public WsEntity, public ComponentSet, - public std::enable_shared_from_this -{ -public: - static const uint64_t const_id = 0x01ff; - QHash> _comps_list; - - QString templetName() const override; - uint64_t entityID() const override; - QString name() const override; - void append(std::shared_ptr ins) override; - void remove(const QString& component_type) override; - QList> components() const override; - - std::shared_ptr defaultNew() const override; - QList getEntryWithSignature(const WsRespondSignatureType& t) const override; - QList getEntryWithInType(const QString& msg_type) const override; - - void recoveryFrom(const QJsonObject& obj) override; - void saveTo(QJsonObject& obj) const override; -}; - - -class SIMSBASIC_EXPORT ImmediateKernel : public Immediate, public std::enable_shared_from_this { -private: - std::shared_ptr _bind_entity = nullptr; - -public: - ImmediateKernel(std::shared_ptr bind); - /// - /// 获取实体管理器id - /// - /// - virtual uint64_t entityManagerID() const override; - /// - /// 实体内指定类型的调用立即执行 - /// - /// - /// - virtual QList> execute(const WsRespondSignatureType& resp_signature, std::shared_ptr in) override; - /// - /// 实体内兼容调用立即执行 - /// - /// - /// - virtual QList> execute(std::shared_ptr in) override; - -}; \ No newline at end of file