Skip to content

Commit 9288773

Browse files
authored
feat: improve assoc api + C++ wrappers (#246)
1 parent d97a63b commit 9288773

File tree

3 files changed

+119
-39
lines changed

3 files changed

+119
-39
lines changed

ecsact/runtime/common.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,35 @@ ECSACT_ALWAYS_INLINE To ecsact_id_cast(From) {
7474
);
7575
}
7676

77+
template<typename From, typename To>
78+
constexpr auto ecsact_id_castable() -> bool {
79+
return false;
80+
}
81+
7782
# define ECSACT_CAST_ID_FN(From, To) \
7883
template<> \
7984
ECSACT_ALWAYS_INLINE To ecsact_id_cast<To, From>(From id) { \
8085
return (To)id; \
86+
} \
87+
template<> \
88+
constexpr auto ecsact_id_castable<From, To>() -> bool { \
89+
return true; \
90+
}
91+
92+
# define ECSACT_COMPARE_ID_FN(From, To) \
93+
ECSACT_ALWAYS_INLINE bool operator==(const From& a, const To& b) { \
94+
return ecsact_id_cast<To>(a) == b; \
95+
} \
96+
ECSACT_ALWAYS_INLINE bool operator!=(const From& a, const To& b) { \
97+
return ecsact_id_cast<To>(a) != b; \
8198
}
8299
#else
83100
ECSACT_ALWAYS_INLINE int32_t ecsact_id_cast(int32_t id) {
84101
return id;
85102
}
86103

87104
# define ECSACT_CAST_ID_FN(From, To)
105+
# define ECSACT_COMPARE_ID_FN(From, To)
88106
#endif
89107

90108
ECSACT_CAST_ID_FN(ecsact_system_id, ecsact_system_like_id)
@@ -122,8 +140,28 @@ ECSACT_CAST_ID_FN(ecsact_system_like_id, ecsact_system_like_id)
122140
ECSACT_CAST_ID_FN(ecsact_transient_id, ecsact_transient_id)
123141
ECSACT_CAST_ID_FN(ecsact_component_like_id, ecsact_component_like_id)
124142

143+
ECSACT_COMPARE_ID_FN(ecsact_system_id, ecsact_system_like_id)
144+
ECSACT_COMPARE_ID_FN(ecsact_action_id, ecsact_system_like_id)
145+
ECSACT_COMPARE_ID_FN(ecsact_action_id, ecsact_composite_id)
146+
ECSACT_COMPARE_ID_FN(ecsact_component_id, ecsact_composite_id)
147+
ECSACT_COMPARE_ID_FN(ecsact_transient_id, ecsact_composite_id)
148+
ECSACT_COMPARE_ID_FN(ecsact_component_like_id, ecsact_composite_id)
149+
150+
ECSACT_COMPARE_ID_FN(ecsact_component_id, ecsact_decl_id)
151+
ECSACT_COMPARE_ID_FN(ecsact_transient_id, ecsact_decl_id)
152+
ECSACT_COMPARE_ID_FN(ecsact_system_id, ecsact_decl_id)
153+
ECSACT_COMPARE_ID_FN(ecsact_action_id, ecsact_decl_id)
154+
ECSACT_COMPARE_ID_FN(ecsact_variant_id, ecsact_decl_id)
155+
ECSACT_COMPARE_ID_FN(ecsact_system_like_id, ecsact_decl_id)
156+
ECSACT_COMPARE_ID_FN(ecsact_composite_id, ecsact_decl_id)
157+
ECSACT_COMPARE_ID_FN(ecsact_component_like_id, ecsact_decl_id)
158+
159+
ECSACT_COMPARE_ID_FN(ecsact_component_id, ecsact_component_like_id)
160+
ECSACT_COMPARE_ID_FN(ecsact_transient_id, ecsact_component_like_id)
161+
125162
#undef ECSACT_TYPED_ID
126163
#undef ECSACT_CAST_ID_FN
164+
#undef ECSACT_COMPARE_ID_FN
127165

