From patchwork Wed Jan 27 12:58:34 2021 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: 41835 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 CF2073971C3C; Wed, 27 Jan 2021 12:59:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CF2073971C3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1611752351; bh=IMAJmyAnvqlExA+Te96eT75k6mPliMHDurcni5mQK7M=; h=Date:In-Reply-To:References:Subject:To:List-Id:List-Unsubscribe: List-Archive:List-Help:List-Subscribe:From:Reply-To:Cc:From; b=LPRR5dk99a2/mxRBHSIZZIpa5ayvc8jSvvCvOCFyvA2G0CkAOjE9PRrC7Na/7XZkD 1R+ZqzM7ZixtcJ/zvydPtcejJsPredDVXhhBFqljjDQ6Si6vzdiIOlZsLp2TQkNbiK PNQWx6CtqdE8f1H9CAuZl+m+q/JBg7x+0GcJrvCs= X-Original-To: libabigail@sourceware.org Delivered-To: libabigail@sourceware.org Received: from mail-wr1-x44a.google.com (mail-wr1-x44a.google.com [IPv6:2a00:1450:4864:20::44a]) by sourceware.org (Postfix) with ESMTPS id 7C67B382D830 for ; Wed, 27 Jan 2021 12:59:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 7C67B382D830 Received: by mail-wr1-x44a.google.com with SMTP id o17so912327wrv.4 for ; Wed, 27 Jan 2021 04:59:09 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=IMAJmyAnvqlExA+Te96eT75k6mPliMHDurcni5mQK7M=; b=CO8Ba2cdWr3zGyHYOob+VCJUxXV6WMWsTxt9s8R7niG8CvRNEFeMRAAE7imICGvQ5L wJ5nOkPSRIeaTbOwcl6dtXbj/UkMwfThzr3butEE1XAGS1uXmohoglK42h2qWHOJTr38 FJLs0j7CKB/Czg6ryDhMkRIfy315+V0Go8tKsNcM+1Z1RTWTdGK57p6qVpGRHeI67KKA l2meo46hTdktP5Rcx6RaLWltvwo7TTncl7PVMLG5EgHpNLTsmHRYbAQfyDRX7PMnK8QE c/rF7rj8zVQWZLe8sWbdGcHglFSFa6ycqg4Li2JmIhaEWzrvBismqdeJfmRSQvkaGRJa 7uNQ== X-Gm-Message-State: AOAM531drfqoX9UoUSS7WacSdAq+QmC4BaUnKAdCyVNw5+3mgvk5hSEo 5QUXV/KVdEEmeA7Sl2Ivc4P6Noox1uhLnYKWHmiQO1yR/anfKr91fw2Rps/trcuUdos3wUfAa5+ fFFTTMUAOweL1s/bPDussGoWXCQU4jYdaUMRwb2ed9rmWN75w3hHkXszJnZr2ReToqk0MzmY= X-Google-Smtp-Source: ABdhPJwqR4vxv9uU5aWG5Ejg5IGsCLYyoGM1BxgB7otlKP2iJbJ8jFj6e3gzpaf9jVxmfqutLgUKJQw7AB0CTg== X-Received: from lux.lon.corp.google.com ([2a00:79e0:d:210:7220:84ff:fe09:a3aa]) (user=maennich job=sendgmr) by 2002:a7b:cb54:: with SMTP id v20mr4251488wmj.148.1611752348145; Wed, 27 Jan 2021 04:59:08 -0800 (PST) Date: Wed, 27 Jan 2021 12:58:34 +0000 In-Reply-To: <20210127125853.886677-1-maennich@google.com> Message-Id: <20210127125853.886677-2-maennich@google.com> Mime-Version: 1.0 References: <20200619214305.562-1-maennich@google.com> <20210127125853.886677-1-maennich@google.com> X-Mailer: git-send-email 2.30.0.280.ga3ce27912f-goog Subject: [PATCH 01/20] abg-cxx-compat: add simplified version of std::optional To: libabigail@sourceware.org X-Spam-Status: No, score=-22.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, 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 | 84 ++++++++++++++++++++++++++++++++++++++++ tests/test-cxx-compat.cc | 52 +++++++++++++++++++++++++ 2 files changed, 136 insertions(+) diff --git a/include/abg-cxx-compat.h b/include/abg-cxx-compat.h index 838fa53707d1..443905c75138 100644 --- a/include/abg-cxx-compat.h +++ b/include/abg-cxx-compat.h @@ -8,8 +8,92 @@ #ifndef __ABG_CXX_COMPAT_H #define __ABG_CXX_COMPAT_H +// 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 >= 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 maintenance 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 35dc4f6104c2..2b0caaae1ab4 100644 --- a/tests/test-cxx-compat.cc +++ b/tests/test-cxx-compat.cc @@ -12,3 +12,55 @@ #include "lib/catch.hpp" #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); +}