meta_dtor.cpp 2.2 KB

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