128166
#if defined(_WIN32) && (!defined(_MSVC_TRADITIONAL) || _MSVC_TRADITIONAL)
129167
# define ECSACT_MSVC_TRADITIONAL

ecsact/runtime/dynamic.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,11 @@ ECSACT_DYNAMIC_API_FN(void, ecsact_remove_system_assoc_field)
404404
ecsact_field_id
405405
);
406406

407-
ECSACT_DYNAMIC_API_FN(void, ecsact_set_system_assoc_capbility)
407+
ECSACT_DYNAMIC_API_FN(void, ecsact_set_system_assoc_capability)
408408
( //
409409
ecsact_system_like_id,
410410
ecsact_system_assoc_id,
411+
ecsact_component_like_id,
411412
ecsact_system_capability
412413
);
413414

@@ -543,7 +544,7 @@ ECSACT_DYNAMIC_API_FN(void, ecsact_set_system_notify_component_setting)
543544
fn(ecsact_remove_system_assoc, __VA_ARGS__); \
544545
fn(ecsact_add_system_assoc_field, __VA_ARGS__); \
545546
fn(ecsact_remove_system_assoc_field, __VA_ARGS__); \
546-
fn(ecsact_set_system_assoc_capbility, __VA_ARGS__); \
547+
fn(ecsact_set_system_assoc_capability, __VA_ARGS__); \
547548
fn(ecsact_set_system_association_capability, __VA_ARGS__); \
548549
fn(ecsact_unset_system_association_capability, __VA_ARGS__); \
549550
fn(ecsact_add_system_generates, __VA_ARGS__); \

ecsact/runtime/meta.hh

Lines changed: 78 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <optional>
66
#include <filesystem>
77
#include <unordered_map>
8+
#include "ecsact/runtime/common.h"
89
#include "ecsact/runtime/meta.h"
910

