// SPDX-FileCopyrightText: 2022 Kitsune Ral // SPDX-License-Identifier: LGPL-2.1-or-later #pragma once #include namespace Quotient { //! \brief A minimal subset of std::expected from C++23 template , bool> = true> class Expected { private: template using enable_if_constructible_t = std::enable_if_t< std::is_constructible_v || std::is_constructible_v>; public: using value_type = T; using error_type = E; Expected() = default; Expected(const Expected&) = default; Expected(Expected&&) noexcept = default; ~Expected() = default; template > QUO_IMPLICIT Expected(X&& x) // NOLINT(google-explicit-constructor) : data(std::forward(x)) {} Expected& operator=(const Expected&) = default; Expected& operator=(Expected&&) noexcept = default; template > Expected& operator=(X&& x) { data = std::forward(x); return *this; } bool has_value() const { return std::holds_alternative(data); } explicit operator bool() const { return has_value(); } const value_type& value() const& { return std::get(data); } value_type& value() & { return std::get(data); } value_type value() && { return std::get(std::move(data)); } const value_type& operator*() const& { return value(); } value_type& operator*() & { return value(); } const value_type* operator->() const& { return std::get_if(&data); } value_type* operator->() & { return std::get_if(&data); } template T value_or(U&& fallback) const& { if (has_value()) return value(); return std::forward(fallback); } template T value_or(U&& fallback) && { if (has_value()) return value(); return std::forward(fallback); } const E& error() const& { return std::get(data); } E& error() & { return std::get(data); } private: std::variant data; }; } // namespace Quotient