1
0

custom_identifier.cpp 1.3 KB

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