public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2 1/2] [gdb] Add template functions assign_return/set_if_changed
@ 2023-06-08  9:17 Tom de Vries
  2023-06-08  9:17 ` [PATCH v2 2/2] [gdb/tui] Simplify tui_update_variables Tom de Vries
  2023-06-19  9:19 ` [PATCH v2 1/2] [gdb] Add template functions assign_return/set_if_changed Tom de Vries
  0 siblings, 2 replies; 3+ messages in thread
From: Tom de Vries @ 2023-06-08  9:17 UTC (permalink / raw)
  To: gdb-patches

Add template functions assign_return_if_changed and assign_set_if_changed in
gdb/utils.h:
...
template<typename T> void assign_set_if_changed (T &lval, T val, bool &changed)
{ ... }
template<typename T> bool assign_return_if_changed (T &lval, T val)
{ ... }
...

This allows us to rewrite code like this:
...
  if (tui_border_attrs != entry->value)
    {
      tui_border_attrs = entry->value;
      need_redraw = true;
    }
...
into this:
...
  need_redraw |= assign_return_if_changed<int> (tui_border_attrs, entry->value);
...
or:
...
  assign_set_if_changed<int> (tui_border_attrs, entry->value, need_redraw);
...

The names are a composition of the functionality.  The functions:
- assign VAL to LVAL, and either
- return true if the assignment changed LVAL, or
- set CHANGED to true if the assignment changed LVAL.

Tested on x86_64-linux.
---
 gdb/utils.c | 39 +++++++++++++++++++++++++++++++++++++++
 gdb/utils.h | 27 +++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)

diff --git a/gdb/utils.c b/gdb/utils.c
index f18228d1086..46bfd9a5bbb 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -3631,6 +3631,43 @@ copy_bitwise (gdb_byte *dest, ULONGEST dest_offset,
     }
 }
 
+#if GDB_SELF_TEST
+static void
+test_assign_set_return_if_changed ()
+{
+  bool changed;
+  int a;
+
+  for (bool initial : { false, true })
+    {
+      changed = initial;
+      a = 1;
+      assign_set_if_changed (a, 1, changed);
+      SELF_CHECK (a == 1);
+      SELF_CHECK (changed == initial);
+    }
+
+  for (bool initial : { false, true })
+    {
+      changed = initial;
+      a = 1;
+      assign_set_if_changed (a, 2, changed);
+      SELF_CHECK (a == 2);
+      SELF_CHECK (changed == true);
+    }
+
+  a = 1;
+  changed = assign_return_if_changed (a, 1);
+  SELF_CHECK (a == 1);
+  SELF_CHECK (changed == false);
+
+  a = 1;
+  assign_set_if_changed (a, 2, changed);
+  SELF_CHECK (a == 2);
+  SELF_CHECK (changed == true);
+}
+#endif
+
 void _initialize_utils ();
 void
 _initialize_utils ()
@@ -3695,5 +3732,7 @@ When set, debugging messages will be marked with seconds and microseconds."),
   selftests::register_test ("strncmp_iw_with_mode",
 			    strncmp_iw_with_mode_tests);
   selftests::register_test ("pager", test_pager);
+  selftests::register_test ("assign_set_return_if_changed",
+			    test_assign_set_return_if_changed);
 #endif
 }
