]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
[gdbsupport] Add gdb::array_view::{iterator,const_iterator}
authorTom de Vries <tdevries@suse.de>
Sat, 19 Oct 2024 06:10:38 +0000 (08:10 +0200)
committerTom de Vries <tdevries@suse.de>
Sat, 19 Oct 2024 06:10:38 +0000 (08:10 +0200)
While trying to substitute some std::vector type A in the code with a
gdb::array_view:
...
- using A = std::vector<T>
+ using A = gdb::array_view<T>
....
I ran into the problem that the code was using A::iterator while
gdb::array_view doesn't define such a type.

Fix this by:
- adding types gdb::array_view::iterator and gdb::array_view::const_iterator,
- using them in gdb::array_view::(c)begin and gdb::array_view::(c)end, as is
  usual, and
- using them explicitly in a unit test.

Tested on aarch64-linux.

Approved-By: Tom Tromey <tom@tromey.com>
gdb/unittests/array-view-selftests.c
gdbsupport/array-view.h

index eb3ae304790f2823d30328e40d2ccbe5aa809413..c07b5720ef0abe5bbe5c01c503d407394b135665 100644 (file)
@@ -364,6 +364,39 @@ check_range_for ()
   SELF_CHECK (sum == 1 + 2 + 3 + 4);
 }
 
+template<typename T>
+static void
+check_iterator ()
+{
+  T data[] = {1, 2, 3, 4};
+  gdb::array_view<T> view (data);
+
+  typename std::decay<T>::type sum = 0;
+  for (typename gdb::array_view<T>::iterator it = view.begin ();
+       it != view.end (); it++)
+    {
+      *it *= 2;
+      sum += *it;
+    }
+
+  SELF_CHECK (sum == 2 + 4 + 6 + 8);
+}
+
+template<typename T>
+static void
+check_const_iterator ()
+{
+  T data[] = {1, 2, 3, 4};
+  gdb::array_view<T> view (data);
+
+  typename std::decay<T>::type sum = 0;
+  for (typename gdb::array_view<T>::const_iterator it = view.cbegin ();
+       it != view.cend (); it++)
+    sum += *it;
+
+  SELF_CHECK (sum == 1 + 2 + 3 + 4);
+}
+
 /* Entry point.  */
 
 static void
@@ -490,6 +523,9 @@ run_tests ()
 
   check_range_for<gdb_byte> ();
   check_range_for<const gdb_byte> ();
+  check_iterator<gdb_byte> ();
+  check_const_iterator<gdb_byte> ();
+  check_const_iterator<const gdb_byte> ();
 
   /* Check that the right ctor overloads are taken when the element is
      a container.  */
index 0dea26f67acd68ddcca3ec89f6fc137b392e0d59..5bf9ed77dbf364412d526626185702f3ca3af871 100644 (file)
@@ -88,6 +88,8 @@ public:
   using reference = T &;
   using const_reference = const T &;
   using size_type = size_t;
+  using const_iterator = const T *;
+  using iterator = T *;
 
   /* Default construction creates an empty view.  */
   constexpr array_view () noexcept
@@ -157,11 +159,11 @@ public:
   constexpr T *data () noexcept { return m_array; }
   constexpr const T *data () const noexcept { return m_array; }
 
-  constexpr T *begin () const noexcept { return m_array; }
-  constexpr const T *cbegin () const noexcept { return m_array; }
+  constexpr iterator begin () const noexcept { return m_array; }
+  constexpr const_iterator cbegin () const noexcept { return m_array; }
 
-  constexpr T *end () const noexcept { return m_array + m_size; }
-  constexpr const T *cend () const noexcept { return m_array + m_size; }
+  constexpr iterator end () const noexcept { return m_array + m_size; }
+  constexpr const_iterator cend () const noexcept { return m_array + m_size; }
 
   constexpr reference operator[] (size_t index) noexcept
   {