Skip to content

Commit

Permalink
[new] 新增cpp-rawptr-bin目标
Browse files Browse the repository at this point in the history
[change] cpp-bin目标改名为cpp-sharedptr-bin
  • Loading branch information
pirunxi committed Mar 23, 2024
1 parent 5aeb05b commit d9de3de
Show file tree
Hide file tree
Showing 28 changed files with 484 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Luban.Cpp.CodeTarget;

[CodeTarget("cpp-bin")]
public class CppBinCodeTarget : CppCodeTargetBase
[CodeTarget("cpp-rawptr-bin")]
public class CppBinRawptrCodeTarget : CppCodeTargetBase
{
protected override void OnCreateTemplateContext(TemplateContext ctx)
{
base.OnCreateTemplateContext(ctx);
ctx.PushGlobal(new CppBinTemplateExtension());
ctx.PushGlobal(new CppRawptrBinTemplateExtension());
}
}
15 changes: 15 additions & 0 deletions src/Luban.Cpp/CodeTarget/CppBinSharedptrCodeTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Luban.CodeTarget;
using Luban.Cpp.TemplateExtensions;
using Scriban;

namespace Luban.Cpp.CodeTarget;

[CodeTarget("cpp-sharedptr-bin")]
public class CppBinSharedptrCodeTarget : CppCodeTargetBase
{
protected override void OnCreateTemplateContext(TemplateContext ctx)
{
base.OnCreateTemplateContext(ctx);
ctx.PushGlobal(new CppSharedptrBinTemplateExtension());
}
}
29 changes: 20 additions & 9 deletions src/Luban.Cpp/Luban.Cpp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@
</ItemGroup>

<ItemGroup>
<None Update="Templates\cpp-rawptr-bin\bean.sbn">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Templates\cpp-rawptr-bin\schema_cpp.sbn">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Templates\cpp-rawptr-bin\schema_h.sbn">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Templates\cpp-rawptr-bin\table.sbn">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Templates\cpp-rawptr-bin\tables.sbn">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Templates\cs-bin\bean.sbn">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand All @@ -32,28 +47,24 @@
<None Update="Templates\common\cs\enum.sbn">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Templates\cpp-bin\bean.sbn">
<None Update="Templates\cpp-sharedptr-bin\bean.sbn">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Templates\cpp-bin\schema_cpp.sbn">
<None Update="Templates\cpp-sharedptr-bin\schema_cpp.sbn">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Templates\cpp-bin\schema_h.sbn">
<None Update="Templates\cpp-sharedptr-bin\schema_h.sbn">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Templates\cpp-bin\table.sbn">
<None Update="Templates\cpp-sharedptr-bin\table.sbn">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Templates\cpp-bin\tables.sbn">
<None Update="Templates\cpp-sharedptr-bin\tables.sbn">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Templates\common\cpp\enum.sbn">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<Folder Include="TypeVisitors\" />
</ItemGroup>

</Project>
13 changes: 0 additions & 13 deletions src/Luban.Cpp/TemplateExtensions/CppBinTemplateExtension.cs

This file was deleted.

18 changes: 18 additions & 0 deletions src/Luban.Cpp/TemplateExtensions/CppRawptrBinTemplateExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Luban.Cpp.TypeVisitors;
using Luban.Types;
using Scriban.Runtime;

namespace Luban.Cpp.TemplateExtensions;

