Browse Source

added const begin/end on views + tests

Michele Caini 7 years ago
parent
commit
20732c9206
2 changed files with 202 additions and 84 deletions
  1. 146 0
      src/entt/entity/view.hpp
  2. 56 84
      test/entt/entity/view.cpp

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

@@ -141,6 +141,24 @@ public:
         return view.cbegin();
     }
 
+    /**
+     * @brief Returns an iterator to the first entity that has the given
+     * components.
+     *
+     * The returned iterator points to the first entity that has the given
+     * components. If the view is empty, the returned iterator will be equal to
+     * `end()`.
+     *
+     * @note
+     * Input iterators stay true to the order imposed to the underlying data
+     * structures.
+     *
+     * @return An iterator to the first entity that has the given components.
+     */
+    inline const_iterator_type begin() const ENTT_NOEXCEPT {
+        return cbegin();
+    }
+
     /**
      * @brief Returns an iterator to the first entity that has the given
      * components.
@@ -178,6 +196,25 @@ public:
         return view.cend();
     }
 
+    /**
+     * @brief Returns an iterator that is past the last entity that has the
+     * given components.
+     *
+     * The returned iterator points to the entity following the last entity that
+     * has the given components. Attempting to dereference the returned iterator
+     * results in undefined behavior.
+     *
+     * @note
+     * Input iterators stay true to the order imposed to the underlying data
+     * structures.
+     *
+     * @return An iterator to the entity following the last entity that has the
+     * given components.
+     */
+    inline const_iterator_type end() const ENTT_NOEXCEPT {
+        return cend();
+    }
+
     /**
      * @brief Returns an iterator that is past the last entity that has the
      * given components.
@@ -580,6 +617,24 @@ public:
         return iterator_type{ unchecked(view), extent(), view->cbegin(), view->cend() };
     }
 
+    /**
+     * @brief Returns an iterator to the first entity that has the given
+     * components.
+     *
+     * The returned iterator points to the first entity that has the given
+     * components. If the view is empty, the returned iterator will be equal to
+     * `end()`.
+     *
+     * @note
+     * Input iterators stay true to the order imposed to the underlying data
+     * structures.
+     *
+     * @return An iterator to the first entity that has the given components.
+     */
+    inline const_iterator_type begin() const ENTT_NOEXCEPT {
+        return cbegin();
+    }
+
     /**
      * @brief Returns an iterator to the first entity that has the given
      * components.
@@ -618,6 +673,25 @@ public:
         return iterator_type{ unchecked(view), extent(), view->cend(), view->cend() };
     }
 
+    /**
+     * @brief Returns an iterator that is past the last entity that has the
+     * given components.
+     *
+     * The returned iterator points to the entity following the last entity that
+     * has the given components. Attempting to dereference the returned iterator
+     * results in undefined behavior.
+     *
+     * @note
+     * Input iterators stay true to the order imposed to the underlying data
+     * structures.
+     *
+     * @return An iterator to the entity following the last entity that has the
+     * given components.
+     */
+    inline const_iterator_type end() const ENTT_NOEXCEPT {
+        return cend();
+    }
+
     /**
      * @brief Returns an iterator that is past the last entity that has the
      * given components.
@@ -952,6 +1026,24 @@ public:
         return pool.view_type::cbegin();
     }
 
+    /**
+     * @brief Returns an iterator to the first entity that has the given
+     * component.
+     *
+     * The returned iterator points to the first entity that has the given
+     * component. If the view is empty, the returned iterator will be equal to
+     * `end()`.
+     *
+     * @note
+     * Input iterators stay true to the order imposed to the underlying data
+     * structures.
+     *
+     * @return An iterator to the first entity that has the given component.
+     */
+    inline const_iterator_type begin() const ENTT_NOEXCEPT {
+        return cbegin();
+    }
+
     /**
      * @brief Returns an iterator to the first entity that has the given
      * component.
@@ -989,6 +1081,25 @@ public:
         return pool.view_type::cend();
     }
 
+    /**
+     * @brief Returns an iterator that is past the last entity that has the
+     * given component.
+     *
+     * The returned iterator points to the entity following the last entity that
+     * has the given component. Attempting to dereference the returned iterator
+     * results in undefined behavior.
+     *
+     * @note
+     * Input iterators stay true to the order imposed to the underlying data
+     * structures.
+     *
+     * @return An iterator to the entity following the last entity that has the
+     * given component.
+     */
+    inline const_iterator_type end() const ENTT_NOEXCEPT {
+        return cend();
+    }
+
     /**
      * @brief Returns an iterator that is past the last entity that has the
      * given component.
@@ -1246,6 +1357,22 @@ public:
         return pool.cbegin();
     }
 
+    /**
+     * @brief Returns an iterator to the first instance of the given type.
+     *
+     * The returned iterator points to the first instance of the given type. If
+     * the view is empty, the returned iterator will be equal to `end()`.
+     *
+     * @note
+     * Input iterators stay true to the order imposed to the underlying data
+     * structures.
+     *
+     * @return An iterator to the first instance of the given type.
+     */
+    inline const_iterator_type begin() const ENTT_NOEXCEPT {
+        return cbegin();
+    }
+
     /**
      * @brief Returns an iterator to the first instance of the given type.
      *
@@ -1281,6 +1408,25 @@ public:
         return pool.cend();
     }
 
+    /**
+     * @brief Returns an iterator that is past the last instance of the given
+     * type.
+     *
+     * The returned iterator points to the element following the last instance
+     * of the given type. Attempting to dereference the returned iterator
+     * results in undefined behavior.
+     *
+     * @note
+     * Input iterators stay true to the order imposed to the underlying data
+     * structures.
+     *
+     * @return An iterator to the element following the last instance of the
+     * given type.
+     */
+    inline const_iterator_type end() const ENTT_NOEXCEPT {
+        return cend();
+    }
+
     /**
      * @brief Returns an iterator that is past the last instance of the given
      * type.

+ 56 - 84
test/entt/entity/view.cpp

@@ -49,32 +49,25 @@ TEST(View, SingleComponent) {
 TEST(View, SingleComponentBeginEnd) {
     entt::DefaultRegistry registry;
     auto view = registry.view<int>();
+    const auto &cview = view;
 
     for(auto i = 0; i < 3; ++i) {
         registry.assign<int>(registry.create());
     }
 
-    auto begin = view.begin();
-    auto end = view.end();
-
-    ASSERT_NE(begin, end);
-    ASSERT_NE(++begin, end);
-    ASSERT_NE(begin++, end);
-    ASSERT_EQ(begin+1, end);
-    ASSERT_NE(begin, end);
-    ASSERT_EQ((begin += 1), end);
-    ASSERT_EQ(begin, end);
-
-    auto cbegin = view.cbegin();
-    auto cend = view.cend();
-
-    ASSERT_NE(cbegin, cend);
-    ASSERT_NE(++cbegin, cend);
-    ASSERT_NE(cbegin++, cend);
-    ASSERT_EQ(cbegin+1, cend);
-    ASSERT_NE(cbegin, cend);
-    ASSERT_EQ((cbegin += 1), cend);
-    ASSERT_EQ(cbegin, cend);
+    auto test = [](auto begin, auto end) {
+        ASSERT_NE(begin, end);
+        ASSERT_NE(++begin, end);
+        ASSERT_NE(begin++, end);
+        ASSERT_EQ(begin+1, end);
+        ASSERT_NE(begin, end);
+        ASSERT_EQ((begin += 1), end);
+        ASSERT_EQ(begin, end);
+    };
+
+    test(view.begin(), view.end());
+    test(cview.begin(), cview.end());
+    test(view.cbegin(), view.cend());
 }
 
 TEST(View, SingleComponentContains) {
@@ -180,6 +173,7 @@ TEST(View, MultipleComponent) {
 TEST(View, MultipleComponentBeginEnd) {
     entt::DefaultRegistry registry;
     auto view = registry.view<int, char>();
+    const auto &cview = view;
 
     for(auto i = 0; i < 3; ++i) {
         const auto entity = registry.create();
@@ -187,27 +181,19 @@ TEST(View, MultipleComponentBeginEnd) {
         registry.assign<char>(entity);
     }
 
-    auto begin = view.begin();
-    auto end = view.end();
-
-    ASSERT_NE(begin, end);
-    ASSERT_NE(++begin, end);
-    ASSERT_NE(begin++, end);
-    ASSERT_EQ(begin+1, end);
-    ASSERT_NE(begin, end);
-    ASSERT_EQ((begin += 1), end);
-    ASSERT_EQ(begin, end);
-
-    auto cbegin = view.cbegin();
-    auto cend = view.cend();
-
-    ASSERT_NE(cbegin, cend);
-    ASSERT_NE(++cbegin, cend);
-    ASSERT_NE(cbegin++, cend);
-    ASSERT_EQ(cbegin+1, cend);
-    ASSERT_NE(cbegin, cend);
-    ASSERT_EQ((cbegin += 1), cend);
-    ASSERT_EQ(cbegin, cend);
+    auto test = [](auto begin, auto end) {
+        ASSERT_NE(begin, end);
+        ASSERT_NE(++begin, end);
+        ASSERT_NE(begin++, end);
+        ASSERT_EQ(begin+1, end);
+        ASSERT_NE(begin, end);
+        ASSERT_EQ((begin += 1), end);
+        ASSERT_EQ(begin, end);
+    };
+
+    test(cview.begin(), cview.end());
+    test(view.begin(), view.end());
+    test(view.cbegin(), view.cend());
 }
 
 TEST(View, MultipleComponentContains) {
@@ -398,6 +384,7 @@ TEST(PersistentView, NoPrepare) {
 TEST(PersistentView, BeginEnd) {
     entt::DefaultRegistry registry;
     auto view = registry.view<int, char>(entt::persistent_t{});
+    const auto &cview = view;
 
     for(auto i = 0; i < 3; ++i) {
         const auto entity = registry.create();
@@ -405,27 +392,19 @@ TEST(PersistentView, BeginEnd) {
         registry.assign<char>(entity);
     }
 
-    auto begin = view.begin();
-    auto end = view.end();
-
-    ASSERT_NE(begin, end);
-    ASSERT_NE(++begin, end);
-    ASSERT_NE(begin++, end);
-    ASSERT_EQ(begin+1, end);
-    ASSERT_NE(begin, end);
-    ASSERT_EQ((begin += 1), end);
-    ASSERT_EQ(begin, end);
-
-    auto cbegin = view.cbegin();
-    auto cend = view.cend();
-
-    ASSERT_NE(cbegin, cend);
-    ASSERT_NE(++cbegin, cend);
-    ASSERT_NE(cbegin++, cend);
-    ASSERT_EQ(cbegin+1, cend);
-    ASSERT_NE(cbegin, cend);
-    ASSERT_EQ((cbegin += 1), cend);
-    ASSERT_EQ(cbegin, cend);
+    auto test = [](auto begin, auto end) {
+        ASSERT_NE(begin, end);
+        ASSERT_NE(++begin, end);
+        ASSERT_NE(begin++, end);
+        ASSERT_EQ(begin+1, end);
+        ASSERT_NE(begin, end);
+        ASSERT_EQ((begin += 1), end);
+        ASSERT_EQ(begin, end);
+    };
+
+    test(cview.begin(), cview.end());
+    test(view.begin(), view.end());
+    test(view.cbegin(), view.cend());
 }
 
 TEST(PersistentView, Contains) {
@@ -585,32 +564,25 @@ TEST(RawView, Functionalities) {
 TEST(RawView, BeginEnd) {
     entt::DefaultRegistry registry;
     auto view = registry.view<int>(entt::raw_t{});
+    const auto &cview = view;
 
     for(auto i = 0; i < 3; ++i) {
         registry.assign<int>(registry.create());
     }
 
-    auto begin = view.begin();
-    auto end = view.end();
-
-    ASSERT_NE(begin, end);
-    ASSERT_NE(++begin, end);
-    ASSERT_NE(begin++, end);
-    ASSERT_EQ(begin+1, end);
-    ASSERT_NE(begin, end);
-    ASSERT_EQ((begin += 1), end);
-    ASSERT_EQ(begin, end);
-
-    auto cbegin = view.cbegin();
-    auto cend = view.cend();
-
-    ASSERT_NE(cbegin, cend);
-    ASSERT_NE(++cbegin, cend);
-    ASSERT_NE(cbegin++, cend);
-    ASSERT_EQ(cbegin+1, cend);
-    ASSERT_NE(cbegin, cend);
-    ASSERT_EQ((cbegin += 1), cend);
-    ASSERT_EQ(cbegin, cend);
+    auto test = [](auto begin, auto end) {
+        ASSERT_NE(begin, end);
+        ASSERT_NE(++begin, end);
+        ASSERT_NE(begin++, end);
+        ASSERT_EQ(begin+1, end);
+        ASSERT_NE(begin, end);
+        ASSERT_EQ((begin += 1), end);
+        ASSERT_EQ(begin, end);
+    };
+
+    test(cview.begin(), cview.end());
+    test(view.begin(), view.end());
+    test(view.cbegin(), view.cend());
 }
 
 TEST(RawView, Empty) {