diff options
author | Tom de Vries <tdevries@suse.de> | 2023-06-19 11:15:56 +0200 |
---|---|---|
committer | Tom de Vries <tdevries@suse.de> | 2023-06-19 11:15:56 +0200 |
commit | 2e12e798825b9119a250f512416e04b62ed7a2a9 (patch) | |
tree | 2a85823a60deeff308e615f58aba2fdde17e3a9c /gdb/utils.h | |
parent | 71a75b51a62b5cbc12c68b9c4d1dcae0f8a59263 (diff) | |
download | gdb-2e12e798825b9119a250f512416e04b62ed7a2a9.zip gdb-2e12e798825b9119a250f512416e04b62ed7a2a9.tar.gz gdb-2e12e798825b9119a250f512416e04b62ed7a2a9.tar.bz2 |
[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<typename T> void assign_set_if_changed (T &lval, const T &val, bool &changed)
{ ... }
template<typename T> 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<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.
Diffstat (limited to 'gdb/utils.h')
-rw-r--r-- | gdb/utils.h | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/gdb/utils.h b/gdb/utils.h index 00b123e..3faac20 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, 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<typename T> +bool +assign_return_if_changed (T &lval, const T &val) +{ + if (lval == val) + return false; + + lval = val; + return true; +} + #endif /* UTILS_H */ |