Skip to content

Commit

Permalink
feat: new stream toggle api C++ wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy committed Sep 16, 2024
1 parent a32770b commit 2b1d907
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
10 changes: 10 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
59 changes: 59 additions & 0 deletions cpp_systems_header_codegen/cpp_systems_header_codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<ecsact_component_like_id>& stream_components
) -> void {
ctx.write("template<typename T>\n");
block(ctx, "auto stream_toggle(bool stream_enable) -> void", [&] {
write_context_method_error_body(
ctx,
std::format(
"{} context.stream_toggle<T> 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<ecsact_system_assoc_id> assoc_ids
Expand Down Expand Up @@ -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<ecsact_decl_id>(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,
Expand Down Expand Up @@ -391,6 +437,7 @@ struct context_body_details {
std::set<ecsact_component_like_id> update_components;
std::set<ecsact_component_like_id> remove_components;
std::set<ecsact_component_like_id> optional_components;
std::set<ecsact_component_like_id> stream_components;

static auto from_caps(auto caps) -> context_body_details {
auto details = context_body_details{};
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}

Expand Down
5 changes: 5 additions & 0 deletions ecsact/cpp/execution_context.hh
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ struct execution_context {
);
}

template<typename C>
ECSACT_ALWAYS_INLINE auto stream_toggle(bool enable_stream_data) -> void {
ecsact_system_execution_context_stream_toggle(_ctx, C::id);
}

template<typename C>
requires(!std::is_empty_v<C>)
ECSACT_ALWAYS_INLINE auto add(const C& new_component) -> void {
Expand Down

0 comments on commit 2b1d907

Please # to comment.