diff --git a/gdb/utils.h b/gdb/utils.h
index 00b123e6117..d78d54911bf 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -337,4 +337,31 @@ extern void copy_bitwise (gdb_byte *dest, ULONGEST dest_offset,
 
 extern int readline_hidden_cols;
 
+/* Assign VAL to LVAL, and set CHANGED to true if the assignment changed
+   LVAL.  */
+
+template<typename T>
+void
+assign_set_if_changed (T &lval, T val, bool &changed)
+{
+  if (lval == val)
+    return;
+
+  lval = val;
+  changed = true;
+}
+
+/* Assign VAL to LVAL, and return true if the assignment changed LVAL.  */
+
+template<typename T>
+bool
+assign_return_if_changed (T &lval, T val)
+{
+  if (lval == val)
+    return false;
+
+  lval = val;
+  return true;
+}
+
 #endif /* UTILS_H */

base-commit: 12f7174bf069f407d5b6f12e926ceabe45e450e1
-- 
2.35.3


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

* [PATCH v2 2/2] [gdb/tui] Simplify tui_update_variables
  2023-06-08  9:17 [PATCH v2 1/2] [gdb] Add template functions assign_return/set_if_changed Tom de Vries
@ 2023-06-08  9:17 ` Tom de Vries
  2023-06-19  9:19 ` [PATCH v2 1/2] [gdb] Add template functions assign_return/set_if_changed Tom de Vries
  1 sibling, 0 replies; 3+ messages in thread
From: Tom de Vries @ 2023-06-08  9:17 UTC (permalink / raw)
  To: gdb-patches

Simplify tui_update_variables by using template function
assign_return_if_changed.

Tested on x86_64-linux.
---
 gdb/tui/tui-win.c | 22 +++++++---------------
 1 file changed, 7 insertions(+), 15 deletions(-)

diff --git a/gdb/tui/tui-win.c b/gdb/tui/tui-win.c
index 71f961e04d2..3e2605ff24a 100644
--- a/gdb/tui/tui-win.c
+++ b/gdb/tui/tui-win.c
@@ -284,28 +284,20 @@ tui_update_variables ()
   struct tui_translate *entry;
 
   entry = translate (tui_border_mode, tui_border_mode_translate);
-  if (tui_border_attrs != entry->value)
-    {
-      tui_border_attrs = entry->value;
-      need_redraw = true;
-    }
+  need_redraw
+    |= assign_return_if_changed<int> (tui_border_attrs, entry->value);
+
   entry = translate (tui_active_border_mode, tui_border_mode_translate);
-  if (tui_active_border_attrs != entry->value)
-    {
-      tui_active_border_attrs = entry->value;
-      need_redraw = true;
-    }
+  need_redraw
+    |= assign_return_if_changed<int> (tui_active_border_attrs, entry->value);
 
   /* If one corner changes, all characters are changed.
      Only check the first one.  The ACS characters are determined at
      run time by curses terminal management.  */
   entry = translate (tui_border_kind, tui_border_kind_translate_lrcorner);
   int val = (entry->value < 0) ? ACS_LRCORNER : entry->value;
-  if (tui_border_lrcorner != (chtype) val)
-    {
-      tui_border_lrcorner = val;
-      need_redraw = true;
-    }
+  need_redraw |= assign_return_if_changed<chtype> (tui_border_lrcorner, val);
+
   entry = translate (tui_border_kind, tui_border_kind_translate_llcorner);
   tui_border_llcorner = (entry->value < 0) ? ACS_LLCORNER : entry->value;
 
-- 
2.35.3


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

* Re: [PATCH v2 1/2] [gdb] Add template functions assign_return/set_if_changed
  2023-06-08  9:17 [PATCH v2 1/2] [gdb] Add template functions assign_return/set_if_changed Tom de Vries
  2023-06-08  9:17 ` [PATCH v2 2/2] [gdb/tui] Simplify tui_update_variables Tom de Vries
@ 2023-06-19  9:19 ` Tom de Vries
  1 sibling, 0 replies; 3+ messages in thread
From: Tom de Vries @ 2023-06-19  9:19 UTC (permalink / raw)
  To: gdb-patches

On 6/8/23 11:17, Tom de Vries via Gdb-patches wrote:
> template<typename T> void assign_set_if_changed (T &lval, T val, bool &changed)
> { ... }
> template<typename T> bool assign_return_if_changed (T &lval, T val)
> { ... }

I realize that "T val" could mean calling a copy constructor for passing 
the argument, so I've changed this into a "const T &val".

With that change, I've pushed the series.

Thanks,
- Tom

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

end of thread, other threads:[~2023-06-19  9:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-08  9:17 [PATCH v2 1/2] [gdb] Add template functions assign_return/set_if_changed Tom de Vries
2023-06-08  9:17 ` [PATCH v2 2/2] [gdb/tui] Simplify tui_update_variables Tom de Vries
2023-06-19  9:19 ` [PATCH v2 1/2] [gdb] Add template functions assign_return/set_if_changed Tom de Vries

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).