meta: added resolve_type utility

This commit is contained in:
Michele Caini
2020-04-25 17:36:57 +02:00
parent fe0f26ce9c
commit 89dc7f817f
3 changed files with 20 additions and 3 deletions

View File

@@ -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

View File

@@ -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 {

View File

@@ -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>());