Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ErrorOr to Nearby Platform #3012

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/platform/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ cc_library(
"byte_array.h",
"callable.h",
"exception.h",
"expected.h",
"feature_flags.h",
"input_stream.h",
"listeners.h",
Expand Down Expand Up @@ -506,6 +507,7 @@ cc_test(
"count_down_latch_test.cc",
"crypto_test.cc",
"direct_executor_test.cc",
"expected_test.cc",
"future_test.cc",
"multi_thread_executor_test.cc",
"mutex_test.cc",
Expand Down
119 changes: 119 additions & 0 deletions internal/platform/expected.h
Original file line number Diff line number Diff line change
@@ -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 changes: 51 additions & 0 deletions internal/platform/expected_test.cc
Original file line number Diff line number Diff line change
@@ -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.pb.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
Loading