1
0

custom_identifier.cpp 1.6 KB

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