Skip to content

Commit

Permalink
Fix test project
Browse files Browse the repository at this point in the history
  • Loading branch information
BentouDev committed Feb 11, 2024
1 parent b2e7472 commit b1552d7
Show file tree
Hide file tree
Showing 16 changed files with 144 additions and 167 deletions.
27 changes: 24 additions & 3 deletions Source/Preprocessor/Core/Generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,17 @@ fs::path Generator::BuildHeaderOutputPath(const std::string& include)
return path;
}

fs::path Generator::BuildUnitOutputPath(const std::string& include)
{
TMustacheView out_name_view(Context.Generator.CurrentPattern->UnitOutName);

fs::path path = Context.Generator.CurrentPattern->OutputDir;
std::string filename = out_name_view.render({ "header_name", include });
path.append(filename);

return path;
}

fs::path Generator::BuildClassOutputPath(std::shared_ptr<ClassInfo> type)
{
TMustacheView out_name_view(Context.Generator.CurrentPattern->ClassOutName);
Expand Down Expand Up @@ -260,8 +271,7 @@ void Generator::GenerateFiles()
include_map[type->FromInclude].enums.push_back(type);
}

for (auto& templ : Context.Generator.CurrentPattern->HeaderTemplates)
{
auto emit_per_file = [&]<typename TFunc>(const std::unique_ptr<MustacheTemplate>& templ, TFunc&& func) {
if (templ->View)
{
for (auto& [include, data] : include_map)
Expand All @@ -286,18 +296,29 @@ void Generator::GenerateFiles()
}
}

mapped_data.set("header_path", include);
mapped_data.set("classes", classes);
mapped_data.set("enums", enums);

fs::path inc_path = include;

fs::path path = BuildHeaderOutputPath(inc_path.filename().string());
fs::path path = func(inc_path.stem().string());
std::fstream file(path, std::ios_base::out);

file << templ->View->render(mapped_data);
file.close();
}
}
};

for (auto& templ : Context.Generator.CurrentPattern->HeaderTemplates)
{
emit_per_file(templ, [&](const std::string& include) { return BuildHeaderOutputPath(include); });
}

for (auto& templ : Context.Generator.CurrentPattern->UnitTemplates)
{
emit_per_file(templ, [&](const std::string& include) { return BuildUnitOutputPath(include); });
}

// Class templates
Expand Down
1 change: 1 addition & 0 deletions Source/Preprocessor/Core/Generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Generator
TMustacheData BuildMethodData (std::shared_ptr<ClassInfo>& type);

fs::path BuildHeaderOutputPath(const std::string& include_name);
fs::path BuildUnitOutputPath(const std::string& include);
fs::path BuildClassOutputPath (std::shared_ptr<ClassInfo> type);
fs::path BuildEnumOutputPath (std::shared_ptr<EnumInfo> type);

Expand Down
2 changes: 1 addition & 1 deletion Source/Preprocessor/Core/Parsing/CursorHandlerFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ namespace Handlers
continue;
}
// Comma separated attributes
else if (parthensis_depth == 0 && isCurrent(idx, ','))
else if (attribute_brackets_depth != 0 && parthensis_depth == 0 && isCurrent(idx, ','))
{
addAtrribute(current_attribute_begin, idx);

Expand Down
5 changes: 5 additions & 0 deletions Source/Preprocessor/Core/SourcePattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ void SourcePattern::LoadTemplates()
LoadTemplate(templ);
}

for (auto& templ : UnitTemplates)
{
LoadTemplate(templ);
}

LoadTemplate(MainTemplate);
}

Expand Down
2 changes: 2 additions & 0 deletions Source/Preprocessor/Core/SourcePattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class SourcePattern

std::unique_ptr<MustacheTemplate> MainTemplate;
std::vector<std::unique_ptr<MustacheTemplate>> HeaderTemplates;
std::vector<std::unique_ptr<MustacheTemplate>> UnitTemplates;
std::vector<std::unique_ptr<MustacheTemplate>> ClassTemplates;
std::vector<std::unique_ptr<MustacheTemplate>> EnumTemplates;
std::vector<std::unique_ptr<SourceFile>> Sources;
Expand All @@ -51,6 +52,7 @@ class SourcePattern
fs::path OutputDir;
std::string MainOutName;
std::string HeaderOutName;
std::string UnitOutName;
std::string ClassOutName;
std::string EnumOutName;
bool IncludeSourceHeader;
Expand Down
13 changes: 11 additions & 2 deletions Source/Preprocessor/Utils/ArgumentParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,16 @@ void ArgumentParser::ParseTemplates(const nlohmann::json& parser, const fs::path
}
}

pattern->ClassOutName = element["class_file_name"].get<std::string>();
pattern->UnitOutName = try_get_str("unit_file_name");
if (auto itr = element.find("unit_template"); itr != element.end())
{
for (auto& tmpl : itr.value())
{
pattern->UnitTemplates.emplace_back(CreateTemplate(directory, tmpl.get<std::string>()));
}
}

pattern->ClassOutName = try_get_str("class_file_name");
if (auto itr = element.find("class_template"); itr != element.end())
{
for (auto& tmpl : itr.value())
Expand All @@ -237,7 +246,7 @@ void ArgumentParser::ParseTemplates(const nlohmann::json& parser, const fs::path
}
}

pattern->EnumOutName = element["enum_file_name"].get<std::string>();
pattern->EnumOutName = try_get_str("enum_file_name");

