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

storage: non-const to const iterator conversion guaranteed

Michele Caini 4 лет назад
Родитель
Сommit
ef7163acd6
2 измененных файлов с 21 добавлено и 0 удалено
  1. 7 0
      src/entt/entity/storage.hpp
  2. 14 0
      test/entt/entity/storage.cpp

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

@@ -30,6 +30,8 @@ namespace internal {
 
 template<typename Container>
 class storage_iterator final {
+    friend storage_iterator<const Container>;
+
     static constexpr auto packed_page_v = ENTT_PACKED_PAGE;
 
     using container_type = std::remove_const_t<Container>;
@@ -53,6 +55,11 @@ public:
         : packed{ref},
           index{idx} {}
 
+    template<bool Const = std::is_const_v<Container>, typename = std::enable_if_t<Const>>
+    storage_iterator(const storage_iterator<std::remove_const_t<Container>> &other) ENTT_NOEXCEPT
+        : packed{other.packed},
+          index{other.index} {}
+
     storage_iterator &operator++() ENTT_NOEXCEPT {
         return --index, *this;
     }

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

@@ -931,6 +931,20 @@ TEST(Storage, ConstReverseIterator) {
     ASSERT_GE(cend, pool.crend());
 }
 
+TEST(Storage, IteratorConversion) {
+    entt::storage<boxed_int> pool;
+    pool.emplace(entt::entity{3}, 42);
+
+    typename entt::storage<boxed_int>::iterator it = pool.begin();
+    typename entt::storage<boxed_int>::const_iterator cit = it;
+
+    static_assert(std::is_same_v<decltype(*it), boxed_int &>);
+    static_assert(std::is_same_v<decltype(*cit), const boxed_int &>);
+
+    ASSERT_EQ(it->value, 42);
+    ASSERT_EQ(it->value, cit->value);
+}
+
 TEST(Storage, Raw) {
     entt::storage<int> pool;