From: Tom de Vries Date: Mon, 19 Jun 2023 09:15:56 +0000 (+0200) Subject: [gdb] Add template functions assign_return/set_if_changed X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=2e12e798825b9119a250f512416e04b62ed7a2a9;p=binutils-gdb.git [gdb] Add template functions assign_return/set_if_changed Add template functions assign_return_if_changed and assign_set_if_changed in gdb/utils.h: ... template void assign_set_if_changed (T &lval, const T &val, bool &changed) { ... } template bool assign_return_if_changed (T &lval, const 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 (tui_border_attrs, entry->value); ... or: ... assign_set_if_changed (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. --- 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..3faac20ec0f 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 +void +assign_set_if_changed (T &lval, const 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 +bool +assign_return_if_changed (T &lval, const T &val) +{ + if (lval == val) + return false; + + lval = val; + return true; +} + #endif /* UTILS_H */