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_
|
|
|
|
|
|
|
|
#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>
|
|
|
|
class StatusOr {
|
|
|
|
public:
|
2020-02-28 01:23:44 +08:00
|
|
|
explicit StatusOr() : variant_(absl::UnknownError("")) {}
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusOr(const absl::Status& status) : variant_(status) { EnsureNotOk(); }
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusOr& operator=(const absl::Status& status) {
|
2019-03-19 00:21:48 +08:00
|
|
|
variant_ = status;
|
|
|
|
EnsureNotOk();
|
|
|
|
}
|
|
|
|
|
|
|
|
StatusOr(const T& value) : variant_{value} {}
|
|
|
|
StatusOr(T&& value) : variant_{std::move(value)} {}
|
|
|
|
|
|
|
|
StatusOr(const StatusOr&) = default;
|
|
|
|
StatusOr& operator=(const StatusOr&) = default;
|
|
|
|
|
|
|
|
StatusOr(StatusOr&&) = default;
|
|
|
|
StatusOr& operator=(StatusOr&&) = default;
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
StatusOr(const StatusOr<U>& other) {
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
StatusOr& operator=(const StatusOr<U>& other) {
|
|
|
|
if (other.ok()) {
|
|
|
|
variant_ = other.ValueOrDie();
|
|
|
|
} else {
|
|
|
|
variant_ = other.status();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit operator bool() const { return ok(); }
|
|
|
|
ABSL_MUST_USE_RESULT bool ok() const {
|
|
|
|
return absl::holds_alternative<T>(variant_);
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status status() const {
|
|
|
|
return ok() ? absl::OkStatus() : absl::get<absl::Status>(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-02-28 01:23:44 +08:00
|
|
|
return std::move(absl::get<T>(variant_));
|
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_
|