/* * libfud * Copyright 2024 Dominick Allen * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef FUD_RESULT_HPP #define FUD_RESULT_HPP #include #include namespace fud { /** \brief A result type which contains either a T on success or an E on error. */ template class [[nodiscard]] Result { public: using ResultType = Result; constexpr Result(const T& value) : m_value{value} { } constexpr Result(const E& value) : m_value{value} { } constexpr Result(T&& value) : m_value{std::move(value)} { } constexpr Result(E&& value) : m_value{std::move(value)} { } static constexpr ResultType okay(const T& okay) { return ResultType{okay}; } static constexpr ResultType okay(T&& okay) { return ResultType{std::move(okay)}; } static constexpr ResultType error(const E& error) { return ResultType{error}; } static constexpr ResultType error(E&& error) { return ResultType{std::move(error)}; } template static constexpr ResultType okay(const Result& okayRes) { return ResultType{okayRes.getOkay()}; } template static constexpr ResultType okay(Result&& okayRes) { return ResultType{okayRes.takeOkay()}; } template static constexpr ResultType error(const Result& errorRes) { return ResultType{errorRes.getError()}; } template static constexpr ResultType error(Result&& errorRes) { return ResultType{errorRes.takeError()}; } [[nodiscard]] constexpr bool isOkay() const { return (m_value.index() == 0); } [[nodiscard]] constexpr bool isError() const { return (m_value.index() == 1); } [[nodiscard]] constexpr const T& getOkay() const& { return std::get(m_value); } [[nodiscard]] constexpr const T& getOkayOr(const T& alternative) const& { if (!isOkay()) { return alternative; } return std::get(m_value); } [[nodiscard]] constexpr const E& getError() const& { return std::get(m_value); } [[nodiscard]] constexpr const E& getErrorOr(const E& alternative) const& { if (!isError()) { return alternative; } return std::get(m_value); } [[nodiscard]] constexpr T&& takeOkay() { return std::move(std::get(m_value)); } [[nodiscard]] constexpr T&& takeOkayOr(T&& alternative) { if (!isOkay()) { return std::move(alternative); } return std::move(std::get(m_value)); } [[nodiscard]] constexpr E&& takeError() { return std::move(std::get(m_value)); } [[nodiscard]] constexpr E&& takeErrorOr(E&& alternative) { if (!isError()) { return std::move(alternative); } return std::move(std::get(m_value)); } private: constexpr Result() : m_value() { } std::variant m_value; }; #define M_TakeOrReturn(HYGIENE_EXPRESSION) \ ({ \ auto HYGIENE_RESULT{(HYGIENE_EXPRESSION)}; \ if (HYGIENE_RESULT.isError()) { \ return HYGIENE_RESULT.takeError(); \ } \ HYGIENE_RESULT.takeOkay(); \ }) } // namespace fud #endif