Просмотр исходного кода

meta: added resolve_type utility

Michele Caini 5 лет назад
Родитель
Сommit
89dc7f817f
3 измененных файлов с 20 добавлено и 3 удалено
  1. 8 2
      docs/md/meta.md
  2. 10 0
      src/entt/meta/resolve.hpp
  3. 2 1
      test/entt/meta/meta.cpp

+ 8 - 2
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<my_type>();
 
-// 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<my_type>::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

+ 10 - 0
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 {

+ 2 - 1
test/entt/meta/meta.cpp

@@ -289,7 +289,8 @@ struct Meta: ::testing::Test {
 };
 
 TEST_F(Meta, Resolve) {
-    ASSERT_EQ(entt::resolve<derived_type>(), entt::resolve("derived"_hs));
+    ASSERT_EQ(entt::resolve<derived_type>(), entt::resolve_id("derived"_hs));
+    ASSERT_EQ(entt::resolve<derived_type>(), entt::resolve_type(entt::type_info<derived_type>::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<char>());