meta_handle.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <gtest/gtest.h>
  2. #include <entt/core/hashed_string.hpp>
  3. #include <entt/meta/factory.hpp>
  4. #include <entt/meta/meta.hpp>
  5. #include <entt/meta/resolve.hpp>
  6. struct clazz_t {
  7. clazz_t()
  8. : value{} {}
  9. void incr() {
  10. ++value;
  11. }
  12. void decr() {
  13. --value;
  14. }
  15. int value;
  16. };
  17. struct MetaHandle: ::testing::Test {
  18. void SetUp() override {
  19. using namespace entt::literals;
  20. entt::meta<clazz_t>()
  21. .type("clazz"_hs)
  22. .func<&clazz_t::incr>("incr"_hs)
  23. .func<&clazz_t::decr>("decr"_hs);
  24. }
  25. void TearDown() override {
  26. entt::meta_reset();
  27. }
  28. };
  29. TEST_F(MetaHandle, Functionalities) {
  30. using namespace entt::literals;
  31. clazz_t instance{};
  32. entt::meta_handle handle{};
  33. ASSERT_FALSE(handle);
  34. handle = entt::meta_handle{instance};
  35. ASSERT_TRUE(handle);
  36. ASSERT_TRUE(handle->invoke("incr"_hs));
  37. ASSERT_EQ(instance.value, 1);
  38. auto any = entt::forward_as_meta(instance);
  39. handle = entt::meta_handle{any};
  40. ASSERT_FALSE(std::as_const(handle)->invoke("decr"_hs));
  41. ASSERT_TRUE(handle->invoke("decr"_hs));
  42. ASSERT_EQ(instance.value, 0);
  43. }