#ifndef RFL_RESULT_HPP_ #define RFL_RESULT_HPP_ #include #include #include #include #include #include #include namespace rfl { /// To be returned class Error { public: Error(const std::string& _what) : what_(_what) {} ~Error() = default; /// Returns the error message, equivalent to .what() in std::exception. const std::string& what() const { return what_; } private: /// Documents what went wrong std::string what_; }; /// Can be used when we are simply interested in whether an operation was /// successful. struct Nothing {}; /// Helper class to be used to within the Result class to support the operator*. /// This is necessary, because we only want this class to be unwrapped, not /// normal std::tuple. template struct HelperTuple { using TupleType = std::tuple; TupleType tuple_; }; template struct is_helper_tuple : std::false_type {}; template struct is_helper_tuple> : std::true_type {}; template struct value_type { using Type = T; }; template struct value_type> { using Type = HelperTuple::TupleType; }; /// The Result class is used for monadic error handling. template class Result { static_assert(!std::is_same(), "The result type cannot be Error."); public: using ValueType = typename value_type::Type; Result(const T& _val) : t_or_err_(_val) {} Result(T&& _val) noexcept : t_or_err_(std::forward(_val)) {} Result(const Error& _err) : t_or_err_(_err) {} Result(Error&& _err) noexcept : t_or_err_(std::forward(_err)) {} template , bool>::type = true> Result(const Result& _other) : t_or_err_( _other.transform([](const U& _u) { return T(_u); }).t_or_err_) {} ~Result() = default; /// Returns Result, if successful and error otherwise. /// Inspired by .and(...) in the Rust std::result type. template Result and_other(const Result& _r) const noexcept { const auto f = [&](const auto& _) { return _r; }; return and_then(f); } /// Monadic operation - F must be a function of type T -> Result. template auto and_then(const F& _f) { const auto apply = [&_f](auto&& _t) { if constexpr (is_helper_tuple()) { using TupleType = std::decay_t; return std::apply(_f, std::forward(_t.tuple_)); } else { return _f(_t); } }; /// Result_U is expected to be of type Result. using Result_U = typename std::invoke_result::type; const auto handle_variant = [apply](TOrError&& _t_or_err) -> Result_U { if constexpr (!std::is_same, Error>()) { return apply(std::forward(_t_or_err)); } else { return _t_or_err; } }; return std::visit(handle_variant, t_or_err_); } /// Monadic operation - F must be a function of type T -> Result. template auto and_then(const F& _f) const { const auto apply = [&_f](const auto& _t) { if constexpr (is_helper_tuple()) { return std::apply(_f, _t.tuple_); } else { return _f(_t); } }; /// Result_U is expected to be of type Result. using Result_U = typename std::invoke_result::type; const auto handle_variant = [apply](const TOrError& _t_or_err) -> Result_U { if constexpr (!std::is_same()) { return apply(_t_or_err); } else { return _t_or_err; } }; return std::visit(handle_variant, t_or_err_); } /// Results types can be iterated over, which even make it possible to use /// them within a std::range. ValueType* begin() noexcept { const auto get_ptr = [this](const TOrError& _t_or_err) -> ValueType* { if constexpr (!std::is_same()) { if constexpr (is_helper_tuple()) { return &_t_or_err.tuple_; } else { return &_t_or_err; } } else { return nullptr; } }; return std::visit(get_ptr, t_or_err_); } /// Results types can be iterated over, which even make it possible to use /// them within a std::range. const ValueType* begin() const noexcept { const auto get_ptr = [this]( const TOrError& _t_or_err) -> const ValueType* { if constexpr (!std::is_same()) { if constexpr (is_helper_tuple()) { return &_t_or_err.tuple_; } else { return &_t_or_err; } } else { return nullptr; } }; return std::visit(get_ptr, t_or_err_); } /// Results types can be iterated over, which even make it possible to use /// them within a std::range. ValueType* end() noexcept { const auto get_ptr = [this](const TOrError& _t_or_err) -> ValueType* { if constexpr (!std::is_same()) { if constexpr (is_helper_tuple()) { return &_t_or_err.tuple_ + 1; } else { return &_t_or_err + 1; } } else { return nullptr; } }; return std::visit(get_ptr, t_or_err_); } /// Results types can be iterated over, which even make it possible to use /// them within a std::range. const ValueType* end() const noexcept { const auto get_ptr = [this]( const TOrError& _t_or_err) -> const ValueType* { if constexpr (!std::is_same()) { if constexpr (is_helper_tuple()) { return &_t_or_err.tuple_ + 1; } else { return &_t_or_err + 1; } } else { return nullptr; } }; return std::visit(get_ptr, t_or_err_); } /// Returns an std::optional if this does in fact contain an error /// or std::nullopt otherwise. std::optional error() const noexcept { const auto get_err = [this]( const TOrError& _t_or_err) -> std::optional { if constexpr (!std::is_same()) { return std::nullopt; } else { return _t_or_err; } }; return std::visit(get_err, t_or_err_); } /// Returns true if the result contains a value, false otherwise. operator bool() const noexcept { return !std::holds_alternative(t_or_err_); } /// Allows access to the underlying value. Careful: Will result in undefined /// behavior, if the result contains an error. T& operator*() { return *std::get_if(&t_or_err_); } /// Allows read access to the underlying value. Careful: Will result in /// undefined behavior, if the result contains an error. const T& operator*() const { return *std::get_if(&t_or_err_); } /// Assigns the underlying object. template , bool>::type = true> inline void operator=(const Result& _other) { const auto to_t = [](const U& _u) -> T { return _u; }; t_or_err_ = _other.transform(to_t).t_or_err_; } /// Expects a function that takes of type Error -> Result and returns /// Result. template Result or_else(const F& _f) const { const auto handle_variant = [&_f](const TOrError& _t_or_err) -> Result { if constexpr (std::is_same()) { return _f(_t_or_err); } else { return _t_or_err; } }; return std::visit(handle_variant, t_or_err_); } /// Returns the value contained if successful or the provided result r if /// not. Result or_other(const Result& _r) const noexcept { const auto f = [&](const auto& _) { return _r; }; return or_else(f); } /// Functor operation - F must be a function of type T -> U. template auto transform(const F& _f) { const auto apply = [&_f](auto&& _t) { if constexpr (is_helper_tuple()) { using TupleType = std::decay_t; return std::apply(_f, std::forward(_t.tuple_)); } else { return _f(std::forward(_t)); } }; /// Result_U is expected to be of type Result. using U = typename std::invoke_result::type; const auto handle_variant = [apply](TOrError&& _t_or_err) -> rfl::Result { if constexpr (!std::is_same, Error>()) { return apply(std::forward(_t_or_err)); } else { return _t_or_err; } }; return std::visit(handle_variant, t_or_err_); } /// Functor operation - F must be a function of type T -> U. template auto transform(const F& _f) const { const auto apply = [&_f](const auto& _t) { if constexpr (is_helper_tuple()) { return std::apply(_f, _t.tuple_); } else { return _f(_t); } }; /// Result_U is expected to be of type Result. using U = typename std::invoke_result::type; const auto handle_variant = [apply]( const TOrError& _t_or_err) -> rfl::Result { if constexpr (!std::is_same()) { return apply(_t_or_err); } else { return _t_or_err; } }; return std::visit(handle_variant, t_or_err_); } /// Returns the value if the result does not contain an error, throws an /// exceptions if not. Similar to .unwrap() in Rust. ValueType& value() { const auto handle_variant = [&](TOrError& _t_or_err) -> ValueType& { if constexpr (!std::is_same()) { if constexpr (is_helper_tuple()) { return _t_or_err.tuple_; } else { return _t_or_err; } } else { throw std::runtime_error(_t_or_err.what()); } }; return std::visit(handle_variant, t_or_err_); } /// Returns the value if the result does not contain an error, throws an /// exceptions if not. Similar to .unwrap() in Rust. const ValueType& value() const { const auto handle_variant = [&](const TOrError& _t_or_err) -> const ValueType& { if constexpr (!std::is_same()) { if constexpr (is_helper_tuple()) { return _t_or_err.tuple_; } else { return _t_or_err; } } else { throw std::runtime_error(_t_or_err.what()); } }; return std::visit(handle_variant, t_or_err_); } /// Returns the value or a default. T value_or(const T& _default) const noexcept { const auto handle_variant = [&](const TOrError& _t_or_err) -> T { if constexpr (!std::is_same()) { return _t_or_err; } else { return _default; } }; return std::visit(handle_variant, t_or_err_); } private: /// The underlying variant, can either be T or Error. std::variant t_or_err_; }; /// operator* allows to combine result types as a product type. template inline auto operator*(const Result& _rt, const Result& _ru) { const auto f1 = [&_ru](const T& _t) { const auto f2 = [&_t](const U& _u) { return HelperTuple(std::make_tuple(_t, _u)); }; return _ru.transform(f2); }; return _rt.and_then(f1); } template inline auto operator*(const Result>& _rt, const Result>& _ru) { const auto f1 = [&_ru](const T&... _t) { const auto f2 = [&](const U&... _u) { return HelperTuple(std::make_tuple(_t..., _u...)); }; return _ru.transform(f2); }; return _rt.and_then(f1); } template inline auto operator*(const Result>& _rt, const Result& _ru) { const auto f = [](const U& _u) { return HelperTuple(std::make_tuple(_u)); }; return _rt * _ru.transform(f); } template inline auto operator*(const Result& _rt, const Result>& _ru) { const auto f = [](const T& _t) { return HelperTuple(std::make_tuple(_t)); }; return _rt.transform(f) * _ru; } template inline auto operator*(const Result& _rt, const U& _u) { return _rt * Result(_u); } template inline auto operator*(const T& _t, const Result& _ru) { return Result(_t) * _ru; } template inline auto operator*(Result&& _rt, Result&& _ru) { const auto f1 = [&_ru](T&& _t) { const auto f2 = [&_t](U&& _u) { return HelperTuple(std::forward_as_tuple(_t, _u)); }; return _ru.transform(f2); }; return _rt.and_then(f1); } template inline auto operator*(Result>&& _rt, Result>&& _ru) { const auto f1 = [&_ru](T&&... _t) { const auto f2 = [&](U&&... _u) { return HelperTuple(std::forward_as_tuple(_t..., _u...)); }; return _ru.transform(f2); }; return _rt.and_then(f1); } template inline auto operator*(Result>&& _rt, Result&& _ru) { const auto f = [](U&& _u) { return HelperTuple(std::forward_as_tuple(_u)); }; return _rt * _ru.transform(f); } template inline auto operator*(Result&& _rt, Result>&& _ru) { const auto f = [](T&& _t) { return HelperTuple(std::forward_as_tuple(_t)); }; return _rt.transform(f) * _ru; } template inline auto operator*(Result&& _rt, U&& _u) { return _rt * Result(_u); } template inline auto operator*(T&& _t, Result&& _ru) { return Result(_t) * _ru; } } // namespace rfl #endif