diff --git a/src/common/expected.h b/src/common/expected.h deleted file mode 100644 index 5fccfbcbdd..0000000000 --- a/src/common/expected.h +++ /dev/null @@ -1,986 +0,0 @@ -// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project -// SPDX-License-Identifier: GPL-2.0-or-later - -// This is based on the proposed implementation of std::expected (P0323) -// https://github.com/TartanLlama/expected/blob/master/include/tl/expected.hpp - -#pragma once - -#include -#include - -namespace Common { - -template -class Expected; - -template -class Unexpected { -public: - Unexpected() = delete; - - constexpr explicit Unexpected(const E& e) : m_val{e} {} - - constexpr explicit Unexpected(E&& e) : m_val{std::move(e)} {} - - constexpr E& value() & { - return m_val; - } - - constexpr const E& value() const& { - return m_val; - } - - constexpr E&& value() && { - return std::move(m_val); - } - - constexpr const E&& value() const&& { - return std::move(m_val); - } - -private: - E m_val; -}; - -template -constexpr auto operator<=>(const Unexpected& lhs, const Unexpected& rhs) { - return lhs.value() <=> rhs.value(); -} - -struct unexpect_t { - constexpr explicit unexpect_t() = default; -}; - -namespace detail { - -struct no_init_t { - constexpr explicit no_init_t() = default; -}; - -/** - * This specialization is for when T is not trivially destructible, - * so the destructor must be called on destruction of `expected' - * Additionally, this requires E to be trivially destructible - */ -template > - requires std::is_trivially_destructible_v -struct expected_storage_base { - constexpr expected_storage_base() : m_val{T{}}, m_has_val{true} {} - - constexpr expected_storage_base(no_init_t) : m_has_val{false} {} - - template >* = nullptr> - constexpr expected_storage_base(std::in_place_t, Args&&... args) - : m_val{std::forward(args)...}, m_has_val{true} {} - - template &, Args&&...>>* = - nullptr> - constexpr expected_storage_base(std::in_place_t, std::initializer_list il, Args&&... args) - : m_val{il, std::forward(args)...}, m_has_val{true} {} - - template >* = nullptr> - constexpr explicit expected_storage_base(unexpect_t, Args&&... args) - : m_unexpect{std::forward(args)...}, m_has_val{false} {} - - template &, Args&&...>>* = - nullptr> - constexpr explicit expected_storage_base(unexpect_t, std::initializer_list il, - Args&&... args) - : m_unexpect{il, std::forward(args)...}, m_has_val{false} {} - - ~expected_storage_base() { - if (m_has_val) { - m_val.~T(); - } - } - - union { - T m_val; - Unexpected m_unexpect; - }; - - bool m_has_val; -}; - -/** - * This specialization is for when T is trivially destructible, - * so the destructor of `expected` can be trivial - * Additionally, this requires E to be trivially destructible - */ -template - requires std::is_trivially_destructible_v -struct expected_storage_base { - constexpr expected_storage_base() : m_val{T{}}, m_has_val{true} {} - - constexpr expected_storage_base(no_init_t) : m_has_val{false} {} - - template >* = nullptr> - constexpr expected_storage_base(std::in_place_t, Args&&... args) - : m_val{std::forward(args)...}, m_has_val{true} {} - - template &, Args&&...>>* = - nullptr> - constexpr expected_storage_base(std::in_place_t, std::initializer_list il, Args&&... args) - : m_val{il, std::forward(args)...}, m_has_val{true} {} - - template >* = nullptr> - constexpr explicit expected_storage_base(unexpect_t, Args&&... args) - : m_unexpect{std::forward(args)...}, m_has_val{false} {} - - template &, Args&&...>>* = - nullptr> - constexpr explicit expected_storage_base(unexpect_t, std::initializer_list il, - Args&&... args) - : m_unexpect{il, std::forward(args)...}, m_has_val{false} {} - - ~expected_storage_base() = default; - - union { - T m_val; - Unexpected m_unexpect; - }; - - bool m_has_val; -}; - -template -struct expected_operations_base : expected_storage_base { - using expected_storage_base::expected_storage_base; - - template - void construct(Args&&... args) noexcept { - new (std::addressof(this->m_val)) T{std::forward(args)...}; - this->m_has_val = true; - } - - template - void construct_with(Rhs&& rhs) noexcept { - new (std::addressof(this->m_val)) T{std::forward(rhs).get()}; - this->m_has_val = true; - } - - template - void construct_error(Args&&... args) noexcept { - new (std::addressof(this->m_unexpect)) Unexpected{std::forward(args)...}; - this->m_has_val = false; - } - - void assign(const expected_operations_base& rhs) noexcept { - if (!this->m_has_val && rhs.m_has_val) { - geterr().~Unexpected(); - construct(rhs.get()); - } else { - assign_common(rhs); - } - } - - void assign(expected_operations_base&& rhs) noexcept { - if (!this->m_has_val && rhs.m_has_val) { - geterr().~Unexpected(); - construct(std::move(rhs).get()); - } else { - assign_common(rhs); - } - } - - template - void assign_common(Rhs&& rhs) { - if (this->m_has_val) { - if (rhs.m_has_val) { - get() = std::forward(rhs).get(); - } else { - destroy_val(); - construct_error(std::forward(rhs).geterr()); - } - } else { - if (!rhs.m_has_val) { - geterr() = std::forward(rhs).geterr(); - } - } - } - - bool has_value() const { - return this->m_has_val; - } - - constexpr T& get() & { - return this->m_val; - } - - constexpr const T& get() const& { - return this->m_val; - } - - constexpr T&& get() && { - return std::move(this->m_val); - } - - constexpr const T&& get() const&& { - return std::move(this->m_val); - } - - constexpr Unexpected& geterr() & { - return this->m_unexpect; - } - - constexpr const Unexpected& geterr() const& { - return this->m_unexpect; - } - - constexpr Unexpected&& geterr() && { - return std::move(this->m_unexpect); - } - - constexpr const Unexpected&& geterr() const&& { - return std::move(this->m_unexpect); - } - - constexpr void destroy_val() { - get().~T(); - } -}; - -/** - * This manages conditionally having a trivial copy constructor - * This specialization is for when T is trivially copy constructible - * Additionally, this requires E to be trivially copy constructible - */ -template > - requires std::is_trivially_copy_constructible_v -struct expected_copy_base : expected_operations_base { - using expected_operations_base::expected_operations_base; -}; - -/** - * This specialization is for when T is not trivially copy constructible - * Additionally, this requires E to be trivially copy constructible - */ -template - requires std::is_trivially_copy_constructible_v -struct expected_copy_base : expected_operations_base { - using expected_operations_base::expected_operations_base; - - expected_copy_base() = default; - - expected_copy_base(const expected_copy_base& rhs) - : expected_operations_base{no_init_t{}} { - if (rhs.has_value()) { - this->construct_with(rhs); - } else { - this->construct_error(rhs.geterr()); - } - } - - expected_copy_base(expected_copy_base&&) = default; - - expected_copy_base& operator=(const expected_copy_base&) = default; - - expected_copy_base& operator=(expected_copy_base&&) = default; -}; - -/** - * This manages conditionally having a trivial move constructor - * This specialization is for when T is trivially move constructible - * Additionally, this requires E to be trivially move constructible - */ -template > - requires std::is_trivially_move_constructible_v -struct expected_move_base : expected_copy_base { - using expected_copy_base::expected_copy_base; -}; - -/** - * This specialization is for when T is not trivially move constructible - * Additionally, this requires E to be trivially move constructible - */ -template - requires std::is_trivially_move_constructible_v -struct expected_move_base : expected_copy_base { - using expected_copy_base::expected_copy_base; - - expected_move_base() = default; - - expected_move_base(const expected_move_base&) = default; - - expected_move_base(expected_move_base&& rhs) noexcept(std::is_nothrow_move_constructible_v) - : expected_copy_base{no_init_t{}} { - if (rhs.has_value()) { - this->construct_with(std::move(rhs)); - } else { - this->construct_error(std::move(rhs.geterr())); - } - } - - expected_move_base& operator=(const expected_move_base&) = default; - - expected_move_base& operator=(expected_move_base&&) = default; -}; - -/** - * This manages conditionally having a trivial copy assignment operator - * This specialization is for when T is trivially copy assignable - * Additionally, this requires E to be trivially copy assignable - */ -template , - std::is_trivially_copy_constructible, - std::is_trivially_destructible>> - requires std::conjunction_v, - std::is_trivially_copy_constructible, - std::is_trivially_destructible> -struct expected_copy_assign_base : expected_move_base { - using expected_move_base::expected_move_base; -}; - -/** - * This specialization is for when T is not trivially copy assignable - * Additionally, this requires E to be trivially copy assignable - */ -template - requires std::conjunction_v, - std::is_trivially_copy_constructible, - std::is_trivially_destructible> -struct expected_copy_assign_base : expected_move_base { - using expected_move_base::expected_move_base; - - expected_copy_assign_base() = default; - - expected_copy_assign_base(const expected_copy_assign_base&) = default; - - expected_copy_assign_base(expected_copy_assign_base&&) = default; - - expected_copy_assign_base& operator=(const expected_copy_assign_base& rhs) { - this->assign(rhs); - return *this; - } - - expected_copy_assign_base& operator=(expected_copy_assign_base&&) = default; -}; - -/** - * This manages conditionally having a trivial move assignment operator - * This specialization is for when T is trivially move assignable - * Additionally, this requires E to be trivially move assignable - */ -template , - std::is_trivially_move_constructible, - std::is_trivially_destructible>> - requires std::conjunction_v, - std::is_trivially_move_constructible, - std::is_trivially_destructible> -struct expected_move_assign_base : expected_copy_assign_base { - using expected_copy_assign_base::expected_copy_assign_base; -}; - -/** - * This specialization is for when T is not trivially move assignable - * Additionally, this requires E to be trivially move assignable - */ -template - requires std::conjunction_v, - std::is_trivially_move_constructible, - std::is_trivially_destructible> -struct expected_move_assign_base : expected_copy_assign_base { - using expected_copy_assign_base::expected_copy_assign_base; - - expected_move_assign_base() = default; - - expected_move_assign_base(const expected_move_assign_base&) = default; - - expected_move_assign_base(expected_move_assign_base&&) = default; - - expected_move_assign_base& operator=(const expected_move_assign_base&) = default; - - expected_move_assign_base& operator=(expected_move_assign_base&& rhs) noexcept( - std::conjunction_v, - std::is_nothrow_move_assignable>) { - this->assign(std::move(rhs)); - return *this; - } -}; - -/** - * expected_delete_ctor_base will conditionally delete copy and move constructors - * depending on whether T is copy/move constructible - * Additionally, this requires E to be copy/move constructible - */ -template , - bool EnableMove = std::is_move_constructible_v> - requires std::conjunction_v, std::is_move_constructible> -struct expected_delete_ctor_base { - expected_delete_ctor_base() = default; - expected_delete_ctor_base(const expected_delete_ctor_base&) = default; - expected_delete_ctor_base(expected_delete_ctor_base&&) noexcept = default; - expected_delete_ctor_base& operator=(const expected_delete_ctor_base&) = default; - expected_delete_ctor_base& operator=(expected_delete_ctor_base&&) noexcept = default; -}; - -template - requires std::conjunction_v, std::is_move_constructible> -struct expected_delete_ctor_base { - expected_delete_ctor_base() = default; - expected_delete_ctor_base(const expected_delete_ctor_base&) = default; - expected_delete_ctor_base(expected_delete_ctor_base&&) noexcept = delete; - expected_delete_ctor_base& operator=(const expected_delete_ctor_base&) = default; - expected_delete_ctor_base& operator=(expected_delete_ctor_base&&) noexcept = default; -}; - -template - requires std::conjunction_v, std::is_move_constructible> -struct expected_delete_ctor_base { - expected_delete_ctor_base() = default; - expected_delete_ctor_base(const expected_delete_ctor_base&) = delete; - expected_delete_ctor_base(expected_delete_ctor_base&&) noexcept = default; - expected_delete_ctor_base& operator=(const expected_delete_ctor_base&) = default; - expected_delete_ctor_base& operator=(expected_delete_ctor_base&&) noexcept = default; -}; - -template - requires std::conjunction_v, std::is_move_constructible> -struct expected_delete_ctor_base { - expected_delete_ctor_base() = default; - expected_delete_ctor_base(const expected_delete_ctor_base&) = delete; - expected_delete_ctor_base(expected_delete_ctor_base&&) noexcept = delete; - expected_delete_ctor_base& operator=(const expected_delete_ctor_base&) = default; - expected_delete_ctor_base& operator=(expected_delete_ctor_base&&) noexcept = default; -}; - -/** - * expected_delete_assign_base will conditionally delete copy and move assignment operators - * depending on whether T is copy/move constructible + assignable - * Additionally, this requires E to be copy/move constructible + assignable - */ -template < - typename T, typename E, - bool EnableCopy = std::conjunction_v, std::is_copy_assignable>, - bool EnableMove = std::conjunction_v, std::is_move_assignable>> - requires std::conjunction_v, std::is_move_constructible, - std::is_copy_assignable, std::is_move_assignable> -struct expected_delete_assign_base { - expected_delete_assign_base() = default; - expected_delete_assign_base(const expected_delete_assign_base&) = default; - expected_delete_assign_base(expected_delete_assign_base&&) noexcept = default; - expected_delete_assign_base& operator=(const expected_delete_assign_base&) = default; - expected_delete_assign_base& operator=(expected_delete_assign_base&&) noexcept = default; -}; - -template - requires std::conjunction_v, std::is_move_constructible, - std::is_copy_assignable, std::is_move_assignable> -struct expected_delete_assign_base { - expected_delete_assign_base() = default; - expected_delete_assign_base(const expected_delete_assign_base&) = default; - expected_delete_assign_base(expected_delete_assign_base&&) noexcept = default; - expected_delete_assign_base& operator=(const expected_delete_assign_base&) = default; - expected_delete_assign_base& operator=(expected_delete_assign_base&&) noexcept = delete; -}; - -template - requires std::conjunction_v, std::is_move_constructible, - std::is_copy_assignable, std::is_move_assignable> -struct expected_delete_assign_base { - expected_delete_assign_base() = default; - expected_delete_assign_base(const expected_delete_assign_base&) = default; - expected_delete_assign_base(expected_delete_assign_base&&) noexcept = default; - expected_delete_assign_base& operator=(const expected_delete_assign_base&) = delete; - expected_delete_assign_base& operator=(expected_delete_assign_base&&) noexcept = default; -}; - -template - requires std::conjunction_v, std::is_move_constructible, - std::is_copy_assignable, std::is_move_assignable> -struct expected_delete_assign_base { - expected_delete_assign_base() = default; - expected_delete_assign_base(const expected_delete_assign_base&) = default; - expected_delete_assign_base(expected_delete_assign_base&&) noexcept = default; - expected_delete_assign_base& operator=(const expected_delete_assign_base&) = delete; - expected_delete_assign_base& operator=(expected_delete_assign_base&&) noexcept = delete; -}; - -/** - * This is needed to be able to construct the expected_default_ctor_base which follows, - * while still conditionally deleting the default constructor. - */ -struct default_constructor_tag { - constexpr explicit default_constructor_tag() = default; -}; - -/** - * expected_default_ctor_base will ensure that expected - * has a deleted default constructor if T is not default constructible - * This specialization is for when T is default constructible - */ -template > -struct expected_default_ctor_base { - constexpr expected_default_ctor_base() noexcept = default; - constexpr expected_default_ctor_base(expected_default_ctor_base const&) noexcept = default; - constexpr expected_default_ctor_base(expected_default_ctor_base&&) noexcept = default; - expected_default_ctor_base& operator=(expected_default_ctor_base const&) noexcept = default; - expected_default_ctor_base& operator=(expected_default_ctor_base&&) noexcept = default; - - constexpr explicit expected_default_ctor_base(default_constructor_tag) {} -}; - -template -struct expected_default_ctor_base { - constexpr expected_default_ctor_base() noexcept = delete; - constexpr expected_default_ctor_base(expected_default_ctor_base const&) noexcept = default; - constexpr expected_default_ctor_base(expected_default_ctor_base&&) noexcept = default; - expected_default_ctor_base& operator=(expected_default_ctor_base const&) noexcept = default; - expected_default_ctor_base& operator=(expected_default_ctor_base&&) noexcept = default; - - constexpr explicit expected_default_ctor_base(default_constructor_tag) {} -}; - -template -using expected_enable_forward_value = - std::enable_if_t && - !std::is_same_v, std::in_place_t> && - !std::is_same_v, std::remove_cvref_t> && - !std::is_same_v, std::remove_cvref_t>>; - -template -using expected_enable_from_other = std::enable_if_t< - std::is_constructible_v && std::is_constructible_v && - !std::is_constructible_v&> && !std::is_constructible_v&&> && - !std::is_constructible_v&> && - !std::is_constructible_v&&> && - !std::is_convertible_v&, T> && !std::is_convertible_v&&, T> && - !std::is_convertible_v&, T> && - !std::is_convertible_v&&, T>>; - -} // namespace detail - -template -class Expected : private detail::expected_move_assign_base, - private detail::expected_delete_ctor_base, - private detail::expected_delete_assign_base, - private detail::expected_default_ctor_base { -public: - using value_type = T; - using error_type = E; - using unexpected_type = Unexpected; - - constexpr Expected() = default; - constexpr Expected(const Expected&) = default; - constexpr Expected(Expected&&) = default; - Expected& operator=(const Expected&) = default; - Expected& operator=(Expected&&) = default; - - template >* = nullptr> - constexpr Expected(std::in_place_t, Args&&... args) - : impl_base{std::in_place, std::forward(args)...}, - ctor_base{detail::default_constructor_tag{}} {} - - template &, Args&&...>>* = - nullptr> - constexpr Expected(std::in_place_t, std::initializer_list il, Args&&... args) - : impl_base{std::in_place, il, std::forward(args)...}, - ctor_base{detail::default_constructor_tag{}} {} - - template >* = nullptr, - std::enable_if_t>* = nullptr> - constexpr explicit Expected(const Unexpected& e) - : impl_base{unexpect_t{}, e.value()}, ctor_base{detail::default_constructor_tag{}} {} - - template >* = nullptr, - std::enable_if_t>* = nullptr> - constexpr Expected(Unexpected const& e) - : impl_base{unexpect_t{}, e.value()}, ctor_base{detail::default_constructor_tag{}} {} - - template >* = nullptr, - std::enable_if_t>* = nullptr> - constexpr explicit Expected(Unexpected&& e) noexcept(std::is_nothrow_constructible_v) - : impl_base{unexpect_t{}, std::move(e.value())}, ctor_base{ - detail::default_constructor_tag{}} {} - - template >* = nullptr, - std::enable_if_t>* = nullptr> - constexpr Expected(Unexpected&& e) noexcept(std::is_nothrow_constructible_v) - : impl_base{unexpect_t{}, std::move(e.value())}, ctor_base{ - detail::default_constructor_tag{}} {} - - template >* = nullptr> - constexpr explicit Expected(unexpect_t, Args&&... args) - : impl_base{unexpect_t{}, std::forward(args)...}, - ctor_base{detail::default_constructor_tag{}} {} - - template &, Args&&...>>* = - nullptr> - constexpr explicit Expected(unexpect_t, std::initializer_list il, Args&&... args) - : impl_base{unexpect_t{}, il, std::forward(args)...}, - ctor_base{detail::default_constructor_tag{}} {} - - template && - std::is_convertible_v)>* = nullptr, - detail::expected_enable_from_other* = nullptr> - constexpr explicit Expected(const Expected& rhs) - : ctor_base{detail::default_constructor_tag{}} { - if (rhs.has_value()) { - this->construct(*rhs); - } else { - this->construct_error(rhs.error()); - } - } - - template && - std::is_convertible_v)>* = nullptr, - detail::expected_enable_from_other* = nullptr> - constexpr Expected(const Expected& rhs) : ctor_base{detail::default_constructor_tag{}} { - if (rhs.has_value()) { - this->construct(*rhs); - } else { - this->construct_error(rhs.error()); - } - } - - template && std::is_convertible_v)>* = - nullptr, - detail::expected_enable_from_other* = nullptr> - constexpr explicit Expected(Expected&& rhs) - : ctor_base{detail::default_constructor_tag{}} { - if (rhs.has_value()) { - this->construct(std::move(*rhs)); - } else { - this->construct_error(std::move(rhs.error())); - } - } - - template && std::is_convertible_v)>* = - nullptr, - detail::expected_enable_from_other* = nullptr> - constexpr Expected(Expected&& rhs) : ctor_base{detail::default_constructor_tag{}} { - if (rhs.has_value()) { - this->construct(std::move(*rhs)); - } else { - this->construct_error(std::move(rhs.error())); - } - } - - template >* = nullptr, - detail::expected_enable_forward_value* = nullptr> - constexpr explicit Expected(U&& v) : Expected{std::in_place, std::forward(v)} {} - - template >* = nullptr, - detail::expected_enable_forward_value* = nullptr> - constexpr Expected(U&& v) : Expected{std::in_place, std::forward(v)} {} - - template >* = nullptr, - std::enable_if_t<( - !std::is_same_v, std::remove_cvref_t> && - !std::conjunction_v, std::is_same>> && - std::is_constructible_v && std::is_assignable_v && - std::is_nothrow_move_constructible_v)>* = nullptr> - Expected& operator=(U&& v) { - if (has_value()) { - val() = std::forward(v); - } else { - err().~Unexpected(); - new (valptr()) T{std::forward(v)}; - this->m_has_val = true; - } - - return *this; - } - - template >* = nullptr, - std::enable_if_t<( - !std::is_same_v, std::remove_cvref_t> && - !std::conjunction_v, std::is_same>> && - std::is_constructible_v && std::is_assignable_v && - std::is_nothrow_move_constructible_v)>* = nullptr> - Expected& operator=(U&& v) { - if (has_value()) { - val() = std::forward(v); - } else { - auto tmp = std::move(err()); - err().~Unexpected(); - new (valptr()) T{std::forward(v)}; - this->m_has_val = true; - } - - return *this; - } - - template && - std::is_assignable_v>* = nullptr> - Expected& operator=(const Unexpected& rhs) { - if (!has_value()) { - err() = rhs; - } else { - this->destroy_val(); - new (errptr()) Unexpected{rhs}; - this->m_has_val = false; - } - - return *this; - } - - template && - std::is_move_assignable_v>* = nullptr> - Expected& operator=(Unexpected&& rhs) noexcept { - if (!has_value()) { - err() = std::move(rhs); - } else { - this->destroy_val(); - new (errptr()) Unexpected{std::move(rhs)}; - this->m_has_val = false; - } - - return *this; - } - - template >* = nullptr> - void emplace(Args&&... args) { - if (has_value()) { - val() = T{std::forward(args)...}; - } else { - err().~Unexpected(); - new (valptr()) T{std::forward(args)...}; - this->m_has_val = true; - } - } - - template >* = nullptr> - void emplace(Args&&... args) { - if (has_value()) { - val() = T{std::forward(args)...}; - } else { - auto tmp = std::move(err()); - err().~Unexpected(); - new (valptr()) T{std::forward(args)...}; - this->m_has_val = true; - } - } - - template &, - Args&&...>>* = nullptr> - void emplace(std::initializer_list il, Args&&... args) { - if (has_value()) { - T t{il, std::forward(args)...}; - val() = std::move(t); - } else { - err().~Unexpected(); - new (valptr()) T{il, std::forward(args)...}; - this->m_has_val = true; - } - } - - template &, - Args&&...>>* = nullptr> - void emplace(std::initializer_list il, Args&&... args) { - if (has_value()) { - T t{il, std::forward(args)...}; - val() = std::move(t); - } else { - auto tmp = std::move(err()); - err().~Unexpected(); - new (valptr()) T{il, std::forward(args)...}; - this->m_has_val = true; - } - } - - constexpr T* operator->() { - return valptr(); - } - - constexpr const T* operator->() const { - return valptr(); - } - - template - constexpr U& operator*() & { - return val(); - } - - template - constexpr const U& operator*() const& { - return val(); - } - - template - constexpr U&& operator*() && { - return std::move(val()); - } - - template - constexpr const U&& operator*() const&& { - return std::move(val()); - } - - constexpr bool has_value() const noexcept { - return this->m_has_val; - } - - constexpr explicit operator bool() const noexcept { - return this->m_has_val; - } - - template - constexpr U& value() & { - return val(); - } - - template - constexpr const U& value() const& { - return val(); - } - - template - constexpr U&& value() && { - return std::move(val()); - } - - template - constexpr const U&& value() const&& { - return std::move(val()); - } - - constexpr E& error() & { - return err().value(); - } - - constexpr const E& error() const& { - return err().value(); - } - - constexpr E&& error() && { - return std::move(err().value()); - } - - constexpr const E&& error() const&& { - return std::move(err().value()); - } - - template - constexpr T value_or(U&& v) const& { - static_assert(std::is_copy_constructible_v && std::is_convertible_v, - "T must be copy-constructible and convertible from U&&"); - return bool(*this) ? **this : static_cast(std::forward(v)); - } - - template - constexpr T value_or(U&& v) && { - static_assert(std::is_move_constructible_v && std::is_convertible_v, - "T must be move-constructible and convertible from U&&"); - return bool(*this) ? std::move(**this) : static_cast(std::forward(v)); - } - -private: - static_assert(!std::is_reference_v, "T must not be a reference"); - static_assert(!std::is_same_v>, - "T must not be std::in_place_t"); - static_assert(!std::is_same_v>, "T must not be unexpect_t"); - static_assert(!std::is_same_v>>, - "T must not be Unexpected"); - static_assert(!std::is_reference_v, "E must not be a reference"); - - T* valptr() { - return std::addressof(this->m_val); - } - - const T* valptr() const { - return std::addressof(this->m_val); - } - - Unexpected* errptr() { - return std::addressof(this->m_unexpect); - } - - const Unexpected* errptr() const { - return std::addressof(this->m_unexpect); - } - - template - constexpr U& val() { - return this->m_val; - } - - template - constexpr const U& val() const { - return this->m_val; - } - - constexpr Unexpected& err() { - return this->m_unexpect; - } - - constexpr const Unexpected& err() const { - return this->m_unexpect; - } - - using impl_base = detail::expected_move_assign_base; - using ctor_base = detail::expected_default_ctor_base; -}; - -template -constexpr bool operator==(const Expected& lhs, const Expected& rhs) { - return (lhs.has_value() != rhs.has_value()) - ? false - : (!lhs.has_value() ? lhs.error() == rhs.error() : *lhs == *rhs); -} - -template -constexpr bool operator!=(const Expected& lhs, const Expected& rhs) { - return !operator==(lhs, rhs); -} - -template -constexpr bool operator==(const Expected& x, const U& v) { - return x.has_value() ? *x == v : false; -} - -template -constexpr bool operator==(const U& v, const Expected& x) { - return x.has_value() ? *x == v : false; -} - -template -constexpr bool operator!=(const Expected& x, const U& v) { - return !operator==(x, v); -} - -template -constexpr bool operator!=(const U& v, const Expected& x) { - return !operator==(v, x); -} - -template -constexpr bool operator==(const Expected& x, const Unexpected& e) { - return x.has_value() ? false : x.error() == e.value(); -} - -template -constexpr bool operator==(const Unexpected& e, const Expected& x) { - return x.has_value() ? false : x.error() == e.value(); -} - -template -constexpr bool operator!=(const Expected& x, const Unexpected& e) { - return !operator==(x, e); -} - -template -constexpr bool operator!=(const Unexpected& e, const Expected& x) { - return !operator==(e, x); -} - -} // namespace Common diff --git a/src/core/hle/service/sockets/bsd.cpp b/src/core/hle/service/sockets/bsd.cpp index 6053cdd7f2..b8078bfd99 100644 --- a/src/core/hle/service/sockets/bsd.cpp +++ b/src/core/hle/service/sockets/bsd.cpp @@ -24,9 +24,6 @@ #include "network/network.h" #include -using Common::Expected; -using Common::Unexpected; - namespace Service::Sockets { namespace { @@ -464,13 +461,22 @@ void BSD::DuplicateSocket(HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; auto input = rp.PopRaw(); - Expected res = DuplicateSocketImpl(input.fd); IPC::ResponseBuilder rb{ctx, 4}; rb.Push(ResultSuccess); - rb.PushRaw(OutputParameters{ - .ret = res.value_or(0), - .bsd_errno = res ? Errno::SUCCESS : res.error(), - }); + + auto const res_v = DuplicateSocketImpl(input.fd); + if (auto* res = std::get_if(&res_v)) { + rb.PushRaw(OutputParameters{ + .ret = *res, + .bsd_errno = Errno::SUCCESS, + }); + } else { + auto* err = std::get_if(&res_v); + rb.PushRaw(OutputParameters{ + .ret = 0, + .bsd_errno = *err, + }); + } } void BSD::EventFd(HLERequestContext& ctx) { @@ -977,15 +983,15 @@ Errno BSD::CloseImpl(s32 fd) { return bsd_errno; } -Expected BSD::DuplicateSocketImpl(s32 fd) { +std::variant BSD::DuplicateSocketImpl(s32 fd) { if (!IsFileDescriptorValid(fd)) { - return Unexpected(Errno::BADF); + return Errno::BADF; } const s32 new_fd = FindFreeFileDescriptorHandle(); if (new_fd < 0) { LOG_ERROR(Service, "No more file descriptors available"); - return Unexpected(Errno::MFILE); + return Errno::MFILE; } file_descriptors[new_fd] = FileDescriptor{ diff --git a/src/core/hle/service/sockets/bsd.h b/src/core/hle/service/sockets/bsd.h index 32b5bdc3cf..15e4a33592 100644 --- a/src/core/hle/service/sockets/bsd.h +++ b/src/core/hle/service/sockets/bsd.h @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project +// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project // SPDX-License-Identifier: GPL-3.0-or-later // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later @@ -8,9 +8,9 @@ #include #include #include +#include #include "common/common_types.h" -#include "common/expected.h" #include "common/socket_types.h" #include "core/hle/service/service.h" #include "core/hle/service/sockets/sockets.h" @@ -35,7 +35,7 @@ public: // These methods are called from SSL; the first two are also called from // this class for the corresponding IPC methods. // On the real device, the SSL service makes IPC calls to this service. - Common::Expected DuplicateSocketImpl(s32 fd); + std::variant DuplicateSocketImpl(s32 fd); Errno CloseImpl(s32 fd); std::optional> GetSocket(s32 fd); diff --git a/src/core/hle/service/sockets/sfdnsres.cpp b/src/core/hle/service/sockets/sfdnsres.cpp index fa921790e4..d74b913220 100644 --- a/src/core/hle/service/sockets/sfdnsres.cpp +++ b/src/core/hle/service/sockets/sfdnsres.cpp @@ -207,16 +207,15 @@ static std::pair GetHostByNameRequestImpl(HLERequestConte return {0, GetAddrInfoError::AGAIN}; } - auto res = Network::GetAddressInfo(host, /*service*/ std::nullopt); - if (!res.has_value()) { - return {0, Translate(res.error())}; + auto res_v = Network::GetAddressInfo(host, /*service*/ std::nullopt); + if (auto* res = std::get_if>(&res_v)) { + const std::vector data = SerializeAddrInfoAsHostEnt(*res, host); + const u32 data_size = u32(data.size()); + ctx.WriteBuffer(data, 0); + return {data_size, GetAddrInfoError::SUCCESS}; } - - const std::vector data = SerializeAddrInfoAsHostEnt(res.value(), host); - const u32 data_size = static_cast(data.size()); - ctx.WriteBuffer(data, 0); - - return {data_size, GetAddrInfoError::SUCCESS}; + auto* err = std::get_if(&res_v); + return {0, Translate(*err)}; } void SFDNSRES::GetHostByNameRequest(HLERequestContext& ctx) { @@ -332,16 +331,15 @@ static std::pair GetAddrInfoRequestImpl(HLERequestContext // Serialized hints are also passed in a buffer, but are ignored for now. - auto res = Network::GetAddressInfo(host, service); - if (!res.has_value()) { - return {0, Translate(res.error())}; + auto res_v = Network::GetAddressInfo(host, service); + if (auto* res = std::get_if>(&res_v)) { + const std::vector data = SerializeAddrInfo(*res, host); + const u32 data_size = u32(data.size()); + ctx.WriteBuffer(data, 0); + return {data_size, GetAddrInfoError::SUCCESS}; } - - const std::vector data = SerializeAddrInfo(res.value(), host); - const u32 data_size = static_cast(data.size()); - ctx.WriteBuffer(data, 0); - - return {data_size, GetAddrInfoError::SUCCESS}; + auto* err = std::get_if(&res_v); + return {0, Translate(*err)}; } void SFDNSRES::GetAddrInfoRequest(HLERequestContext& ctx) { diff --git a/src/core/hle/service/ssl/ssl.cpp b/src/core/hle/service/ssl/ssl.cpp index b66c686b2a..766673c653 100644 --- a/src/core/hle/service/ssl/ssl.cpp +++ b/src/core/hle/service/ssl/ssl.cpp @@ -160,29 +160,26 @@ private: auto bsd = system.ServiceManager().GetService("bsd:u"); ASSERT_OR_EXECUTE(bsd, { return ResultInternalError; }); - auto res = bsd->DuplicateSocketImpl(fd); - if (!res.has_value()) { - LOG_ERROR(Service_SSL, "Failed to duplicate socket with fd {}", fd); - return ResultInvalidSocket; - } - - const s32 duplicated_fd = *res; - - if (do_not_close_socket) { - *out_fd = duplicated_fd; - } else { - *out_fd = -1; - fd_to_close = duplicated_fd; - } - - std::optional> sock = bsd->GetSocket(duplicated_fd); - if (!sock.has_value()) { - LOG_ERROR(Service_SSL, "invalid socket fd {} after duplication", duplicated_fd); - return ResultInvalidSocket; + auto const res_v = bsd->DuplicateSocketImpl(fd); + if (auto *res = std::get_if(&res_v)) { + const s32 duplicated_fd = *res; + if (do_not_close_socket) { + *out_fd = duplicated_fd; + } else { + *out_fd = -1; + fd_to_close = duplicated_fd; + } + std::optional> sock = bsd->GetSocket(duplicated_fd); + if (!sock.has_value()) { + LOG_ERROR(Service_SSL, "invalid socket fd {} after duplication", duplicated_fd); + return ResultInvalidSocket; + } + socket = std::move(*sock); + backend->SetSocket(socket); + return ResultSuccess; } - socket = std::move(*sock); - backend->SetSocket(socket); - return ResultSuccess; + LOG_ERROR(Service_SSL, "Failed to duplicate socket with fd {}", fd); + return ResultInvalidSocket; } Result SetHostNameImpl(const std::string& hostname) { diff --git a/src/core/internal_network/network.cpp b/src/core/internal_network/network.cpp index c95d7ca2da..077a2e042e 100644 --- a/src/core/internal_network/network.cpp +++ b/src/core/internal_network/network.cpp @@ -28,7 +28,6 @@ #include "common/assert.h" #include "common/common_types.h" -#include "common/expected.h" #include "common/logging.h" #include "common/settings.h" #include "core/internal_network/network.h" @@ -733,15 +732,14 @@ u32 IPv4AddressToInteger(IPv4Address ip_addr) { static_cast(ip_addr[2]) << 8 | static_cast(ip_addr[3]); } -Common::Expected, GetAddrInfoError> GetAddressInfo( +std::variant, GetAddrInfoError> GetAddressInfo( const std::string& host, const std::optional& service) { addrinfo hints{}; hints.ai_family = AF_INET; // Switch only supports IPv4. addrinfo* addrinfo; - s32 gai_err = getaddrinfo(host.c_str(), service.has_value() ? service->c_str() : nullptr, - &hints, &addrinfo); + s32 gai_err = getaddrinfo(host.c_str(), service.has_value() ? service->c_str() : nullptr, &hints, &addrinfo); if (gai_err != 0) { - return Common::Unexpected(TranslateGetAddrInfoErrorFromNative(gai_err)); + return TranslateGetAddrInfoErrorFromNative(gai_err); } std::vector ret; for (auto* current = addrinfo; current; current = current->ai_next) { diff --git a/src/core/internal_network/network.h b/src/core/internal_network/network.h index ce259c0e66..2c9ae523c5 100644 --- a/src/core/internal_network/network.h +++ b/src/core/internal_network/network.h @@ -9,6 +9,7 @@ #include #include #include +#include #include "common/common_funcs.h" #include "common/common_types.h" @@ -124,7 +125,6 @@ std::string IPv4AddressToString(IPv4Address ip_addr); u32 IPv4AddressToInteger(IPv4Address ip_addr); // named to avoid name collision with Windows macro -Common::Expected, GetAddrInfoError> GetAddressInfo( - const std::string& host, const std::optional& service); +std::variant, GetAddrInfoError> GetAddressInfo(const std::string& host, const std::optional& service); } // namespace Network