meta_dtor.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <utility>
  2. #include <gtest/gtest.h>
  3. #include <entt/core/hashed_string.hpp>
  4. #include <entt/locator/locator.hpp>
  5. #include <entt/meta/context.hpp>
  6. #include <entt/meta/factory.hpp>
  7. #include <entt/meta/meta.hpp>
  8. #include <entt/meta/node.hpp>
  9. #include <entt/meta/resolve.hpp>
  10. struct clazz_t {
  11. clazz_t() {
  12. ++counter;
  13. }
  14. static void destroy_decr(clazz_t &) {
  15. --counter;
  16. }
  17. void destroy_incr() const {
  18. ++counter;
  19. }
  20. inline static int counter = 0; // NOLINT
  21. };
  22. struct MetaDtor: ::testing::Test {
  23. void SetUp() override {
  24. using namespace entt::literals;
  25. entt::meta<clazz_t>()
  26. .type("clazz"_hs)
  27. .dtor<clazz_t::destroy_decr>();
  28. clazz_t::counter = 0;
  29. }
  30. void TearDown() override {
  31. entt::meta_reset();
  32. }
  33. };
  34. TEST_F(MetaDtor, Functionalities) {
  35. ASSERT_EQ(clazz_t::counter, 0);
  36. auto any = entt::resolve<clazz_t>().construct();
  37. auto cref = std::as_const(any).as_ref();
  38. auto ref = any.as_ref();
  39. ASSERT_TRUE(any);
  40. ASSERT_TRUE(cref);
  41. ASSERT_TRUE(ref);
  42. ASSERT_EQ(clazz_t::counter, 1);
  43. cref.reset();
  44. ref.reset();
  45. ASSERT_TRUE(any);
  46. ASSERT_FALSE(cref);
  47. ASSERT_FALSE(ref);
  48. ASSERT_EQ(clazz_t::counter, 1);
  49. any.reset();
  50. ASSERT_FALSE(any);
  51. ASSERT_FALSE(cref);
  52. ASSERT_FALSE(ref);
  53. ASSERT_EQ(clazz_t::counter, 0);
  54. }
  55. TEST_F(MetaDtor, AsRefConstruction) {
  56. ASSERT_EQ(clazz_t::counter, 0);
  57. clazz_t instance{};
  58. auto any = entt::forward_as_meta(instance);
  59. auto cany = entt::forward_as_meta(std::as_const(instance));
  60. auto cref = cany.as_ref();
  61. auto ref = any.as_ref();
  62. ASSERT_TRUE(any);
  63. ASSERT_TRUE(cany);
  64. ASSERT_TRUE(cref);
  65. ASSERT_TRUE(ref);
  66. ASSERT_EQ(clazz_t::counter, 1);
  67. any.reset();
  68. cany.reset();
  69. cref.reset();
  70. ref.reset();
  71. ASSERT_FALSE(any);
  72. ASSERT_FALSE(cany);
  73. ASSERT_FALSE(cref);
  74. ASSERT_FALSE(ref);
  75. ASSERT_EQ(clazz_t::counter, 1);
  76. }
  77. TEST_F(MetaDtor, ReRegistration) {
  78. SetUp();
  79. auto &&node = entt::internal::resolve<clazz_t>(entt::internal::meta_context::from(entt::locator<entt::meta_ctx>::value_or()));
  80. ASSERT_NE(node.dtor.dtor, nullptr);
  81. entt::meta<clazz_t>().dtor<&clazz_t::destroy_incr>();
  82. entt::resolve<clazz_t>().construct().reset();
  83. ASSERT_EQ(clazz_t::counter, 2);
  84. }