From 2b1d9079d33e4d20630782d8d729d4577d1400b8 Mon Sep 17 00:00:00 2001 From: Ezekiel Warren Date: Mon, 16 Sep 2024 16:46:18 -0700 Subject: [PATCH] feat: new stream toggle api C++ wrapper --- MODULE.bazel | 10 ++++ .../cpp_systems_header_codegen.cc | 59 +++++++++++++++++++ ecsact/cpp/execution_context.hh | 5 ++ 3 files changed, 74 insertions(+) diff --git a/MODULE.bazel b/MODULE.bazel index f0da1d0..0c6e50a 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -34,3 +34,13 @@ register_toolchains( "@ecsact_toolchain//:all", dev_dependency = True, ) + +local_path_override( + module_name = "ecsact_runtime", + path = "../ecsact_runtime", +) + +local_path_override( + module_name = "ecsact_interpret", + path = "../ecsact_interpret", +) diff --git a/cpp_systems_header_codegen/cpp_systems_header_codegen.cc b/cpp_systems_header_codegen/cpp_systems_header_codegen.cc index e9c4c22..561a254 100644 --- a/cpp_systems_header_codegen/cpp_systems_header_codegen.cc +++ b/cpp_systems_header_codegen/cpp_systems_header_codegen.cc @@ -211,6 +211,27 @@ static auto write_context_has_decl( ctx.write("\n\n"); } +static auto write_context_stream_toggle_decl( + ecsact::codegen_plugin_context& ctx, + std::string_view sys_like_full_name, + const std::set& stream_components +) -> void { + ctx.write("template\n"); + block(ctx, "auto stream_toggle(bool stream_enable) -> void", [&] { + write_context_method_error_body( + ctx, + std::format( + "{} context.stream_toggle may only be called with stream components " + "declared by the system. Did you forget to add stream toggle " + "capabilities?", + sys_like_full_name + ), + stream_components + ); + }); + ctx.write("\n\n"); +} + static auto write_context_other_decl( ecsact::codegen_plugin_context& ctx, std::vector assoc_ids @@ -357,6 +378,31 @@ static auto write_context_remove_specialize( ctx.write("\n"); } +static auto write_context_stream_toggle_specialize( + ecsact::codegen_plugin_context& ctx, + ecsact_component_like_id comp_id +) -> void { + auto decl_id = ecsact_id_cast(comp_id); + auto full_name = ecsact_meta_decl_full_name(decl_id); + auto cpp_full_name = cpp_identifier(full_name); + + block( + ctx, + std::format( + "template<> auto stream_toggle<{}>(bool enable_stream) -> void", + cpp_full_name + ), + [&] { + ctx.write(std::format( + "return _ctx.stream_toggle<{}>(enable_stream);\n", + cpp_full_name + )); + } + ); + + ctx.write("\n"); +} + static auto write_context_other_specialize( ecsact::codegen_plugin_context& ctx, ecsact_system_like_id system_like_id, @@ -391,6 +437,7 @@ struct context_body_details { std::set update_components; std::set remove_components; std::set optional_components; + std::set stream_components; static auto from_caps(auto caps) -> context_body_details { auto details = context_body_details{}; @@ -415,6 +462,10 @@ struct context_body_details { if((cap & ECSACT_SYS_CAP_OPTIONAL) == ECSACT_SYS_CAP_OPTIONAL) { details.optional_components.emplace(comp_id); } + + if((cap & ECSACT_SYS_CAP_OPTIONAL) == ECSACT_SYS_CAP_STREAM_TOGGLE) { + details.stream_components.emplace(comp_id); + } } return details; @@ -465,6 +516,10 @@ static auto write_context_body_common( write_context_has_decl(ctx, ctx_name, details.optional_components); } + if(!details.stream_components.empty()) { + write_context_stream_toggle_decl(ctx, ctx_name, details.stream_components); + } + for(auto get_comp_id : details.get_components) { write_context_get_specialize(ctx, get_comp_id); } @@ -481,6 +536,10 @@ static auto write_context_body_common( write_context_remove_specialize(ctx, remove_comp_id); } + for(auto stream_comp_id : details.stream_components) { + write_context_stream_toggle_specialize(ctx, stream_comp_id); + } + write_context_entity(ctx); } diff --git a/ecsact/cpp/execution_context.hh b/ecsact/cpp/execution_context.hh index 2beca79..3c2d8f6 100644 --- a/ecsact/cpp/execution_context.hh +++ b/ecsact/cpp/execution_context.hh @@ -59,6 +59,11 @@ struct execution_context { ); } + template + ECSACT_ALWAYS_INLINE auto stream_toggle(bool enable_stream_data) -> void { + ecsact_system_execution_context_stream_toggle(_ctx, C::id); + } + template requires(!std::is_empty_v) ECSACT_ALWAYS_INLINE auto add(const C& new_component) -> void {