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
|
|
|
};
|
|
|
|
|
2019-07-17 16:32:06 +08:00
|
|
|
template <typename TValue>
|
2019-07-17 16:57:31 +08:00
|
|
|
class [[nodiscard]] Result final {
|
2019-06-26 22:01:51 +08:00
|
|
|
public:
|
2019-07-17 16:32:06 +08:00
|
|
|
explicit Result(const TValue &value) : value_(value) {}
|
|
|
|
explicit Result(TValue &&value) : value_(std::move(value)) {}
|
2019-06-26 22:01:51 +08:00
|
|
|
explicit Result(const Error &error) : error_(error) {}
|
|
|
|
|
2019-07-17 16:32:06 +08:00
|
|
|
bool HasValue() const { return value_.has_value(); }
|
|
|
|
bool HasError() const { return error_.has_value(); }
|
2019-06-26 22:01:51 +08:00
|
|
|
|
2019-07-25 21:45:18 +08:00
|
|
|
TValue &GetValue() & {
|
2019-07-17 16:32:06 +08:00
|
|
|
CHECK(value_) << "The storage result is an error!";
|
|
|
|
return *value_;
|
2019-06-26 22:01:51 +08:00
|
|
|
}
|
|
|
|
|
2019-07-25 21:45:18 +08:00
|
|
|
TValue &&GetValue() && {
|
|
|
|
CHECK(value_) << "The storage result is an error!";
|
|
|
|
return std::move(*value_);
|
|
|
|
}
|
|
|
|
|
|
|
|
const TValue &GetValue() const & {
|
2019-07-17 16:32:06 +08:00
|
|
|
CHECK(value_) << "The storage result is an error!";
|
|
|
|
return *value_;
|
|
|
|
}
|
|
|
|
|
2019-07-25 21:45:18 +08:00
|
|
|
const TValue &&GetValue() const && {
|
|
|
|
CHECK(value_) << "The storage result is an error!";
|
|
|
|
return std::move(*value_);
|
|
|
|
}
|
|
|
|
|
|
|
|
TValue &operator*() & {
|
2019-07-17 16:32:06 +08:00
|
|
|
CHECK(value_) << "The storage result is an error!";
|
|
|
|
return *value_;
|
|
|
|
}
|
|
|
|
|
2019-07-25 21:45:18 +08:00
|
|
|
TValue &&operator*() && {
|
|
|
|
CHECK(value_) << "The storage result is an error!";
|
|
|
|
return std::move(*value_);
|
|
|
|
}
|
|
|
|
|
|
|
|
const TValue &operator*() const & {
|
2019-07-17 16:32:06 +08:00
|
|
|
CHECK(value_) << "The storage result is an error!";
|
|
|
|
return *value_;
|
|
|
|
}
|
|
|
|
|
2019-07-25 21:45:18 +08:00
|
|
|
const TValue &&operator*() const && {
|
|
|
|
CHECK(value_) << "The storage result is an error!";
|
|
|
|
return std::move(*value_);
|
|
|
|
}
|
|
|
|
|
2019-07-17 16:32:06 +08:00
|
|
|
TValue *operator->() {
|
|
|
|
CHECK(value_) << "The storage result is an error!";
|
|
|
|
return &*value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TValue *operator->() const {
|
|
|
|
CHECK(value_) << "The storage result is an error!";
|
|
|
|
return &*value_;
|
2019-06-26 22:01:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Error GetError() const {
|
2019-07-17 16:32:06 +08:00
|
|
|
CHECK(error_) << "The storage result is a value!";
|
2019-06-26 22:01:51 +08:00
|
|
|
return *error_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-07-17 16:32:06 +08:00
|
|
|
std::optional<TValue> value_;
|
2019-06-26 22:01:51 +08:00
|
|
|
std::optional<Error> error_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace storage
|