From 3825de6b07e3b73c61b7fcf2ea6466f6c4bb5db4 Mon Sep 17 00:00:00 2001 From: walon Date: Wed, 11 Aug 2021 16:10:55 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E8=B0=83=E6=95=B4=E3=80=91cfg=20c++?= =?UTF-8?q?=20=E5=AF=B9=E4=BA=8E=E5=8F=AF=E7=A9=BA=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=20shared=5Fptr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Source/Generate/CppCodeBinRender.cs | 17 +++++++++++------ src/Luban.Job.Cfg/Source/JobController.cs | 1 + .../TypeVisitors/CppDeserializeVisitor.cs | 2 +- .../Source/TypeVisitors/CppDefineTypeName.cs | 4 ++-- ...eName.cs => CppRawUnderingDefineTypeName.cs} | 8 ++++---- .../CppSharedPtrUnderingDefineTypeName.cs | 14 ++++++++++++++ 6 files changed, 33 insertions(+), 13 deletions(-) rename src/Luban.Job.Common/Source/TypeVisitors/{CppUnderingDefineTypeName.cs => CppRawUnderingDefineTypeName.cs} (90%) create mode 100644 src/Luban.Job.Common/Source/TypeVisitors/CppSharedPtrUnderingDefineTypeName.cs diff --git a/src/Luban.Job.Cfg/Source/Generate/CppCodeBinRender.cs b/src/Luban.Job.Cfg/Source/Generate/CppCodeBinRender.cs index 4b18f53..d1e72da 100644 --- a/src/Luban.Job.Cfg/Source/Generate/CppCodeBinRender.cs +++ b/src/Luban.Job.Cfg/Source/Generate/CppCodeBinRender.cs @@ -47,7 +47,7 @@ class {{name}} : public {{if parent_def_type}} {{parent_def_type.cpp_full_name}} { public: - static bool deserialize{{name}}(ByteBuf& _buf, {{name}}*& _out); + static bool deserialize{{name}}(ByteBuf& _buf, std::shared_ptr<{{name}}>& _out); {{name}}() { @@ -143,6 +143,12 @@ class {{name}} const std::unordered_map<{{cpp_define_type key_type}}, {{cpp_define_type value_type}}>& getDataMap() const { return _dataMap; } const std::vector<{{cpp_define_type value_type}}>& getDataList() const { return _dataList; } + {{value_type.bean.cpp_full_name}}* getRaw({{cpp_define_type key_type}} key) + { + auto it = _dataMap.find(key); + return it != _dataMap.end() ? it->second.get() : nullptr; + } + {{cpp_define_type value_type}} get({{cpp_define_type key_type}} key) { auto it = _dataMap.find(key); @@ -267,7 +273,7 @@ namespace {{x.top_module}} return true; } - bool {{type.cpp_full_name}}::deserialize{{type.name}}(ByteBuf& _buf, {{type.cpp_full_name}}*& _out) + bool {{type.cpp_full_name}}::deserialize{{type.name}}(ByteBuf& _buf, std::shared_ptr<{{type.cpp_full_name}}>& _out) { {{~if type.is_abstract_type~}} int id; @@ -275,20 +281,19 @@ namespace {{x.top_module}} switch (id) { {{~for child in type.hierarchy_not_abstract_children~}} - case {{child.cpp_full_name}}::ID: { _out = new {{child.cpp_full_name}}(); if (_out->deserialize(_buf)) { return true; } else { delete _out; _out = nullptr; return false;} } + case {{child.cpp_full_name}}::ID: { _out.reset(new {{child.cpp_full_name}}()); if (_out->deserialize(_buf)) { return true; } else { _out.reset(); return false;} } {{~end~}} default: { _out = nullptr; return false;} } {{~else~}} - _out = new {{type.cpp_full_name}}(); + _out.reset(new {{type.cpp_full_name}}()); if (_out->deserialize(_buf)) { return true; } else { - delete _out; - _out = nullptr; + _out.reset(); return false; } {{~end~}} diff --git a/src/Luban.Job.Cfg/Source/JobController.cs b/src/Luban.Job.Cfg/Source/JobController.cs index a3fdb8f..3c84160 100644 --- a/src/Luban.Job.Cfg/Source/JobController.cs +++ b/src/Luban.Job.Cfg/Source/JobController.cs @@ -625,6 +625,7 @@ namespace Luban.Job.Cfg @$" #pragma once #include +#include #include ""bright/serialization/ByteBuf.h"" #include ""bright/CfgBean.hpp"" diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/CppDeserializeVisitor.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/CppDeserializeVisitor.cs index d1d8850..a05b979 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/CppDeserializeVisitor.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/CppDeserializeVisitor.cs @@ -11,7 +11,7 @@ namespace Luban.Job.Cfg.TypeVisitors { if (type.IsNullable) { - return $"{{ bool _has_value_; if(!{bufName}.readBool(_has_value_)){{return false;}} if(_has_value_) {{{type.Apply(CppUnderingDefineTypeName.Ins)} _temp_; {type.Apply(CppUnderingDeserializeVisitor.Ins, bufName, "_temp_")} {(type.IsBean ? $"{fieldName} = _temp_;" : $"{fieldName} = new {type.Apply(CppUnderingDefineTypeName.Ins)}; *{fieldName} = _temp_;")} }} else {{ {fieldName} = nullptr; }} }}"; + return $"{{ bool _has_value_; if(!{bufName}.readBool(_has_value_)){{return false;}} if(_has_value_) {{ {fieldName}.reset({(type.IsBean ? "" : $"new {type.Apply(CppRawUnderingDefineTypeName.Ins)}()")}); {type.Apply(CppUnderingDeserializeVisitor.Ins, bufName, $"{(type.IsBean ? "" : "*")}{fieldName}")} }} else {{ {fieldName}.reset(); }} }}"; } else { diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CppDefineTypeName.cs b/src/Luban.Job.Common/Source/TypeVisitors/CppDefineTypeName.cs index cd8063d..e76a4d3 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/CppDefineTypeName.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CppDefineTypeName.cs @@ -9,12 +9,12 @@ namespace Luban.Job.Common.TypeVisitors public override string DoAccept(TType type) { //return type.IsNullable ? $"std::optional<{type.Apply(CppUnderingDefineTypeName.Ins)}>" : type.Apply(CppUnderingDefineTypeName.Ins); - return type.Apply(CppUnderingDefineTypeName.Ins) + (type.IsNullable ? "*" : ""); + return type.IsNullable ? $"std::shared_ptr<{type.Apply(CppSharedPtrUnderingDefineTypeName.Ins)}>" : type.Apply(CppSharedPtrUnderingDefineTypeName.Ins); } public override string Accept(TBean type) { - return type.Apply(CppUnderingDefineTypeName.Ins); + return type.Apply(CppSharedPtrUnderingDefineTypeName.Ins); } } } diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CppUnderingDefineTypeName.cs b/src/Luban.Job.Common/Source/TypeVisitors/CppRawUnderingDefineTypeName.cs similarity index 90% rename from src/Luban.Job.Common/Source/TypeVisitors/CppUnderingDefineTypeName.cs rename to src/Luban.Job.Common/Source/TypeVisitors/CppRawUnderingDefineTypeName.cs index 358474a..935fc01 100644 --- a/src/Luban.Job.Common/Source/TypeVisitors/CppUnderingDefineTypeName.cs +++ b/src/Luban.Job.Common/Source/TypeVisitors/CppRawUnderingDefineTypeName.cs @@ -2,9 +2,9 @@ using Luban.Job.Common.Types; namespace Luban.Job.Common.TypeVisitors { - public class CppUnderingDefineTypeName : ITypeFuncVisitor + public class CppRawUnderingDefineTypeName : ITypeFuncVisitor { - public static CppUnderingDefineTypeName Ins { get; } = new CppUnderingDefineTypeName(); + public static CppRawUnderingDefineTypeName Ins { get; } = new(); public string Accept(TBool type) { @@ -76,9 +76,9 @@ namespace Luban.Job.Common.TypeVisitors return "std::string"; } - public string Accept(TBean type) + public virtual string Accept(TBean type) { - return type.Bean.CppFullName + "*"; + return $"{type.Bean.CppFullName}*"; } public string Accept(TArray type) diff --git a/src/Luban.Job.Common/Source/TypeVisitors/CppSharedPtrUnderingDefineTypeName.cs b/src/Luban.Job.Common/Source/TypeVisitors/CppSharedPtrUnderingDefineTypeName.cs new file mode 100644 index 0000000..e7bdaa5 --- /dev/null +++ b/src/Luban.Job.Common/Source/TypeVisitors/CppSharedPtrUnderingDefineTypeName.cs @@ -0,0 +1,14 @@ +using Luban.Job.Common.Types; + +namespace Luban.Job.Common.TypeVisitors +{ + public class CppSharedPtrUnderingDefineTypeName : CppRawUnderingDefineTypeName + { + public new static CppSharedPtrUnderingDefineTypeName Ins { get; } = new(); + + public override string Accept(TBean type) + { + return $"std::shared_ptr<{type.Bean.CppFullName}>"; + } + } +}