storage: add default entity type to storage_type

This commit is contained in:
Michele Caini
2022-05-25 10:32:40 +02:00
parent fe3b5b03eb
commit 7428cb353c
6 changed files with 43 additions and 40 deletions

View File

@@ -7,16 +7,22 @@
namespace entt {
template<typename Entity, typename = std::allocator<Entity>>
/*! @brief Default entity identifier. */
enum class entity : id_type {};
template<typename Entity = entity, typename = std::allocator<Entity>>
class basic_sparse_set;
template<typename Type, typename, typename = std::allocator<Type>, typename = void>
template<typename Type, typename = entity, typename = std::allocator<Type>, typename = void>
class basic_storage;
template<typename, typename = entity, typename = void>
struct storage_type;
template<typename Type>
class sigh_storage_mixin;
template<typename>
template<typename = entity>
class basic_registry;
template<typename, typename, typename, typename = void>
@@ -88,21 +94,18 @@ using owned_t = type_list<Type...>;
template<typename... Type>
inline constexpr owned_t<Type...> owned{};
/*! @brief Default entity identifier. */
enum class entity : id_type {};
/*! @brief Alias declaration for the most common use case. */
using sparse_set = basic_sparse_set<entity>;
using sparse_set = basic_sparse_set<>;
/**
* @brief Alias declaration for the most common use case.
* @tparam Type Type of objects assigned to the entities.
*/
template<typename Type>
using storage = basic_storage<Type, entity>;
using storage = basic_storage<Type>;
/*! @brief Alias declaration for the most common use case. */
using registry = basic_registry<entity>;
using registry = basic_registry<>;
/*! @brief Alias declaration for the most common use case. */
using observer = basic_observer<registry>;

View File

@@ -903,7 +903,7 @@ public:
* @tparam Type Storage value type.
* @tparam Entity A valid entity type (see entt_traits for more details).
*/
template<typename Type, typename Entity, typename = void>
template<typename Type, typename Entity, typename>
struct storage_type {
/*! @brief Resulting type after component-to-storage conversion. */
using type = sigh_storage_mixin<basic_storage<Type, Entity>>;

View File

@@ -665,10 +665,10 @@ TEST(NonOwningGroup, Storage) {
const auto entity = registry.create();
const auto group = registry.group(entt::get<int, const char>);
static_assert(std::is_same_v<decltype(group.storage<0u>()), entt::storage_type_t<int, entt::entity> &>);
static_assert(std::is_same_v<decltype(group.storage<int>()), entt::storage_type_t<int, entt::entity> &>);
static_assert(std::is_same_v<decltype(group.storage<1u>()), const entt::storage_type_t<char, entt::entity> &>);
static_assert(std::is_same_v<decltype(group.storage<const char>()), const entt::storage_type_t<char, entt::entity> &>);
static_assert(std::is_same_v<decltype(group.storage<0u>()), entt::storage_type_t<int> &>);
static_assert(std::is_same_v<decltype(group.storage<int>()), entt::storage_type_t<int> &>);
static_assert(std::is_same_v<decltype(group.storage<1u>()), const entt::storage_type_t<char> &>);
static_assert(std::is_same_v<decltype(group.storage<const char>()), const entt::storage_type_t<char> &>);
ASSERT_EQ(group.size(), 0u);
@@ -1445,10 +1445,10 @@ TEST(OwningGroup, Storage) {
const auto entity = registry.create();
const auto group = registry.group<int>(entt::get<const char>);
static_assert(std::is_same_v<decltype(group.storage<0u>()), entt::storage_type_t<int, entt::entity> &>);
static_assert(std::is_same_v<decltype(group.storage<int>()), entt::storage_type_t<int, entt::entity> &>);
static_assert(std::is_same_v<decltype(group.storage<1u>()), const entt::storage_type_t<char, entt::entity> &>);
static_assert(std::is_same_v<decltype(group.storage<const char>()), const entt::storage_type_t<char, entt::entity> &>);
static_assert(std::is_same_v<decltype(group.storage<0u>()), entt::storage_type_t<int> &>);
static_assert(std::is_same_v<decltype(group.storage<int>()), entt::storage_type_t<int> &>);
static_assert(std::is_same_v<decltype(group.storage<1u>()), const entt::storage_type_t<char> &>);
static_assert(std::is_same_v<decltype(group.storage<const char>()), const entt::storage_type_t<char> &>);
ASSERT_EQ(group.size(), 0u);

View File

@@ -1943,11 +1943,11 @@ TEST(Registry, RuntimePools) {
auto &storage = registry.storage<empty_type>("other"_hs);
const auto entity = registry.create();
static_assert(std::is_same_v<decltype(registry.storage<empty_type>()), entt::storage_type_t<empty_type, entt::entity> &>);
static_assert(std::is_same_v<decltype(std::as_const(registry).storage<empty_type>()), const entt::storage_type_t<empty_type, entt::entity> &>);
static_assert(std::is_same_v<decltype(registry.storage<empty_type>()), entt::storage_type_t<empty_type> &>);
static_assert(std::is_same_v<decltype(std::as_const(registry).storage<empty_type>()), const entt::storage_type_t<empty_type> &>);
static_assert(std::is_same_v<decltype(registry.storage("other"_hs)->second), entt::storage_type_t<empty_type, entt::entity>::base_type &>);
static_assert(std::is_same_v<decltype(std::as_const(registry).storage("other"_hs)->second), const entt::storage_type_t<empty_type, entt::entity>::base_type &>);
static_assert(std::is_same_v<decltype(registry.storage("other"_hs)->second), entt::storage_type_t<empty_type>::base_type &>);
static_assert(std::is_same_v<decltype(std::as_const(registry).storage("other"_hs)->second), const entt::storage_type_t<empty_type>::base_type &>);
ASSERT_NE(registry.storage("other"_hs), registry.storage().end());
ASSERT_EQ(std::as_const(registry).storage("rehto"_hs), registry.storage().end());

View File

@@ -376,8 +376,8 @@ TEST(SingleComponentView, FrontBack) {
TEST(SingleComponentView, DeductionGuide) {
entt::registry registry;
entt::storage_type_t<int, entt::entity> istorage;
entt::storage_type_t<stable_type, entt::entity> sstorage;
entt::storage_type_t<int> istorage;
entt::storage_type_t<stable_type> sstorage;
static_assert(std::is_same_v<entt::basic_view<entt::entity, entt::get_t<int>, entt::exclude_t<>>, decltype(entt::basic_view{istorage})>);
static_assert(std::is_same_v<entt::basic_view<entt::entity, entt::get_t<const int>, entt::exclude_t<>>, decltype(entt::basic_view{std::as_const(istorage)})>);
@@ -448,12 +448,12 @@ TEST(SingleComponentView, Storage) {
const auto view = registry.view<int>();
const auto cview = registry.view<const char>();
static_assert(std::is_same_v<decltype(view.storage()), entt::storage_type_t<int, entt::entity> &>);
static_assert(std::is_same_v<decltype(view.storage<0u>()), entt::storage_type_t<int, entt::entity> &>);
static_assert(std::is_same_v<decltype(view.storage<int>()), entt::storage_type_t<int, entt::entity> &>);
static_assert(std::is_same_v<decltype(cview.storage()), const entt::storage_type_t<char, entt::entity> &>);
static_assert(std::is_same_v<decltype(cview.storage<0u>()), const entt::storage_type_t<char, entt::entity> &>);
static_assert(std::is_same_v<decltype(cview.storage<const char>()), const entt::storage_type_t<char, entt::entity> &>);
static_assert(std::is_same_v<decltype(view.storage()), entt::storage_type_t<int> &>);
static_assert(std::is_same_v<decltype(view.storage<0u>()), entt::storage_type_t<int> &>);
static_assert(std::is_same_v<decltype(view.storage<int>()), entt::storage_type_t<int> &>);
static_assert(std::is_same_v<decltype(cview.storage()), const entt::storage_type_t<char> &>);
static_assert(std::is_same_v<decltype(cview.storage<0u>()), const entt::storage_type_t<char> &>);
static_assert(std::is_same_v<decltype(cview.storage<const char>()), const entt::storage_type_t<char> &>);
ASSERT_EQ(view.size(), 0u);
ASSERT_EQ(cview.size(), 0u);
@@ -1031,9 +1031,9 @@ TEST(MultiComponentView, ExtendedGet) {
TEST(MultiComponentView, DeductionGuide) {
entt::registry registry;
entt::storage_type_t<int, entt::entity> istorage;
entt::storage_type_t<double, entt::entity> dstorage;
entt::storage_type_t<stable_type, entt::entity> sstorage;
entt::storage_type_t<int> istorage;
entt::storage_type_t<double> dstorage;
entt::storage_type_t<stable_type> sstorage;
static_assert(std::is_same_v<entt::basic_view<entt::entity, entt::get_t<int, double>, entt::exclude_t<>>, decltype(entt::basic_view{istorage, dstorage})>);
static_assert(std::is_same_v<entt::basic_view<entt::entity, entt::get_t<const int, double>, entt::exclude_t<>>, decltype(entt::basic_view{std::as_const(istorage), dstorage})>);
@@ -1159,8 +1159,8 @@ TEST(MultiComponentView, StableTypeWithExcludedComponent) {
TEST(MultiComponentView, SameComponentTypes) {
entt::registry registry;
entt::storage_type_t<int, entt::entity> storage;
entt::storage_type_t<int, entt::entity> other;
entt::storage_type_t<int> storage;
entt::storage_type_t<int> other;
entt::basic_view view{storage, other};
storage.bind(entt::forward_as_any(registry));
@@ -1240,10 +1240,10 @@ TEST(MultiComponentView, Storage) {
const auto entity = registry.create();
const auto view = registry.view<int, const char>();
static_assert(std::is_same_v<decltype(view.storage<0u>()), entt::storage_type_t<int, entt::entity> &>);
static_assert(std::is_same_v<decltype(view.storage<int>()), entt::storage_type_t<int, entt::entity> &>);
static_assert(std::is_same_v<decltype(view.storage<1u>()), const entt::storage_type_t<char, entt::entity> &>);
static_assert(std::is_same_v<decltype(view.storage<const char>()), const entt::storage_type_t<char, entt::entity> &>);
static_assert(std::is_same_v<decltype(view.storage<0u>()), entt::storage_type_t<int> &>);
static_assert(std::is_same_v<decltype(view.storage<int>()), entt::storage_type_t<int> &>);
static_assert(std::is_same_v<decltype(view.storage<1u>()), const entt::storage_type_t<char> &>);
static_assert(std::is_same_v<decltype(view.storage<const char>()), const entt::storage_type_t<char> &>);
ASSERT_EQ(view.size_hint(), 0u);

View File

@@ -20,7 +20,7 @@ template<typename, typename, typename = void>
struct has_on_construct: std::false_type {};
template<typename Entity, typename Type>
struct has_on_construct<Entity, Type, std::void_t<decltype(&entt::storage_type_t<Type, Entity>::on_construct)>>: std::true_type {};
struct has_on_construct<Entity, Type, std::void_t<decltype(&entt::storage_type_t<Type>::on_construct)>>: std::true_type {};
template<typename Entity, typename Type>
inline constexpr auto has_on_construct_v = has_on_construct<Entity, Type>::value;