Skip to content

Commit

Permalink
feat: improve assoc api + C++ wrappers (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy authored May 27, 2024
1 parent d97a63b commit 9288773
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 39 deletions.
38 changes: 38 additions & 0 deletions ecsact/runtime/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,35 @@ ECSACT_ALWAYS_INLINE To ecsact_id_cast(From) {
);
}

template<typename From, typename To>
constexpr auto ecsact_id_castable() -> bool {
return false;
}

# define ECSACT_CAST_ID_FN(From, To) \
template<> \
ECSACT_ALWAYS_INLINE To ecsact_id_cast<To, From>(From id) { \
return (To)id; \
} \
template<> \
constexpr auto ecsact_id_castable<From, To>() -> bool { \
return true; \
}

# define ECSACT_COMPARE_ID_FN(From, To) \
ECSACT_ALWAYS_INLINE bool operator==(const From& a, const To& b) { \
return ecsact_id_cast<To>(a) == b; \
} \
ECSACT_ALWAYS_INLINE bool operator!=(const From& a, const To& b) { \
return ecsact_id_cast<To>(a) != b; \
}
#else
ECSACT_ALWAYS_INLINE int32_t ecsact_id_cast(int32_t id) {
return id;
}

# define ECSACT_CAST_ID_FN(From, To)
# define ECSACT_COMPARE_ID_FN(From, To)
#endif

ECSACT_CAST_ID_FN(ecsact_system_id, ecsact_system_like_id)
Expand Down Expand Up @@ -122,8 +140,28 @@ ECSACT_CAST_ID_FN(ecsact_system_like_id, ecsact_system_like_id)
ECSACT_CAST_ID_FN(ecsact_transient_id, ecsact_transient_id)
ECSACT_CAST_ID_FN(ecsact_component_like_id, ecsact_component_like_id)

ECSACT_COMPARE_ID_FN(ecsact_system_id, ecsact_system_like_id)
ECSACT_COMPARE_ID_FN(ecsact_action_id, ecsact_system_like_id)
ECSACT_COMPARE_ID_FN(ecsact_action_id, ecsact_composite_id)
ECSACT_COMPARE_ID_FN(ecsact_component_id, ecsact_composite_id)
ECSACT_COMPARE_ID_FN(ecsact_transient_id, ecsact_composite_id)
ECSACT_COMPARE_ID_FN(ecsact_component_like_id, ecsact_composite_id)

ECSACT_COMPARE_ID_FN(ecsact_component_id, ecsact_decl_id)
ECSACT_COMPARE_ID_FN(ecsact_transient_id, ecsact_decl_id)
ECSACT_COMPARE_ID_FN(ecsact_system_id, ecsact_decl_id)
ECSACT_COMPARE_ID_FN(ecsact_action_id, ecsact_decl_id)
ECSACT_COMPARE_ID_FN(ecsact_variant_id, ecsact_decl_id)
ECSACT_COMPARE_ID_FN(ecsact_system_like_id, ecsact_decl_id)
ECSACT_COMPARE_ID_FN(ecsact_composite_id, ecsact_decl_id)
ECSACT_COMPARE_ID_FN(ecsact_component_like_id, ecsact_decl_id)

ECSACT_COMPARE_ID_FN(ecsact_component_id, ecsact_component_like_id)
ECSACT_COMPARE_ID_FN(ecsact_transient_id, ecsact_component_like_id)

#undef ECSACT_TYPED_ID
#undef ECSACT_CAST_ID_FN
#undef ECSACT_COMPARE_ID_FN

#if defined(_WIN32) && (!defined(_MSVC_TRADITIONAL) || _MSVC_TRADITIONAL)
# define ECSACT_MSVC_TRADITIONAL
Expand Down
5 changes: 3 additions & 2 deletions ecsact/runtime/dynamic.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,11 @@ ECSACT_DYNAMIC_API_FN(void, ecsact_remove_system_assoc_field)
ecsact_field_id
);

ECSACT_DYNAMIC_API_FN(void, ecsact_set_system_assoc_capbility)
ECSACT_DYNAMIC_API_FN(void, ecsact_set_system_assoc_capability)
( //
ecsact_system_like_id,
ecsact_system_assoc_id,
ecsact_component_like_id,
ecsact_system_capability
);

Expand Down Expand Up @@ -543,7 +544,7 @@ ECSACT_DYNAMIC_API_FN(void, ecsact_set_system_notify_component_setting)
fn(ecsact_remove_system_assoc, __VA_ARGS__); \
fn(ecsact_add_system_assoc_field, __VA_ARGS__); \
fn(ecsact_remove_system_assoc_field, __VA_ARGS__); \
fn(ecsact_set_system_assoc_capbility, __VA_ARGS__); \
fn(ecsact_set_system_assoc_capability, __VA_ARGS__); \
fn(ecsact_set_system_association_capability, __VA_ARGS__); \
fn(ecsact_unset_system_association_capability, __VA_ARGS__); \
fn(ecsact_add_system_generates, __VA_ARGS__); \
Expand Down
115 changes: 78 additions & 37 deletions ecsact/runtime/meta.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <optional>
#include <filesystem>
#include <unordered_map>
#include "ecsact/runtime/common.h"
#include "ecsact/runtime/meta.h"

