Skip to content

Commit 519a9f5

Browse files
authored
feat: added decl_name function to meta header codegen (#136)
1 parent 74ebca8 commit 519a9f5

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

cpp_meta_header_codegen/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ cc_ecsact_codegen_plugin(
99
copts = copts,
1010
no_validate_test = True, # file name is too long on Windows
1111
output_extension = "meta.hh",
12-
deps = ["//:support"],
12+
deps = [
13+
"//:support",
14+
"//:cpp_codegen_plugin_util",
15+
],
1316
)
1417

1518
alias(

cpp_meta_header_codegen/cpp_meta_header_codegen.cc

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "ecsact/codegen_plugin.h"
1010
#include "ecsact/codegen_plugin.hh"
1111
#include "ecsact/lang-support/lang-cc.hh"
12+
#include "ecsact/cpp_codegen_plugin_util.hh"
1213

1314
namespace fs = std::filesystem;
1415
namespace stdr = std::ranges;
@@ -449,10 +450,31 @@ static inline auto write_lazy_system_iteration_rate( //
449450
cpp_identifier(get_sys_full_name(ctx.package_id, id)),
450451
"> : std::integral_constant<int32_t, ",
451452
lazy_iteration_rate,
452-
"> {};"
453+
"> {};\n\n"
453454
);
454455
}
455456

457+
template<typename DeclId>
458+
static inline auto write_decl_full_name_specialization( //
459+
ecsact::codegen_plugin_context& ctx,
460+
DeclId id
461+
) {
462+
using ecsact::cc_lang_support::cpp_identifier;
463+
using ecsact::cpp_codegen_plugin_util::method_printer;
464+
465+
auto decl_id = ecsact_id_cast<ecsact_decl_id>(id);
466+
auto decl_full_name = ecsact::meta::decl_full_name(decl_id);
467+
auto decl_cpp_ident = cpp_identifier(decl_full_name);
468+
469+
auto method_name = "ecsact::decl_full_name<" + decl_cpp_ident + ">";
470+
471+
ctx.write("template<> constexpr ");
472+
auto printer =
473+
method_printer{ctx, method_name}.return_type("std::string_view");
474+
475+
ctx.write("return \"" + decl_full_name + "\";");
476+
}
477+
456478
void ecsact_codegen_plugin(
457479
ecsact_package_id package_id,
458480
ecsact_codegen_write_fn_t write_fn
@@ -615,4 +637,20 @@ void ecsact_codegen_plugin(
615637
for(auto& sys_id : ecsact::meta::get_system_ids(ctx.package_id)) {
616638
write_lazy_system_iteration_rate(ctx, sys_id);
617639
}
640+
641+
for(auto& sys_id : ecsact::meta::get_system_ids(ctx.package_id)) {
642+
write_decl_full_name_specialization(ctx, sys_id);
643+
}
644+
645+
for(auto& act_id : ecsact::meta::get_action_ids(ctx.package_id)) {
646+
write_decl_full_name_specialization(ctx, act_id);
647+
}
648+
649+
for(auto& comp_id : ecsact::meta::get_component_ids(ctx.package_id)) {
650+
write_decl_full_name_specialization(ctx, comp_id);
651+
}
652+
653+
for(auto& trans_id : ecsact::meta::get_transient_ids(ctx.package_id)) {
654+
write_decl_full_name_specialization(ctx, trans_id);
655+
}
618656
}

ecsact/cpp/type_info.hh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
#include <cstdint>
44
#include <type_traits>
55
#include <array>
6+
#include <string_view>
67
#include "ecsact/lib.hh"
78
#include "ecsact/runtime/common.h"
89
#include "ecsact/runtime/definitions.h"
910

11+
namespace ecsact::detail {
12+
template<typename>
13+
constexpr bool unimplemented_by_meta_cpp_codegen = false;
14+
}
15+
1016
namespace ecsact {
1117

1218
template<typename SystemLikeT>
@@ -82,4 +88,22 @@ struct field_info {
8288
template<typename C>
8389
constexpr std::array<field_info, fields_count<C>()> fields_info();
8490

91+
/**
92+
* @returns the ecsact declaration full name. The same value that should be
93+
* returned by ecsact_meta_decl_name
94+
*/
95+
template<typename Decl>
96+
constexpr auto decl_full_name() -> std::string_view {
97+
static_assert(detail::unimplemented_by_meta_cpp_codegen<Decl>, R"(
98+
-----------------------------------------------------------------------------
99+
| (!) CODEGEN ERROR |
100+
| `ecsact::decl_full_name<>` template specialization cannot be found. This is |
101+
| typically generated by ecsact_cpp_meta_header_codegen. Make sure the passed |
102+
| in template parameter (Decl) is an ecsact declaration type and the .meta.hh |
103+
| file is included in the source this method is being used. |
104+
-----------------------------------------------------------------------------
105+
)");
106+
return {};
107+
}
108+
85109
} // namespace ecsact

0 commit comments

Comments
 (0)