Skip to content

Commit

Permalink
sigh mixin: operator bool and registry functions
Browse files Browse the repository at this point in the history
  • Loading branch information
skypjack committed Oct 1, 2024
1 parent 8430836 commit 61b5c19
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src/entt/entity/mixin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,27 @@ class basic_sigh_mixin final: public Type {
return sink{destruction};
}

/**
* @brief Checks if a mixin refers to a valid registry.
* @return True if the mixin refers to a valid registry, false otherwise.
*/
[[nodiscard]] explicit operator bool() const noexcept {
return (owner != nullptr);
}

/**
* @brief Returns a pointer to the underlying registry, if any.
* @return A pointer to the underlying registry, if any.
*/
[[nodiscard]] const registry_type &registry() const noexcept {
return owner_or_assert();
}

/*! @copydoc registry */
[[nodiscard]] registry_type &registry() noexcept {
return owner_or_assert();
}

/**
* @brief Emplace elements into a storage.
*
Expand Down
48 changes: 44 additions & 4 deletions test/entt/entity/sigh_mixin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <entt/entity/mixin.hpp>
#include <entt/entity/registry.hpp>
#include <entt/entity/storage.hpp>
#include "../../common/config.h"
#include "../../common/entity.h"
#include "../../common/linter.hpp"
#include "../../common/non_default_constructible.h"
Expand Down Expand Up @@ -57,9 +58,13 @@ struct SighMixin: testing::Test {
using type = Type;
};

template<typename Type>
using SighMixinDeathTest = SighMixin<Type>;

using SighMixinTypes = ::testing::Types<int, test::pointer_stable>;

TYPED_TEST_SUITE(SighMixin, SighMixinTypes, );
TYPED_TEST_SUITE(SighMixinDeathTest, SighMixinTypes, );

TYPED_TEST(SighMixin, Functionalities) {
using value_type = typename TestFixture::type;
Expand Down Expand Up @@ -459,23 +464,50 @@ TEST(SighMixin, AutoSignal) {
ASSERT_FALSE(registry.valid(entity));
}

TYPED_TEST(SighMixin, Registry) {
using value_type = typename TestFixture::type;

entt::registry registry;
entt::sigh_mixin<entt::storage<value_type>> pool;

ASSERT_FALSE(pool);

pool.bind(registry);

ASSERT_TRUE(pool);
ASSERT_EQ(&pool.registry(), &registry);
ASSERT_EQ(&std::as_const(pool).registry(), &registry);
}

ENTT_DEBUG_TYPED_TEST(SighMixinDeathTest, Registry) {
using value_type = typename TestFixture::type;
entt::sigh_mixin<entt::storage<value_type>> pool;
ASSERT_DEATH([[maybe_unused]] auto &registry = pool.registry(), "");
ASSERT_DEATH([[maybe_unused]] const auto &registry = std::as_const(pool).registry(), "");
}

TYPED_TEST(SighMixin, CustomRegistry) {
using value_type = typename TestFixture::type;
using registry_type = test::basic_custom_registry<test::entity>;

registry_type registry;
auto &pool = registry.storage<value_type>();
entt::basic_sigh_mixin<entt::basic_storage<value_type, test::entity>, registry_type> pool;
const std::array entity{registry.create(), registry.create()};

ASSERT_FALSE(pool);

testing::StaticAssertTypeEq<decltype(pool), entt::basic_sigh_mixin<entt::basic_storage<value_type, test::entity>, registry_type> &>();
pool.bind(static_cast<entt::basic_registry<test::entity> &>(registry));

ASSERT_TRUE(pool);

std::size_t on_construct{};
std::size_t on_destroy{};

pool.on_construct().template connect<&listener<registry_type>>(on_construct);
pool.on_destroy().template connect<&listener<registry_type>>(on_destroy);

pool.emplace(registry.create());
pool.emplace(registry.create());
pool.emplace(entity[0u]);
pool.emplace(entity[1u]);

ASSERT_EQ(on_construct, 2u);
ASSERT_EQ(on_destroy, 0u);
Expand All @@ -486,6 +518,14 @@ TYPED_TEST(SighMixin, CustomRegistry) {
ASSERT_EQ(on_destroy, 2u);
}

ENTT_DEBUG_TYPED_TEST(SighMixinDeathTest, CustomRegistry) {
using value_type = typename TestFixture::type;
using registry_type = test::basic_custom_registry<test::entity>;
entt::basic_sigh_mixin<entt::basic_storage<value_type, test::entity>, registry_type> pool;
ASSERT_DEATH([[maybe_unused]] auto &registry = pool.registry(), "");
ASSERT_DEATH([[maybe_unused]] const auto &registry = std::as_const(pool).registry(), "");
}

TYPED_TEST(SighMixin, CustomAllocator) {
using value_type = typename TestFixture::type;
using storage_type = entt::sigh_mixin<entt::basic_storage<value_type, entt::entity, test::throwing_allocator<value_type>>>;
Expand Down

0 comments on commit 61b5c19

Please sign in to comment.