Просмотр исходного кода

iterable_adaptor: decouple iterator types

Michele Caini 4 лет назад
Родитель
Сommit
75a0c672b1
3 измененных файлов с 17 добавлено и 12 удалено
  1. 11 10
      src/entt/core/iterator.hpp
  2. 4 0
      test/entt/core/iterator.cpp
  3. 2 2
      test/entt/entity/storage.cpp

+ 11 - 10
src/entt/core/iterator.hpp

@@ -56,16 +56,17 @@ private:
 
 /**
  * @brief Utility class to create an iterable object from a pair of iterators.
- * @tparam It Type of iterators.
+ * @tparam It Type of iterator.
+ * @tparam Sentinel Type of sentinel.
  */
-template<typename It>
+template<typename It, typename Sentinel = It>
 struct iterable_adaptor final {
     /*! @brief Type of the objects returned during iteration. */
     using value_type = typename std::iterator_traits<It>::value_type;
     /*! @brief Iterator type. */
     using iterator = It;
-    /*! @brief Const iterator type. */
-    using const_iterator = iterator;
+    /*! @brief Sentinel type. */
+    using sentinel = Sentinel;
 
     /*! @brief Default constructor. */
     iterable_adaptor() = default;
@@ -75,7 +76,7 @@ struct iterable_adaptor final {
      * @param from Begin iterator.
      * @param to End iterator.
      */
-    iterable_adaptor(It from, It to)
+    iterable_adaptor(iterator from, sentinel to)
         : first{from},
           last{to} {}
 
@@ -83,7 +84,7 @@ struct iterable_adaptor final {
      * @brief Returns an iterator to the beginning.
      * @return An iterator to the first element of the range.
      */
-    [[nodiscard]] const_iterator begin() const ENTT_NOEXCEPT {
+    [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
         return first;
     }
 
@@ -92,23 +93,23 @@ struct iterable_adaptor final {
      * @return An iterator to the element following the last element of the
      * range.
      */
-    [[nodiscard]] const_iterator end() const ENTT_NOEXCEPT {
+    [[nodiscard]] sentinel end() const ENTT_NOEXCEPT {
         return last;
     }
 
     /*! @copydoc begin */
-    [[nodiscard]] const_iterator cbegin() const ENTT_NOEXCEPT {
+    [[nodiscard]] iterator cbegin() const ENTT_NOEXCEPT {
         return begin();
     }
 
     /*! @copydoc end */
-    [[nodiscard]] const_iterator cend() const ENTT_NOEXCEPT {
+    [[nodiscard]] sentinel cend() const ENTT_NOEXCEPT {
         return end();
     }
 
 private:
     It first;
-    It last;
+    Sentinel last;
 };
 
 } // namespace entt

+ 4 - 0
test/entt/core/iterator.cpp

@@ -37,4 +37,8 @@ TEST(Iterator, IterableAdaptor) {
     ASSERT_EQ(*iterable.cbegin(), 1);
     ASSERT_EQ(*++iterable.cbegin(), 2);
     ASSERT_EQ(++iterable.cbegin(), --iterable.end());
+
+    for(auto value: entt::iterable_adaptor<const int *, const void *>{vec.data(), vec.data() + 1u}) {
+        ASSERT_EQ(value, 1);
+    }
 }

+ 2 - 2
test/entt/entity/storage.cpp

@@ -1158,7 +1158,7 @@ TEST(Storage, Iterable) {
 }
 
 TEST(Storage, ConstIterable) {
-    using iterator = typename entt::storage<boxed_int>::const_iterable::const_iterator;
+    using iterator = typename entt::storage<boxed_int>::const_iterable::iterator;
 
     static_assert(std::is_same_v<iterator::value_type, std::tuple<entt::entity, const boxed_int &>>);
     static_assert(std::is_same_v<iterator::value_type, std::tuple<entt::entity, const boxed_int &>>);
@@ -1200,7 +1200,7 @@ TEST(Storage, IterableIteratorConversion) {
     pool.emplace(entt::entity{3}, 42);
 
     typename entt::storage<boxed_int>::iterable::iterator it = pool.each().begin();
-    typename entt::storage<boxed_int>::const_iterable::const_iterator cit = it;
+    typename entt::storage<boxed_int>::const_iterable::iterator cit = it;
 
     static_assert(std::is_same_v<decltype(*it), std::tuple<entt::entity, boxed_int &>>);
     static_assert(std::is_same_v<decltype(*cit), std::tuple<entt::entity, const boxed_int &>>);