type_info: temporarily reintroduce type_info::index for performance reasons

This commit is contained in:
Michele Caini
2021-09-10 11:29:05 +02:00
parent 46b686910b
commit 7ab08779dc
4 changed files with 33 additions and 11 deletions

View File

@@ -491,6 +491,18 @@ auto info = entt::type_id<a_type>();
These are the information made available by this object:
* The index associated with a given type:
```cpp
auto idx = entt::type_id<a_type>().index();
```
This is also an alias for the following:
```cpp
auto idx = entt::type_index<std::remove_const_t<std::remove_reference_t<a_type>>>::value();
```
* The hash value associated with a given type:
```cpp
@@ -500,7 +512,7 @@ These are the information made available by this object:
This is also an alias for the following:
```cpp
auto hash = entt::type_hash<a_type>::value();
auto hash = entt::type_hash<std::remove_const_t<std::remove_reference_t<a_type>>>::value();
```
* The name associated with a given type:
@@ -512,7 +524,7 @@ These are the information made available by this object:
This is also an alias for the following:
```cpp
auto name = entt::type_name<a_type>::value();
auto name = entt::type_name<std::remove_const_t<std::remove_reference_t<a_type>>>::value();
```
Where all accessed features are available at compile-time, the `type_info` class

View File

@@ -151,7 +151,8 @@ struct type_name final {
struct type_info final {
/*! @brief Default constructor. */
constexpr type_info() ENTT_NOEXCEPT
: identifier{},
: seq{},
identifier{},
alias{}
{}
@@ -166,7 +167,8 @@ struct type_info final {
*/
template<typename Type>
constexpr type_info(std::in_place_type_t<Type>) ENTT_NOEXCEPT
: identifier{type_hash<std::remove_reference_t<std::remove_const_t<Type>>>::value()},
: seq{type_index<std::remove_reference_t<std::remove_const_t<Type>>>::value()},
identifier{type_hash<std::remove_reference_t<std::remove_const_t<Type>>>::value()},
alias{type_name<std::remove_reference_t<std::remove_const_t<Type>>>::value()}
{}
@@ -190,6 +192,14 @@ struct type_info final {
return alias.data() != nullptr;
}
/**
* @brief Type index.
* @return Type index.
*/
[[nodiscard]] constexpr id_type index() const ENTT_NOEXCEPT {
return seq;
}
/**
* @brief Type hash.
* @return Type hash.
@@ -216,6 +226,7 @@ struct type_info final {
}
private:
id_type seq;
id_type identifier;
std::string_view alias;
};

View File

@@ -183,16 +183,14 @@ public:
* empty and thus invalid element otherwise.
*/
poly_storage & storage(const type_info info) {
auto it = std::find_if(pools.begin(), pools.end(), [info](auto &&pdata) { return pdata.poly && pdata.poly->value_type().hash() == info.hash(); });
ENTT_ASSERT(it != pools.end(), "Storage not available");
return it->poly;
ENTT_ASSERT(info.index() < pools.size() && pools[info.index()].poly, "Storage not available");
return pools[info.index()].poly;
}
/*! @copydoc storage */
const poly_storage & storage(const type_info info) const {
auto it = std::find_if(pools.cbegin(), pools.cend(), [info](auto &&pdata) { return pdata.poly && pdata.poly->value_type().hash() == info.hash(); });
ENTT_ASSERT(it != pools.cend(), "Storage not available");
return it->poly;
ENTT_ASSERT(info.index() < pools.size() && pools[info.index()].poly, "Storage not available");
return pools[info.index()].poly;
}
/**

View File

@@ -54,7 +54,7 @@ TEST(TypeInfo, Functionalities) {
ASSERT_EQ(entt::type_id<int &>(), entt::type_id<int &&>());
ASSERT_EQ(entt::type_id<int &>(), entt::type_id<int>());
auto info = entt::type_id<int>();
auto info = entt::type_id<const int &>();
const auto unnamed = entt::type_id<float>();
entt::type_info empty{};
@@ -62,6 +62,7 @@ TEST(TypeInfo, Functionalities) {
ASSERT_TRUE(info == info);
ASSERT_FALSE(info != info);
ASSERT_EQ(info.index(), entt::type_index<int>::value());
ASSERT_EQ(info.hash(), entt::type_hash<int>::value());
ASSERT_EQ(info.name(), entt::type_name<int>::value());