From 1df7f359d2821261c8e12bca846e2b3b8cf7312f Mon Sep 17 00:00:00 2001 From: Pierric Gimmig Date: Sat, 4 Jan 2025 21:26:22 -0800 Subject: [PATCH] Remove unused AnyErrorOf --- src/OrbitBase/AnyErrorOfTest.cpp | 169 ------------------- src/OrbitBase/CMakeLists.txt | 1 - src/OrbitBase/include/OrbitBase/AnyErrorOf.h | 133 --------------- 3 files changed, 303 deletions(-) delete mode 100644 src/OrbitBase/AnyErrorOfTest.cpp delete mode 100644 src/OrbitBase/include/OrbitBase/AnyErrorOf.h diff --git a/src/OrbitBase/AnyErrorOfTest.cpp b/src/OrbitBase/AnyErrorOfTest.cpp deleted file mode 100644 index 047baf92690..00000000000 --- a/src/OrbitBase/AnyErrorOfTest.cpp +++ /dev/null @@ -1,169 +0,0 @@ -// Copyright (c) 2023 The Orbit Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include -#include - -#include -#include -#include -#include - -#include "OrbitBase/AnyErrorOf.h" -#include "OrbitBase/Result.h" -#include "TestUtils/TestUtils.h" - -namespace orbit_base { -using orbit_test_utils::HasError; -using testing::Eq; - -namespace { -// We are defining 6 arbitrary error types here. E1..E3 are copyable, while U1...U3 are move-only. -// All error types need to have `.message()` member function that returns something convertible to -// `std::string`. - -template -struct ErrorBase { - [[nodiscard]] static std::string_view message() { return {}; } - - // The error types don't hold any state, so all instances are equal to each other. - [[nodiscard]] friend bool operator==(const T& /*unused*/, const T& /*unused*/) { return true; } - [[nodiscard]] friend bool operator!=(const T& /*unused*/, const T& /*unused*/) { return false; } -}; - -struct E1 : ErrorBase {}; -struct E2 : ErrorBase {}; -struct E3 : ErrorBase {}; - -template -struct MoveOnlyErrorBase : ErrorBase { - MoveOnlyErrorBase() = default; - MoveOnlyErrorBase(const MoveOnlyErrorBase&) = delete; - MoveOnlyErrorBase(MoveOnlyErrorBase&&) = default; - MoveOnlyErrorBase& operator=(const MoveOnlyErrorBase&) = delete; - MoveOnlyErrorBase& operator=(MoveOnlyErrorBase&&) = default; -}; - -struct U1 : MoveOnlyErrorBase {}; -struct U2 : MoveOnlyErrorBase {}; -struct U3 : MoveOnlyErrorBase {}; -} // namespace - -TEST(AnyErrorOf, CopyConstructionFromErrorType) { - E1 error_value{}; - - // Copy construction - AnyErrorOf error{error_value}; - - EXPECT_TRUE(std::holds_alternative(error)); - EXPECT_EQ(error, E1{}); - EXPECT_NE(error, E2{}); -} - -TEST(AnyErrorOf, MoveConstructionFromErrorType) { - // Move construction - AnyErrorOf error{U1{}}; - - EXPECT_TRUE(std::holds_alternative(error)); - EXPECT_EQ(error, U1{}); - EXPECT_NE(error, E2{}); -} - -TEST(AnyErrorOf, CopyAssignmentFromErrorType) { - E2 error_value{}; - AnyErrorOf error{E1{}}; - - // Copy assignment - error = error_value; - - EXPECT_TRUE(std::holds_alternative(error)); - EXPECT_NE(error, E1{}); - EXPECT_EQ(error, E2{}); -} - -TEST(AnyErrorOf, MoveAssignmentFromErrorType) { - AnyErrorOf error{E1{}}; - - // Move assignment - error = U2{}; - - EXPECT_TRUE(std::holds_alternative(error)); - EXPECT_NE(error, E1{}); - EXPECT_EQ(error, U2{}); -} - -TEST(AnyErrorOf, CopyConstructionFromCompatibleAnyErrorOf) { - AnyErrorOf source{E2{}}; - - // Copy construction - AnyErrorOf destination{source}; - - EXPECT_TRUE(std::holds_alternative(destination)); - EXPECT_NE(destination, E1{}); - EXPECT_EQ(destination, E2{}); - EXPECT_NE(destination, E3{}); -} - -TEST(AnyErrorOf, MoveConstructionFromCompatibleAnyErrorOf) { - AnyErrorOf source{U2{}}; - - // Move construction - AnyErrorOf destination{std::move(source)}; - - EXPECT_TRUE(std::holds_alternative(destination)); - EXPECT_NE(destination, E1{}); - EXPECT_EQ(destination, U2{}); - EXPECT_NE(destination, E3{}); -} - -TEST(AnyErrorOf, CopyAssignmentFromCompatibleAnyErrorOf) { - AnyErrorOf source{E2{}}; - AnyErrorOf destination{}; - - // Copy assignment - destination = source; - - EXPECT_TRUE(std::holds_alternative(destination)); - EXPECT_NE(destination, E1{}); - EXPECT_EQ(destination, E2{}); - EXPECT_NE(destination, E3{}); -} - -TEST(AnyErrorOf, MoveAssignmentFromCompatibleAnyErrorOf) { - AnyErrorOf source{E2{}}; - AnyErrorOf destination{}; - - // Move assignment - destination = std::move(source); - - EXPECT_TRUE(std::holds_alternative(destination)); - EXPECT_NE(destination, U1{}); - EXPECT_EQ(destination, E2{}); - EXPECT_NE(destination, E3{}); -} - -TEST(AnyErrorOf, OutcomeTryConstructsAnyErrorOfFromErrorType) { - const auto converts_result = [&]() -> Result> { - // Imagine we call a function that returns `ErrorMessageOr`, but we have to return - // `Result>`. The needed conversion should be - // seamless. - OUTCOME_TRY((Result{E1{}})); - return outcome::success(); - }; - - EXPECT_THAT(converts_result(), HasError(Eq(E1{}))); -} - -TEST(AnyErrorOf, OutcomeTryConstructsAnyErrorOfFromCompatibleAnyErrorOf) { - const auto converts_result = [&]() -> Result> { - // Imagine we call a function that returns `Result>`, - // but we have to return `Result>`. - // The needed conversion should be seamless. - OUTCOME_TRY((Result>{E1{}})); - return outcome::success(); - }; - - EXPECT_THAT(converts_result(), HasError(Eq(E1{}))); -} -} // namespace orbit_base diff --git a/src/OrbitBase/CMakeLists.txt b/src/OrbitBase/CMakeLists.txt index a44d70688fb..ce5598f32e4 100644 --- a/src/OrbitBase/CMakeLists.txt +++ b/src/OrbitBase/CMakeLists.txt @@ -17,7 +17,6 @@ target_include_directories(OrbitBase PRIVATE target_sources(OrbitBase PRIVATE include/OrbitBase/Action.h include/OrbitBase/Align.h - include/OrbitBase/AnyErrorOf.h include/OrbitBase/AnyInvocable.h include/OrbitBase/AnyMovable.h include/OrbitBase/Append.h diff --git a/src/OrbitBase/include/OrbitBase/AnyErrorOf.h b/src/OrbitBase/include/OrbitBase/AnyErrorOf.h deleted file mode 100644 index f8092eae9d9..00000000000 --- a/src/OrbitBase/include/OrbitBase/AnyErrorOf.h +++ /dev/null @@ -1,133 +0,0 @@ -// Copyright (c) 2023 The Orbit Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef ORBIT_BASE_ANY_ERROR_OF_H_ -#define ORBIT_BASE_ANY_ERROR_OF_H_ - -#include -#include -#include - -#include "OrbitBase/ParameterPackTrait.h" - -namespace orbit_base { - -// A wrapper around `std::variant` holding one instance of multiple possible error types. It's -// mainly meant to be used with `Result` (`Result`) in cases where a function -// may return one of multiple possible error types. `AnyErrorOf` has a `.message()` member -// function that returns the error message of the holding error by forwarding the call to the -// `.message()` function of the holding error. -// -// `AnyErrorOf` behaves like `std::variant` except for the following properties: -// 1. `ErrorTypes...` may not have duplicate types - all types must be unique. -// 2. An empty list of `ErrorTypes...` is not allowed. -// 3. All error types must have a `.message()` function that's either marked `const` or `static` and -// that returns something that's convertible to `std::string`. -// 4. `AnyErrorOf` can be converted into `AnyErrorOf` if `T2s` contains at least all -// the types present in `T1s`. The order of the types has no meaning. -// 5. `AnyErrorOf` can be directly compared (equality and inequality) to a value of one of its error -// types if the given error type defines an equality comparison operator. The compared values are -// considered equal if `AnyErrorOf` holds a value of the comparing error type and if the equality -// comparison operator returns true. -template -class AnyErrorOf : public std::variant { - using Base = std::variant; - - public: - using Base::Base; - using Base::operator=; - - static_assert(ParameterPackTrait::kSize >= 1, - "AnyError<> (AnyErrorOf with no error types) is not allowed."); - - static_assert(!ParameterPackTrait::kHasDuplicates, - "AnyError must not have duplicate error types."); - - template - constexpr static bool kCanBeConstructedFromTypesAndIsNotCopy = [] { - constexpr ParameterPackTrait kThisTrait{}; - constexpr ParameterPackTrait kOtherTrait{}; - - // We return false if `AnyErrorOf` is the same type as `AnyErrorOf`. - // This avoids collisions with the copy and move constructors/assignment operators. - if (kThisTrait == kOtherTrait) return false; - - // We only allow conversion from an instance of `AnyErrorOf` with `Types` being a - // subset of `ErrorTypes`. - return kThisTrait.IsSubset(kOtherTrait); - }(); - - template - using EnableIfCanBeConstructedFromTypesAndIsNotCopy = - std::enable_if_t, int>; - - // Is true iff `Type` is in the `ErrorTypes...` parameter pack. - template - constexpr static bool kIsAnErrorType = - ParameterPackTrait::template kContains; - - template - auto ToBase(Variant&& other) { - return std::visit( - [](auto&& alternative) -> Base { return std::forward(alternative); }, - std::forward(other)); - } - - // The following converting constructors/assignment operators allow conversion of any AnyErrorOf - // type into a compatible AnyErrorOf type. An AnyErrorOf type is considered compatible - // if its error type list is a super set of the other's error type list. The order of the types - // doesn't matter though. - // - // Examples: - // - AnyErrorOf can be converted into AnyErrorOf but not into AnyErrorOf. - // - AnyErrorOf can be converted into AnyErrorOf. - // - AnyErrorOf can be converted into AnyErrorOf. - template = 0> - // NOLINTNEXTLINE(google-explicit-constructor) - AnyErrorOf(const AnyErrorOf& other) : Base{ToBase(other)} {} - - template = 0> - // NOLINTNEXTLINE(google-explicit-constructor) - AnyErrorOf(AnyErrorOf&& other) : Base{ToBase(std::move(other))} {} - - template = 0> - AnyErrorOf& operator=(const AnyErrorOf& other) { - *this = ToBase(other); - return *this; - } - - template = 0> - AnyErrorOf& operator=(AnyErrorOf&& other) { - *this = ToBase(std::move(other)); - return *this; - } - - [[nodiscard]] std::string message() const { - return std::visit([](const auto& error) { return std::string{error.message()}; }, *this); - } - - // We allow transparent comparison with any of the error types. - template , int> = 0> - [[nodiscard]] friend bool operator==(const AnyErrorOf& lhs, const T& rhs) { - return std::holds_alternative(lhs) && std::get(lhs) == rhs; - } - - template , int> = 0> - [[nodiscard]] friend bool operator==(const T& lhs, const AnyErrorOf& rhs) { - return std::holds_alternative(lhs) && std::get(lhs) == rhs; - } - - template , int> = 0> - [[nodiscard]] friend bool operator!=(const AnyErrorOf& lhs, const T& rhs) { - return !(lhs == rhs); - } - - template , int> = 0> - [[nodiscard]] friend bool operator!=(const T& lhs, const AnyErrorOf& rhs) { - return !(lhs == rhs); - } -}; -} // namespace orbit_base - -#endif // ORBIT_BASE_ANY_ERROR_OF_H_