storage: storage_for[_t] utility

This commit is contained in:
Michele Caini
2022-05-26 08:25:47 +02:00
parent aa3e769463
commit c6e613c317
3 changed files with 35 additions and 0 deletions

View File

@@ -19,6 +19,9 @@ class basic_storage;
template<typename, typename = entity, typename = void>
struct storage_type;
template<typename, typename = entity>
struct storage_for;
template<typename Type>
class sigh_storage_mixin;

View File

@@ -916,6 +916,24 @@ struct storage_type {
template<typename... Args>
using storage_type_t = typename storage_type<Args...>::type;
/**
* Type-to-storage conversion utility that preserves constness.
* @tparam Type Storage value type, eventually const.
* @tparam Entity A valid entity type (see entt_traits for more details).
*/
template<typename Type, typename Entity>
struct storage_for {
/*! @brief Type-to-storage conversion result. */
using type = constness_as_t<typename storage_type<std::remove_const_t<Type>, Entity>::type, Type>;
};
/**
* @brief Helper type.
* @tparam Args Arguments to forward.
*/
template<typename... Args>
using storage_for_t = typename storage_for<Args...>::type;
} // namespace entt
#endif

View File

@@ -1814,4 +1814,18 @@ TEST(Storage, UsesAllocatorConstruction) {
ASSERT_EQ(memory_resource.do_deallocate_counter(), 0u);
}
TEST(Storage, StorageType) {
// just a bunch of static asserts to avoid regressions
static_assert(std::is_same_v<entt::storage_type_t<char, entt::entity>, entt::sigh_storage_mixin<entt::basic_storage<char, entt::entity>>>);
static_assert(std::is_same_v<entt::storage_type_t<int>, entt::sigh_storage_mixin<entt::storage<int>>>);
}
TEST(Storage, StorageFor) {
// just a bunch of static asserts to avoid regressions
static_assert(std::is_same_v<entt::storage_for_t<const double, entt::entity>, const entt::sigh_storage_mixin<entt::basic_storage<double, entt::entity>>>);
static_assert(std::is_same_v<entt::storage_for_t<char, entt::entity>, entt::sigh_storage_mixin<entt::basic_storage<char, entt::entity>>>);
static_assert(std::is_same_v<entt::storage_for_t<const bool>, const entt::sigh_storage_mixin<entt::storage<bool>>>);
static_assert(std::is_same_v<entt::storage_for_t<int>, entt::sigh_storage_mixin<entt::storage<int>>>);
}
#endif