diff options
author | Lancelot SIX <lsix@lancelotsix.com> | 2021-09-14 23:37:25 +0100 |
---|---|---|
committer | Lancelot SIX <lsix@lancelotsix.com> | 2021-10-03 17:53:16 +0100 |
commit | 1a98bb552d752d1dbedc8fdae504629d21d776f2 (patch) | |
tree | e963b0bc4faa6aec5a34a72b33dcaee274e5da8a /gdb/command.h | |
parent | 702991711a91bd47b209289562843a11e7009396 (diff) | |
download | gdb-1a98bb552d752d1dbedc8fdae504629d21d776f2.zip gdb-1a98bb552d752d1dbedc8fdae504629d21d776f2.tar.gz gdb-1a98bb552d752d1dbedc8fdae504629d21d776f2.tar.bz2 |
gdb: Setting setter return a bool to tell if the value changed
GDB can notify observers when a parameter is changed.
To do that, do_set_command (in gdb/cli/cli-setshow.c) compares the new
value against the old one before updating it, and based on that notifies
observers. This looks like something like:
int valuechanged = 0;
switch (cmd->var.type ())
{
case var_integer:
{
LONGEST new_val = parse_and_eval_long (arg)
if (new_val != cmd->var.get<int> ())
{
cmd->var.get<int> (new_val);
value_changes = 1;
}
}
case var_uinteger:
case var_zuinteger:
{
unsigned int val
= parse_cli_var_uinteger (c->var->type (), &arg, true);
if (c->var->get<unsigned int> () != val)
{
c->var->set<unsigned int> (val);
option_changed = true;
}
}
case...
/* And so on for all possible var_types. */
}
This comparison is done for each possible var_type, which leads to
unnecessary logic duplication.
In this patch I propose to move all those checks in one place within the
setting setter method. This limits the code duplication and simplifies
the do_set_command implementation.
This patch also changes slightly the way a value change is detected.
Instead of comparing the user provided value against the current value
of the setting, we compare the value of the setting before and after the
set operation. This is meant to handle edge cases where trying to set
an unrecognized value would be equivalent to a noop (the actual value
remains unchanged). Doing this requires that the original value needs
to be copied before the update, which can be non trivial for
std::string.
There should be no user visible change introduced by this commit.
Tested on x86_64 GNU/Linux.
[1] https://review.lttng.org/c/binutils-gdb/+/5831/41
Change-Id: If064b9cede3eb56275aacd2b286f74eceb1aed11
Diffstat (limited to 'gdb/command.h')
-rw-r--r-- | gdb/command.h | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/gdb/command.h b/gdb/command.h index 2abfbc4..7c226f1 100644 --- a/gdb/command.h +++ b/gdb/command.h @@ -300,7 +300,9 @@ struct setting return *static_cast<const T *> (m_var); } - /* Sets the value of the setting to V. + /* Sets the value of the setting to V. Returns true if the setting was + effectively changed, false if the update failed and the setting is left + unchanged. If we have a user-provided setter, use it to set the setting. Otherwise copy the value V to the internally referenced buffer. @@ -310,12 +312,14 @@ struct setting The var_type of the setting must match T. */ template<typename T> - void set (const T &v) + bool set (const T &v) { /* Check that the current instance is of one of the supported types for this instantiation. */ gdb_assert (var_type_uses<T> (m_var_type)); + const T old_value = this->get<T> (); + if (m_var == nullptr) { gdb_assert (m_setter != nullptr); @@ -324,6 +328,8 @@ struct setting } else *static_cast<T *> (m_var) = v; + + return old_value != this->get<T> (); } private: |