Kaynağa Gözat

storage: storage_for[_t] utility

Michele Caini 3 yıl önce
ebeveyn
işleme
c6e613c317

+ 3 - 0
src/entt/entity/fwd.hpp

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

+ 18 - 0
src/entt/entity/storage.hpp

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

+ 14 - 0
test/entt/entity/storage.cpp

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