Skip to content

Commit 6205135

Browse files
committed
feat: new parallel parameter values support
1 parent 9c1c3a6 commit 6205135

File tree

9 files changed

+121
-16
lines changed

9 files changed

+121
-16
lines changed

MODULE.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ module(
77
bazel_dep(name = "rules_cc", version = "0.0.9")
88
bazel_dep(name = "bazel_skylib", version = "1.5.0")
99
bazel_dep(name = "magic_enum", version = "0.9.3")
10-
bazel_dep(name = "ecsact_runtime", version = "0.6.4")
11-
bazel_dep(name = "ecsact_parse", version = "0.5.0")
10+
bazel_dep(name = "ecsact_runtime", version = "0.6.5")
11+
bazel_dep(name = "ecsact_parse", version = "0.5.1")
1212

1313
bazel_dep(name = "toolchains_llvm", version = "1.0.0", dev_dependency = True)
1414
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)

ecsact/interpret/eval.cc

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,33 @@ auto statement_param<bool>( //
138138
return result;
139139
}
140140

141+
template<>
142+
auto statement_param<std::string_view>( //
143+
const ecsact_statement& statement,
144+
std::string_view param_name
145+
) -> std::optional<std::string_view> {
146+
auto result = std::optional<std::string_view>{};
147+
for(auto& param : view_statement_params(statement)) {
148+
if(std::string_view{
149+
param.name.data,
150+
static_cast<size_t>(param.name.length)
151+
} != param_name) {
152+
continue;
153+
}
154+
155+
if(param.value.type != ECSACT_STATEMENT_PARAM_VALUE_TYPE_STRING) {
156+
break;
157+
}
158+
159+
result = std::string_view{
160+
param.value.data.string_value.data,
161+
static_cast<size_t>(param.value.data.string_value.length),
162+
};
163+
}
164+
165+
return result;
166+
}
167+
141168
template<typename FirstT, typename SecondT>
142169
auto statement_param( //
143170
const ecsact_statement& statement,
@@ -162,6 +189,38 @@ auto statement_param( //
162189
return std::nullopt;
163190
}
164191

192+
static auto parallel_param(const ecsact_statement& statement
193+
) -> std::variant<ecsact_parallel_execution, ecsact_eval_error_code> {
194+
using result_t =
195+
std::variant<ecsact_parallel_execution, ecsact_eval_error_code>;
196+
197+
auto param = statement_param<bool, std::string_view>(statement, "parallel");
198+
if(!param) {
199+
return ECSACT_PAR_EXEC_AUTO;
200+
}
201+
202+
auto result = std::visit(
203+
overloaded{
204+
[&](bool param) -> result_t {
205+
return param ? ECSACT_PAR_EXEC_PREFERRED : ECSACT_PAR_EXEC_DENY;
206+
},
207+
[&](std::string_view param) -> result_t {
208+
if(param == "auto") {
209+
return ECSACT_PAR_EXEC_AUTO;
210+
} else if(param == "preferred") {
211+
return ECSACT_PAR_EXEC_PREFERRED;
212+
} else if(param == "deny") {
213+
return ECSACT_PAR_EXEC_DENY;
214+
} else {
215+
return ECSACT_EVAL_ERR_INVALID_PARAMETER_VALUE;
216+
}
217+
}
218+
},
219+
*param
220+
);
221+
return result;
222+
}
223+
165224
auto allow_statement_params( //
166225
const ecsact_statement& statement,
167226
const ecsact_statement* context,
@@ -803,10 +862,17 @@ static ecsact_eval_error eval_system_statement(
803862
ecsact_set_system_lazy_iteration_rate(sys_id, lazy_value);
804863
}
805864

806-
auto parallel = statement_param<bool>(statement, "parallel").value_or(false);
865+
auto parallel = parallel_param(statement);
866+
if(auto err_code = std::get_if<ecsact_eval_error_code>(&parallel)) {
867+
return ecsact_eval_error{
868+
.code = *err_code,
869+
.relevant_content = data.system_name,
870+
};
871+
}
872+
807873
ecsact_set_system_parallel_execution(
808874
ecsact_id_cast<ecsact_system_like_id>(sys_id),
809-
parallel
875+
std::get<ecsact_parallel_execution>(parallel)
810876
);
811877

812878
return {};
@@ -845,10 +911,17 @@ static ecsact_eval_error eval_action_statement(
845911
data.action_name.length
846912
);
847913

848-
auto parallel = statement_param<bool>(statement, "parallel").value_or(false);
914+
auto parallel = parallel_param(statement);
915+
if(auto err_code = std::get_if<ecsact_eval_error_code>(&parallel)) {
916+
return ecsact_eval_error{
917+
.code = *err_code,
918+
.relevant_content = data.action_name,
919+
};
920+
}
921+
849922
ecsact_set_system_parallel_execution(
850923
ecsact_id_cast<ecsact_system_like_id>(act_id),
851-
parallel
924+
std::get<ecsact_parallel_execution>(parallel)
852925
);
853926

854927
return {};

ecsact/interpret/eval_error.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ typedef enum ecsact_eval_error_code {
8585
/// currently not allowed.
8686
ECSACT_EVAL_ERR_SAME_FIELDS_SYSTEM_ASSOCIATION,
8787

88+
/// Statement supports the given parameter name but the value is invalid
89+
ECSACT_EVAL_ERR_INVALID_PARAMETER_VALUE,
90+
8891
/// Internal error. Should not happen and is an indiciation of a bug.
8992
ECSACT_EVAL_ERR_INTERNAL = 999,
9093

parse-resolver-runtime/parse-resolver-runtime.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ struct system_like {
7474
/** in execution order */
7575
std::vector<ecsact_system_id> nested_systems;
7676

77-
bool parallel_execution = false;
77+
ecsact_parallel_execution parallel_execution = {};
7878
};
7979

8080
struct action_def : composite, system_like {
@@ -1073,14 +1073,14 @@ int32_t ecsact_meta_get_lazy_iteration_rate( //
10731073
}
10741074

10751075
void ecsact_set_system_parallel_execution( //
1076-
ecsact_system_like_id system_like_id,
1077-
bool parallel_execution
1076+
ecsact_system_like_id system_like_id,
1077+
ecsact_parallel_execution parallel_execution
10781078
) {
10791079
auto& def = get_system_like(system_like_id);
10801080
def.parallel_execution = parallel_execution;
10811081
}
10821082

1083-
bool ecsact_meta_get_system_parallel_execution( //
1083+
ecsact_parallel_execution ecsact_meta_get_system_parallel_execution( //
10841084
ecsact_system_like_id system_like_id
10851085
) {
10861086
auto& def = get_system_like(system_like_id);

test/MODULE.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module(name = "ecsact_interpret_test")
33
bazel_dep(name = "rules_cc", version = "0.0.9")
44
bazel_dep(name = "bazel_skylib", version = "1.5.0")
55
bazel_dep(name = "googletest", version = "1.14.0")
6-
bazel_dep(name = "ecsact_parse", version = "0.5.0")
7-
bazel_dep(name = "ecsact_runtime", version = "0.6.4")
6+
bazel_dep(name = "ecsact_parse", version = "0.5.1")
7+
bazel_dep(name = "ecsact_runtime", version = "0.6.5")
88

99
bazel_dep(name = "toolchains_llvm", version = "1.0.0", dev_dependency = True)
1010
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)

test/errors/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
load("@rules_cc//cc:defs.bzl", "cc_test")
21
load("@ecsact_interpret//bazel:copts.bzl", "copts")
2+
load("@rules_cc//cc:defs.bzl", "cc_test")
33

44
# buildifier: keep sorted
55
_TESTS = [
@@ -9,6 +9,7 @@ _TESTS = [
99
"no_package_statement_first",
1010
"unknown_association_field",
1111
"unknown_component_notify_settings",
12+
"unknown_param_value",
1213
]
1314

1415
[cc_test(

test/errors/unknown_param_value.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "gtest/gtest.h"
2+
#include "ecsact/interpret/eval.h"
3+
4+
#include "test_lib.hh"
5+
6+
TEST(UnknownParamValue, UnknownParallelParam) {
7+
auto errs =
8+
ecsact_interpret_test_files({"errors/unknown_param_value.ecsact"});
9+
ASSERT_EQ(errs.size(), 1);
10+
ASSERT_EQ(errs[0].eval_error, ECSACT_EVAL_ERR_INVALID_PARAMETER_VALUE);
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package unknown_param_value;
2+
3+
component A { i32 a; };
4+
5+
system UnknownParamValue(parallel: what) {
6+
readwrite A;
7+
}

test/test.cc

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ TEST(EcsactParseRuntimeInterop, Simple) {
134134
field_id
135135
);
136136
ASSERT_STREQ(assoc_field_name, "test_entity");
137+
138+
ASSERT_EQ(
139+
ecsact_meta_get_system_parallel_execution(
140+
ecsact_id_cast<ecsact_system_like_id>(sys_id)
141+
),
142+
ECSACT_PAR_EXEC_AUTO
143+
);
137144
}},
138145
{"TestNestedSystem",
139146
[&](ecsact_system_id sys_id) {
@@ -157,9 +164,12 @@ TEST(EcsactParseRuntimeInterop, Simple) {
157164
);
158165
ASSERT_EQ(sys_caps[0], ECSACT_SYS_CAP_READWRITE);
159166

160-
ASSERT_TRUE(ecsact_meta_get_system_parallel_execution(
161-
ecsact_id_cast<ecsact_system_like_id>(sys_id)
162-
));
167+
ASSERT_EQ(
168+
ecsact_meta_get_system_parallel_execution(
169+
ecsact_id_cast<ecsact_system_like_id>(sys_id)
170+
),
171+
ECSACT_PAR_EXEC_PREFERRED
172+
);
163173
}},
164174
};
165175

0 commit comments

Comments
 (0)