From 89dc7f817f224f368bb6e4fbfb3c5cdeb1daf433 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Sat, 25 Apr 2020 17:36:57 +0200 Subject: [PATCH] meta: added resolve_type utility --- docs/md/meta.md | 10 ++++++++-- src/entt/meta/resolve.hpp | 10 ++++++++++ test/entt/meta/meta.cpp | 3 ++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/docs/md/meta.md b/docs/md/meta.md index 6b69e591a..c1dec0091 100644 --- a/docs/md/meta.md +++ b/docs/md/meta.md @@ -266,17 +266,23 @@ To search for a reflected type there are a few options: // direct access to a reflected type auto by_type = entt::resolve(); -// search for a reflected type by identifier +// lookup of a reflected type by identifier auto by_id = entt::resolve_id("reflected_type"_hs); + +// lookup of a reflected type by type id +auto by_type_id = entt::resolve_type(entt::type_info::id()); ``` There exits also an overload of the `resolve` function to use to iterate all the -reflected types at once: +reflected types at once as well as a `resolve_if` function to use to perform +more refined searches when needed: ```cpp resolve([](auto type) { // ... }); + +auto by_lookup = resolve_if([](auto type) { return type.is_floating_point(); }); ``` In all cases, the returned value is an instance of `meta_type`. This kind of diff --git a/src/entt/meta/resolve.hpp b/src/entt/meta/resolve.hpp index 6acb4d20c..4bcb52b7a 100644 --- a/src/entt/meta/resolve.hpp +++ b/src/entt/meta/resolve.hpp @@ -44,6 +44,16 @@ inline meta_type resolve_id(const id_type id) ENTT_NOEXCEPT { } +/** + * @brief Returns the meta type associated with a given type id, if any. + * @param id Unique identifier. + * @return The meta type associated with the given type id, if any. + */ +inline meta_type resolve_type(const id_type id) ENTT_NOEXCEPT { + return resolve_if([id](const auto type) { return type.type_id() == id; }); +} + + /*! @copydoc resolve_id */ [[deprecated("use entt::resolve_id instead")]] inline meta_type resolve(const id_type id) ENTT_NOEXCEPT { diff --git a/test/entt/meta/meta.cpp b/test/entt/meta/meta.cpp index 4326a1720..4e692e916 100644 --- a/test/entt/meta/meta.cpp +++ b/test/entt/meta/meta.cpp @@ -289,7 +289,8 @@ struct Meta: ::testing::Test { }; TEST_F(Meta, Resolve) { - ASSERT_EQ(entt::resolve(), entt::resolve("derived"_hs)); + ASSERT_EQ(entt::resolve(), entt::resolve_id("derived"_hs)); + ASSERT_EQ(entt::resolve(), entt::resolve_type(entt::type_info::id())); // it could be "char"_hs rather than entt::hashed_string::value("char") if it weren't for a bug in VS2017 ASSERT_EQ(entt::resolve_if([](auto type) { return type.id() == entt::hashed_string::value("char"); }), entt::resolve());