Skip to content

association improvements, fixes, and tests #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ build --incompatible_strict_action_env
build --enable_runfiles

build:windows --platforms=//bazel/platforms:windows
build:windows --host_platform=//bazel/platforms:windows

build:linux --platforms=//bazel/platforms:linux
build:linux --extra_toolchains=@llvm_toolchain//:cc-toolchain-x86_64-linux
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/ci-windows.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# workaround long path issues on GitHub actions Windows machine
startup --output_user_root=C:/tmp
import %workspace%/.github/workflows/ci.bazelrc

1 change: 1 addition & 0 deletions .github/workflows/ci.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ build --repository_cache=~/.cache/bazel-repo
test --test_output=errors
# Allows tests to run bazelisk-in-bazel, since this is the cache folder used
test --test_env=XDG_CACHE_HOME

4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
/Users/runneradmin/.cache/bazel-disk-cache
key: ${{runner.os}}-bazel-cache
- uses: actions/checkout@v3
- run: bazel --bazelrc=.github/workflows/ci.bazelrc test //...
- run: bazel --bazelrc=.bazelrc --bazelrc=.github/workflows/ci-windows.bazelrc test //...
test-linux:
runs-on: ubuntu-latest
steps:
Expand All @@ -45,4 +45,4 @@ jobs:
env:
# Bazelisk will download bazel to here, ensure it is cached between runs.
XDG_CACHE_HOME: ~/.cache/bazel-repo
run: bazel --bazelrc=$GITHUB_WORKSPACE/.github/workflows/ci.bazelrc --bazelrc=.bazelrc test //...
run: bazel --bazelrc=.bazelrc --bazelrc=$GITHUB_WORKSPACE/.github/workflows/ci.bazelrc test //...
6 changes: 3 additions & 3 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ http_archive(

http_archive(
name = "rules_ecsact",
sha256 = "dfb1a2e7093110b2daf30c6d1a83f6d0bf19fb9dd52b12b1577c055fd217e990",
strip_prefix = "rules_ecsact-0.2.0",
url = "https://github.com/ecsact-dev/rules_ecsact/archive/refs/tags/0.2.0.tar.gz",
sha256 = "e6d888c63aa536b5b7c6af10d217cdb8ad98b2262fa9d02515a99edbd0d94eea",
strip_prefix = "rules_ecsact-0.2.1",
url = "https://github.com/ecsact-dev/rules_ecsact/archive/refs/tags/0.2.1.tar.gz",
)

http_archive(
Expand Down
2 changes: 1 addition & 1 deletion bazel/copts.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"""

copts = select({
"@bazel_tools//tools/cpp:msvc": ["/std:c++20", "/Zc:preprocessor", "/permissive-"],
"@bazel_tools//tools/cpp:msvc": ["/std:c++20", "/Zc:preprocessor", "/permissive-", "/diagnostics:caret"],
"//conditions:default": ["-std=c++20"],
})
8 changes: 8 additions & 0 deletions ecsact/entt/detail/internal_markers.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cstdint>
#include <type_traits>
#include <concepts>

namespace ecsact::entt::detail {

Expand All @@ -11,6 +12,13 @@ struct association {
static constexpr auto field_offset = FieldOffset;
};

template<typename Assoc>
concept association_concept = //
requires {
{ typename Assoc::component_type{} };
{ Assoc::field_offset } -> std::convertible_to<std::size_t>;
};

template<typename C>
struct temp_storage;

Expand Down
5 changes: 3 additions & 2 deletions ecsact/entt/detail/system_execution_context.hh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ struct system_execution_context_base {
const void* action;
};

namespace detail {}

template<typename Package, typename SystemCapabilitiesInfo>
struct system_execution_context : system_execution_context_base {
using system_execution_context_base::const_cptr_t;
Expand All @@ -50,7 +52,7 @@ struct system_execution_context : system_execution_context_base {

using caps_info = SystemCapabilitiesInfo;
using package = Package;
using view_type = ecsact::entt::view_from_system_capabilities_type<caps_info>;
using view_type = ecsact::entt::system_or_association_view_t<caps_info>;
using association_views_type =
ecsact::entt::association_views_type<caps_info>;

Expand Down Expand Up @@ -380,7 +382,6 @@ struct system_execution_context : system_execution_context_base {
mp_for_each<associations>([&]<typename Assoc>(Assoc) {
using ComponentT = typename Assoc::component_type;
constexpr std::size_t offset = Assoc::field_offset;
auto compnent_name = typeid(ComponentT).name();
const ComponentT& comp = info.registry.template get<ComponentT>(entity);

auto field_entity_value = *reinterpret_cast<const ecsact_entity_id*>(
Expand Down
28 changes: 19 additions & 9 deletions ecsact/entt/runtime.hh
Original file line number Diff line number Diff line change
Expand Up @@ -657,9 +657,10 @@ private:
using caps_info = ecsact::system_capabilities_info<SystemT>;
using associations = typename caps_info::associations;

auto view = system_view<SystemT>(info.registry);
auto assoc_views = system_association_views<SystemT>(info.registry);
auto assoc_views_itrs = system_association_views_iterators(assoc_views);

auto view = system_view<SystemT>(info.registry);
const void* action_data = nullptr;

auto itr_view = [&] {
Expand All @@ -672,9 +673,10 @@ private:
using Assoc = mp_at<associations, mp_size_t<I>>;
using ComponentT = typename Assoc::component_type;

auto& assoc_view = std::get<I>(assoc_views);
auto& assoc_view_itr = std::get<I>(assoc_views_itrs);
constexpr auto offset = Assoc::field_offset;

auto& assoc_view = std::get<I>(assoc_views);
auto& assoc_view_itr = std::get<I>(assoc_views_itrs);
assert(view.contains(entity));
auto& comp = view.template get<ComponentT>(entity);

Expand All @@ -684,12 +686,16 @@ private:
auto entt_field_entity_value =
info.get_entt_entity_id(field_entity_value);

bool found_associated_entity = false;
for(; assoc_view_itr != assoc_view.end(); ++assoc_view_itr) {
found_associated_entity = *assoc_view_itr ==
entt_field_entity_value;
if(found_associated_entity) {
break;
bool found_associated_entity = *assoc_view_itr ==
entt_field_entity_value;
if(!found_associated_entity) {
assoc_view_itr = assoc_view.begin();
for(; assoc_view_itr != assoc_view.end(); ++assoc_view_itr) {
found_associated_entity = *assoc_view_itr ==
entt_field_entity_value;
if(found_associated_entity) {
break;
}
}
}

Expand All @@ -708,6 +714,10 @@ private:
action_data,
actions
);

mp_for_each<mp_iota_c<mp_size<associations>::value>>([&](auto I) {
++std::get<I>(assoc_views_itrs);
});
}
}
};
Expand Down
69 changes: 50 additions & 19 deletions ecsact/entt/system_view.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ auto system_view_helper(

namespace ecsact::entt {

template<typename SystemCapabilitiesInfo>
template<
typename SystemCapabilitiesInfo,
typename ExtraGetTypes = ::ecsact::mp_list<>,
typename ExtraExcludeTypes = ::ecsact::mp_list<>>
auto view_from_system_capabilities(::entt::registry& registry) {
using caps_info = SystemCapabilitiesInfo;

Expand Down Expand Up @@ -50,48 +53,61 @@ auto view_from_system_capabilities(::entt::registry& registry) {
readwrite_components,
include_components,
removes_components,
ExtraGetTypes,
mp_transform<beforechange_storage, readwrite_components>>,
::ecsact::mp_list<>>>;

using exclude_types = mp_unique<mp_flatten<
mp_push_back<exclude_components, adds_components>,
mp_push_back<exclude_components, adds_components, ExtraExcludeTypes>,
::ecsact::mp_list<>>>;

return detail::system_view_helper(get_types{}, exclude_types{}, registry);
}

template<typename SystemCapabilitiesInfo>
template<
typename SystemCapabilitiesInfo,
typename ExtraGetTypes = ::ecsact::mp_list<>,
typename ExtraExcludeTypes = ::ecsact::mp_list<>>
using view_from_system_capabilities_type =
decltype(view_from_system_capabilities<SystemCapabilitiesInfo>(
std::declval<::entt::registry&>()
));
decltype(view_from_system_capabilities<
SystemCapabilitiesInfo,
ExtraGetTypes,
ExtraExcludeTypes>(std::declval<::entt::registry&>()));

template<typename Assoc>
auto association_view(::entt::registry& registry) {
using ecsact::entt::detail::association;
using assoc_marker_component =
association<typename Assoc::component_type, Assoc::field_offset>;
using extra_get_types = ::ecsact::mp_list<assoc_marker_component>;
using view_type = view_from_system_capabilities_type<Assoc, extra_get_types>;

return view_from_system_capabilities<Assoc, extra_get_types>(registry);
}

template<typename Assoc>
using association_view_type =
decltype(association_view<Assoc>(std::declval<::entt::registry&>()));

template<typename SystemCapabilitiesInfo>
auto association_views(::entt::registry& registry) {
using boost::mp11::mp_empty;
using boost::mp11::mp_for_each;
using boost::mp11::mp_rename;
using boost::mp11::mp_transform;

using caps_info = SystemCapabilitiesInfo;
using assocs = typename caps_info::associations;

using result_type = mp_rename<
mp_transform<
view_from_system_capabilities_type,
typename caps_info::associations>,
mp_transform<association_view_type, typename caps_info::associations>,
std::tuple>;

static_assert(
boost::mp11::mp_size<typename caps_info::associations>::value ==
std::tuple_size_v<result_type>,
"[INTERNAL] result_type failure"
);

result_type result;

mp_for_each<typename caps_info::associations>([&]<typename Assoc>(Assoc) {
using view_type = view_from_system_capabilities_type<Assoc>;
std::get<view_type>(result) =
view_from_system_capabilities<Assoc>(registry);
mp_for_each<assocs>([&]<typename Assoc>(Assoc) {
std::get<association_view_type<Assoc>>(result) =
association_view<Assoc>(registry);
});

return result;
Expand Down Expand Up @@ -133,4 +149,19 @@ auto system_association_views_iterators(AssocViews& assoc_views) {
assoc_views
);
}

template<typename T>
struct system_or_association_view {
using type = view_from_system_capabilities_type<T>;
};

template<ecsact::entt::detail::association_concept T>
struct system_or_association_view<T> {
using type = association_view_type<T>;
};

template<typename T>
using system_or_association_view_t =
typename system_or_association_view<T>::type;

} // namespace ecsact::entt
12 changes: 12 additions & 0 deletions runtime/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,15 @@ cc_test(
"@com_google_googletest//:gtest_main",
],
)

cc_binary(
name = "debug_print_entt_views",
srcs = ["debug_print_entt_views.cc"],
copts = copts,
deps = [
":runtime__public_cc",
"//:lib",
"@boost//libs/mp11",
"@com_github_skypjack_entt//:entt",
],
)
68 changes: 68 additions & 0 deletions runtime/test/debug_print_entt_views.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <iostream>
#include <type_traits>
#include <boost/mp11.hpp>
#include "ecsact/entt/system_view.hh"
#include "runtime_test.ecsact.meta.hh"

auto replace_all(
std::string& str,
std::string_view find,
std::string_view replace
) {
auto idx = str.find(find);
while(idx != std::string::npos) {
str.replace(idx, find.size(), replace);
idx = str.find(find, idx + replace.size());
}
}

template<typename T>
auto pretty_type_string() {
std::string pretty_type = typeid(T).name();

replace_all(pretty_type, "class ", "");
replace_all(pretty_type, "struct ", "");
replace_all(pretty_type, "enum ", "");
replace_all(pretty_type, "ecsact::entt::detail::", "");
replace_all(pretty_type, "entt::basic_view", "view");
replace_all(pretty_type, "runtime_test::", "");
replace_all(pretty_type, ",", ", ");
replace_all(pretty_type, "> >", ">>");

return pretty_type;
}

template<typename S>
auto print_system_views() {
using boost::mp11::mp_for_each;
using ecsact::entt::system_association_views_type;
using ecsact::entt::system_view_type;

std::cout //
<< " " << pretty_type_string<S>() << "\n"
<< " | " << pretty_type_string<system_view_type<S>>() << "\n";

mp_for_each<system_association_views_type<S>>([]<typename V>(V) {
std::cout //
<< " | " << pretty_type_string<V>() << " (association view)\n";
});

std::cout << "\n";
}

auto main() -> int {
using boost::mp11::mp_for_each;
using runtime_test::package;

std::cout << " ==== [ System Views ] ==== \n";
mp_for_each<typename package::systems>([]<typename S>(S) {
print_system_views<S>();
});

std::cout << " ==== [ Action Views ] ==== \n";
mp_for_each<typename package::actions>([]<typename S>(S) {
print_system_views<S>();
});

return 0;
}
Loading