Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi via Gdb-patches <gdb-patches@sourceware.org>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: [PATCH 1/2] gdb: return *this in target_waitstatus setters
Date: Wed,  1 Dec 2021 20:40:01 -0500	[thread overview]
Message-ID: <20211202014002.796783-1-simon.marchi@polymtl.ca> (raw)

From: Simon Marchi <simon.marchi@efficios.com>

While playing with some code creating target_waitstatus objects, I was
mildly annoyed by the fact that we can't just return a new
target_waitstatus object.  We have to do:

    target_waitstatus ws;
    ws.set_exited (123);
    return ws;

Make the setters return the "this" object as a reference, such that it's
possible to do:

    return target_waitstatus ().set_exited (123);

I initially thought of adding static creation functions, which you would
use like:

    return target_waitstatus::make_exited (123);

However, making the setters return a reference to the object achieves
pretty much the same thing, with less new code.

Change-Id: I45159b7f9fcd9db5b20603480e323020b14ed147
---
 gdb/target/waitstatus.h | 48 +++++++++++++++++++++++++++--------------
 1 file changed, 32 insertions(+), 16 deletions(-)

diff --git a/gdb/target/waitstatus.h b/gdb/target/waitstatus.h
index 48405d222f4e..631847732adb 100644
--- a/gdb/target/waitstatus.h
+++ b/gdb/target/waitstatus.h
@@ -218,109 +218,125 @@ struct target_waitstatus
 
   /* Setters: set the wait status kind plus any associated data.  */
 
-  void set_exited (int exit_status)
+  target_waitstatus &set_exited (int exit_status)
   {
     this->reset ();
     m_kind = TARGET_WAITKIND_EXITED;
     m_value.exit_status = exit_status;
+    return *this;
   }
 
-  void set_stopped (gdb_signal sig)
+  target_waitstatus &set_stopped (gdb_signal sig)
   {
     this->reset ();
     m_kind = TARGET_WAITKIND_STOPPED;
     m_value.sig = sig;
+    return *this;
   }
 
-  void set_signalled (gdb_signal sig)
+  target_waitstatus &set_signalled (gdb_signal sig)
   {
     this->reset ();
     m_kind = TARGET_WAITKIND_SIGNALLED;
     m_value.sig = sig;
+    return *this;
   }
 
-  void set_loaded ()
+  target_waitstatus &set_loaded ()
   {
     this->reset ();
     m_kind = TARGET_WAITKIND_LOADED;
+    return *this;
   }
 
-  void set_forked (ptid_t child_ptid)
+  target_waitstatus &set_forked (ptid_t child_ptid)
   {
     this->reset ();
     m_kind = TARGET_WAITKIND_FORKED;
     m_value.child_ptid = child_ptid;
+    return *this;
   }
 
-  void set_vforked (ptid_t child_ptid)
+  target_waitstatus &set_vforked (ptid_t child_ptid)
   {
     this->reset ();
     m_kind = TARGET_WAITKIND_VFORKED;
     m_value.child_ptid = child_ptid;
+    return *this;
   }
 
-  void set_execd (gdb::unique_xmalloc_ptr<char> execd_pathname)
+  target_waitstatus &set_execd (gdb::unique_xmalloc_ptr<char> execd_pathname)
   {
     this->reset ();
     m_kind = TARGET_WAITKIND_EXECD;
     m_value.execd_pathname = execd_pathname.release ();
+    return *this;
   }
 
-  void set_vfork_done ()
+  target_waitstatus &set_vfork_done ()
   {
     this->reset ();
     m_kind = TARGET_WAITKIND_VFORK_DONE;
+    return *this;
   }
 
-  void set_syscall_entry (int syscall_number)
+  target_waitstatus &set_syscall_entry (int syscall_number)
   {
     this->reset ();
     m_kind = TARGET_WAITKIND_SYSCALL_ENTRY;
     m_value.syscall_number = syscall_number;
+    return *this;
   }
 
-  void set_syscall_return (int syscall_number)
+  target_waitstatus &set_syscall_return (int syscall_number)
   {
     this->reset ();
     m_kind = TARGET_WAITKIND_SYSCALL_RETURN;
     m_value.syscall_number = syscall_number;
+    return *this;
   }
 
-  void set_spurious ()
+  target_waitstatus &set_spurious ()
   {
     this->reset ();
     m_kind = TARGET_WAITKIND_SPURIOUS;
+    return *this;
   }
 
-  void set_ignore ()
+  target_waitstatus &set_ignore ()
   {
     this->reset ();
     m_kind = TARGET_WAITKIND_IGNORE;
+    return *this;
   }
 
-  void set_no_history ()
+  target_waitstatus &set_no_history ()
   {
     this->reset ();
     m_kind = TARGET_WAITKIND_NO_HISTORY;
+    return *this;
   }
 
-  void set_no_resumed ()
+  target_waitstatus &set_no_resumed ()
   {
     this->reset ();
     m_kind = TARGET_WAITKIND_NO_RESUMED;
+    return *this;
   }
 
-  void set_thread_created ()
+  target_waitstatus &set_thread_created ()
   {
     this->reset ();
     m_kind = TARGET_WAITKIND_THREAD_CREATED;
+    return *this;
   }
 
-  void set_thread_exited (int exit_status)
+  target_waitstatus &set_thread_exited (int exit_status)
   {
     this->reset ();
     m_kind = TARGET_WAITKIND_THREAD_EXITED;
     m_value.exit_status = exit_status;
+    return *this;
   }
 
   /* Get the kind of this wait status.  */
-- 
2.33.1


             reply	other threads:[~2021-12-02  1:40 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-02  1:40 Simon Marchi via Gdb-patches [this message]
2021-12-02  1:40 ` [PATCH 2/2] gdb: change store_waitstatus to return a target_waitstatus by value Simon Marchi via Gdb-patches
2021-12-02 17:20   ` John Baldwin
2021-12-02 17:21     ` Simon Marchi via Gdb-patches
2021-12-03 13:10 ` [PATCH 1/2] gdb: return *this in target_waitstatus setters Pedro Alves
2021-12-03 13:35   ` Simon Marchi via Gdb-patches

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=20211202014002.796783-1-simon.marchi@polymtl.ca \
    --to=gdb-patches@sourceware.org \
    --cc=simon.marchi@efficios.com \
    --cc=simon.marchi@polymtl.ca \
    /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