#include #include #include #include #include #include #include #include #include #include #include enum class my_entity : entt::id_type { disabled = 0x10000000, _entt_enum_as_bitmask }; struct entity_traits { using value_type = my_entity; using entity_type = std::uint32_t; using version_type = std::uint16_t; static constexpr entity_type entity_mask = 0xFFFF; // 16b static constexpr entity_type version_mask = 0x0FFF; // 12b }; template<> struct entt::entt_traits: entt::basic_entt_traits { static constexpr std::size_t page_size = ENTT_SPARSE_PAGE; }; namespace { [[nodiscard]] bool is_disabled(const my_entity entity) { return ((entity & my_entity::disabled) == my_entity::disabled); } } // namespace TEST(Example, DisabledEntity) { entt::basic_registry registry{}; auto view = registry.view(); const my_entity entity = registry.create(entt::entt_traits::construct(4u, 1u)); const my_entity other = registry.create(entt::entt_traits::construct(3u, 0u)); registry.emplace(entity); registry.emplace(other); ASSERT_FALSE(is_disabled(*registry.storage().find(entity))); ASSERT_FALSE(is_disabled(*registry.storage().find(other))); ASSERT_FALSE(is_disabled(*registry.storage().find(entity))); ASSERT_FALSE(is_disabled(*registry.storage().find(other))); registry.storage().bump(entity | my_entity::disabled); ASSERT_TRUE(is_disabled(*registry.storage().find(entity))); ASSERT_FALSE(is_disabled(*registry.storage().find(other))); ASSERT_FALSE(is_disabled(*registry.storage().find(entity))); ASSERT_FALSE(is_disabled(*registry.storage().find(other))); view.use(); ASSERT_EQ(std::distance(view.begin(), view.end()), 2); for(auto entt: view) { if(entt::to_entity(entt) == entt::to_entity(entity)) { ASSERT_NE(entt, entity); ASSERT_EQ(entt::to_version(entt), entt::to_version(entity)); ASSERT_TRUE(is_disabled(entt)); } else { ASSERT_EQ(entt, other); ASSERT_EQ(entt::to_version(entt), entt::to_version(other)); ASSERT_FALSE(is_disabled(entt)); } } view.use(); ASSERT_EQ(std::distance(view.begin(), view.end()), 2); for(auto entt: view) { ASSERT_FALSE(is_disabled(entt)); } registry.storage().bump(entity); registry.storage().bump(other | my_entity::disabled); ASSERT_FALSE(is_disabled(*registry.storage().find(entity))); ASSERT_FALSE(is_disabled(*registry.storage().find(other))); ASSERT_FALSE(is_disabled(*registry.storage().find(entity))); ASSERT_TRUE(is_disabled(*registry.storage().find(other))); view.use(); ASSERT_EQ(std::distance(view.begin(), view.end()), 2); for(auto entt: view) { ASSERT_FALSE(is_disabled(entt)); } view.use(); ASSERT_EQ(std::distance(view.begin(), view.end()), 2); for(auto entt: view) { if(entt::to_entity(entt) == entt::to_entity(other)) { ASSERT_NE(entt, other); ASSERT_EQ(entt::to_version(entt), entt::to_version(other)); ASSERT_TRUE(is_disabled(entt)); } else { ASSERT_EQ(entt, entity); ASSERT_EQ(entt::to_version(entt), entt::to_version(entity)); ASSERT_FALSE(is_disabled(entt)); } } registry.storage().bump(other); ASSERT_FALSE(is_disabled(*registry.storage().find(entity))); ASSERT_FALSE(is_disabled(*registry.storage().find(other))); ASSERT_FALSE(is_disabled(*registry.storage().find(entity))); ASSERT_FALSE(is_disabled(*registry.storage().find(other))); view.use(); ASSERT_EQ(std::distance(view.begin(), view.end()), 2); for(auto entt: view) { ASSERT_FALSE(is_disabled(entt)); } view.use(); ASSERT_EQ(std::distance(view.begin(), view.end()), 2); for(auto entt: view) { ASSERT_FALSE(is_disabled(entt)); } }