[libcxx-commits] [libcxx] 5ed6dc4 - [libc++] Introduce helper functions __make_iter in vector and string

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Nov 15 13:17:34 PST 2022


Author: Louis Dionne
Date: 2022-11-15T16:17:13-05:00
New Revision: 5ed6dc468192c81a5a7dd4fc50f587c05861c98c

URL: https://github.com/llvm/llvm-project/commit/5ed6dc468192c81a5a7dd4fc50f587c05861c98c
DIFF: https://github.com/llvm/llvm-project/commit/5ed6dc468192c81a5a7dd4fc50f587c05861c98c.diff

LOG: [libc++] Introduce helper functions __make_iter in vector and string

This prepares the terrain for introducing a new type of bounded iterator
that can't be constructed like __wrap_iter. This reverts part of the
changes made to std::vector in 4eab04f84.

Differential Revision: https://reviews.llvm.org/D138036

Added: 
    

Modified: 
    libcxx/include/string
    libcxx/include/vector

Removed: 
    


################################################################################
diff  --git a/libcxx/include/string b/libcxx/include/string
index 03bb5ea8ab72..b36d83f5e6dc 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -813,6 +813,14 @@ private:
         std::__debug_db_insert_c(this);
     }
 
+    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator __make_iterator(pointer __p) {
+        return iterator(this, __p);
+    }
+
+    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator __make_const_iterator(const_pointer __p) const {
+        return const_iterator(this, __p);
+    }
+
 public:
   _LIBCPP_TEMPLATE_DATA_VIS static const size_type npos = -1;
 
@@ -1029,16 +1037,16 @@ public:
 
     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
     iterator begin() _NOEXCEPT
-        {return iterator(this, __get_pointer());}
+        {return __make_iterator(__get_pointer());}
     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
     const_iterator begin() const _NOEXCEPT
-        {return const_iterator(this, __get_pointer());}
+        {return __make_const_iterator(__get_pointer());}
     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
     iterator end() _NOEXCEPT
-        {return iterator(this, __get_pointer() + size());}
+        {return __make_iterator(__get_pointer() + size());}
     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
     const_iterator end() const _NOEXCEPT
-        {return const_iterator(this, __get_pointer() + size());}
+        {return __make_const_iterator(__get_pointer() + size());}
 
     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
     reverse_iterator rbegin() _NOEXCEPT

diff  --git a/libcxx/include/vector b/libcxx/include/vector
index d9122620c00d..a55c4926171f 100644
--- a/libcxx/include/vector
+++ b/libcxx/include/vector
@@ -686,9 +686,9 @@ private:
     _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __append(size_type __n);
     _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __append(size_type __n, const_reference __x);
     _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
-    iterator       __make_iter(pointer __p) _NOEXCEPT;
+    iterator       __make_iter(pointer __p) _NOEXCEPT { return iterator(this, __p); }
     _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
-    const_iterator __make_iter(const_pointer __p) const _NOEXCEPT;
+    const_iterator __make_iter(const_pointer __p) const _NOEXCEPT { return const_iterator(this, __p); }
     _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
     _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
     _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_range(pointer __from_s, pointer __from_e, pointer __to);
@@ -1377,7 +1377,7 @@ inline _LIBCPP_HIDE_FROM_ABI
 typename vector<_Tp, _Allocator>::iterator
 vector<_Tp, _Allocator>::begin() _NOEXCEPT
 {
-    return iterator(this, this->__begin_);
+    return __make_iter(this->__begin_);
 }
 
 template <class _Tp, class _Allocator>
@@ -1386,7 +1386,7 @@ inline _LIBCPP_HIDE_FROM_ABI
 typename vector<_Tp, _Allocator>::const_iterator
 vector<_Tp, _Allocator>::begin() const _NOEXCEPT
 {
-    return const_iterator(this, this->__begin_);
+    return __make_iter(this->__begin_);
 }
 
 template <class _Tp, class _Allocator>
@@ -1395,7 +1395,7 @@ inline _LIBCPP_HIDE_FROM_ABI
 typename vector<_Tp, _Allocator>::iterator
 vector<_Tp, _Allocator>::end() _NOEXCEPT
 {
-    return iterator(this, this->__end_);
+    return __make_iter(this->__end_);
 }
 
 template <class _Tp, class _Allocator>
@@ -1404,7 +1404,7 @@ inline _LIBCPP_HIDE_FROM_ABI
 typename vector<_Tp, _Allocator>::const_iterator
 vector<_Tp, _Allocator>::end() const _NOEXCEPT
 {
-    return const_iterator(this, this->__end_);
+    return __make_iter(this->__end_);
 }
 
 template <class _Tp, class _Allocator>
@@ -1588,8 +1588,7 @@ vector<_Tp, _Allocator>::erase(const_iterator __position)
     this->__destruct_at_end(std::move(__p + 1, this->__end_, __p));
     if (!__libcpp_is_constant_evaluated())
         this->__invalidate_iterators_past(__p - 1);
-    iterator __r = iterator(this, __p);
-    return __r;
+    return __make_iter(__p);
 }
 
 template <class _Tp, class _Allocator>
@@ -1609,8 +1608,7 @@ vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last)
         if (!__libcpp_is_constant_evaluated())
             this->__invalidate_iterators_past(__p - 1);
     }
-    iterator __r = iterator(this, __p);
-    return __r;
+    return __make_iter(__p);
 }
 
 template <class _Tp, class _Allocator>
@@ -1664,7 +1662,7 @@ vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
         __v.push_back(__x);
         __p = __swap_out_circular_buffer(__v, __p);
     }
-    return iterator(this, __p);
+    return __make_iter(__p);
 }
 
 template <class _Tp, class _Allocator>
@@ -1694,7 +1692,7 @@ vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
         __v.push_back(std::move(__x));
         __p = __swap_out_circular_buffer(__v, __p);
     }
-    return iterator(this, __p);
+    return __make_iter(__p);
 }
 
 template <class _Tp, class _Allocator>
@@ -1726,7 +1724,7 @@ vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
         __v.emplace_back(std::forward<_Args>(__args)...);
         __p = __swap_out_circular_buffer(__v, __p);
     }
-    return iterator(this, __p);
+    return __make_iter(__p);
 }
 
 template <class _Tp, class _Allocator>
@@ -1767,7 +1765,7 @@ vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_
             __p = __swap_out_circular_buffer(__v, __p);
         }
     }
-    return iterator(this, __p);
+    return __make_iter(__p);
 }
 
 template <class _Tp, class _Allocator>
@@ -1804,14 +1802,14 @@ vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __firs
         }
         catch (...)
         {
-            erase(iterator(this, __old_last), end());
+            erase(__make_iter(__old_last), end());
             throw;
         }
 #endif // _LIBCPP_NO_EXCEPTIONS
     }
     __p = std::rotate(__p, __old_last, this->__end_);
-    insert(iterator(this, __p), std::make_move_iterator(__v.begin()),
-                                std::make_move_iterator(__v.end()));
+    insert(__make_iter(__p), std::make_move_iterator(__v.begin()),
+                             std::make_move_iterator(__v.end()));
     return begin() + __off;
 }
 
@@ -1856,7 +1854,7 @@ vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __fi
             __p = __swap_out_circular_buffer(__v, __p);
         }
     }
-    return iterator(this, __p);
+    return __make_iter(__p);
 }
 
 template <class _Tp, class _Allocator>


        


More information about the libcxx-commits mailing list