1011
namespace ecsact::meta {
@@ -179,6 +180,17 @@ ECSACT_ALWAYS_INLINE std::vector<ecsact_field_id> get_field_ids(
179180
return field_ids;
180181
}
181182

183+
template<typename CompositeID>
184+
ECSACT_ALWAYS_INLINE auto get_field_type(
185+
CompositeID id,
186+
ecsact_field_id field_id
187+
) -> ecsact_field_type {
188+
return ecsact_meta_field_type(
189+
ecsact_id_cast<ecsact_composite_id>(id),
190+
field_id
191+
);
192+
}
193+
182194
ECSACT_ALWAYS_INLINE std::vector<ecsact_system_id> get_system_ids(
183195
ecsact_package_id package_id
184196
) {
@@ -484,67 +496,85 @@ ECSACT_ALWAYS_INLINE auto get_system_generates_components(
484496
return result;
485497
}
486498

487-
template<typename SystemLikeID, typename ComponentLikeID>
488-
ECSACT_ALWAYS_INLINE auto system_association_fields(
489-
SystemLikeID system_id,
490-
ComponentLikeID component_id
491-
) {
492-
auto sys_like_id = ecsact_id_cast<ecsact_system_like_id>(system_id);
493-
auto comp_like_id = ecsact_id_cast<ecsact_component_like_id>(component_id);
494-
495-
std::vector<ecsact_field_id> field_ids;
496-
field_ids.resize(
497-
ecsact_meta_system_association_fields_count(sys_like_id, comp_like_id)
499+
template<typename SystemLikeID>
500+
ECSACT_ALWAYS_INLINE auto system_assoc_ids( //
501+
SystemLikeID system_id
502+
) -> std::vector<ecsact_system_assoc_id> {
503+
auto result = std::vector<ecsact_system_assoc_id>{};
504+
result.resize(32);
505+
auto assoc_count = int32_t{};
506+
507+
ecsact_meta_system_assoc_ids(
508+
ecsact_id_cast<ecsact_system_like_id>(system_id),
509+
static_cast<int32_t>(result.size()),
510+
result.data(),
511+
&assoc_count
498512
);
499513

500-
ecsact_meta_system_association_fields(
501-
sys_like_id,
502-
comp_like_id,
503-
static_cast<int32_t>(field_ids.size()),
504-
field_ids.data(),
505-
nullptr
514+
result.resize(assoc_count);
515+
516+
return result;
517+
}
518+
519+
template<typename SystemLikeID>
520+
ECSACT_ALWAYS_INLINE auto system_assoc_component_id( //
521+
SystemLikeID system_id,
522+
ecsact_system_assoc_id assoc_id
523+
) -> ecsact_component_like_id {
524+
return ecsact_meta_system_assoc_component_id(
525+
ecsact_id_cast<ecsact_system_like_id>(system_id),
526+
assoc_id
506527
);
528+
}
507529

508-
return field_ids;
530+
template<typename SystemLikeID>
531+
ECSACT_ALWAYS_INLINE auto system_assoc_fields(
532+
SystemLikeID system_id,
533+
ecsact_system_assoc_id assoc_id
534+
) -> std::vector<ecsact_field_id> {
535+
auto result = std::vector<ecsact_field_id>{};
536+
result.resize(32);
537+
auto fields_count = int32_t{};
538+
ecsact_meta_system_assoc_fields(
539+
system_id,
540+
assoc_id,
541+
result.size(),
542+
result.data(),
543+
&fields_count
544+
);
545+
result.resize(fields_count);
546+
return result;
509547
}
510548

511-
template<typename SystemLikeID, typename ComponentLikeID>
512-
ECSACT_ALWAYS_INLINE auto system_association_capabilities(
513-
SystemLikeID system_id,
514-
ComponentLikeID component_id,
515-
ecsact_field_id field_id
549+
template<typename SystemLikeID>
550+
ECSACT_ALWAYS_INLINE auto system_assoc_capabilities(
551+
SystemLikeID id,
552+
ecsact_system_assoc_id assoc_id
516553
) {
517554
using result_t =
518-
std::unordered_map<ecsact_component_like_id, ecsact_system_capability>;
519-
520-
auto sys_like_id = ecsact_id_cast<ecsact_system_like_id>(system_id);
521-
auto comp_like_id = ecsact_id_cast<ecsact_component_like_id>(component_id);
555+
std::vector<std::pair<ecsact_component_like_id, ecsact_system_capability>>;
522556

523-
auto count = ecsact_meta_system_association_capabilities_count(
524-
sys_like_id,
525-
comp_like_id,
526-
field_id
527-
);
557+
const auto sys_like_id = ecsact_id_cast<ecsact_system_like_id>(id);
558+
auto count = ecsact_meta_system_capabilities_count(sys_like_id);
528559
std::vector<ecsact_component_like_id> components;
529560
std::vector<ecsact_system_capability> capabilities;
530561
components.resize(count);
531562
capabilities.resize(count);
532563

533-
ecsact_meta_system_association_capabilities(
564+
ecsact_meta_system_assoc_capabilities(
534565
sys_like_id,
535-
comp_like_id,
536-
field_id,
566+
assoc_id,
537567
count,
538568
components.data(),
539569
capabilities.data(),
540570
nullptr
541571
);
542572

543573
result_t result;
544-
result.reserve(count);
574+
result.resize(count);
545575

546576
for(decltype(count) i = 0; count > i; ++i) {
547-
result[components[i]] = capabilities[i];
577+
result[i] = {components[i], capabilities[i]};
548578
}
549579

550580
return result;
@@ -592,6 +622,17 @@ auto system_notify_settings( //
592622
return result;
593623
}
594624

625+
template<typename CompositeID>
626+
ECSACT_ALWAYS_INLINE auto field_name( //
627+
CompositeID id,
628+
ecsact_field_id field_id
629+
) -> std::string {
630+
return ecsact_meta_field_name(
631+
ecsact_id_cast<ecsact_composite_id>(id),
632+
field_id
633+
);
634+
}
635+
595636
ECSACT_ALWAYS_INLINE auto main_package() -> std::optional<ecsact_package_id> {
596637
auto main_pkg_id = ecsact_meta_main_package();
597638
if(main_pkg_id == static_cast<ecsact_package_id>(-1)) {

0 commit comments

Comments
 (0)