86 lines
2.4 KiB
C++
86 lines
2.4 KiB
C++
#ifndef QUERY_RESULT_HPP
|
|
#define QUERY_RESULT_HPP
|
|
|
|
#include <variant>
|
|
|
|
namespace matador::utils {
|
|
|
|
template < typename ValueType, typename ErrorType >
|
|
class [[nodiscard]] result;
|
|
|
|
template <typename ValueType>
|
|
struct is_result : std::false_type {};
|
|
|
|
template < typename ValueType, typename ErrorType >
|
|
struct is_result<result<ValueType, ErrorType>> : std::true_type {};
|
|
|
|
template < typename ValueType >
|
|
class ok
|
|
{
|
|
public:
|
|
explicit constexpr ok(const ValueType &value) : value_(value) {}
|
|
explicit constexpr ok(ValueType &&value) : value_(std::move(value)) {}
|
|
|
|
constexpr ValueType&& release() { return std::move(value_); }
|
|
const ValueType& value() const { return value_; }
|
|
|
|
private:
|
|
ValueType value_;
|
|
};
|
|
|
|
template < typename ErrorType >
|
|
class error
|
|
{
|
|
public:
|
|
explicit constexpr error(const ErrorType &error) : error_(error) {}
|
|
explicit constexpr error(ErrorType &&error) : error_(std::move(error)) {}
|
|
|
|
constexpr ErrorType&& release() { return std::move(error_); }
|
|
const ErrorType& value() const { return error_; }
|
|
|
|
private:
|
|
ErrorType error_;
|
|
};
|
|
|
|
template < typename ValueType, typename ErrorType >
|
|
class result
|
|
{
|
|
public:
|
|
using value_type = ok<ValueType>;
|
|
using error_type = error<ErrorType>;
|
|
|
|
result(value_type value) : result_(value) {} // NOLINT(*-explicit-constructor)
|
|
result(error_type error) : result_(error) {} // NOLINT(*-explicit-constructor)
|
|
|
|
operator bool() const { return is_ok(); } // NOLINT(*-explicit-constructor)
|
|
|
|
[[nodiscard]] bool is_ok() const { return std::holds_alternative<value_type>(result_); }
|
|
[[nodiscard]] bool is_error() const { return std::holds_alternative<error_type>(result_); }
|
|
|
|
ValueType&& release() { return std::get<value_type>(result_).release(); }
|
|
ErrorType&& release_error() { return std::get<error_type>(result_).release(); }
|
|
|
|
const ValueType& value() const { return std::get<value_type>(result_).value(); }
|
|
const ErrorType& err() const { return std::get<error_type>(result_).value(); }
|
|
ErrorType err() { return std::get<error_type>(result_).value(); }
|
|
|
|
constexpr const ValueType* operator->() const { return &value(); }
|
|
constexpr ValueType* operator->() { return &value(); }
|
|
|
|
template<typename SecondValueType, typename SecondErrorType, typename Func>
|
|
result<SecondValueType, SecondErrorType> and_then(Func f) {
|
|
if (is_ok()) {
|
|
return f(value());
|
|
}
|
|
|
|
return *this;
|
|
}
|
|
|
|
private:
|
|
std::variant<value_type, error_type> result_;
|
|
};
|
|
|
|
}
|
|
|
|
#endif //QUERY_RESULT_HPP
|