public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@efficios.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 1/5] gdb: pass/return setting setter/getter scalar values by value
Date: Mon,  1 Nov 2021 11:50:05 -0400	[thread overview]
Message-ID: <20211101155009.457224-2-simon.marchi@efficios.com> (raw)
In-Reply-To: <20211101155009.457224-1-simon.marchi@efficios.com>

From: Simon Marchi <simon.marchi@polymtl.ca>

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/cli/cli-decode.c | 52 +++++++++++++--------------
 gdb/command.h        | 84 ++++++++++++++++++++++++++------------------
 gdb/remote.c         |  5 ++-
 3 files changed, 80 insertions(+), 61 deletions(-)

diff --git a/gdb/cli/cli-decode.c b/gdb/cli/cli-decode.c
index 27a80192c485..396aefb4b011 100644
--- a/gdb/cli/cli-decode.c
+++ b/gdb/cli/cli-decode.c
@@ -572,8 +572,8 @@ add_setshow_cmd_full (const char *name,
 		      var_types var_type, T *var,
 		      const char *set_doc, const char *show_doc,
 		      const char *help_doc,
-		      setting_setter_ftype<T> set_setting_func,
-		      setting_getter_ftype<T> get_setting_func,
+		      typename the_types<T>::set set_setting_func,
+		      typename the_types<T>::get get_setting_func,
 		      cmd_func_ftype *set_func,
 		      show_value_ftype *show_func,
 		      struct cmd_list_element **set_list,
@@ -628,8 +628,8 @@ 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<const char *> set_func,
-		      setting_getter_ftype<const char *> get_func,
+		      typename the_types<const char *>::set set_func,
+		      typename the_types<const char *>::get get_func,
 		      show_value_ftype *show_func,
 		      cmd_list_element **set_list,
 		      cmd_list_element **show_list)
@@ -682,8 +682,8 @@ 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<enum auto_boolean> set_func,
-			      setting_getter_ftype<enum auto_boolean> get_func,
+			      typename the_types<enum auto_boolean>::set set_func,
+			      typename the_types<enum auto_boolean>::get get_func,
 			      show_value_ftype *show_func,
 			      cmd_list_element **set_list,
 			      cmd_list_element **show_list)
@@ -737,8 +737,8 @@ 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<bool> set_func,
-			 setting_getter_ftype<bool> get_func,
+			 typename the_types<bool>::set set_func,
+			 typename the_types<bool>::get get_func,
 			 show_value_ftype *show_func,
 			 cmd_list_element **set_list,
 			 cmd_list_element **show_list)
@@ -784,8 +784,8 @@ 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<std::string> set_func,
-			  setting_getter_ftype<std::string> get_func,
+			  typename the_types<std::string>::set set_func,
+			  typename the_types<std::string>::get get_func,
 			  show_value_ftype *show_func,
 			  cmd_list_element **set_list,
 			  cmd_list_element **show_list)
@@ -833,8 +833,8 @@ 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<std::string> set_func,
-			setting_getter_ftype<std::string> get_func,
+			typename the_types<std::string>::set set_func,
+			typename the_types<std::string>::get get_func,
 			show_value_ftype *show_func,
 			cmd_list_element **set_list,
 			cmd_list_element **show_list)
@@ -883,8 +883,8 @@ 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<std::string> set_func,
-				 setting_getter_ftype<std::string> get_func,
+				 typename the_types<std::string>::set set_func,
+				 typename the_types<std::string>::get get_func,
 				 show_value_ftype *show_func,
 				 cmd_list_element **set_list,
 				 cmd_list_element **show_list)
@@ -933,8 +933,8 @@ 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<std::string> set_func,
-				   setting_getter_ftype<std::string> get_func,
+				   typename the_types<std::string>::set set_func,
+				   typename the_types<std::string>::get get_func,
 				   show_value_ftype *show_func,
 				   cmd_list_element **set_list,
 				   cmd_list_element **show_list)
@@ -1001,8 +1001,8 @@ 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<int> set_func,
-			 setting_getter_ftype<int> get_func,
+			 typename the_types<int>::set set_func,
+			 typename the_types<int>::get get_func,
 			 show_value_ftype *show_func,
 			 cmd_list_element **set_list,
 			 cmd_list_element **show_list)
@@ -1050,8 +1050,8 @@ 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<unsigned int> set_func,
-			  setting_getter_ftype<unsigned int> get_func,
+			  typename the_types<unsigned int>::set set_func,
+			  typename the_types<unsigned int>::get get_func,
 			  show_value_ftype *show_func,
 			  cmd_list_element **set_list,
 			  cmd_list_element **show_list)
@@ -1095,8 +1095,8 @@ 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<int> set_func,
-			  setting_getter_ftype<int> get_func,
+			  typename the_types<int>::set set_func,
+			  typename the_types<int>::get get_func,
 			  show_value_ftype *show_func,
 			  cmd_list_element **set_list,
 			  cmd_list_element **show_list)
