custom_identifier.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 = typename entt::entt_traits<entt::entity>::entity_type;
  7. static constexpr auto null = entt::null;
  8. constexpr entity_id(entity_type value = null)
  9. : entt{value}
  10. {}
  11. constexpr entity_id(const entity_id &other)
  12. : entt{other.entt}
  13. {}
  14. constexpr operator entity_type() const {
  15. return entt;
  16. }
  17. private:
  18. entity_type entt;
  19. };
  20. template<>
  21. struct entt::entt_traits<entity_id>: entt::entt_traits<entt::entity> {};
  22. TEST(Example, CustomIdentifier) {
  23. entt::basic_registry<entity_id> registry{};
  24. entity_id entity{};
  25. ASSERT_FALSE(registry.valid(entity));
  26. ASSERT_TRUE(entity == entt::null);
  27. entity = registry.create();
  28. ASSERT_TRUE(registry.valid(entity));
  29. ASSERT_TRUE(entity != entt::null);
  30. ASSERT_FALSE((registry.all_of<int, char>(entity)));
  31. ASSERT_EQ(registry.try_get<int>(entity), nullptr);
  32. registry.emplace<int>(entity, 42);
  33. ASSERT_TRUE((registry.any_of<int, char>(entity)));
  34. ASSERT_EQ(registry.get<int>(entity), 42);
  35. registry.destroy(entity);
  36. ASSERT_FALSE(registry.valid(entity));
  37. ASSERT_TRUE(entity != entt::null);
  38. entity = registry.create();
  39. ASSERT_TRUE(registry.valid(entity));
  40. ASSERT_TRUE(entity != entt::null);
  41. }