custom_identifier.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include <type_traits>
  2. #include <gtest/gtest.h>
  3. #include <entt/entity/entity.hpp>
  4. #include <entt/entity/registry.hpp>
  5. struct entity_id {
  6. using entity_type = std::uint32_t;
  7. static constexpr auto null = entt::null;
  8. constexpr entity_id(entity_type value = null) ENTT_NOEXCEPT
  9. : entt{value} {}
  10. constexpr entity_id(const entity_id &other) ENTT_NOEXCEPT
  11. : entt{other.entt} {}
  12. constexpr operator entity_type() const ENTT_NOEXCEPT {
  13. return entt;
  14. }
  15. private:
  16. entity_type entt;
  17. };
  18. TEST(Example, CustomIdentifier) {
  19. entt::basic_registry<entity_id> registry{};
  20. entity_id entity{};
  21. ASSERT_FALSE(registry.valid(entity));
  22. ASSERT_TRUE(entity == entt::null);
  23. entity = registry.create();
  24. ASSERT_TRUE(registry.valid(entity));
  25. ASSERT_TRUE(entity != entt::null);
  26. ASSERT_FALSE((registry.all_of<int, char>(entity)));
  27. ASSERT_EQ(registry.try_get<int>(entity), nullptr);
  28. registry.emplace<int>(entity, 42);
  29. ASSERT_TRUE((registry.any_of<int, char>(entity)));
  30. ASSERT_EQ(registry.get<int>(entity), 42);
  31. registry.destroy(entity);
  32. ASSERT_FALSE(registry.valid(entity));
  33. ASSERT_TRUE(entity != entt::null);
  34. entity = registry.create();
  35. ASSERT_TRUE(registry.valid(entity));
  36. ASSERT_TRUE(entity != entt::null);
  37. }