@@ -1137,8 +1137,8 @@ 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<int> set_func,
-				     setting_getter_ftype<int> get_func,
+				     typename the_types<int>::set set_func,
+				     typename the_types<int>::get get_func,
 				     show_value_ftype *show_func,
 				     cmd_list_element **set_list,
 				     cmd_list_element **show_list)
@@ -1182,8 +1182,8 @@ 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<unsigned int> set_func,
-			   setting_getter_ftype<unsigned int> get_func,
+			   typename the_types<unsigned int>::set set_func,
+			   typename the_types<unsigned int>::get get_func,
 			   show_value_ftype *show_func,
 			   cmd_list_element **set_list,
 			   cmd_list_element **show_list)
diff --git a/gdb/command.h b/gdb/command.h
index 9afe70cf66a6..ef3c52949dca 100644
--- a/gdb/command.h
+++ b/gdb/command.h
@@ -181,15 +181,31 @@ inline bool var_type_uses<const char *> (var_types t)
   return t == var_enum;
 }
 
-/* Function signature for a callback used to get a value from a setting.  */
+template<bool is_scalar, typename T> struct the_types2;
 
 template<typename T>
-using setting_getter_ftype = const T &(*) ();
+struct the_types2<true, T>
+{
+  using type = T;
+  using set = void (*) (type);
+  using get = type (*) ();
+};
 
-/* Function signature for a callback used to set a value to a setting.  */
+template<typename T>
+struct the_types2<false, T>
+{
+  using type = const T &;
+  using set = void (*) (type);
+  using get = type (*) ();
+};
 
 template<typename T>
-using setting_setter_ftype = void (*) (const T &);
+struct the_types
+{
+  using type = typename the_types2<std::is_scalar<T>::value, T>::type;
+  using set = typename the_types2<std::is_scalar<T>::value, T>::set;
+  using get = typename the_types2<std::is_scalar<T>::value, T>::get;
+};
 
 /* Generic/type-erased function pointer.  */
 
@@ -225,8 +241,8 @@ struct setting
   template<typename T>
   static erased_args erase_args (var_types var_type,
 				 T *var,
-				 setting_setter_ftype<T> set_setting_func,
-				 setting_getter_ftype<T> get_setting_func)
+				 typename the_types<T>::set set_setting_func,
+				 typename the_types<T>::get get_setting_func)
   {
     gdb_assert (var_type_uses<T> (var_type));
   /* The getter and the setter must be both provided or both omitted.  */
@@ -260,8 +276,8 @@ struct setting
      Type T must match the var type VAR_TYPE (see VAR_TYPE_USES).  */
   template<typename T>
   setting (var_types var_type,
-	   setting_setter_ftype<T> setter,
-	   setting_getter_ftype<T> getter)
+	   typename the_types<T>::set setter,
+	   typename the_types<T>::get getter)
     : m_var_type (var_type)
   {
     gdb_assert (var_type_uses<T> (var_type));
@@ -285,14 +301,14 @@ struct setting
      The template parameter T is the type of the variable used to store the
      setting.  */
   template<typename T>
-  const T &get () const
+  typename the_types<T>::type get () const
   {
     gdb_assert (var_type_uses<T> (m_var_type));
 
     if (m_var == nullptr)
       {
 	gdb_assert (m_getter != nullptr);
-	auto getter = reinterpret_cast<setting_getter_ftype<T>> (m_getter);
+	auto getter = reinterpret_cast<typename the_types<T>::get> (m_getter);
 	return getter ();
       }
     else
@@ -322,7 +338,7 @@ struct setting
     if (m_var == nullptr)
       {
 	gdb_assert (m_setter != nullptr);
-	auto setter = reinterpret_cast<setting_setter_ftype<T>> (m_setter);
+	auto setter = reinterpret_cast<typename the_types<T>::set> (m_setter);
 	setter (v);
       }
     else
@@ -644,8 +660,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<const char *> set_func,
-   setting_getter_ftype<const char *> get_func, show_value_ftype *show_func,
+   const char *help_doc, typename the_types<const char *>::set set_func,
+   typename the_types<const char *>::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
@@ -657,8 +673,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<enum auto_boolean> set_func,
-   setting_getter_ftype<enum auto_boolean> get_func,
+   typename the_types<enum auto_boolean>::set set_func,
+   typename the_types<enum auto_boolean>::get get_func,
    show_value_ftype *show_func, cmd_list_element **set_list,
    cmd_list_element **show_list);
 
@@ -671,8 +687,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<bool> set_func,
-   setting_getter_ftype<bool> get_func, show_value_ftype *show_func,
+   typename the_types<bool>::set set_func,
+   typename the_types<bool>::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
@@ -684,8 +700,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<std::string> set_func,
-   setting_getter_ftype<std::string> get_func, show_value_ftype *show_func,
+   typename the_types<std::string>::set set_func,
+   typename the_types<std::string>::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
@@ -697,8 +713,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<std::string> set_func,
-   setting_getter_ftype<std::string> get_func,
+   typename the_types<std::string>::set set_func,
+   typename the_types<std::string>::get get_func,
    show_value_ftype *show_func, cmd_list_element **set_list,
    cmd_list_element **show_list);
 