public class CppRawptrBinTemplateExtension : ScriptObject
{
public static string Deserialize(string bufName, string fieldName, TType type)
{
return type.Apply(CppRawptrDeserializeVisitor.Ins, bufName, fieldName);
}

public static string DeclaringTypeName(TType type)
{
return type.Apply(CppRawptrDeclaringTypeNameVisitor.Ins);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Luban.Cpp.TypeVisitors;
using Luban.Types;
using Scriban.Runtime;

namespace Luban.Cpp.TemplateExtensions;

public class CppSharedptrBinTemplateExtension : ScriptObject
{
public static string Deserialize(string bufName, string fieldName, TType type)
{
return type.Apply(CppSharedptrDeserializeVisitor.Ins, bufName, fieldName);
}

public static string DeclaringTypeName(TType type)
{
return type.Apply(CppSharedptrDeclaringTypeNameVisitor.Ins);
}
}
5 changes: 0 additions & 5 deletions src/Luban.Cpp/TemplateExtensions/CppTemplateExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ public static string MakeCppName(string typeName)
return TypeUtil.MakeCppFullName("", typeName);
}

public static string DeclaringTypeName(TType type)
{
return type.Apply(DeclaringTypeNameVisitor.Ins);
}

public static string GetterName(string originName)
{
var words = originName.Split('_').Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
Expand Down
32 changes: 32 additions & 0 deletions src/Luban.Cpp/Templates/cpp-rawptr-bin/bean.sbn
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{{namespace_with_grace_begin __namespace}}

{{~if __this.comment != '' ~}}
/**
* {{escape_comment __this.comment}}
*/
{{~end~}}
struct {{__name}} : public{{if __parent_def_type}} {{make_cpp_name __parent_def_type.full_name}} {{else}} luban::CfgBean {{end}}
{
static bool deserialize{{__name}}(::luban::ByteBuf& _buf, {{__name}}*& _out);

virtual ~{{__name}}() {}

bool deserialize(::luban::ByteBuf& _buf);

{{~ for field in __export_fields ~}}
{{~if field.comment != '' ~}}
/**
* {{escape_comment field.comment}}
*/
{{~end~}}
{{declaring_type_name field.ctype}} {{format_field_name __code_style field.name}};
{{~end~}}

{{~if !__this.is_abstract_type~}}
static constexpr int __ID__ = {{__this.id}};

int getTypeId() const override { return __ID__; }
{{~end~}}
};

{{namespace_with_grace_end __namespace}}
45 changes: 45 additions & 0 deletions src/Luban.Cpp/Templates/cpp-rawptr-bin/schema_cpp.sbn
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "{{__schema_header_file}}"

{{namespace_with_grace_begin __top_module}}

{{~for bean in __beans~}}

bool {{make_cpp_name bean.full_name}}::deserialize(::luban::ByteBuf& _buf)
{
{{~if bean.parent_def_type~}}
if (!{{make_cpp_name bean.parent_def_type.full_name}}::deserialize(_buf))
{
return false;
}
{{~end~}}

{{~ for field in bean.export_fields ~}}
{{deserialize '_buf' (format_field_name __code_style field.name) field.ctype}}
{{~end~}}

return true;
}

{{~if bean.is_abstract_type~}}
bool {{make_cpp_name bean.full_name}}::deserialize{{bean.name}}(::luban::ByteBuf& _buf, {{make_cpp_name bean.full_name}}*& _out)
{
int32_t id;
if (!_buf.readInt(id)) return false;
switch (id)
{
{{~for child in bean.hierarchy_not_abstract_children~}}
case {{make_type_cpp_name child}}::__ID__: { _out = LUBAN_NEW({{make_type_cpp_name child}}){}; if (_out->deserialize(_buf)) { return true; } else { _out = nullptr; return false;} }
{{~end~}}
default: { _out = nullptr; return false;}
}
}
{{~else~}}
bool {{make_cpp_name bean.full_name}}::deserialize{{bean.name}}(::luban::ByteBuf& _buf, {{make_cpp_name bean.full_name}}*& _out)
{
_out = LUBAN_NEW({{make_type_cpp_name bean}}){};
return _out->deserialize(_buf);
}
{{~end~}}

{{~end~}}
{{namespace_with_grace_end __top_module}}
134 changes: 134 additions & 0 deletions src/Luban.Cpp/Templates/cpp-rawptr-bin/table.sbn
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
{{namespace_with_grace_begin __namespace}}

{{~if __this.comment != '' ~}}
/**
* {{escape_comment __this.comment}}
*/
{{~end~}}

class {{__name}}
{
{{~if __this.is_map_table ~}}
private:
::luban::HashMap<{{declaring_type_name __key_type}}, {{declaring_type_name __value_type}}> _dataMap;
::luban::Vector<{{declaring_type_name __value_type}}> _dataList;

public:
~{{__name}}()
{
for (auto& _v : _dataList)
{
LUBAN_FREE(_v);
}
}

bool load(::luban::ByteBuf& _buf)
{
int n;
if (!_buf.readSize(n)) return false;
for(; n > 0 ; --n)
{
{{declaring_type_name __value_type}} _v;
{{deserialize '_buf' '_v' __value_type}}
_dataList.push_back(_v);
_dataMap[_v->{{format_field_name __code_style __this.index_field.name}}] = _v;
}
return true;
}

const ::luban::HashMap<{{declaring_type_name __key_type}}, {{declaring_type_name __value_type}}>& getDataMap() const { return _dataMap; }
const ::luban::Vector<{{declaring_type_name __value_type}}>& getDataList() const { return _dataList; }

{{declaring_type_name __value_type}} get({{declaring_type_name __key_type}} key)
{
auto it = _dataMap.find(key);
return it != _dataMap.end() ? it->second : nullptr;
}

{{~else if __this.is_list_table~}}
private:
::luban::Vector<{{declaring_type_name __value_type}}> _dataList;
{{~if __this.is_union_index~}}

{{~else if !__this.index_list.empty?~}}
{{~for idx in __this.index_list~}}
::luban::HashMap<{{declaring_type_name idx.type}}, {{declaring_type_name __value_type}}> _dataMap_{{idx.index_field.name}};
{{~end~}}
{{~else~}}
{{~end~}}

public:
bool load(::luban::ByteBuf& _buf)
{
int n;
if (!_buf.readSize(n)) return false;
for(; n > 0 ; --n)
{
{{declaring_type_name __value_type}} _v;
{{deserialize '_buf' '_v' __value_type}}
_dataList.push_back(_v);
{{~if __this.is_union_index~}}

{{~else if !__this.index_list.empty?~}}
{{~for idx in __this.index_list~}}
_dataMap_{{idx.index_field.name}}[_v->{{idx.index_field.name}}] = _v;
{{~end~}}
{{~else~}}
{{~end~}}
}
return true;
}

const ::luban::Vector<{{declaring_type_name __value_type}}>& getDataList() const { return _dataList; }

{{~if __this.is_union_index~}}

{{~else if !__this.index_list.empty?~}}
{{~for idx in __this.index_list~}}
::luban::HashMap<{{declaring_type_name idx.type}}, {{declaring_type_name __value_type}}>& getDataMapBy{{idx.index_field.name}}()
{
return _dataMap_{{idx.index_field.name}};
}

{{declaring_type_name __value_type}} getBy{{idx.index_field.name}}({{declaring_type_name idx.type}} key)
{
auto it = _dataMap_{{idx.index_field.name}}.find(key);
return it != _dataMap_{{idx.index_field.name}}.end() ? it->second : nullptr;
}
{{~end~}}
{{~else~}}

{{declaring_type_name __value_type}} get(size_t index) const
{
return _dataList[index];
}
{{~end~}}
{{~else~}}
private:
{{declaring_type_name __value_type}} _data;

public:
{{declaring_type_name __value_type}} data() const { return _data; }

bool load(::luban::ByteBuf& _buf)
{
int n;
if (!_buf.readSize(n)) return false;
if (n != 1) return false;
{{deserialize '_buf' '_data' __value_type}}
return true;
}


{{~ for field in __value_type.def_bean.hierarchy_export_fields ~}}
{{~if field.comment != '' ~}}
/**
* {{escape_comment field.comment}}
*/
{{~end~}}
{{declaring_type_name field.ctype}}& {{getter_name field.name}}() const { return _data->{{format_field_name __code_style field.name}}; }
{{~end~}}
{{~end~}}
};

{{namespace_with_grace_end __namespace}}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ bool {{make_cpp_name bean.full_name}}::deserialize(::luban::ByteBuf& _buf)
bool {{make_cpp_name bean.full_name}}::deserialize{{bean.name}}(::luban::ByteBuf& _buf, ::luban::SharedPtr<{{make_cpp_name bean.full_name}}>& _out)
{
{{~if bean.is_abstract_type~}}
int id;
int32_t id;
if (!_buf.readInt(id)) return false;
switch (id)
{
Expand All @@ -47,4 +47,4 @@ bool {{make_cpp_name bean.full_name}}::deserialize{{bean.name}}(::luban::ByteBuf
}

{{~end~}}
{{namespace_with_grace_end __top_module}}
{{namespace_with_grace_end __top_module}}
Loading

0 comments on commit d9de3de

Please sign in to comment.