2019-06-26 22:01:51 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
#include <glog/logging.h>
|
|
|
|
|
|
|
|
namespace storage {
|
|
|
|
|
|
|
|
enum class Error : uint8_t {
|
|
|
|
SERIALIZATION_ERROR,
|
2019-07-01 22:11:12 +08:00
|
|
|
DELETED_OBJECT,
|
2019-07-08 21:10:05 +08:00
|
|
|
VERTEX_HAS_EDGES,
|
2019-06-26 22:01:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename TReturn>
|
|
|
|
class Result final {
|
|
|
|
public:
|
|
|
|
explicit Result(const TReturn &ret) : return_(ret) {}
|
|
|
|
explicit Result(TReturn &&ret) : return_(std::move(ret)) {}
|
|
|
|
explicit Result(const Error &error) : error_(error) {}
|
|
|
|
|
|
|
|
bool IsReturn() const { return return_.has_value(); }
|
|
|
|
bool IsError() const { return error_.has_value(); }
|
|
|
|
|
2019-07-01 22:11:12 +08:00
|
|
|
TReturn &GetReturn() {
|
2019-06-26 22:01:51 +08:00
|
|
|
CHECK(return_) << "The storage result is an error!";
|
2019-07-01 22:11:12 +08:00
|
|
|
return return_.value();
|
2019-06-26 22:01:51 +08:00
|
|
|
}
|
|
|
|
|
2019-07-01 22:11:12 +08:00
|
|
|
const TReturn &GetReturn() const {
|
2019-06-26 22:01:51 +08:00
|
|
|
CHECK(return_) << "The storage result is an error!";
|
2019-07-01 22:11:12 +08:00
|
|
|
return return_.value();
|
2019-06-26 22:01:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Error GetError() const {
|
|
|
|
CHECK(error_) << "The storage result is a return value!";
|
|
|
|
return *error_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::optional<TReturn> return_;
|
|
|
|
std::optional<Error> error_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace storage
|