This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[PATCH 2/5] gdb::optional: Add observers
- From: Pedro Alves <palves at redhat dot com>
- To: gdb-patches at sourceware dot org
- Date: Wed, 29 Mar 2017 03:24:55 +0100
- Subject: [PATCH 2/5] gdb::optional: Add observers
- Authentication-results: sourceware.org; auth=none
- Authentication-results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com
- Authentication-results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=palves at redhat dot com
- Dkim-filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 2D9BF7FD41
- Dmarc-filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 2D9BF7FD41
- References: <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