Browse Source

view: add operator[] to access (eventually unwrapped) components by entity (close #775)

Michele Caini 4 years ago
parent
commit
097509dd2a
2 changed files with 40 additions and 2 deletions
  1. 18 0
      src/entt/entity/view.hpp
  2. 22 2
      test/entt/entity/view.cpp

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

@@ -483,6 +483,15 @@ public:
         return (it != end() && *it == entt) ? it : end();
         return (it != end() && *it == entt) ? it : end();
     }
     }
 
 
+    /**
+     * @brief Returns the components assigned to the given entity.
+     * @param entt A valid identifier.
+     * @return The components assigned to the given entity.
+     */
+    [[nodiscard]] decltype(auto) operator[](const entity_type entt) const {
+        return get<Component...>(entt);
+    }
+
     /**
     /**
      * @brief Checks if a view is properly initialized.
      * @brief Checks if a view is properly initialized.
      * @return True if the view is properly initialized, false otherwise.
      * @return True if the view is properly initialized, false otherwise.
@@ -846,6 +855,15 @@ public:
         return begin()[pos];
         return begin()[pos];
     }
     }
 
 
+    /**
+     * @brief Returns the component assigned to the given entity.
+     * @param entt A valid identifier.
+     * @return The component assigned to the given entity.
+     */
+    [[nodiscard]] decltype(auto) operator[](const entity_type entt) const {
+        return get<Component>(entt);
+    }
+
     /**
     /**
      * @brief Checks if a view is properly initialized.
      * @brief Checks if a view is properly initialized.
      * @return True if the view is properly initialized, false otherwise.
      * @return True if the view is properly initialized, false otherwise.

+ 22 - 2
test/entt/entity/view.cpp

@@ -130,15 +130,18 @@ TEST(SingleComponentView, ElementAccess) {
     auto cview = std::as_const(registry).view<const int>();
     auto cview = std::as_const(registry).view<const int>();
 
 
     const auto e0 = registry.create();
     const auto e0 = registry.create();
-    registry.emplace<int>(e0);
+    registry.emplace<int>(e0, 42);
 
 
     const auto e1 = registry.create();
     const auto e1 = registry.create();
-    registry.emplace<int>(e1);
+    registry.emplace<int>(e1, 3);
 
 
     for(auto i = 0u; i < view.size(); ++i) {
     for(auto i = 0u; i < view.size(); ++i) {
         ASSERT_EQ(view[i], i ? e0 : e1);
         ASSERT_EQ(view[i], i ? e0 : e1);
         ASSERT_EQ(cview[i], i ? e0 : e1);
         ASSERT_EQ(cview[i], i ? e0 : e1);
     }
     }
+
+    ASSERT_EQ(view[e0], 42);
+    ASSERT_EQ(view[e1], 3);
 }
 }
 
 
 TEST(SingleComponentView, Contains) {
 TEST(SingleComponentView, Contains) {
@@ -601,6 +604,23 @@ TEST(MultiComponentView, ReverseIterator) {
     ASSERT_EQ(*begin.operator->(), entity);
     ASSERT_EQ(*begin.operator->(), entity);
 }
 }
 
 
+TEST(MultiComponentView, ElementAccess) {
+    entt::registry registry;
+    auto view = registry.view<int, char>();
+    auto cview = std::as_const(registry).view<const int, const char>();
+
+    const auto e0 = registry.create();
+    registry.emplace<int>(e0, 42);
+    registry.emplace<char>(e0, '0');
+
+    const auto e1 = registry.create();
+    registry.emplace<int>(e1, 3);
+    registry.emplace<char>(e1, '1');
+
+    ASSERT_EQ(view[e0], std::make_tuple(42, '0'));
+    ASSERT_EQ(view[e1], std::make_tuple(3, '1'));
+}
+
 TEST(MultiComponentView, Contains) {
 TEST(MultiComponentView, Contains) {
     entt::registry registry;
     entt::registry registry;