From fcef6471ed3c07d6eac2d9e0010552994f0891aa Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Sun, 31 Oct 2021 12:02:03 -0400 Subject: gdb: pass/return setting setter/getter scalar values by value The getter and setter in struct setting always receive and return values by const reference. This is not necessary for scalar values (like bool and int), but more importantly it makes it a bit annoying to write a getter, you have to use a scratch static variable or something similar that you can refer to: const bool & my_getter () { static bool value; value = function_returning_bool (); return value; } Change the getter and setter function signatures to receive and return value by value instead of by reference, when the underlying data type is scalar. This means that string-based settings will still use references, but all others will be by value. The getter above would then be re-written as: bool my_getter () { return function_returning_bool (); } This is useful for a patch later in this series that defines a boolean setting with a getter and a setter. Change-Id: Ieca3a2419fcdb75a6f75948b2c920b548a0af0fd --- gdb/command.h | 84 +++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 34 deletions(-) (limited to 'gdb/command.h') diff --git a/gdb/command.h b/gdb/command.h index d6e94b9..bcd3882 100644 --- a/gdb/command.h +++ b/gdb/command.h @@ -180,15 +180,31 @@ inline bool var_type_uses (var_types t) return t == var_enum; } -/* Function signature for a callback used to get a value from a setting. */ +template struct setting_func_types_1; template -using setting_getter_ftype = const T &(*) (); +struct setting_func_types_1 +{ + using type = T; + using set = void (*) (type); + using get = type (*) (); +}; -/* Function signature for a callback used to set a value to a setting. */ +template +struct setting_func_types_1 +{ + using type = const T &; + using set = void (*) (type); + using get = type (*) (); +}; template -using setting_setter_ftype = void (*) (const T &); +struct setting_func_types +{ + using type = typename setting_func_types_1::value, T>::type; + using set = typename setting_func_types_1::value, T>::set; + using get = typename setting_func_types_1::value, T>::get; +}; /* Generic/type-erased function pointer. */ @@ -224,8 +240,8 @@ struct setting template static erased_args erase_args (var_types var_type, T *var, - setting_setter_ftype set_setting_func, - setting_getter_ftype get_setting_func) + typename setting_func_types::set set_setting_func, + typename setting_func_types::get get_setting_func) { gdb_assert (var_type_uses (var_type)); /* The getter and the setter must be both provided or both omitted. */ @@ -259,8 +275,8 @@ struct setting Type T must match the var type VAR_TYPE (see VAR_TYPE_USES). */ template setting (var_types var_type, - setting_setter_ftype setter, - setting_getter_ftype getter) + typename setting_func_types::set setter, + typename setting_func_types::get getter) : m_var_type (var_type) { gdb_assert (var_type_uses (var_type)); @@ -284,14 +300,14 @@ struct setting The template parameter T is the type of the variable used to store the setting. */ template - const T &get () const + typename setting_func_types::type get () const { gdb_assert (var_type_uses (m_var_type)); if (m_var == nullptr) { gdb_assert (m_getter != nullptr); - auto getter = reinterpret_cast> (m_getter); + auto getter = reinterpret_cast::get> (m_getter); return getter (); } else @@ -321,7 +337,7 @@ struct setting if (m_var == nullptr) { gdb_assert (m_setter != nullptr); - auto setter = reinterpret_cast> (m_setter); + auto setter = reinterpret_cast::set> (m_setter); setter (v); } else @@ -643,8 +659,8 @@ extern set_show_commands add_setshow_enum_cmd extern set_show_commands add_setshow_enum_cmd (const char *name, command_class theclass, const char *const *enumlist, const char *set_doc, const char *show_doc, - const char *help_doc, setting_setter_ftype set_func, - setting_getter_ftype get_func, show_value_ftype *show_func, + const char *help_doc, setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); extern set_show_commands add_setshow_auto_boolean_cmd @@ -656,8 +672,8 @@ extern set_show_commands add_setshow_auto_boolean_cmd extern set_show_commands add_setshow_auto_boolean_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); @@ -670,8 +686,8 @@ extern set_show_commands add_setshow_boolean_cmd extern set_show_commands add_setshow_boolean_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, show_value_ftype *show_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); extern set_show_commands add_setshow_filename_cmd @@ -683,8 +699,8 @@ extern set_show_commands add_setshow_filename_cmd extern set_show_commands add_setshow_filename_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, show_value_ftype *show_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); extern set_show_commands add_setshow_string_cmd @@ -696,8 +712,8 @@ extern set_show_commands add_setshow_string_cmd extern set_show_commands add_setshow_string_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); @@ -710,8 +726,8 @@ extern set_show_commands add_setshow_string_noescape_cmd extern set_show_commands add_setshow_string_noescape_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, show_value_ftype *show_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); extern set_show_commands add_setshow_optional_filename_cmd @@ -723,8 +739,8 @@ extern set_show_commands add_setshow_optional_filename_cmd extern set_show_commands add_setshow_optional_filename_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); @@ -737,8 +753,8 @@ extern set_show_commands add_setshow_integer_cmd extern set_show_commands add_setshow_integer_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, show_value_ftype *show_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); extern set_show_commands add_setshow_uinteger_cmd @@ -750,8 +766,8 @@ extern set_show_commands add_setshow_uinteger_cmd extern set_show_commands add_setshow_uinteger_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, show_value_ftype *show_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); extern set_show_commands add_setshow_zinteger_cmd @@ -763,8 +779,8 @@ extern set_show_commands add_setshow_zinteger_cmd extern set_show_commands add_setshow_zinteger_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, show_value_ftype *show_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); extern set_show_commands add_setshow_zuinteger_cmd @@ -776,8 +792,8 @@ extern set_show_commands add_setshow_zuinteger_cmd extern set_show_commands add_setshow_zuinteger_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, - setting_getter_ftype get_func, show_value_ftype *show_func, + setting_func_types::set set_func, + setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); extern set_show_commands add_setshow_zuinteger_unlimited_cmd @@ -789,7 +805,7 @@ extern set_show_commands add_setshow_zuinteger_unlimited_cmd extern set_show_commands add_setshow_zuinteger_unlimited_cmd (const char *name, command_class theclass, const char *set_doc, const char *show_doc, const char *help_doc, - setting_setter_ftype set_func, setting_getter_ftype get_func, + setting_func_types::set set_func, setting_func_types::get get_func, show_value_ftype *show_func, cmd_list_element **set_list, cmd_list_element **show_list); -- cgit v1.1