if (auto itr = element.find("enum_template"); itr != element.end())
{
Expand Down
17 changes: 0 additions & 17 deletions tests/zetsubou_build_step/config/agnes/inject.h

This file was deleted.

53 changes: 0 additions & 53 deletions tests/zetsubou_build_step/config/agnes/reflection_class.mustache

This file was deleted.

17 changes: 0 additions & 17 deletions tests/zetsubou_build_step/config/agnes/reflection_enum.mustache

This file was deleted.

69 changes: 11 additions & 58 deletions tests/zetsubou_build_step/config/agnes/reflection_header.mustache
Original file line number Diff line number Diff line change
@@ -1,66 +1,19 @@
#include <Reflection/ReflectionInfo.h>
#include "{{header_path}}"

namespace Meta::ReflectionData
// Classes
namespace Meta
{
{{#classes}}
class meta_{{class_name}} : public ClassInfo
{
static meta_{{class_name}} Data;

// private constructor
meta_{{class_name}}() {
Fields = {
{{#fields}}
{ {{name}}, &{{{canonical_name}}}::{{name}}, {{access}}, Meta::GetType<{{{type}}}> },
{{/fields}}
};
Methods = {
{{#methods}}
{ {{name}}, detail::MethodWrapper<decltype(&{{{canonical_name}}}::{{name}}), &{{{canonical_name}}}::{{name}}> },
{{/methods}}
};
}

const std::string Name = "{{class_name}}";
const std::string CanonicalName = "{{{canonical_name}}}";

public:
ClassInfo& Get() { return Data; }
};

TReflectionVector<Attribute> {{class_name}}Meta::Attributes = {
{{#attributes}}
{{{value}}},
{{/attributes}}
};

TReflectionVector<FieldInfo> {{class_name}}Meta::Fields = {
{{#fields}}
{ {{name}}, &{{{canonical_name}}}::{{name}}, {{access}}, Meta::GetType<{{{type}}}> },
{{/fields}}
};

TReflectionVector<MethodInfo> {{class_name}}Meta::Methods = {
{{#methods}}
{ {{name}}, detail::MethodWrapper<decltype(&{{{canonical_name}}}::{{name}}), &{{{canonical_name}}}::{{name}}> },
{{/methods}}
};
template<>
void RegisterClassRtti<typename {{{canonical_name}}}>();
{{/classes}}
}

// Enums
namespace Meta
{
{{#enums}}
class {{enum_name}}_Enum
{
std::vector<EnumValue> Values = {
{{#values}}
{ {{name}}, {{value}} },
{{/values}}
};

std::vector<Attribute> Attributes = {
{{#attributes}}
{{value}},
{{/attributes}}
};
};
template<>
void RegisterEnumRtti<typename {{{canonical_name}}}>();
{{/enums}}
}
10 changes: 8 additions & 2 deletions tests/zetsubou_build_step/config/agnes/reflection_main.mustache
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
{{#includes}}
#include "{{path}}.h"
#include "{{path}}"
{{/includes}}

namespace Meta
{
void RegisterReflection()
{
// Classes
{{#classes}}
Meta::GetType<typename {{{canonical_name}}}>();
RegisterClassRtti<typename {{{canonical_name}}}>();
{{/classes}}

// Enums
{{#enums}}
RegisterEnumRtti<typename {{{canonical_name}}}>();
{{/enums}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
"main_file_name" : "reflection_main.cpp",

"header_template" : ["reflection_header.mustache"],
"header_file_name" : "{{header_name}}.gen.cpp",
"header_file_name" : "{{header_name}}.gen.h",

"class_template" : ["reflection_class.mustache"],
"class_file_name" : "{{class_name}}.gen.cpp",

"enum_template" : ["reflection_enum.mustache"],
"enum_file_name" : "{{enum_name}}.gen.cpp",
"unit_template" : ["reflection_unit.mustache"],
"unit_file_name" : "{{header_name}}.gen.cpp",

"output_dir" : "Generated",

Expand Down
50 changes: 49 additions & 1 deletion tests/zetsubou_build_step/config/agnes/reflection_unit.mustache
Original file line number Diff line number Diff line change
@@ -1 +1,49 @@
#include "{{output_file_name}}.h"
#include "{{header_path}}"
#include <iostream>

// Classes
namespace Meta
{
{{#classes}}
template<>
void RegisterClassRtti<typename {{{canonical_name}}}>()
{
std::cout << "Class: " << "{{{canonical_name}}}" << std::endl;
std::cout << " attributes:" << std::endl;
{{#attributes}}
std::cout << " val: " << "{{value}}" << std::endl;
{{/attributes}}

std::cout << " fields:" << std::endl;
{{#fields}}
std::cout << " name: " << "{{name}}" << " type: " << "{{{type}}}" << std::endl;
{{/fields}}

std::cout << std::endl;
}
{{/classes}}
}

// Enums
namespace Meta
{
{{#enums}}
template<>
void RegisterEnumRtti<typename {{{canonical_name}}}>()
{
std::cout << "Enum: " << "{{{canonical_name}}}" << std::endl;
std::cout << " attributes:" << std::endl;
{{#attributes}}
std::cout << " val: " << "{{value}}" << std::endl;
{{/attributes}}

std::cout << " values:" << std::endl;
{{#values}}
std::cout << " name: " << "{{name}}" << " val: " << "{{value}}" << std::endl;
{{/values}}
std::cout << std::endl;
}
{{/enums}}
}
Loading

0 comments on commit b1552d7

Please sign in to comment.