registry: prepare allocator support (still an unused parameter)

This commit is contained in:
Michele Caini
2022-08-10 11:10:18 +02:00
parent 72bc9e3973
commit 2988a74fe4
4 changed files with 40 additions and 32 deletions

View File

@@ -25,7 +25,7 @@ struct storage_for;
template<typename Type>
class sigh_storage_mixin;
template<typename = entity>
template<typename Entity = entity, typename = std::allocator<Entity>>
class basic_registry;
template<typename, typename, typename = void>

View File

@@ -244,8 +244,9 @@ private:
/**
* @brief Fast and reliable entity-component system.
* @tparam Entity A valid entity type (see entt_traits for more details).
* @tparam Allocator Type of allocator used to manage memory and elements.
*/
template<typename Entity>
template<typename Entity, typename Allocator>
class basic_registry {
using entity_traits = entt_traits<Entity>;
using basic_common_type = basic_sparse_set<Entity>;

View File

@@ -24,7 +24,8 @@ namespace entt {
*/
template<typename Type>
class sigh_storage_mixin final: public Type {
using sigh_type = sigh<void(basic_registry<typename Type::entity_type> &, const typename Type::entity_type), typename Type::allocator_type>;
using basic_registry_type = basic_registry<typename Type::entity_type, typename Type::base_type::allocator_type>;
using sigh_type = sigh<void(basic_registry_type &, const typename Type::entity_type), typename Type::allocator_type>;
using basic_iterator = typename Type::basic_iterator;
void pop(basic_iterator first, basic_iterator last) override {
@@ -38,7 +39,7 @@ class sigh_storage_mixin final: public Type {
}
}
basic_iterator try_emplace(const typename Type::entity_type entt, const bool force_back, const void *value) final {
basic_iterator try_emplace(const typename basic_registry_type::entity_type entt, const bool force_back, const void *value) final {
ENTT_ASSERT(owner != nullptr, "Invalid pointer to registry");
Type::try_emplace(entt, force_back, value);
construction.publish(*owner, entt);
@@ -50,6 +51,8 @@ public:
using allocator_type = typename Type::allocator_type;
/*! @brief Underlying entity identifier. */
using entity_type = typename Type::entity_type;
/*! @brief Expected registry type. */
using registry_type = basic_registry_type;
/*! @brief Default constructor. */
sigh_storage_mixin()
@@ -216,13 +219,13 @@ public:
* @param value A variable wrapped in an opaque container.
*/
void bind(any value) noexcept final {
auto *reg = any_cast<basic_registry<entity_type>>(&value);
auto *reg = any_cast<basic_registry_type>(&value);
owner = reg ? reg : owner;
Type::bind(std::move(value));
}
private:
basic_registry<entity_type> *owner;
basic_registry_type *owner;
sigh_type construction;
sigh_type destruction;
sigh_type update;

View File

@@ -25,7 +25,8 @@ struct counter {
int value{};
};
void listener(counter &counter, entt::registry &, entt::entity) {
template<typename Registry>
void listener(counter &counter, Registry &, typename Registry::entity_type) {
++counter.value;
}
@@ -39,8 +40,8 @@ TEST(SighStorageMixin, GenericType) {
counter on_destroy{};
pool.bind(entt::forward_as_any(registry));
pool.on_construct().connect<&listener>(on_construct);
pool.on_destroy().connect<&listener>(on_destroy);
pool.on_construct().connect<&listener<entt::registry>>(on_construct);
pool.on_destroy().connect<&listener<entt::registry>>(on_destroy);
ASSERT_NE(base.emplace(entities[0u]), base.end());
@@ -104,8 +105,8 @@ TEST(SighStorageMixin, StableType) {
counter on_destroy{};
pool.bind(entt::forward_as_any(registry));
pool.on_construct().connect<&listener>(on_construct);
pool.on_destroy().connect<&listener>(on_destroy);
pool.on_construct().connect<&listener<entt::registry>>(on_construct);
pool.on_destroy().connect<&listener<entt::registry>>(on_destroy);
ASSERT_NE(base.emplace(entities[0u]), base.end());
@@ -169,8 +170,8 @@ TEST(SighStorageMixin, EmptyType) {
counter on_destroy{};
pool.bind(entt::forward_as_any(registry));
pool.on_construct().connect<&listener>(on_construct);
pool.on_destroy().connect<&listener>(on_destroy);
pool.on_construct().connect<&listener<entt::registry>>(on_construct);
pool.on_destroy().connect<&listener<entt::registry>>(on_destroy);
ASSERT_NE(base.emplace(entities[0u]), base.end());
@@ -234,8 +235,8 @@ TEST(SighStorageMixin, NonDefaultConstructibleType) {
counter on_destroy{};
pool.bind(entt::forward_as_any(registry));
pool.on_construct().connect<&listener>(on_construct);
pool.on_destroy().connect<&listener>(on_destroy);
pool.on_construct().connect<&listener<entt::registry>>(on_construct);
pool.on_destroy().connect<&listener<entt::registry>>(on_destroy);
ASSERT_EQ(base.emplace(entities[0u]), base.end());
@@ -288,8 +289,8 @@ TEST(SighStorageMixin, VoidType) {
counter on_destroy{};
pool.bind(entt::forward_as_any(registry));
pool.on_construct().connect<&listener>(on_construct);
pool.on_destroy().connect<&listener>(on_destroy);
pool.on_construct().connect<&listener<entt::registry>>(on_construct);
pool.on_destroy().connect<&listener<entt::registry>>(on_destroy);
pool.emplace(entt::entity{99});
@@ -320,8 +321,8 @@ TEST(SighStorageMixin, Move) {
counter on_destroy{};
pool.bind(entt::forward_as_any(registry));
pool.on_construct().connect<&listener>(on_construct);
pool.on_destroy().connect<&listener>(on_destroy);
pool.on_construct().connect<&listener<entt::registry>>(on_construct);
pool.on_destroy().connect<&listener<entt::registry>>(on_destroy);
pool.emplace(entt::entity{3}, 3);
@@ -373,12 +374,12 @@ TEST(SighStorageMixin, Swap) {
counter on_destroy{};
pool.bind(entt::forward_as_any(registry));
pool.on_construct().connect<&listener>(on_construct);
pool.on_destroy().connect<&listener>(on_destroy);
pool.on_construct().connect<&listener<entt::registry>>(on_construct);
pool.on_destroy().connect<&listener<entt::registry>>(on_destroy);
other.bind(entt::forward_as_any(registry));
other.on_construct().connect<&listener>(on_construct);
other.on_destroy().connect<&listener>(on_destroy);
other.on_construct().connect<&listener<entt::registry>>(on_construct);
other.on_destroy().connect<&listener<entt::registry>>(on_destroy);
pool.emplace(entt::entity{42}, 41);
@@ -412,14 +413,15 @@ TEST(SighStorageMixin, Swap) {
TEST(SighStorageMixin, CustomAllocator) {
auto test = [](auto pool, auto alloc) {
entt::registry registry;
using registry_type = typename decltype(pool)::registry_type;
registry_type registry;
counter on_construct{};
counter on_destroy{};
pool.bind(entt::forward_as_any(registry));
pool.on_construct().template connect<&listener>(on_construct);
pool.on_destroy().template connect<&listener>(on_destroy);
pool.on_construct().template connect<&listener<registry_type>>(on_construct);
pool.on_destroy().template connect<&listener<registry_type>>(on_destroy);
pool.reserve(1u);
@@ -473,18 +475,19 @@ TEST(SighStorageMixin, ThrowingAllocator) {
auto test = [](auto pool) {
using pool_allocator_type = typename decltype(pool)::allocator_type;
using value_type = typename decltype(pool)::value_type;
using registry_type = typename decltype(pool)::registry_type;
typename std::decay_t<decltype(pool)>::base_type &base = pool;
constexpr auto packed_page_size = entt::component_traits<typename decltype(pool)::value_type>::page_size;
constexpr auto sparse_page_size = entt::entt_traits<typename decltype(pool)::entity_type>::page_size;
entt::registry registry;
registry_type registry;
counter on_construct{};
counter on_destroy{};
pool.bind(entt::forward_as_any(registry));
pool.on_construct().template connect<&listener>(on_construct);
pool.on_destroy().template connect<&listener>(on_destroy);
pool.on_construct().template connect<&listener<registry_type>>(on_construct);
pool.on_destroy().template connect<&listener<registry_type>>(on_destroy);
pool_allocator_type::trigger_on_allocate = true;
@@ -546,14 +549,15 @@ TEST(SighStorageMixin, ThrowingAllocator) {
TEST(SighStorageMixin, ThrowingComponent) {
entt::sigh_storage_mixin<entt::storage<test::throwing_type>> pool;
entt::registry registry;
using registry_type = typename decltype(pool)::registry_type;
registry_type registry;
counter on_construct{};
counter on_destroy{};
pool.bind(entt::forward_as_any(registry));
pool.on_construct().connect<&listener>(on_construct);
pool.on_destroy().connect<&listener>(on_destroy);
pool.on_construct().connect<&listener<registry_type>>(on_construct);
pool.on_destroy().connect<&listener<registry_type>>(on_destroy);
test::throwing_type::trigger_on_value = 42;