update liquid template

This commit is contained in:
ouczbs 2024-04-26 19:22:14 +08:00
parent b4fa0405fc
commit fd98728283
8 changed files with 390 additions and 133 deletions

View File

@ -53,6 +53,10 @@ namespace refl
ClassList = new List<ClassMeta>(); ClassList = new List<ClassMeta>();
ChildList = new List<ModuleMeta>(); ChildList = new List<ModuleMeta>();
} }
public bool HasMeta()
{
return ClassList.Count > 0 || ChildList.Count > 0;
}
public static ClassMeta ParseCppClass(CppClass cppClass) public static ClassMeta ParseCppClass(CppClass cppClass)
{ {
string parentName = "void"; string parentName = "void";

View File

@ -23,6 +23,7 @@ namespace refl
public string NameSpace { get; set; } = ""; public string NameSpace { get; set; } = "";
public string ParentName { get; set; } = ""; public string ParentName { get; set; } = "";
public string Indent { get; set; } = ""; public string Indent { get; set; } = "";
public string Indent2 { get; set; } = "";
public string MetaName { get; set; } = ""; public string MetaName { get; set; } = "";
public List<FieldMetaGenData> FieldList = new List<FieldMetaGenData>(); public List<FieldMetaGenData> FieldList = new List<FieldMetaGenData>();
@ -30,6 +31,14 @@ namespace refl
{ {
Indent = indent; Indent = indent;
NameSpace = nameSpace; NameSpace = nameSpace;
if (string.IsNullOrEmpty(nameSpace))
{
Indent2 = indent;
}
else
{
Indent2 = indent + "\t";
}
} }
} }
@ -55,12 +64,15 @@ namespace refl
{ {
foreach (var name in ModuleMeta.NameSet) foreach (var name in ModuleMeta.NameSet)
{ {
FileList.Add(name, new StringBuilder()); var build = new StringBuilder();
build.AppendLine("#pragma once");
FileList.Add(name, build);
} }
GenModuleMeta(module, null); GenModuleMeta(module, null);
string file_name = Path.GetFileName(target); string file_name = Path.GetFileName(target);
string? dir = Path.GetDirectoryName(target); string? dir = Path.GetDirectoryName(target);
var output = new StringBuilder(); var output = new StringBuilder();
output.AppendLine("#pragma once");
foreach (var pair in FileList) foreach (var pair in FileList)
{ {
string path = $"{pair.Key}_{file_name}".ToLower(); string path = $"{pair.Key}_{file_name}".ToLower();
@ -75,7 +87,15 @@ namespace refl
} }
public static void GenModuleMeta(ModuleMeta module, string? prefix) public static void GenModuleMeta(ModuleMeta module, string? prefix)
{ {
if (!module.HasMeta())
{
return;
}
var gen = new ClassMetaGenData(prefix+"", module.NameSpace); var gen = new ClassMetaGenData(prefix+"", module.NameSpace);
if (prefix != null)
{
GenNamespaceBegin(gen);
}
foreach (var cls in module.ClassList) foreach (var cls in module.ClassList)
{ {
GenClassMeta(cls, gen); GenClassMeta(cls, gen);
@ -84,13 +104,32 @@ namespace refl
{ {
if(prefix == null) if(prefix == null)
{ {
GenModuleMeta(child, prefix + "\r"); GenModuleMeta(child, "");
} }
else else
{ {
GenModuleMeta(child, prefix + "\t"); GenModuleMeta(child, prefix + "\t");
} }
} }
if (prefix != null)
{
GenNamespaceEnd(gen);
}
}
public static void GenNamespaceBegin(ClassMetaGenData gen)
{
foreach (var name in ModuleMeta.NameSet)
{
string line = $"{gen.Indent}namespace {gen.NameSpace} {{";
FileList[name].AppendLine(line);
}
}
public static void GenNamespaceEnd(ClassMetaGenData gen)
{
foreach (var name in ModuleMeta.NameSet)
{
FileList[name].AppendLine($"{gen.Indent}}}");
}
} }
public static void GenClassMeta(ClassMeta cls, ClassMetaGenData gen) public static void GenClassMeta(ClassMeta cls, ClassMetaGenData gen)
{ {

View File

@ -2,7 +2,7 @@
"profiles": { "profiles": {
"refl": { "refl": {
"commandName": "Project", "commandName": "Project",
"commandLineArgs": "build F:\\csharp\\CppAst.NET\\src\\refl\\cpp\\vertex.h -o F:\\csharp\\CppAst.NET\\src\\refl\\cpp\\vertex_gen.inl -t F:\\csharp\\CppAst.NET\\src\\refl\\template\\refl.liquid -v" "commandLineArgs": "build F:\\csharp\\cppast\\src\\refl\\cpp\\vertex.h -o F:\\csharp\\cppast\\src\\refl\\cpp\\vertex_gen.inl -t F:\\csharp\\cppast\\src\\refl\\template\\refl.liquid -v"
} }
} }
} }

View File

@ -1,34 +1,220 @@
struct vec3_Static_Meta { #pragma once
struct TestMain1_Static_Meta {
consteval static auto __StaticFields() { consteval static auto __StaticFields() {
return std::make_tuple(&vec3::x, &vec3::y, &vec3::z, &vec3::norm); return std::make_tuple(&TestMain1::x, &TestMain1::y, &TestMain1::z);
}; };
consteval static auto __MakeStaticFields() { consteval static auto __MakeStaticFields() {
return std::array{ return std::array{
StaticMemberField(&vec3::x, FName("x"), { 0.f}), refl::StaticMemberField(&TestMain1::x, FName("x"), {}),
StaticMemberField(&vec3::y, FName("y"), { 0.f}), refl::StaticMemberField(&TestMain1::y, FName("y"), {}),
StaticMemberField(&vec3::z, FName("z"), { 0.f}), refl::StaticMemberField(&TestMain1::z, FName("z"), {}),
StaticMethodField(&vec3::norm, FName("norm"), { {3,4} }),
}; };
}; };
consteval static int Size() { consteval static int Size() {
return fetch_meta_size<vec3_Static_Meta>(); return refl::fetch_meta_size<TestMain1_Static_Meta>();
}; };
}; };
struct vec3_Meta : public Meta { struct TestMain1_Meta : public refl::Meta {
using MyStatic = vec3_Static_Meta; using MyStatic = TestMain1_Static_Meta;
using MyUClass = UClass_Meta<vec3, vec3_parent>;; using MyUClass = refl::UClass_Meta<TestMain1, void>;;
inline static char s_data[MyStatic::Size()]{}; inline static char s_data[MyStatic::Size()]{};
static auto __MakeFields() { static auto __MakeFields() {
char* memory = &s_data[0]; char* memory = &s_data[0];
return std::array{ return std::array{
StaticMemberField(&vec3::x, FName("x"), { 0.f}), MemberField(&TestMain1::x, FName("x"), memory, {}),
StaticMemberField(&vec3::y, FName("y"), { 0.f}), MemberField(&TestMain1::y, FName("y"), memory, {}),
StaticMemberField(&vec3::z, FName("z"), { 0.f}), MemberField(&TestMain1::z, FName("z"), memory, {}),
StaticMethodField(&vec3::norm, FName("norm"), { {3,4} }),
}; };
}; };
}; };
namespace engineapi {
struct Vector3_Static_Meta {
consteval static auto __StaticFields() {
return std::make_tuple(&Vector3::x, &Vector3::y, &Vector3::z);
};
consteval static auto __MakeStaticFields() {
return std::array{
refl::StaticMemberField(&Vector3::x, FName("x"), {}),
refl::StaticMemberField(&Vector3::y, FName("y"), {}),
refl::StaticMemberField(&Vector3::z, FName("z"), {}),
};
};
consteval static int Size() {
return refl::fetch_meta_size<Vector3_Static_Meta>();
};
};
struct Vector3_Meta : public refl::Meta {
using MyStatic = Vector3_Static_Meta;
using MyUClass = refl::UClass_Meta<Vector3, void>;;
inline static char s_data[MyStatic::Size()]{};
static auto __MakeFields() {
char* memory = &s_data[0];
return std::array{
MemberField(&Vector3::x, FName("x"), memory, {}),
MemberField(&Vector3::y, FName("y"), memory, {}),
MemberField(&Vector3::z, FName("z"), memory, {}),
};
};
};
struct Vector2_Static_Meta {
consteval static auto __StaticFields() {
return std::make_tuple(&Vector2::x, &Vector2::y);
};
consteval static auto __MakeStaticFields() {
return std::array{
refl::StaticMemberField(&Vector2::x, FName("x"), { 1.0 }),
refl::StaticMemberField(&Vector2::y, FName("y"), { 2.0 }),
};
};
consteval static int Size() {
return refl::fetch_meta_size<Vector2_Static_Meta>();
};
};
struct Vector2_Meta : public refl::Meta {
using MyStatic = Vector2_Static_Meta;
using MyUClass = refl::UClass_Meta<Vector2, void>;;
inline static char s_data[MyStatic::Size()]{};
static auto __MakeFields() {
char* memory = &s_data[0];
return std::array{
MemberField(&Vector2::x, FName("x"), memory, { 1.0 }),
MemberField(&Vector2::y, FName("y"), memory, { 2.0 }),
};
};
};
struct PosVertex_Static_Meta {
consteval static auto __StaticFields() {
return std::make_tuple(&PosVertex::Position);
};
consteval static auto __MakeStaticFields() {
return std::array{
refl::StaticMemberField(&PosVertex::Position, FName("Position"), {}),
};
};
consteval static int Size() {
return refl::fetch_meta_size<PosVertex_Static_Meta>();
};
};
struct PosVertex_Meta : public refl::Meta {
using MyStatic = PosVertex_Static_Meta;
using MyUClass = refl::UClass_Meta<PosVertex, Vertex>;;
inline static char s_data[MyStatic::Size()]{};
static auto __MakeFields() {
char* memory = &s_data[0];
return std::array{
MemberField(&PosVertex::Position, FName("Position"), memory, {}),
};
};
};
struct TexVertex_Static_Meta {
consteval static auto __StaticFields() {
return std::make_tuple(&TexVertex::Position, &TexVertex::Normal, &TexVertex::TexCoords);
};
consteval static auto __MakeStaticFields() {
return std::array{
refl::StaticMemberField(&TexVertex::Position, FName("Position"), {}),
refl::StaticMemberField(&TexVertex::Normal, FName("Normal"), {}),
refl::StaticMemberField(&TexVertex::TexCoords, FName("TexCoords"), {}),
};
};
consteval static int Size() {
return refl::fetch_meta_size<TexVertex_Static_Meta>();
};
};
struct TexVertex_Meta : public refl::Meta {
using MyStatic = TexVertex_Static_Meta;
using MyUClass = refl::UClass_Meta<TexVertex, Vertex>;;
inline static char s_data[MyStatic::Size()]{};
static auto __MakeFields() {
char* memory = &s_data[0];
return std::array{
MemberField(&TexVertex::Position, FName("Position"), memory, {}),
MemberField(&TexVertex::Normal, FName("Normal"), memory, {}),
MemberField(&TexVertex::TexCoords, FName("TexCoords"), memory, {}),
};
};
};
struct BoneVertex_Static_Meta {
consteval static auto __StaticFields() {
return std::make_tuple(&BoneVertex::Position, &BoneVertex::TexCoords, &BoneVertex::Normal, &BoneVertex::Tangent, &BoneVertex::Weights, &BoneVertex::BoneIDs);
};
consteval static auto __MakeStaticFields() {
return std::array{
refl::StaticMemberField(&BoneVertex::Position, FName("Position"), {}),
refl::StaticMemberField(&BoneVertex::TexCoords, FName("TexCoords"), {}),
refl::StaticMemberField(&BoneVertex::Normal, FName("Normal"), {}),
refl::StaticMemberField(&BoneVertex::Tangent, FName("Tangent"), {}),
refl::StaticMemberField(&BoneVertex::Weights, FName("Weights"), {}),
refl::StaticMemberField(&BoneVertex::BoneIDs, FName("BoneIDs"), {}),
};
};
consteval static int Size() {
return refl::fetch_meta_size<BoneVertex_Static_Meta>();
};
};
struct BoneVertex_Meta : public refl::Meta {
using MyStatic = BoneVertex_Static_Meta;
using MyUClass = refl::UClass_Meta<BoneVertex, Vertex>;;
inline static char s_data[MyStatic::Size()]{};
static auto __MakeFields() {
char* memory = &s_data[0];
return std::array{
MemberField(&BoneVertex::Position, FName("Position"), memory, {}),
MemberField(&BoneVertex::TexCoords, FName("TexCoords"), memory, {}),
MemberField(&BoneVertex::Normal, FName("Normal"), memory, {}),
MemberField(&BoneVertex::Tangent, FName("Tangent"), memory, {}),
MemberField(&BoneVertex::Weights, FName("Weights"), memory, {}),
MemberField(&BoneVertex::BoneIDs, FName("BoneIDs"), memory, {}),
};
};
};
namespace hello {
struct TestHello_Static_Meta {
consteval static auto __StaticFields() {
return std::make_tuple(&TestHello::x, &TestHello::z);
};
consteval static auto __MakeStaticFields() {
return std::array{
refl::StaticMemberField(&TestHello::x, FName("x"), {}),
refl::StaticMemberField(&TestHello::z, FName("z"), {}),
};
};
consteval static int Size() {
return refl::fetch_meta_size<TestHello_Static_Meta>();
};
};
struct TestHello_Meta : public refl::Meta {
using MyStatic = TestHello_Static_Meta;
using MyUClass = refl::UClass_Meta<TestHello, void>;;
inline static char s_data[MyStatic::Size()]{};
static auto __MakeFields() {
char* memory = &s_data[0];
return std::array{
MemberField(&TestHello::x, FName("x"), memory, {}),
MemberField(&TestHello::z, FName("z"), memory, {}),
};
};
};
}
}

View File

@ -1,28 +1,32 @@
struct vec3_Static_UIMeta { #pragma once
consteval static auto __StaticFields() { namespace engineapi {
return std::make_tuple(&vec3::x); namespace hello {
}; struct TestHello_Static_UIMeta {
consteval static auto __StaticFields() {
return std::make_tuple(&TestHello::y);
};
consteval static auto __MakeStaticFields() { consteval static auto __MakeStaticFields() {
return std::array{ return std::array{
StaticMemberField(&vec3::x, FName("x"), {1.f}), refl::StaticMemberField(&TestHello::y, FName("y"), {}),
}; };
}; };
consteval static int Size() { consteval static int Size() {
return fetch_meta_size<vec3_Static_UIMeta>(); return refl::fetch_meta_size<TestHello_Static_UIMeta>();
}; };
}; };
struct vec3_UIMeta : public Meta {
using MyStatic = vec3_Static_UIMeta;
using MyUClass = UClass_Meta<vec3, vec3_parent>;;
inline static char s_data[MyStatic::Size()]{};
static auto __MakeFields() {
char* memory = &s_data[0];
return std::array{
StaticMemberField(&vec3::x, FName("x"), {1.f}),
};
};
};
struct TestHello_UIMeta : public refl::Meta {
using MyStatic = TestHello_Static_UIMeta;
using MyUClass = refl::UClass_Meta<TestHello, void>;;
inline static char s_data[MyStatic::Size()]{};
static auto __MakeFields() {
char* memory = &s_data[0];
return std::array{
MemberField(&TestHello::y, FName("y"), memory, {}),
};
};
};
}
}

View File

@ -1,45 +1,75 @@
#pragma once
#include <iostream> #include <iostream>
using namespace std; #define MAX_NUM_BONES_PER_VERTEX 4
struct TestMain1 {
__cppast(Meta = {})
float x;
__cppast(Meta = {})
float y;
__cppast(Meta = {})
float z;
};
namespace engineapi {
namespace hello {
struct TestHello{
__cppast(Meta = {})
float x;
__cppast(UIMeta = {})
float y;
__cppast(Meta = {})
float z;
};
}
struct Vector3 {
__cppast(Meta = {})
float x;
__cppast(Meta = {})
float y;
__cppast(Meta = {})
float z;
};
struct Vector2 {
__cppast(Meta = { 1.0 })
float x;
__cppast(Meta = { 2.0 })
float y;
};
struct Vertex {
struct vec3_parent { };
virtual int norm(int x1, int& x2) { struct PosVertex_Meta;
x2 = x1 * x2; struct PosVertex : public Vertex {
return x2; using MyMeta = PosVertex_Meta;
//cout << x2 << "vec3_parent::norm" << endl; __cppast(Meta = {})
} Vector3 Position = {};
}; };
struct vec3_Meta; struct TexVertex_Meta;
struct vec3 : public vec3_parent { struct TexVertex : public Vertex {
using MyMeta = vec3_Meta; using MyMeta = TexVertex_Meta;
__cppast(Meta = { 0.f}, UIMeta = {1.f}) __cppast(Meta = {})
float x = 1; Vector3 Position = {};
__cppast(Meta = { 0.f}) __cppast(Meta = {})
float y = 2; Vector3 Normal = {};
__cppast(Meta = { 0.f}) __cppast(Meta = {})
float z = 3; Vector2 TexCoords = {};
string name{ "hellohellohellohellohellohello" }; };
__cppast(Meta = { {3,4} }) struct BoneVertex_Meta;
int norm(int x1, int& x2)override { struct BoneVertex : public Vertex
int tmp = x1 * 2 + 1; {
x1 = x2; using MyMeta = BoneVertex_Meta;
x2 = tmp; __cppast(Meta = {})
return x2; Vector3 Position = {};
//cout << x2 << "vec3::norm" << endl; __cppast(Meta = {})
} Vector2 TexCoords = {};
virtual float norm1(int& x1) { __cppast(Meta = {})
x1 = x1 * x * y * z; Vector3 Normal = {};
//x = x1; __cppast(Meta = {})
//y = x1 - 1; Vector3 Tangent = {};
//z = x1 - 10; // 骨骼蒙皮数据
//cout << x1 << "::norm1" << endl; __cppast(Meta = {})
return x1; float Weights[MAX_NUM_BONES_PER_VERTEX] = {};
} __cppast(Meta = {})
static void norm2(int x1 = 10) { uint32_t BoneIDs[MAX_NUM_BONES_PER_VERTEX] = {};
cout << x1 << "::norm2" << endl; };
} };
static void norm3(int x1 = 10) { #include "meta_vertex_gen.inl"
x1 = x1 * 10;
cout << x1 << "::norm3" << endl;
}
};
#include "meta.vertex.inl"

View File

@ -1,2 +1,3 @@
#pragma once
#include "meta_vertex_gen.inl" #include "meta_vertex_gen.inl"
#include "uimeta_vertex_gen.inl" #include "uimeta_vertex_gen.inl"

View File

@ -1,48 +1,41 @@
{%- assign indent = Indent -%} {%- assign NewLine = newline_to_br -%}
{%- if NameSpace != blank -%} {{ Indent2 }}struct {{ Name }}_Static_{{MetaName}} {
{{ indent }}namespace {{ NameSpace }} { {{ Indent2 }} consteval static auto __StaticFields() {
{% capture newIndent %}{{ indent }} {% endcapture %} {{ Indent2 }} return std::make_tuple({% for field in FieldList %}&{{Name}}::{{field.Name}}{% unless forloop.last %}, {% endunless %}{% endfor %});
{%- assign indent = newIndent -%} {{ Indent2 }} };
{%- endif -%} {{ NewLine }}
{{ indent }}struct {{ Name }}_Static_{{MetaName}} { {{ Indent2 }} consteval static auto __MakeStaticFields() {
{{ indent }} consteval static auto __StaticFields() { {{ Indent2 }} return std::array{
{{ indent }} return std::make_tuple({% for field in FieldList %}&{{Name}}::{{field.Name}}{% unless forloop.last %}, {% endunless %}{% endfor %}); {{ NewLine }} {%- for field in FieldList -%}
{{ indent }} }; {{ NewLine }} {%- if field.IsMethod -%}
{{ Indent2 }} refl::StaticMethodField(&{{Name}}::{{field.Name}}, FName("{{field.Name}}"){% if field.Meta %}, {{field.Meta}}{% endif %}),
{{ indent }} consteval static auto __MakeStaticFields() { {{ NewLine }} {%- else -%}
{{ indent }} return std::array{ {{ Indent2 }} refl::StaticMemberField(&{{Name}}::{{field.Name}}, FName("{{field.Name}}"){% if field.Meta %}, {{field.Meta}}{% endif %}),
{{ indent }} {%- for field in FieldList -%} {{ NewLine }} {%- endif -%}
{{ indent }} {%- if field.IsMethod -%} {{ NewLine }} {%- endfor -%}
{{ indent }} StaticMethodField(&{{Name}}::{{field.Name}}, FName("{{field.Name}}"){% if field.Meta %}, {{field.Meta}}{% endif %}), {{ Indent2 }} };
{{ indent }} {%- else -%} {{ Indent2 }} };
{{ indent }} StaticMemberField(&{{Name}}::{{field.Name}}, FName("{{field.Name}}"){% if field.Meta %}, {{field.Meta}}{% endif %}), {{ NewLine }}
{{ indent }} {%- endif -%} {{ Indent2 }} consteval static int Size() {
{{ indent }} {%- endfor -%} {{ Indent2 }} return refl::fetch_meta_size<{{ Name }}_Static_{{MetaName}}>();
{{ indent }} }; {{ Indent2 }} };
{{ indent }} }; {{ Indent2 }}};
{{ NewLine }}
{{ indent }} consteval static int Size() { {{ Indent2 }}struct {{ Name }}_{{MetaName}} : public refl::Meta {
{{ indent }} return fetch_meta_size<{{ Name }}_Static_{{MetaName}}>(); {{ Indent2 }} using MyStatic = {{ Name }}_Static_{{MetaName}};
{{ indent }} }; {{ Indent2 }} using MyUClass = refl::UClass_Meta<{{ Name }}, {{ ParentName }}>;;
{{ indent }}}; {{ Indent2 }} inline static char s_data[MyStatic::Size()]{};
{{ Indent2 }} static auto __MakeFields() {
{{ indent }}struct {{ Name }}_{{MetaName}} : public Meta { {{ Indent2 }} char* memory = &s_data[0];
{{ indent }} using MyStatic = {{ Name }}_Static_{{MetaName}}; {{ Indent2 }} return std::array{
{{ indent }} using MyUClass = UClass_Meta<{{ Name }}, {{ ParentName }}>;; {{ NewLine }} {%- for field in FieldList -%}
{{ indent }} inline static char s_data[MyStatic::Size()]{}; {{ NewLine }} {%- if field.IsMethod -%}
{{ indent }} static auto __MakeFields() { {{ Indent2 }} MethodField(&{{Name}}::{{field.Name}}, FName("{{field.Name}}"), memory{% if field.Meta %}, {{field.Meta}}{% endif %}),
{{ indent }} char* memory = &s_data[0]; {{ NewLine }} {%- else -%}
{{ indent }} return std::array{ {{ Indent2 }} MemberField(&{{Name}}::{{field.Name}}, FName("{{field.Name}}"), memory{% if field.Meta %}, {{field.Meta}}{% endif %}),
{{ indent }} {%- for field in FieldList -%} {{ NewLine }} {%- endif -%}
{{ indent }} {%- if field.IsMethod -%} {{ NewLine }} {%- endfor -%}
{{ indent }} StaticMethodField(&{{Name}}::{{field.Name}}, FName("{{field.Name}}"){% if field.Meta %}, {{field.Meta}}{% endif %}), {{ Indent2 }} };
{{ indent }} {%- else -%} {{ Indent2 }} };
{{ indent }} StaticMemberField(&{{Name}}::{{field.Name}}, FName("{{field.Name}}"){% if field.Meta %}, {{field.Meta}}{% endif %}), {{ Indent2 }}};
{{ indent }} {%- endif -%} {{ NewLine }}
{{ indent }} {%- endfor -%}
{{ indent }} };
{{ indent }} };
{{ indent }}};
{% if NameSpace != blank %}
{{ Indent }}}
{% endif %}