public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2 1/2] [gdbsupport] Use std::span-style iterators for gdb::array_view
@ 2024-10-18  8:51 Tom de Vries
  2024-10-18  8:51 ` [PATCH v2 2/2] [gdbsupport] Add gdb::array_view::{iterator,const_iterator} Tom de Vries
  2024-10-18 19:44 ` [PATCH v2 1/2] [gdbsupport] Use std::span-style iterators for gdb::array_view Tom Tromey
  0 siblings, 2 replies; 4+ messages in thread
From: Tom de Vries @ 2024-10-18  8:51 UTC (permalink / raw)
  To: gdb-patches

There's a plan to replace gdb::array_view with std::span (PR31422), and making
gdb::array_view more like std::span helps with that.

One difference is that std::span has:
...
constexpr iterator begin() const noexcept;
constexpr const_iterator cbegin() const noexcept;
...
while gdb::array_view has:
...
constexpr T *begin () noexcept;
constexpr const T *begin () const noexcept;
...

Fix this by renaming the second variant to cbegin, and making first variant
const.

Likewise for gdb::array_view::end.

Tested on aarch64-linux.
---
 gdbsupport/array-view.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/gdbsupport/array-view.h b/gdbsupport/array-view.h
index 93842a40ec1..0dea26f67ac 100644
--- a/gdbsupport/array-view.h
+++ b/gdbsupport/array-view.h
@@ -157,11 +157,11 @@ class array_view
   constexpr T *data () noexcept { return m_array; }
   constexpr const T *data () const noexcept { return m_array; }
 
-  constexpr T *begin () noexcept { return m_array; }
-  constexpr const T *begin () const noexcept { return m_array; }
+  constexpr T *begin () const noexcept { return m_array; }
+  constexpr const T *cbegin () const noexcept { return m_array; }
 
-  constexpr T *end () noexcept { return m_array + m_size; }
-  constexpr const T *end () const noexcept { return m_array + m_size; }
+  constexpr T *end () const noexcept { return m_array + m_size; }
+  constexpr const T *cend () const noexcept { return m_array + m_size; }
 
   constexpr reference operator[] (size_t index) noexcept
   {

base-commit: b2841da4f20b83b22fb46d82a86fe53ba7448469
-- 
2.35.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] [gdbsupport] Add gdb::array_view::{iterator,const_iterator}
  2024-10-18  8:51 [PATCH v2 1/2] [gdbsupport] Use std::span-style iterators for gdb::array_view Tom de Vries
@ 2024-10-18  8:51 ` Tom de Vries
  2024-10-18 19:44   ` [PATCH v2 2/2] [gdbsupport] Add gdb::array_view::{iterator, const_iterator} Tom Tromey
  2024-10-18 19:44 ` [PATCH v2 1/2] [gdbsupport] Use std::span-style iterators for gdb::array_view Tom Tromey
  1 sibling, 1 reply; 4+ messages in thread
From: Tom de Vries @ 2024-10-18  8:51 UTC (permalink / raw)
  To: gdb-patches

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.
---
 gdb/unittests/array-view-selftests.c | 36 ++++++++++++++++++++++++++++
 gdbsupport/array-view.h              | 10 ++++----
 2 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/gdb/unittests/array-view-selftests.c b/gdb/unittests/array-view-selftests.c
index eb3ae304790..c07b5720ef0 100644
--- a/gdb/unittests/array-view-selftests.c
+++ b/gdb/unittests/array-view-selftests.c
@@ -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.  */
diff --git a/gdbsupport/array-view.h b/gdbsupport/array-view.h
index 0dea26f67ac..5bf9ed77dbf 100644
--- a/gdbsupport/array-view.h
+++ b/gdbsupport/array-view.h
@@ -88,6 +88,8 @@ class array_view
   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 @@ class array_view
   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
   {
-- 
2.35.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 1/2] [gdbsupport] Use std::span-style iterators for gdb::array_view
  2024-10-18  8:51 [PATCH v2 1/2] [gdbsupport] Use std::span-style iterators for gdb::array_view Tom de Vries
  2024-10-18  8:51 ` [PATCH v2 2/2] [gdbsupport] Add gdb::array_view::{iterator,const_iterator} Tom de Vries
@ 2024-10-18 19:44 ` Tom Tromey
  1 sibling, 0 replies; 4+ messages in thread
From: Tom Tromey @ 2024-10-18 19:44 UTC (permalink / raw)
  To: Tom de Vries; +Cc: gdb-patches

>>>>> "Tom" == Tom de Vries <tdevries@suse.de> writes:

Tom> Fix this by renaming the second variant to cbegin, and making first variant
Tom> const.

Tom> Likewise for gdb::array_view::end.

Thank you.
Approved-By: Tom Tromey <tom@tromey.com>

Tom

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 2/2] [gdbsupport] Add gdb::array_view::{iterator, const_iterator}
  2024-10-18  8:51 ` [PATCH v2 2/2] [gdbsupport] Add gdb::array_view::{iterator,const_iterator} Tom de Vries
@ 2024-10-18 19:44   ` Tom Tromey
  0 siblings, 0 replies; 4+ messages in thread
From: Tom Tromey @ 2024-10-18 19:44 UTC (permalink / raw)
  To: Tom de Vries; +Cc: gdb-patches

>>>>> "Tom" == Tom de Vries <tdevries@suse.de> writes:

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

Ok.
Approved-By: Tom Tromey <tom@tromey.com>

Tom

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-10-18 19:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-18  8:51 [PATCH v2 1/2] [gdbsupport] Use std::span-style iterators for gdb::array_view Tom de Vries
2024-10-18  8:51 ` [PATCH v2 2/2] [gdbsupport] Add gdb::array_view::{iterator,const_iterator} Tom de Vries
2024-10-18 19:44   ` [PATCH v2 2/2] [gdbsupport] Add gdb::array_view::{iterator, const_iterator} Tom Tromey
2024-10-18 19:44 ` [PATCH v2 1/2] [gdbsupport] Use std::span-style iterators for gdb::array_view Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).