diff --git a/src/storage/v2/result.hpp b/src/storage/v2/result.hpp index 4f9bee556..273680271 100644 --- a/src/storage/v2/result.hpp +++ b/src/storage/v2/result.hpp @@ -1,85 +1,20 @@ #pragma once -#include +#include -#include +#include "utils/result.hpp" namespace storage { +static_assert(std::is_same_v); + enum class Error : uint8_t { SERIALIZATION_ERROR, DELETED_OBJECT, VERTEX_HAS_EDGES, }; -template -class [[nodiscard]] Result final { - public: - explicit Result(const TValue &value) : value_(value) {} - explicit Result(TValue &&value) : value_(std::move(value)) {} - explicit Result(const Error &error) : error_(error) {} - - bool HasValue() const { return value_.has_value(); } - bool HasError() const { return error_.has_value(); } - - TValue &GetValue() & { - CHECK(value_) << "The storage result is an error!"; - return *value_; - } - - TValue &&GetValue() && { - CHECK(value_) << "The storage result is an error!"; - return std::move(*value_); - } - - const TValue &GetValue() const & { - CHECK(value_) << "The storage result is an error!"; - return *value_; - } - - const TValue &&GetValue() const && { - CHECK(value_) << "The storage result is an error!"; - return std::move(*value_); - } - - TValue &operator*() & { - CHECK(value_) << "The storage result is an error!"; - return *value_; - } - - TValue &&operator*() && { - CHECK(value_) << "The storage result is an error!"; - return std::move(*value_); - } - - const TValue &operator*() const & { - CHECK(value_) << "The storage result is an error!"; - return *value_; - } - - const TValue &&operator*() const && { - CHECK(value_) << "The storage result is an error!"; - return std::move(*value_); - } - - 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_; - } - - Error GetError() const { - CHECK(error_) << "The storage result is a value!"; - return *error_; - } - - private: - std::optional value_; - std::optional error_; -}; +template +using Result = utils::BasicResult; } // namespace storage diff --git a/src/utils/result.hpp b/src/utils/result.hpp new file mode 100644 index 000000000..f4c11d33d --- /dev/null +++ b/src/utils/result.hpp @@ -0,0 +1,96 @@ +/// @file +#pragma once + +#include + +#include + +namespace utils { + +template +class [[nodiscard]] BasicResult final { + public: + explicit BasicResult(const TValue &value) : value_(value) {} + explicit BasicResult(TValue &&value) noexcept : value_(std::move(value)) {} + explicit BasicResult(const TError &error) : error_(error) {} + explicit BasicResult(TError &&error) noexcept : error_(std::move(error)) {} + + bool HasValue() const { return value_.has_value(); } + bool HasError() const { return error_.has_value(); } + + TValue &GetValue() & { + CHECK(value_) << "The storage result is an error!"; + return *value_; + } + + TValue &&GetValue() && { + CHECK(value_) << "The storage result is an error!"; + return std::move(*value_); + } + + const TValue &GetValue() const & { + CHECK(value_) << "The storage result is an error!"; + return *value_; + } + + const TValue &&GetValue() const && { + CHECK(value_) << "The storage result is an error!"; + return std::move(*value_); + } + + TValue &operator*() & { + CHECK(value_) << "The storage result is an error!"; + return *value_; + } + + TValue &&operator*() && { + CHECK(value_) << "The storage result is an error!"; + return std::move(*value_); + } + + const TValue &operator*() const & { + CHECK(value_) << "The storage result is an error!"; + return *value_; + } + + const TValue &&operator*() const && { + CHECK(value_) << "The storage result is an error!"; + return std::move(*value_); + } + + 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_; + } + + TError &GetError() & { + CHECK(error_) << "The storage result is a value!"; + return *error_; + } + + TError &&GetError() && { + CHECK(error_) << "The storage result is a value!"; + return std::move(*error_); + } + + const TError &GetError() const & { + CHECK(error_) << "The storage result is a value!"; + return *error_; + } + + const TError &&GetError() const && { + CHECK(error_) << "The storage result is a value!"; + return std::move(*error_); + } + + private: + std::optional value_; + std::optional error_; +}; + +} // namespace utils