namespace ecsact::meta {
Expand Down Expand Up @@ -179,6 +180,17 @@ ECSACT_ALWAYS_INLINE std::vector<ecsact_field_id> get_field_ids(
return field_ids;
}

template<typename CompositeID>
ECSACT_ALWAYS_INLINE auto get_field_type(
CompositeID id,
ecsact_field_id field_id
) -> ecsact_field_type {
return ecsact_meta_field_type(
ecsact_id_cast<ecsact_composite_id>(id),
field_id
);
}

ECSACT_ALWAYS_INLINE std::vector<ecsact_system_id> get_system_ids(
ecsact_package_id package_id
) {
Expand Down Expand Up @@ -484,67 +496,85 @@ ECSACT_ALWAYS_INLINE auto get_system_generates_components(
return result;
}

template<typename SystemLikeID, typename ComponentLikeID>
ECSACT_ALWAYS_INLINE auto system_association_fields(
SystemLikeID system_id,
ComponentLikeID component_id
) {
auto sys_like_id = ecsact_id_cast<ecsact_system_like_id>(system_id);
auto comp_like_id = ecsact_id_cast<ecsact_component_like_id>(component_id);

std::vector<ecsact_field_id> field_ids;
field_ids.resize(
ecsact_meta_system_association_fields_count(sys_like_id, comp_like_id)
template<typename SystemLikeID>
ECSACT_ALWAYS_INLINE auto system_assoc_ids( //
SystemLikeID system_id
) -> std::vector<ecsact_system_assoc_id> {
auto result = std::vector<ecsact_system_assoc_id>{};
result.resize(32);
auto assoc_count = int32_t{};

ecsact_meta_system_assoc_ids(
ecsact_id_cast<ecsact_system_like_id>(system_id),
static_cast<int32_t>(result.size()),
result.data(),
&assoc_count
);

ecsact_meta_system_association_fields(
sys_like_id,
comp_like_id,
static_cast<int32_t>(field_ids.size()),
field_ids.data(),
nullptr
result.resize(assoc_count);

return result;
}

template<typename SystemLikeID>
ECSACT_ALWAYS_INLINE auto system_assoc_component_id( //
SystemLikeID system_id,
ecsact_system_assoc_id assoc_id
) -> ecsact_component_like_id {
return ecsact_meta_system_assoc_component_id(
ecsact_id_cast<ecsact_system_like_id>(system_id),
assoc_id
);
}

return field_ids;
template<typename SystemLikeID>
ECSACT_ALWAYS_INLINE auto system_assoc_fields(
SystemLikeID system_id,
ecsact_system_assoc_id assoc_id
) -> std::vector<ecsact_field_id> {
auto result = std::vector<ecsact_field_id>{};
result.resize(32);
auto fields_count = int32_t{};
ecsact_meta_system_assoc_fields(
system_id,
assoc_id,
result.size(),
result.data(),
&fields_count
);
result.resize(fields_count);
return result;
}

template<typename SystemLikeID, typename ComponentLikeID>
ECSACT_ALWAYS_INLINE auto system_association_capabilities(
SystemLikeID system_id,
ComponentLikeID component_id,
ecsact_field_id field_id
template<typename SystemLikeID>
ECSACT_ALWAYS_INLINE auto system_assoc_capabilities(
SystemLikeID id,
ecsact_system_assoc_id assoc_id
) {
using result_t =
std::unordered_map<ecsact_component_like_id, ecsact_system_capability>;

auto sys_like_id = ecsact_id_cast<ecsact_system_like_id>(system_id);
auto comp_like_id = ecsact_id_cast<ecsact_component_like_id>(component_id);
std::vector<std::pair<ecsact_component_like_id, ecsact_system_capability>>;

auto count = ecsact_meta_system_association_capabilities_count(
sys_like_id,
comp_like_id,
field_id
);
const auto sys_like_id = ecsact_id_cast<ecsact_system_like_id>(id);
auto count = ecsact_meta_system_capabilities_count(sys_like_id);
std::vector<ecsact_component_like_id> components;
std::vector<ecsact_system_capability> capabilities;
components.resize(count);
capabilities.resize(count);

ecsact_meta_system_association_capabilities(
ecsact_meta_system_assoc_capabilities(
sys_like_id,
comp_like_id,
field_id,
assoc_id,
count,
components.data(),
capabilities.data(),
nullptr
);

result_t result;
result.reserve(count);
result.resize(count);

for(decltype(count) i = 0; count > i; ++i) {
result[components[i]] = capabilities[i];
result[i] = {components[i], capabilities[i]};
}

return result;
Expand Down Expand Up @@ -592,6 +622,17 @@ auto system_notify_settings( //
return result;
}

template<typename CompositeID>
ECSACT_ALWAYS_INLINE auto field_name( //
CompositeID id,
ecsact_field_id field_id
) -> std::string {
return ecsact_meta_field_name(
ecsact_id_cast<ecsact_composite_id>(id),
field_id
);
}

ECSACT_ALWAYS_INLINE auto main_package() -> std::optional<ecsact_package_id> {
auto main_pkg_id = ecsact_meta_main_package();
if(main_pkg_id == static_cast<ecsact_package_id>(-1)) {
Expand Down

0 comments on commit 9288773

Please sign in to comment.