Files
nearby/cpp/platform/exception.h
T
Alexey Polyudov 204f76077d nearby: snapshot as of cl/296436629
Signed-off-by: Alexey Polyudov <apolyudov@google.com>
Change-Id: I2cf5bf225b76f4c1541954651f3a7544a14e0cec
2020-04-04 12:52:31 -07:00

58 lines
1.2 KiB
C++

#ifndef PLATFORM_EXCEPTION_H_
#define PLATFORM_EXCEPTION_H_
namespace location {
namespace nearby {
struct Exception {
enum Value {
NONE,
IO,
INTERRUPTED,
INVALID_PROTOCOL_BUFFER,
EXECUTION,
};
};
// ExceptionOr models the concept of the return value of a function that might
// throw an exception.
//
// If ok() returns true, result() is a usable return value. Otherwise,
// exception() explains why such a value is not present.
//
// A typical pattern of usage is as follows:
//
// if (!e.ok()) {
// if (Exception::EXCEPTION_TYPE_1 == e.exception()) {
// // Handle Exception::EXCEPTION_TYPE_1.
// } else if (Exception::EXCEPTION_TYPE_2 == e.exception()) {
// // Handle Exception::EXCEPTION_TYPE_2.
// }
//
// return;
// }
//
// // Use e.result().
template <typename T>
class ExceptionOr {
public:
explicit ExceptionOr(T result);
explicit ExceptionOr(Exception::Value exception);
bool ok() const;
T result() const;
Exception::Value exception() const;
private:
T result_;
Exception::Value exception_;
};
} // namespace nearby
} // namespace location
#include "platform/exception.cc"
#endif // PLATFORM_EXCEPTION_H_