From patchwork Wed Mar 29 02:24:55 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Alves X-Patchwork-Id: 19750 Received: (qmail 126530 invoked by alias); 29 Mar 2017 02:25:05 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 122798 invoked by uid 89); 29 Mar 2017 02:25:02 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=observer, Hx-languages-length:1733, Destroy X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 29 Mar 2017 02:25:01 +0000 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2D9BF7FD41 for ; Wed, 29 Mar 2017 02:25:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 2D9BF7FD41 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@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 2D9BF7FD41 Received: from cascais.lan (ovpn04.gateway.prod.ext.phx2.redhat.com [10.5.9.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id B2F5817ADD for ; Wed, 29 Mar 2017 02:25:00 +0000 (UTC) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH 2/5] gdb::optional: Add observers Date: Wed, 29 Mar 2017 03:24:55 +0100 Message-Id: <1490754298-9455-3-git-send-email-palves@redhat.com> In-Reply-To: <1490754298-9455-1-git-send-email-palves@redhat.com> 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 * 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 {