1
0

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