Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <palves@redhat.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/5] gdb::optional: Add observers
Date: Wed, 29 Mar 2017 02:25:00 -0000	[thread overview]
Message-ID: <1490754298-9455-3-git-send-email-palves@redhat.com> (raw)
In-Reply-To: <1490754298-9455-1-git-send-email-palves@redhat.com>

Currently, gdb::optional is really minimal and can only be used for
lazy initialization.  There's no way to get at the value contained
inside the optinal.  This commit corrects that, by adding observer
methods, mostly copied from libstdc++'s implementation of C++17
std::optional.

This will be used in the following patch.

gdb/ChangeLog:
2017-03-29  Pedro Alves  <palves@redhat.com>

	* common/gdb_optional.h (gdb::optiona): Add operator->, operator*,
	operator bool, has_value and get methods.
---
 gdb/common/gdb_optional.h | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/gdb/common/gdb_optional.h b/gdb/common/gdb_optional.h
index d991da1..fef7a73 100644
--- a/gdb/common/gdb_optional.h
+++ b/gdb/common/gdb_optional.h
@@ -61,6 +61,31 @@ public:
     m_instantiated = true;
   }
 
+  /* Observers.  */
+  constexpr const T *operator-> () const
+  { return std::addressof (this->get ()); }
+
+  T *operator-> ()
+  { return std::addressof (this->get ()); }
+
+  constexpr const T &operator* () const &
+  { return this->get (); }
+
+  T &operator* () &
+  { return this->get (); }
+
+  T &&operator* () &&
+  { return std::move (this->get ()); }
+
+  constexpr const T &&operator* () const &&
+  { return std::move (this->get ()); }
+
+  constexpr explicit operator bool () const noexcept
+  { return m_instantiated; }
+
+  constexpr bool has_value () const noexcept
+  { return m_instantiated; }
+
 private:
 
   /* Destroy the object.  */
@@ -71,6 +96,10 @@ private:
     m_item.~T ();
   }
 
+  /* The get operations have m_instantiated as a precondition.  */
+  T &get () noexcept { return m_item; }
+  constexpr const T &get () const noexcept { return m_item; }
+
   /* The object.  */
   union
   {
-- 
2.5.5


  parent reply	other threads:[~2017-03-29  2:25 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-29  2:25 [PATCH 0/5] dwarf2read.c: Some C++fycation Pedro Alves
2017-03-29  2:25 ` [PATCH 3/5] dwarf2read.c: Make dir_index and file_name_index strong typedefs Pedro Alves
2017-03-29 15:36   ` Simon Marchi
2017-04-04 18:54     ` Pedro Alves
2017-04-04 19:06       ` Pedro Alves
2017-03-29  2:25 ` Pedro Alves [this message]
2017-03-29  2:25 ` [PATCH 1/5] dwarf2read.c: Some C++fycation, use std::vector, std::unique_ptr Pedro Alves
     [not found]   ` <ef286ec115c7c1986e41dd08ecf14fcf@polymtl.ca>
2017-04-04 18:57     ` Pedro Alves
2017-03-29  2:25 ` [PATCH 5/5] dwarf2read.c: C++fy lnp_state_machine Pedro Alves
2017-03-29  2:32 ` [PATCH 4/5] Make sect_offset and cu_offset strong typedefs instead of structs Pedro Alves
2017-03-29 15:46   ` Simon Marchi
2017-04-05 15:13     ` Pedro Alves

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=1490754298-9455-3-git-send-email-palves@redhat.com \
    --to=palves@redhat.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