From patchwork Fri Jun 19 21:42:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Matthias_M=C3=A4nnich?= X-Patchwork-Id: 39695 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 18957389852D; Fri, 19 Jun 2020 21:43:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 18957389852D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1592602996; bh=zPyZ1/fkBBk2ExWTs5gdlYaB4tTHqlX1Cokq60QzFk8=; h=Date:In-Reply-To:References:Subject:To:List-Id:List-Unsubscribe: List-Archive:List-Help:List-Subscribe:From:Reply-To:Cc:From; b=sOvJjq+p0PL0yuJ8u/Z1iA6oXXcZWqiGk8VZnv4yQ7BuqUP3Ld04pkmklyXnq7m4S sv61mPwJsl0VblAkkb3XkqwzNrtxdC2heErJtp8H4bJQ8ip5uN6q1tJd44FSwjllmh bTlIHUaayqItWH4cjQe9ouy/4lBDclqm7Kr+ZkaU= X-Original-To: libabigail@sourceware.org Delivered-To: libabigail@sourceware.org Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by sourceware.org (Postfix) with ESMTPS id 56DC1389852D for ; Fri, 19 Jun 2020 21:43:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 56DC1389852D Received: by mail-yb1-xb49.google.com with SMTP id f16so11677625ybp.5 for ; Fri, 19 Jun 2020 14:43:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=zPyZ1/fkBBk2ExWTs5gdlYaB4tTHqlX1Cokq60QzFk8=; b=EdkW3whgkkogh7dn8IvJ8i5rap6Fa/DSgh9znYSVqZem6Coflzjf1MElywMSRx1wqi /aVKf14ZZffC27/+jubDGw+JEgmwt40btKVcYbL1/50uKTkt+bDVgxKeuDdmJAIAVcYI V5ZE9ak2ms8Kdfp97K2FHNIjTUDg3DzXviSTQ668hIkamLFPaHGfVTPkx1Q2fVOEnBqw hDD4py/7HhiDDXErMsutqwAZoKGmwVFAlIVMEtIT/8vtPXASf2xa2a0nnV73cDYdgN9a +5D4Y+zhATRtK+oXlkY4Uzg76wwfVMqgBZSn+f48iXvqxoxygo583MEXd4V7oev660z6 PYaQ== X-Gm-Message-State: AOAM5302oGKPcCngjcdv82zq85mzTgkKRwktX4tfBIbX59oJ7+rVoaso kzGDbP6GcCMgObNc/wmzn2c3BYBiOL1SPtsBYtQcZX8io4wV4JK3Kea/zVHoph5tPw87GMyyEef dAPc6s4rptbW+t9zHaE3xX2brKCG9ba4IX4hC801NGxWkvrFa18ajGzLzJAiVcypvcAFkmNs= X-Google-Smtp-Source: ABdhPJzsRodoIieURLrzokkpT0vDU+thwMmI7grNk8pX099Zdnm/gxHU8tJx94z+s0uq92/k5OZJ0RBFEJ6PRw== X-Received: by 2002:a25:9d0f:: with SMTP id i15mr8979030ybp.508.1592602993772; Fri, 19 Jun 2020 14:43:13 -0700 (PDT) Date: Fri, 19 Jun 2020 23:42:50 +0200 In-Reply-To: <20200619214305.562-1-maennich@google.com> Message-Id: <20200619214305.562-2-maennich@google.com> Mime-Version: 1.0 References: <20200619214305.562-1-maennich@google.com> X-Mailer: git-send-email 2.27.0.111.gc72c7da667-goog Subject: [PATCH v1 01/16] abg-cxx-compat: add simplified version of std::optional To: libabigail@sourceware.org X-Spam-Status: No, score=-23.6 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libabigail@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Mailing list of the Libabigail project List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-Patchwork-Original-From: Matthias Maennich via Libabigail From: =?utf-8?q?Matthias_M=C3=A4nnich?= Reply-To: Matthias Maennich Cc: maennich@google.com, gprocida@google.com, kernel-team@android.com Errors-To: libabigail-bounces@sourceware.org Sender: "Libabigail" In the absence (but desire) of std::optional, add a simplified version of it to abg_compat:: in case we are compiling with a pre-C++17 standard. Otherwise use std::optional from directly. This is being used by a later patch and serves as a prerequisite. It only serves the purpose of being a compatibility implementation and does not claim to be complete at all. Just enough for the project's needs. * include/abg-cxx-compat.h (abg_compat::optional): Add new class. * tests/tests-cxx-compat.cc: Add new test cases. Reviewed-by: Giuliano Procida Signed-off-by: Matthias Maennich --- include/abg-cxx-compat.h | 86 ++++++++++++++++++++++++++++++++++++++++ tests/test-cxx-compat.cc | 51 ++++++++++++++++++++++++ 2 files changed, 137 insertions(+) diff --git a/include/abg-cxx-compat.h b/include/abg-cxx-compat.h index 714177183945..1283e9191d46 100644 --- a/include/abg-cxx-compat.h +++ b/include/abg-cxx-compat.h @@ -23,6 +23,8 @@ #ifndef __ABG_CXX_COMPAT_H #define __ABG_CXX_COMPAT_H +// C++11 support (mostly via tr1 if compiled with earlier standard) + #if __cplusplus >= 201103L #include @@ -39,6 +41,18 @@ #endif +// C++17 support (via custom implementations if compiled with earlier standard) + +#if __cplusplus >= 201703L + +#include + +#else + +#include // for throwing std::runtime_error("bad_optional_access") + +#endif + namespace abg_compat { #if __cplusplus >= 201103L @@ -77,6 +91,78 @@ using std::tr1::unordered_set; #endif +#if __cplusplus >= 201703L + +using std::optional; + +#else + +// + +/// Simplified implementation of std::optional just enough to be used as a +/// replacement for our purposes and when compiling with pre C++17. +/// +/// The implementation intentionally does not support a whole lot of features +/// to minimize the maintainence effort with this. +template class optional +{ + bool has_value_; + T value_; + +public: + optional() : has_value_(false), value_() {} + optional(const T& value) : has_value_(true), value_(value) {} + + bool + has_value() const + { + return has_value_; + } + + const T& + value() const + { + if (!has_value_) + throw std::runtime_error("bad_optional_access"); + return value_; + } + + const T + value_or(const T& default_value) const + { + if (!has_value_) + return default_value; + return value_; + } + + const T& + operator*() const + { return value_; } + + T& + operator*() + { return value_; } + + const T* + operator->() const + { return &value_; } + + T* + operator->() + { return &value_; } + + optional& + operator=(const T& value) + { + has_value_ = true; + value_ = value; + return *this; + } + + explicit operator bool() const { return has_value_; } +}; + +#endif } #endif // __ABG_CXX_COMPAT_H diff --git a/tests/test-cxx-compat.cc b/tests/test-cxx-compat.cc index 0a230a54e1a5..adeb7b8d7e4f 100644 --- a/tests/test-cxx-compat.cc +++ b/tests/test-cxx-compat.cc @@ -28,3 +28,54 @@ #include "abg-cxx-compat.h" +using abg_compat::optional; + +TEST_CASE("OptionalConstruction", "[abg_compat::optional]") +{ + optional opt1; + REQUIRE_FALSE(opt1.has_value()); + + optional opt2(true); + REQUIRE(opt2.has_value()); + CHECK(opt2.value() == true); + + optional opt3(false); + REQUIRE(opt3.has_value()); + CHECK(opt3.value() == false); +} + +TEST_CASE("OptionalValue", "[abg_compat::optional]") +{ + optional opt; + REQUIRE_FALSE(opt.has_value()); + REQUIRE_THROWS(opt.value()); + + opt = true; + REQUIRE_NOTHROW(opt.value()); + CHECK(opt.value() == true); +} + +TEST_CASE("OptionalValueOr", "[abg_compat::optional]") +{ + optional opt; + REQUIRE_FALSE(opt.has_value()); + + const std::string& mine = "mine"; + // Ensure we get a copy of our own value. + CHECK(opt.value_or(mine) == mine); + + // Now set the value + const std::string& other = "other"; + opt = other; + CHECK(opt.value_or(mine) != mine); + CHECK(opt.value_or(mine) == other); +} + +TEST_CASE("OptionalDeref", "[abg_compat::optional]") +{ + optional opt("asdf"); + REQUIRE(opt.has_value()); + + CHECK(*opt == "asdf"); + CHECK(opt->size() == 4); +}