Add ErrorOr to Nearby Platform

PiperOrigin-RevId: 697048097
This commit is contained in:
Edwin Wu
2024-11-15 17:43:19 -08:00
committed by Copybara-Service
parent cae753a97c
commit 2c31d77ea9
4 changed files with 183 additions and 0 deletions
+1
View File
@@ -549,6 +549,7 @@ let package = Package(
"internal/platform/implementation/wifi_utils_test.cc",
"internal/platform/atomic_boolean_test.cc",
"internal/platform/exception_test.cc",
"internal/platform/expected_test.cc",
"internal/platform/error_code_recorder_test.cc",
"internal/platform/future_test.cc",
"internal/platform/cancellation_flag_test.cc",
+12
View File
@@ -45,6 +45,7 @@ cc_library(
"byte_array.h",
"callable.h",
"exception.h",
"expected.h",
"feature_flags.h",
"input_stream.h",
"listeners.h",
@@ -426,6 +427,17 @@ cc_test(
],
)
cc_test(
name = "expected_test",
srcs = ["expected_test.cc"],
deps = [
":base",
"//proto:connections_enums_cc_proto",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "error_code_recorder_test",
srcs = [
+119
View File
@@ -0,0 +1,119 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef PLATFORM_BASE_EXPECTED_H_
#define PLATFORM_BASE_EXPECTED_H_
#include <optional>
#include <string>
#include <utility>
#include <variant>
#include "proto/connections_enums.pb.h"
namespace nearby {
class Error {
public:
Error() = default;
explicit Error(location::nearby::proto::connections::OperationResultCode
operation_result_code)
: operation_result_code_(operation_result_code) {}
bool success() const {
return operation_result_code_ == location::nearby::proto::connections::
OperationResultCode::DETAIL_SUCCESS;
}
bool failure() const { return !success(); }
std::optional<location::nearby::proto::connections::OperationResultCode>
operation_result_code() const {
return operation_result_code_;
}
static Error Success() { return Error(); }
private:
location::nearby::proto::connections::OperationResultCode
operation_result_code_ = location::nearby::proto::connections::
OperationResultCode::DETAIL_UNKNOWN;
};
// Forward declare.
template <typename E>
class Unexpected;
template <typename T, typename E>
class Expected {
public:
constexpr Expected(T value) : data_(std::move(value)) {} // NOLINT
constexpr Expected(Unexpected<E> u); // NOLINT
constexpr operator bool() const { // NOLINT
return has_value();
}
constexpr T& operator*() & { return value(); }
constexpr const T& operator*() const& { return value(); }
constexpr T&& operator*() && { return std::move(value()); }
constexpr const T& operator*() const&& { return std::move(value()); }
constexpr T* operator->() { return &value(); }
constexpr const T* operator->() const { return &value(); }
constexpr bool has_value() const { return std::holds_alternative<T>(data_); }
constexpr bool has_error() const { return std::holds_alternative<E>(data_); }
constexpr T& value() & { return std::get<T>(data_); }
constexpr const T& value() const& { return std::get<T>(data_); }
constexpr T&& value() && { return std::get<T>(std::move(data_)); }
constexpr const T& value() const&& { return std::get<T>(std::move(data_)); }
constexpr E& error() & { return std::get<E>(data_); }
constexpr const E& error() const& { return std::get<E>(data_); }
constexpr E&& error() && { return std::get<E>(std::move(data_)); }
constexpr const E&& error() const&& { return std::get<E>(std::move(data_)); }
private:
std::variant<T, E> data_;
};
template <typename E>
class Unexpected {
public:
constexpr Unexpected(E error) : error_(std::move(error)) {} // NOLINT
private:
template <typename, typename>
friend class Expected;
E error_;
};
Unexpected(const char*) -> Unexpected<std::string>;
template <typename T, typename E>
constexpr Expected<T, E>::Expected(Unexpected<E> u)
: data_(std::move(u.error_)) {}
template <typename T>
class ErrorOr : public Expected<T, Error> {
public:
using Expected<T, Error>::Expected;
};
} // namespace nearby
#endif // PLATFORM_BASE_EXPECTED_H_
+51
View File
@@ -0,0 +1,51 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "internal/platform/expected.h"
#include <memory>
#include "gtest/gtest.h"
#include "proto/connections_enums.proto.h"
namespace nearby {
using ::location::nearby::proto::connections::OperationResultCode;
TEST(ExpectedTest, Expected) {
ErrorOr<int> value = 42;
EXPECT_TRUE(value.has_value());
EXPECT_FALSE(value.has_error());
EXPECT_EQ(*value, 42);
ErrorOr<int> error{Error(OperationResultCode::DETAIL_UNKNOWN)};
EXPECT_FALSE(error.has_value());
EXPECT_TRUE(error.has_error());
EXPECT_EQ(error.error().operation_result_code(),
OperationResultCode::DETAIL_UNKNOWN);
ErrorOr<std::unique_ptr<int>> value2{std::make_unique<int>(42)};
EXPECT_TRUE(value2.has_value());
EXPECT_FALSE(value2.has_error());
EXPECT_NE(value2.value().get(), nullptr);
EXPECT_EQ(*value2.value(), 42);
ErrorOr<std::unique_ptr<int>> error2{
Error(OperationResultCode::IO_FILE_OPENING_ERROR)};
EXPECT_FALSE(error2.has_value());
EXPECT_TRUE(error2.has_error());
EXPECT_EQ(error2.error().operation_result_code(),
OperationResultCode::IO_FILE_OPENING_ERROR);
}
} // namespace nearby