From patchwork Fri Jul 3 16:46:31 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: 39884 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 604993844079; Fri, 3 Jul 2020 16:47:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 604993844079 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1593794861; bh=R0k0bmRmHWlMXsZQ4l93Qyy6b7XPfg32VrU7d+Bz7Jc=; h=Date:In-Reply-To:References:Subject:To:List-Id:List-Unsubscribe: List-Archive:List-Help:List-Subscribe:From:Reply-To:Cc:From; b=Jm4iJ9OD2GqL6dT/7xb8coxPM2HjgM1XFOrfT6yfNqds0E9GdQaxfrkWPd6UeL/IJ iHNf8B9UIrMGq/RVS9RGHRqGCwwJ/B8PcyeIrRnPWjumsslPD5vWkTrQeoddTLwa5U 8fkhGMRE90TgyLRpV6y+xqbn4upk58QMXgrqaGxM= 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 399A6384402F for ; Fri, 3 Jul 2020 16:47:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 399A6384402F Received: by mail-wr1-x44a.google.com with SMTP id i14so32066383wru.17 for ; Fri, 03 Jul 2020 09:47:38 -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=R0k0bmRmHWlMXsZQ4l93Qyy6b7XPfg32VrU7d+Bz7Jc=; b=YcXhb4MoxX3bBQAtj+RYcTXFOt9p1TQBgfYxs0H85G2CnGVH4f9q8lce99H3PfiiBx 0+aP5FhTx/Fl8RrIaMd2DxY/SmxGCkL7BAvUmRpEqv+lSKyZR1NQgLUk9oBoiI6SSeVo NxPx4pfYgtEztR15ZHcF0eCwAVbH06rlEUoI7fKCjKsRPJ/O32v37sXctexHc2LQQ4wN r3d2C/mUIDC91ckLpnSstlMNybVjFiuLewLLtnp9fBlNWA50FLUycX2H6fvbF1fqAAxV lr0JF+xmH2aAkhRnpjLJ1Alwg+bZmJhKZz3+/goOKKnuoiiard2dHlSRmdIVKIOX0XE+ J4gw== X-Gm-Message-State: AOAM530/qQG5SSY9xdI6/Kjto2UyVUStk5IFog5IZI0m8J9gXWp1XjUn q1Agq0Vvn0oaQrlm2hksBgKeLxUQKEwnfIauBXnJHBwzevyLoIYv4VPbnRXmn7GH57GwF8rRvX4 +r/FHv70Nw6m3KIIQFXbsNEVwj7aihcgUobD3byhb/DvdnMhehNdRN2xLpU0xYMhsUrTS/lQ= X-Google-Smtp-Source: ABdhPJxwFIg3c1Or4dMkw5DE85N5aYsRj0erxQoc/lxWuZzsBhPYKRppM7XBrGODLytuQAF1GHGlZXHpTwXO7Q== X-Received: by 2002:a5d:43d0:: with SMTP id v16mr39446817wrr.296.1593794857166; Fri, 03 Jul 2020 09:47:37 -0700 (PDT) Date: Fri, 3 Jul 2020 18:46:31 +0200 In-Reply-To: <20200703164651.1510825-1-maennich@google.com> Message-Id: <20200703164651.1510825-2-maennich@google.com> Mime-Version: 1.0 References: <20200619214305.562-1-maennich@google.com> <20200703164651.1510825-1-maennich@google.com> X-Mailer: git-send-email 2.27.0.212.ge8ba1cc988-goog Subject: [PATCH v2 01/21] abg-cxx-compat: add simplified version of std::optional To: libabigail@sourceware.org X-Spam-Status: No, score=-23.2 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 | 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); +}