Browse Source

registry: removed data<T> and raw<T> (use view<T>::data/::raw instead)

Michele Caini 5 years ago
parent
commit
5a3085c42b

+ 2 - 1
src/entt/entity/helper.hpp

@@ -145,7 +145,8 @@ void invoke(basic_registry<Entity> &reg, const Entity entt) {
  */
 template<typename Entity, typename Component>
 Entity to_entity(const basic_registry<Entity> &reg, const Component &component) {
-    return *(reg.template data<Component>() + (&component - reg.template raw<Component>()));
+    const auto view = reg.template view<const Component>();
+    return *(view.data() + (&component - view.raw()));
 }
 
 

+ 0 - 48
src/entt/entity/registry.hpp

@@ -309,54 +309,6 @@ public:
         }
     }
 
-    /**
-     * @brief Direct access to the list of components of a given pool.
-     *
-     * The returned pointer is such that range
-     * `[raw<Component>(), raw<Component>() + size<Component>())` is always a
-     * valid range, even if the container is empty.
-     *
-     * Components are in the reverse order as imposed by the sorting
-     * functionalities.
-     *
-     * @note
-     * Empty components aren't explicitly instantiated. Therefore, this function
-     * isn't available for them. A compilation error will occur if invoked.
-     *
-     * @tparam Component Type of component in which one is interested.
-     * @return A pointer to the array of components of the given type.
-     */
-    template<typename Component>
-    [[nodiscard]] const Component * raw() const {
-        const auto *cpool = assure<Component>();
-        return cpool ? cpool->raw() : nullptr;
-    }
-
-    /*! @copydoc raw */
-    template<typename Component>
-    [[nodiscard]] Component * raw() {
-        return assure<Component>()->raw();
-    }
-
-    /**
-     * @brief Direct access to the list of entities of a given pool.
-     *
-     * The returned pointer is such that range
-     * `[data<Component>(), data<Component>() + size<Component>())` is always a
-     * valid range, even if the container is empty.
-     *
-     * Entities are in the reverse order as imposed by the sorting
-     * functionalities.
-     *
-     * @tparam Component Type of component in which one is interested.
-     * @return A pointer to the array of entities.
-     */
-    template<typename Component>
-    [[nodiscard]] const entity_type * data() const {
-        const auto *cpool = assure<Component>();
-        return cpool ? cpool->data() : nullptr;
-    }
-
     /**
      * @brief Direct access to the list of entities of a registry.
      *

+ 8 - 2
src/entt/entity/snapshot.hpp

@@ -117,8 +117,14 @@ public:
      */
     template<typename... Component, typename Archive>
     const basic_snapshot & component(Archive &archive) const {
-        (component<Component>(archive, reg->template data<Component>(), reg->template data<Component>() + reg->template size<Component>()), ...);
-        return *this;
+        if constexpr(sizeof...(Component) == 1u) {
+            const auto view = reg->template view<const Component...>();
+            (component<Component>(archive, view.data(), view.data() + view.size()), ...);
+            return *this;
+        } else {
+            (component<Component>(archive), ...);
+            return *this;
+        }
     }
 
     /**

+ 10 - 17
test/entt/entity/registry.cpp

@@ -292,24 +292,15 @@ TEST(Registry, Identifiers) {
     ASSERT_EQ(registry.version(post), registry.current(post));
 }
 
-TEST(Registry, RawData) {
+TEST(Registry, Data) {
     entt::registry registry;
 
     ASSERT_EQ(std::as_const(registry).data(), nullptr);
 
     const auto entity = registry.create();
 
-    ASSERT_EQ(registry.raw<int>(), nullptr);
-    ASSERT_EQ(std::as_const(registry).raw<int>(), nullptr);
-    ASSERT_EQ(std::as_const(registry).data<int>(), nullptr);
     ASSERT_EQ(*std::as_const(registry).data(), entity);
 
-    registry.emplace<int>(entity, 42);
-
-    ASSERT_EQ(*registry.raw<int>(), 42);
-    ASSERT_EQ(*std::as_const(registry).raw<int>(), 42);
-    ASSERT_EQ(*std::as_const(registry).data<int>(), entity);
-
     const auto other = registry.create();
     registry.destroy(entity);
 
@@ -983,13 +974,13 @@ TEST(Registry, SortEmpty) {
     registry.emplace<empty_type>(registry.create());
     registry.emplace<empty_type>(registry.create());
 
-    ASSERT_LT(registry.data<empty_type>()[0], registry.data<empty_type>()[1]);
-    ASSERT_LT(registry.data<empty_type>()[1], registry.data<empty_type>()[2]);
+    ASSERT_LT(registry.view<empty_type>().data()[0], registry.view<empty_type>().data()[1]);
+    ASSERT_LT(registry.view<empty_type>().data()[1], registry.view<empty_type>().data()[2]);
 
     registry.sort<empty_type>(std::less<entt::entity>{});
 
-    ASSERT_GT(registry.data<empty_type>()[0], registry.data<empty_type>()[1]);
-    ASSERT_GT(registry.data<empty_type>()[1], registry.data<empty_type>()[2]);
+    ASSERT_GT(registry.view<empty_type>().data()[0], registry.view<empty_type>().data()[1]);
+    ASSERT_GT(registry.view<empty_type>().data()[1], registry.view<empty_type>().data()[2]);
 }
 
 TEST(Registry, ComponentsWithTypesFromStandardTemplateLibrary) {
@@ -1181,8 +1172,8 @@ TEST(Registry, Insert) {
     ASSERT_FALSE(registry.has<float>(e1));
     ASSERT_FALSE(registry.has<float>(e2));
 
-    const auto view = registry.view<int, char>();
-    registry.insert(view.begin(), view.end(), 3.f);
+    const auto icview = registry.view<int, char>();
+    registry.insert(icview.begin(), icview.end(), 3.f);
 
     ASSERT_EQ(registry.get<float>(e0), 3.f);
     ASSERT_EQ(registry.get<float>(e1), 3.f);
@@ -1190,7 +1181,9 @@ TEST(Registry, Insert) {
 
     registry.clear<float>();
     float value[3]{0.f, 1.f, 2.f};
-    registry.insert<float>(registry.data<int>(), registry.data<int>() + registry.size<int>(), value, value + registry.size<int>());
+
+    const auto iview = registry.view<int>();
+    registry.insert<float>(iview.data(), iview.data() + iview.size(), value, value + iview.size());
 
     ASSERT_EQ(registry.get<float>(e0), 0.f);
     ASSERT_EQ(registry.get<float>(e1), 1.f);

+ 1 - 1
test/entt/entity/registry_no_eto.cpp

@@ -14,7 +14,7 @@ TEST(Registry, NoEto) {
     registry.emplace<empty_type>(entity);
     registry.emplace<int>(entity, 42);
 
-    ASSERT_NE(registry.raw<empty_type>(), nullptr);
+    ASSERT_NE(registry.view<empty_type>().raw(), nullptr);
     ASSERT_NE(registry.try_get<empty_type>(entity), nullptr);
     ASSERT_EQ(registry.view<empty_type>().get(entity), std::as_const(registry).view<const empty_type>().get(entity));
 

+ 30 - 0
test/entt/entity/view.cpp

@@ -63,6 +63,36 @@ TEST(SingleComponentView, Functionalities) {
     ASSERT_FALSE(invalid);
 }
 
+TEST(SingleComponentView, RawData) {
+    entt::registry registry;
+    auto view = registry.view<int>();
+    auto cview = std::as_const(registry).view<const int>();
+
+    const auto entity = registry.create();
+
+    ASSERT_EQ(view.size(), 0u);
+    ASSERT_EQ(cview.size(), 0u);
+    ASSERT_EQ(view.raw(), nullptr);
+    ASSERT_EQ(cview.raw(), nullptr);
+    ASSERT_EQ(view.data(), nullptr);
+    ASSERT_EQ(cview.data(), nullptr);
+
+    registry.emplace<int>(entity, 42);
+
+    ASSERT_NE(view.size(), 0u);
+    ASSERT_NE(cview.size(), 0u);
+    ASSERT_EQ(*view.raw(), 42);
+    ASSERT_EQ(*cview.raw(), 42);
+    ASSERT_EQ(*view.data(), entity);
+    ASSERT_EQ(*cview.data(), entity);
+
+    const auto other = registry.create();
+    registry.destroy(entity);
+
+    ASSERT_EQ(view.size(), 0u);
+    ASSERT_EQ(cview.size(), 0u);
+}
+
 TEST(SingleComponentView, Invalid) {
     entt::registry registry{};
     auto eview = std::as_const(registry).view<const empty_type>();