nearby: snapshot of cl/304428753

Signed-off-by: Alexey Polyudov <apolyudov@google.com>
Change-Id: I0023ac6e40456fc4a8169c3173bcbff3b5ccc476
This commit is contained in:
Alexey Polyudov
2020-04-04 12:54:05 -07:00
parent 204f76077d
commit d455926d89
87 changed files with 2869 additions and 631 deletions
+33 -11
View File
@@ -1,21 +1,34 @@
#ifndef PLATFORM_EXCEPTION_H_
#define PLATFORM_EXCEPTION_H_
#include <utility>
namespace location {
namespace nearby {
struct Exception {
enum Value {
enum Value : int {
NONE,
IO,
INTERRUPTED,
INVALID_PROTOCOL_BUFFER,
EXECUTION,
// New code should use the kConstants.
// Old CONSTANTS are deprecated, and should not be used.
kFailed = -1, // Initial value of Exception; any unknown error.
kSuccess = NONE, // No exception.
kIo = IO, // IO Error happened.
kInterrupted = INTERRUPTED, // Operation was interrupted.
kInvalidProtocolBuffer = INVALID_PROTOCOL_BUFFER, // Couldn't parse.
kExecution = EXECUTION, // Couldn't execute.
kTimeout, // Operarion did not finish within specified time.
};
Value value {kFailed};
};
// ExceptionOr models the concept of the return value of a function that might
// throw an exception.
// ExceptionOr provides experience similar to StatusOr<T> used in
// Google Cloud API, see:
// https://googleapis.github.io/google-cloud-cpp/0.7.0/common/status__or_8h_source.html
//
// If ok() returns true, result() is a usable return value. Otherwise,
// exception() explains why such a value is not present.
@@ -36,22 +49,31 @@ struct Exception {
template <typename T>
class ExceptionOr {
public:
explicit ExceptionOr(T result);
explicit ExceptionOr(Exception::Value exception);
ExceptionOr() = default;
ExceptionOr(T&& result) : result_{std::move(result)}, // NOLINT
exception_{Exception::kSuccess} {}
ExceptionOr(const T& result) : result_{result}, // NOLINT
exception_{Exception::kSuccess} {}
ExceptionOr(Exception::Value exception) : exception_{exception} {} // NOLINT
bool ok() const;
bool ok() const { return exception_.value == Exception::kSuccess; }
T result() const;
Exception::Value exception() const;
T& result() & { return result_; }
const T& result() const & { return result_; }
T&& result() && { return std::move(result_); }
const T&& result() const && { return std::move(result_); }
Exception::Value exception() const { return exception_.value; }
T GetResult() const;
Exception GetException() const;
private:
T result_;
Exception::Value exception_;
Exception exception_ {Exception::kFailed};
};
} // namespace nearby
} // namespace location
#include "platform/exception.cc"
#endif // PLATFORM_EXCEPTION_H_