custom_identifier.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <cstdint>
  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) noexcept
  9. : entt{value} {}
  10. constexpr entity_id(const entity_id &other) noexcept
  11. : entt{other.entt} {}
  12. constexpr entity_id &operator=(const entity_id &other) noexcept {
  13. entt = other.entt;
  14. return *this;
  15. }
  16. constexpr operator entity_type() const noexcept {
  17. return entt;
  18. }
  19. private:
  20. entity_type entt;
  21. };
  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. }