@@ -711,8 +727,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<std::string> set_func,
-   setting_getter_ftype<std::string> get_func, show_value_ftype *show_func,
+   typename the_types<std::string>::set set_func,
+   typename the_types<std::string>::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
@@ -724,8 +740,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<std::string> set_func,
-   setting_getter_ftype<std::string> get_func,
+   typename the_types<std::string>::set set_func,
+   typename the_types<std::string>::get get_func,
    show_value_ftype *show_func, cmd_list_element **set_list,
    cmd_list_element **show_list);
 
@@ -738,8 +754,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<int> set_func,
-   setting_getter_ftype<int> get_func, show_value_ftype *show_func,
+   typename the_types<int>::set set_func,
+   typename the_types<int>::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
@@ -751,8 +767,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<unsigned int> set_func,
-   setting_getter_ftype<unsigned int> get_func, show_value_ftype *show_func,
+   typename the_types<unsigned int>::set set_func,
+   typename the_types<unsigned int>::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
@@ -764,8 +780,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<int> set_func,
-   setting_getter_ftype<int> get_func, show_value_ftype *show_func,
+   typename the_types<int>::set set_func,
+   typename the_types<int>::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
@@ -777,8 +793,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<unsigned int> set_func,
-   setting_getter_ftype<unsigned int> get_func, show_value_ftype *show_func,
+   typename the_types<unsigned int>::set set_func,
+   typename the_types<unsigned int>::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
@@ -790,7 +806,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<int> set_func, setting_getter_ftype<int> get_func,
+   typename the_types<int>::set set_func, typename the_types<int>::get get_func,
    show_value_ftype *show_func, cmd_list_element **set_list,
    cmd_list_element **show_list);
 
diff --git a/gdb/remote.c b/gdb/remote.c
index 0fb427535960..f17e571efa93 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -1873,6 +1873,8 @@ struct packet_config
        have an associated command always have this set to auto.  */
     enum auto_boolean detect;
 
+    cmd_list_element *show_cmd;
+
     /* Does the target support this packet?  */
     enum packet_support support;
   };
@@ -1936,6 +1938,7 @@ add_packet_config_cmd (struct packet_config *config, const char *name,
 				    NULL,
 				    show_remote_protocol_packet_cmd,
 				    &remote_set_cmdlist, &remote_show_cmdlist);
+  config->show_cmd = cmds.show;
 
   /* The command code copies the documentation strings.  */
   xfree (set_doc);
@@ -2245,7 +2248,7 @@ show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
        packet < &remote_protocol_packets[PACKET_MAX];
        packet++)
     {
-      if (&packet->detect == &c->var->get<enum auto_boolean> ())
+      if (c == packet->show_cmd)
 	{
 	  show_packet_config_cmd (packet);
 	  return;
-- 
2.26.2


  reply	other threads:[~2021-11-01 15:50 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-01 15:50 [PATCH 0/5] Change some index-cache commands Simon Marchi
2021-11-01 15:50 ` Simon Marchi [this message]
2021-11-04 18:27   ` [PATCH 1/5] gdb: pass/return setting setter/getter scalar values by value Tom Tromey
2021-11-04 18:59     ` Simon Marchi
2021-11-04 19:51       ` Simon Marchi
2021-11-01 15:50 ` [PATCH 2/5] gdb: remove unnecessary cmd_list_element::aliases nullptr checks Simon Marchi
2021-11-04 18:29   ` Tom Tromey
2021-11-04 19:45     ` Simon Marchi
2021-11-01 15:50 ` [PATCH 3/5] gdb: remove command_class enum class_deprecated Simon Marchi
2021-11-04 18:30   ` Tom Tromey
2021-11-04 19:45     ` Simon Marchi
2021-11-01 15:50 ` [PATCH 4/5] gdb: add "info index-cache stats", deprecate "show index-cache stats" Simon Marchi
2021-11-04 18:33   ` Tom Tromey
2021-11-04 19:19     ` Simon Marchi
2021-11-01 15:50 ` [PATCH 5/5] gdb: introduce "set index-cache enabled", deprecate "set index-cache on/off" Simon Marchi
2021-11-04 18:36   ` Tom Tromey
2021-11-04 19:51     ` Simon Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211101155009.457224-2-simon.marchi@efficios.com \
    --to=simon.marchi@efficios.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).