]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb::optional: Add observers
authorPedro Alves <palves@redhat.com>
Tue, 4 Apr 2017 19:03:25 +0000 (20:03 +0100)
committerPedro Alves <palves@redhat.com>
Tue, 4 Apr 2017 19:03:25 +0000 (20:03 +0100)
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-04-04  Pedro Alves  <palves@redhat.com>

* common/gdb_optional.h (gdb::optiona): Add operator->, operator*,
operator bool, has_value and get methods.

gdb/ChangeLog
gdb/common/gdb_optional.h

index 5e124c46c4c4c58802ca8adc06b80160d2a03c77..f2b64b9e4d040de6ce437dc299070365e3a85afd 100644 (file)
@@ -1,3 +1,8 @@
+2017-04-04  Pedro Alves  <palves@redhat.com>
+
+       * common/gdb_optional.h (gdb::optiona): Add operator->, operator*,
+       operator bool, has_value and get methods.
+
 2017-04-04  Pedro Alves  <palves@redhat.com>
 
        * dwarf2read.c (struct file_entry): Add ctors, and initialize all
index d991da123cf0ee088ba9f10cfca78d5891a23451..fef7a73e02a6ecfb1f37d0e71a43944cbb29c7b6 100644 (file)
@@ -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
   {