【调整】cfg c++ 对于可空类型使用 shared_ptr

main
walon 2021-08-11 16:10:55 +08:00
parent 9edb704199
commit 3825de6b07
6 changed files with 33 additions and 13 deletions

View File

@ -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~}}

View File

@ -625,6 +625,7 @@ namespace Luban.Job.Cfg
@$"
#pragma once
#include <functional>
#include <memory>
#include ""bright/serialization/ByteBuf.h""
#include ""bright/CfgBean.hpp""

View File

@ -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
{

View File

@ -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);
}
}
}

View File

@ -2,9 +2,9 @@ using Luban.Job.Common.Types;
namespace Luban.Job.Common.TypeVisitors
{
public class CppUnderingDefineTypeName : ITypeFuncVisitor<string>
public class CppRawUnderingDefineTypeName : ITypeFuncVisitor<string>
{
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)

View File

@ -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}>";
}
}
}