2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
// http://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.
|
|
|
|
|
|
|
|
// This file and it's implementation provide a custom fork of
|
|
|
|
// util/task/statusor.h. This will become obsolete and will be replaced once
|
|
|
|
// Abseil releases absl::Status.
|
|
|
|
|
|
|
|
#ifndef THIRD_PARTY_SAPI_UTIL_STATUSOR_H_
|
|
|
|
#define THIRD_PARTY_SAPI_UTIL_STATUSOR_H_
|
|
|
|
|
2020-04-01 19:40:18 +08:00
|
|
|
#include <initializer_list>
|
|
|
|
#include <utility>
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/base/internal/raw_logging.h"
|
|
|
|
#include "absl/base/attributes.h"
|
|
|
|
#include "absl/base/log_severity.h"
|
2020-02-28 01:23:44 +08:00
|
|
|
#include "absl/status/status.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/types/variant.h"
|
|
|
|
#include "sandboxed_api/util/raw_logging.h"
|
|
|
|
|
|
|
|
namespace sapi {
|
|
|
|
|
|
|
|
template <typename T>
|
2020-04-01 19:40:18 +08:00
|
|
|
class ABSL_MUST_USE_RESULT StatusOr {
|
|
|
|
template <typename U>
|
|
|
|
friend class StatusOr;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-04-01 19:40:18 +08:00
|
|
|
public:
|
|
|
|
using element_type = T;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-04-01 19:40:18 +08:00
|
|
|
explicit StatusOr() : variant_(absl::UnknownError("")) {}
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
StatusOr(const StatusOr&) = default;
|
|
|
|
StatusOr& operator=(const StatusOr&) = default;
|
|
|
|
|
|
|
|
StatusOr(StatusOr&&) = default;
|
|
|
|
StatusOr& operator=(StatusOr&&) = default;
|
|
|
|
|
2020-04-28 21:12:31 +08:00
|
|
|
// Not implemented:
|
|
|
|
// template <typename U> StatusOr(const StatusOr<U>& other)
|
|
|
|
// template <typename U> StatusOr(StatusOr<U>&& other)
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
StatusOr& operator=(const StatusOr<U>& other) {
|
2020-04-28 21:12:31 +08:00
|
|
|
if (other.ok()) {
|
|
|
|
variant_ = other.value();
|
|
|
|
} else {
|
|
|
|
variant_ = other.status();
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:40:18 +08:00
|
|
|
template <typename U>
|
|
|
|
StatusOr& operator=(StatusOr<U>&& other) {
|
2020-04-28 21:12:31 +08:00
|
|
|
if (other.ok()) {
|
|
|
|
variant_ = std::move(other).value();
|
|
|
|
} else {
|
|
|
|
variant_ = std::move(other).status();
|
|
|
|
}
|
2020-04-01 19:40:18 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
StatusOr(const T& value) : variant_(value) {}
|
|
|
|
|
|
|
|
StatusOr(const absl::Status& status) : variant_(status) { EnsureNotOk(); }
|
|
|
|
|
|
|
|
// Not implemented:
|
2020-04-28 21:12:31 +08:00
|
|
|
// template <typename U = T> StatusOr& operator=(U&& value)
|
2020-04-01 19:40:18 +08:00
|
|
|
|
|
|
|
StatusOr(T&& value) : variant_(std::move(value)) {}
|
|
|
|
|
|
|
|
StatusOr(absl::Status&& value) : variant_(std::move(value)) {}
|
|
|
|
|
|
|
|
StatusOr& operator=(absl::Status&& status) {
|
|
|
|
variant_ = std::move(status);
|
|
|
|
EnsureNotOk();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
explicit StatusOr(absl::in_place_t, Args&&... args)
|
|
|
|
: StatusOr(T(std::forward<Args>(args)...)) {}
|
|
|
|
|
|
|
|
template <typename U, typename... Args>
|
|
|
|
explicit StatusOr(absl::in_place_t, std::initializer_list<U> ilist,
|
|
|
|
Args&&... args)
|
|
|
|
: StatusOr(ilist, U(std::forward<Args>(args)...)) {}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
explicit operator bool() const { return ok(); }
|
2020-04-01 19:40:18 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
ABSL_MUST_USE_RESULT bool ok() const {
|
|
|
|
return absl::holds_alternative<T>(variant_);
|
|
|
|
}
|
|
|
|
|
2020-04-01 19:40:18 +08:00
|
|
|
const absl::Status& status() const& {
|
|
|
|
static const auto* ok_status = new absl::Status();
|
|
|
|
return ok() ? *ok_status : absl::get<absl::Status>(variant_);
|
|
|
|
}
|
|
|
|
|
|
|
|
absl::Status status() && {
|
|
|
|
return ok() ? absl::OkStatus()
|
|
|
|
: std::move(absl::get<absl::Status>(variant_));
|
|
|
|
}
|
|
|
|
|
|
|
|
const T& value() const& {
|
|
|
|
EnsureOk();
|
|
|
|
return absl::get<T>(variant_);
|
|
|
|
}
|
|
|
|
|
|
|
|
T& value() & {
|
|
|
|
EnsureOk();
|
|
|
|
return absl::get<T>(variant_);
|
|
|
|
}
|
|
|
|
|
|
|
|
const T&& value() const&& {
|
|
|
|
EnsureOk();
|
2020-04-28 21:12:31 +08:00
|
|
|
return absl::get<T>(std::move(variant_));
|
2020-04-01 19:40:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
T&& value() && {
|
|
|
|
EnsureOk();
|
2020-04-28 21:12:31 +08:00
|
|
|
return absl::get<T>(std::move(variant_));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const T& ValueOrDie() const& {
|
|
|
|
EnsureOk();
|
|
|
|
return absl::get<T>(variant_);
|
|
|
|
}
|
|
|
|
|
|
|
|
T& ValueOrDie() & {
|
|
|
|
EnsureOk();
|
|
|
|
return absl::get<T>(variant_);
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
T&& ValueOrDie() && {
|
2019-03-19 00:21:48 +08:00
|
|
|
EnsureOk();
|
2020-04-28 21:12:31 +08:00
|
|
|
return absl::get<T>(std::move(variant_));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-04-01 19:40:18 +08:00
|
|
|
const T& operator*() const& {
|
|
|
|
EnsureOk();
|
|
|
|
return absl::get<T>(variant_);
|
|
|
|
}
|
|
|
|
|
|
|
|
T& operator*() & {
|
|
|
|
EnsureOk();
|
|
|
|
return absl::get<T>(variant_);
|
|
|
|
}
|
|
|
|
|
|
|
|
const T&& operator*() const&& {
|
|
|
|
EnsureOk();
|
2020-04-28 21:12:31 +08:00
|
|
|
return absl::get<T>(std::move(variant_));
|
2020-04-01 19:40:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
T&& operator*() && {
|
|
|
|
EnsureOk();
|
2020-04-28 21:12:31 +08:00
|
|
|
return absl::get<T>(std::move(variant_));
|
2020-04-01 19:40:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const T* operator->() const {
|
|
|
|
EnsureOk();
|
|
|
|
return &absl::get<T>(variant_);
|
|
|
|
}
|
|
|
|
|
|
|
|
T* operator->() {
|
|
|
|
EnsureOk();
|
|
|
|
return &absl::get<T>(variant_);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
T value_or(U&& default_value) const& {
|
|
|
|
if (ok()) {
|
|
|
|
return absl::get<T>(variant_);
|
|
|
|
}
|
|
|
|
return std::forward<U>(default_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
T value_or(U&& default_value) && {
|
|
|
|
if (ok()) {
|
2020-04-28 21:12:31 +08:00
|
|
|
return absl::get<T>(std::move(variant_));
|
2020-04-01 19:40:18 +08:00
|
|
|
}
|
|
|
|
return std::forward<U>(default_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IgnoreError() const { /* no-op */
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
T& emplace(Args&&... args) {
|
|
|
|
return variant_.template emplace<T>(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U, typename... Args>
|
|
|
|
T& emplace(std::initializer_list<U> ilist, Args&&... args) {
|
|
|
|
return variant_.template emplace<T>(ilist, std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
private:
|
|
|
|
void EnsureOk() const {
|
|
|
|
if (!ok()) {
|
|
|
|
// GoogleTest needs this exact error message for death tests to work.
|
|
|
|
SAPI_RAW_LOG(FATAL,
|
|
|
|
"Attempting to fetch value instead of handling error %s",
|
|
|
|
status().message());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnsureNotOk() const {
|
|
|
|
if (ok()) {
|
|
|
|
SAPI_RAW_LOG(
|
|
|
|
FATAL,
|
|
|
|
"An OK status is not a valid constructor argument to StatusOr<T>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::variant<absl::Status, T> variant_;
|
2019-03-19 00:21:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace sapi
|
|
|
|
|
|
|
|
#endif // THIRD_PARTY_SAPI_UTIL_STATUSOR_H_
|