early-access version 2179
This commit is contained in:
parent
0d005353e8
commit
480ce685ff
21 changed files with 1283 additions and 291 deletions
|
@ -1,7 +1,7 @@
|
||||||
yuzu emulator early access
|
yuzu emulator early access
|
||||||
=============
|
=============
|
||||||
|
|
||||||
This is the source code for early-access 2177.
|
This is the source code for early-access 2179.
|
||||||
|
|
||||||
## Legal Notice
|
## Legal Notice
|
||||||
|
|
||||||
|
|
|
@ -55,6 +55,7 @@ add_library(common STATIC
|
||||||
dynamic_library.h
|
dynamic_library.h
|
||||||
error.cpp
|
error.cpp
|
||||||
error.h
|
error.h
|
||||||
|
expected.h
|
||||||
fiber.cpp
|
fiber.cpp
|
||||||
fiber.h
|
fiber.h
|
||||||
fs/file.cpp
|
fs/file.cpp
|
||||||
|
|
987
src/common/expected.h
Executable file
987
src/common/expected.h
Executable file
|
@ -0,0 +1,987 @@
|
||||||
|
// Copyright 2021 yuzu Emulator Project
|
||||||
|
// Licensed under GPLv2 or any later version
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
// 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 <type_traits>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace Common {
|
||||||
|
|
||||||
|
template <typename T, typename E>
|
||||||
|
class Expected;
|
||||||
|
|
||||||
|
template <typename E>
|
||||||
|
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 <typename E>
|
||||||
|
constexpr auto operator<=>(const Unexpected<E>& lhs, const Unexpected<E>& 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 <typename T, typename E, bool = std::is_trivially_destructible_v<T>>
|
||||||
|
requires std::is_trivially_destructible_v<E>
|
||||||
|
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 <typename... Args, std::enable_if_t<std::is_constructible_v<T, Args&&...>>* = nullptr>
|
||||||
|
constexpr expected_storage_base(std::in_place_t, Args&&... args)
|
||||||
|
: m_val{std::forward<Args>(args)...}, m_has_val{true} {}
|
||||||
|
|
||||||
|
template <typename U, typename... Args,
|
||||||
|
std::enable_if_t<std::is_constructible_v<T, std::initializer_list<U>&, Args&&...>>* =
|
||||||
|
nullptr>
|
||||||
|
constexpr expected_storage_base(std::in_place_t, std::initializer_list<U> il, Args&&... args)
|
||||||
|
: m_val{il, std::forward<Args>(args)...}, m_has_val{true} {}
|
||||||
|
|
||||||
|
template <typename... Args, std::enable_if_t<std::is_constructible_v<E, Args&&...>>* = nullptr>
|
||||||
|
constexpr explicit expected_storage_base(unexpect_t, Args&&... args)
|
||||||
|
: m_unexpect{std::forward<Args>(args)...}, m_has_val{false} {}
|
||||||
|
|
||||||
|
template <typename U, typename... Args,
|
||||||
|
std::enable_if_t<std::is_constructible_v<E, std::initializer_list<U>&, Args&&...>>* =
|
||||||
|
nullptr>
|
||||||
|
constexpr explicit expected_storage_base(unexpect_t, std::initializer_list<U> il,
|
||||||
|
Args&&... args)
|
||||||
|
: m_unexpect{il, std::forward<Args>(args)...}, m_has_val{false} {}
|
||||||
|
|
||||||
|
~expected_storage_base() {
|
||||||
|
if (m_has_val) {
|
||||||
|
m_val.~T();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
union {
|
||||||
|
T m_val;
|
||||||
|
Unexpected<E> 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 <typename T, typename E>
|
||||||
|
requires std::is_trivially_destructible_v<E>
|
||||||
|
struct expected_storage_base<T, E, true> {
|
||||||
|
constexpr expected_storage_base() : m_val{T{}}, m_has_val{true} {}
|
||||||
|
|
||||||
|
constexpr expected_storage_base(no_init_t) : m_has_val{false} {}
|
||||||
|
|
||||||
|
template <typename... Args, std::enable_if_t<std::is_constructible_v<T, Args&&...>>* = nullptr>
|
||||||
|
constexpr expected_storage_base(std::in_place_t, Args&&... args)
|
||||||
|
: m_val{std::forward<Args>(args)...}, m_has_val{true} {}
|
||||||
|
|
||||||
|
template <typename U, typename... Args,
|
||||||
|
std::enable_if_t<std::is_constructible_v<T, std::initializer_list<U>&, Args&&...>>* =
|
||||||
|
nullptr>
|
||||||
|
constexpr expected_storage_base(std::in_place_t, std::initializer_list<U> il, Args&&... args)
|
||||||
|
: m_val{il, std::forward<Args>(args)...}, m_has_val{true} {}
|
||||||
|
|
||||||
|
template <typename... Args, std::enable_if_t<std::is_constructible_v<E, Args&&...>>* = nullptr>
|
||||||
|
constexpr explicit expected_storage_base(unexpect_t, Args&&... args)
|
||||||
|
: m_unexpect{std::forward<Args>(args)...}, m_has_val{false} {}
|
||||||
|
|
||||||
|
template <typename U, typename... Args,
|
||||||
|
std::enable_if_t<std::is_constructible_v<E, std::initializer_list<U>&, Args&&...>>* =
|
||||||
|
nullptr>
|
||||||
|
constexpr explicit expected_storage_base(unexpect_t, std::initializer_list<U> il,
|
||||||
|
Args&&... args)
|
||||||
|
: m_unexpect{il, std::forward<Args>(args)...}, m_has_val{false} {}
|
||||||
|
|
||||||
|
~expected_storage_base() = default;
|
||||||
|
|
||||||
|
union {
|
||||||
|
T m_val;
|
||||||
|
Unexpected<E> m_unexpect;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool m_has_val;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, typename E>
|
||||||
|
struct expected_operations_base : expected_storage_base<T, E> {
|
||||||
|
using expected_storage_base<T, E>::expected_storage_base;
|
||||||
|
|
||||||
|
template <typename... Args>
|
||||||
|
void construct(Args&&... args) noexcept {
|
||||||
|
new (std::addressof(this->m_val)) T{std::forward<Args>(args)...};
|
||||||
|
this->m_has_val = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Rhs>
|
||||||
|
void construct_with(Rhs&& rhs) noexcept {
|
||||||
|
new (std::addressof(this->m_val)) T{std::forward<Rhs>(rhs).get()};
|
||||||
|
this->m_has_val = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... Args>
|
||||||
|
void construct_error(Args&&... args) noexcept {
|
||||||
|
new (std::addressof(this->m_unexpect)) Unexpected<E>{std::forward<Args>(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<E>();
|
||||||
|
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<E>();
|
||||||
|
construct(std::move(rhs).get());
|
||||||
|
} else {
|
||||||
|
assign_common(rhs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Rhs>
|
||||||
|
void assign_common(Rhs&& rhs) {
|
||||||
|
if (this->m_has_val) {
|
||||||
|
if (rhs.m_has_val) {
|
||||||
|
get() = std::forward<Rhs>(rhs).get();
|
||||||
|
} else {
|
||||||
|
destroy_val();
|
||||||
|
construct_error(std::forward<Rhs>(rhs).geterr());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!rhs.m_has_val) {
|
||||||
|
geterr() = std::forward<Rhs>(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<E>& geterr() & {
|
||||||
|
return this->m_unexpect;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr const Unexpected<E>& geterr() const& {
|
||||||
|
return this->m_unexpect;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr Unexpected<E>&& geterr() && {
|
||||||
|
return std::move(this->m_unexpect);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr const Unexpected<E>&& 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 <typename T, typename E, bool = std::is_trivially_copy_constructible_v<T>>
|
||||||
|
requires std::is_trivially_copy_constructible_v<E>
|
||||||
|
struct expected_copy_base : expected_operations_base<T, E> {
|
||||||
|
using expected_operations_base<T, E>::expected_operations_base;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This specialization is for when T is not trivially copy constructible
|
||||||
|
* Additionally, this requires E to be trivially copy constructible
|
||||||
|
*/
|
||||||
|
template <typename T, typename E>
|
||||||
|
requires std::is_trivially_copy_constructible_v<E>
|
||||||
|
struct expected_copy_base<T, E, false> : expected_operations_base<T, E> {
|
||||||
|
using expected_operations_base<T, E>::expected_operations_base;
|
||||||
|
|
||||||
|
expected_copy_base() = default;
|
||||||
|
|
||||||
|
expected_copy_base(const expected_copy_base& rhs)
|
||||||
|
: expected_operations_base<T, E>{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 <typename T, typename E, bool = std::is_trivially_move_constructible_v<T>>
|
||||||
|
requires std::is_trivially_move_constructible_v<E>
|
||||||
|
struct expected_move_base : expected_copy_base<T, E> {
|
||||||
|
using expected_copy_base<T, E>::expected_copy_base;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This specialization is for when T is not trivially move constructible
|
||||||
|
* Additionally, this requires E to be trivially move constructible
|
||||||
|
*/
|
||||||
|
template <typename T, typename E>
|
||||||
|
requires std::is_trivially_move_constructible_v<E>
|
||||||
|
struct expected_move_base<T, E, false> : expected_copy_base<T, E> {
|
||||||
|
using expected_copy_base<T, E>::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<T>)
|
||||||
|
: expected_copy_base<T, E>{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 <typename T, typename E,
|
||||||
|
bool = std::conjunction_v<std::is_trivially_copy_assignable<T>,
|
||||||
|
std::is_trivially_copy_constructible<T>,
|
||||||
|
std::is_trivially_destructible<T>>>
|
||||||
|
requires std::conjunction_v<std::is_trivially_copy_assignable<E>,
|
||||||
|
std::is_trivially_copy_constructible<E>,
|
||||||
|
std::is_trivially_destructible<E>>
|
||||||
|
struct expected_copy_assign_base : expected_move_base<T, E> {
|
||||||
|
using expected_move_base<T, E>::expected_move_base;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This specialization is for when T is not trivially copy assignable
|
||||||
|
* Additionally, this requires E to be trivially copy assignable
|
||||||
|
*/
|
||||||
|
template <typename T, typename E>
|
||||||
|
requires std::conjunction_v<std::is_trivially_copy_assignable<E>,
|
||||||
|
std::is_trivially_copy_constructible<E>,
|
||||||
|
std::is_trivially_destructible<E>>
|
||||||
|
struct expected_copy_assign_base<T, E, false> : expected_move_base<T, E> {
|
||||||
|
using expected_move_base<T, E>::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 <typename T, typename E,
|
||||||
|
bool = std::conjunction_v<std::is_trivially_move_assignable<T>,
|
||||||
|
std::is_trivially_move_constructible<T>,
|
||||||
|
std::is_trivially_destructible<T>>>
|
||||||
|
requires std::conjunction_v<std::is_trivially_move_assignable<E>,
|
||||||
|
std::is_trivially_move_constructible<E>,
|
||||||
|
std::is_trivially_destructible<E>>
|
||||||
|
struct expected_move_assign_base : expected_copy_assign_base<T, E> {
|
||||||
|
using expected_copy_assign_base<T, E>::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 <typename T, typename E>
|
||||||
|
requires std::conjunction_v<std::is_trivially_move_assignable<E>,
|
||||||
|
std::is_trivially_move_constructible<E>,
|
||||||
|
std::is_trivially_destructible<E>>
|
||||||
|
struct expected_move_assign_base<T, E, false> : expected_copy_assign_base<T, E> {
|
||||||
|
using expected_copy_assign_base<T, E>::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_constructible<T>,
|
||||||
|
std::is_nothrow_move_assignable<T>>) {
|
||||||
|
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 <typename T, typename E, bool EnableCopy = std::is_copy_constructible_v<T>,
|
||||||
|
bool EnableMove = std::is_move_constructible_v<T>>
|
||||||
|
requires std::conjunction_v<std::is_copy_constructible<E>, std::is_move_constructible<E>>
|
||||||
|
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 <typename T, typename E>
|
||||||
|
requires std::conjunction_v<std::is_copy_constructible<E>, std::is_move_constructible<E>>
|
||||||
|
struct expected_delete_ctor_base<T, E, true, false> {
|
||||||
|
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 <typename T, typename E>
|
||||||
|
requires std::conjunction_v<std::is_copy_constructible<E>, std::is_move_constructible<E>>
|
||||||
|
struct expected_delete_ctor_base<T, E, false, true> {
|
||||||
|
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 <typename T, typename E>
|
||||||
|
requires std::conjunction_v<std::is_copy_constructible<E>, std::is_move_constructible<E>>
|
||||||
|
struct expected_delete_ctor_base<T, E, false, false> {
|
||||||
|
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_constructible<T>, std::is_copy_assignable<T>>,
|
||||||
|
bool EnableMove = std::conjunction_v<std::is_move_constructible<T>, std::is_move_assignable<T>>>
|
||||||
|
requires std::conjunction_v<std::is_copy_constructible<E>, std::is_move_constructible<E>,
|
||||||
|
std::is_copy_assignable<E>, std::is_move_assignable<E>>
|
||||||
|
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 <typename T, typename E>
|
||||||
|
requires std::conjunction_v<std::is_copy_constructible<E>, std::is_move_constructible<E>,
|
||||||
|
std::is_copy_assignable<E>, std::is_move_assignable<E>>
|
||||||
|
struct expected_delete_assign_base<T, E, true, false> {
|
||||||
|
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 <typename T, typename E>
|
||||||
|
requires std::conjunction_v<std::is_copy_constructible<E>, std::is_move_constructible<E>,
|
||||||
|
std::is_copy_assignable<E>, std::is_move_assignable<E>>
|
||||||
|
struct expected_delete_assign_base<T, E, false, true> {
|
||||||
|
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 <typename T, typename E>
|
||||||
|
requires std::conjunction_v<std::is_copy_constructible<E>, std::is_move_constructible<E>,
|
||||||
|
std::is_copy_assignable<E>, std::is_move_assignable<E>>
|
||||||
|
struct expected_delete_assign_base<T, E, false, false> {
|
||||||
|
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 <typename T, typename E, bool Enable = std::is_default_constructible_v<T>>
|
||||||
|
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 <typename T, typename E>
|
||||||
|
struct expected_default_ctor_base<T, E, false> {
|
||||||
|
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 <typename T, typename E, typename U>
|
||||||
|
using expected_enable_forward_value =
|
||||||
|
std::enable_if_t<std::is_constructible_v<T, U&&> &&
|
||||||
|
!std::is_same_v<std::remove_cvref_t<U>, std::in_place_t> &&
|
||||||
|
!std::is_same_v<Expected<T, E>, std::remove_cvref_t<U>> &&
|
||||||
|
!std::is_same_v<Unexpected<E>, std::remove_cvref_t<U>>>;
|
||||||
|
|
||||||
|
template <typename T, typename E, typename U, typename G, typename UR, typename GR>
|
||||||
|
using expected_enable_from_other = std::enable_if_t<
|
||||||
|
std::is_constructible_v<T, UR> && std::is_constructible_v<E, GR> &&
|
||||||
|
!std::is_constructible_v<T, Expected<U, G>&> && !std::is_constructible_v<T, Expected<U, G>&&> &&
|
||||||
|
!std::is_constructible_v<T, const Expected<U, G>&> &&
|
||||||
|
!std::is_constructible_v<T, const Expected<U, G>&&> &&
|
||||||
|
!std::is_convertible_v<Expected<U, G>&, T> && !std::is_convertible_v<Expected<U, G>&&, T> &&
|
||||||
|
!std::is_convertible_v<const Expected<U, G>&, T> &&
|
||||||
|
!std::is_convertible_v<const Expected<U, G>&&, T>>;
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
template <typename T, typename E>
|
||||||
|
class Expected : private detail::expected_move_assign_base<T, E>,
|
||||||
|
private detail::expected_delete_ctor_base<T, E>,
|
||||||
|
private detail::expected_delete_assign_base<T, E>,
|
||||||
|
private detail::expected_default_ctor_base<T, E> {
|
||||||
|
public:
|
||||||
|
using value_type = T;
|
||||||
|
using error_type = E;
|
||||||
|
using unexpected_type = Unexpected<E>;
|
||||||
|
|
||||||
|
constexpr Expected() = default;
|
||||||
|
constexpr Expected(const Expected&) = default;
|
||||||
|
constexpr Expected(Expected&&) = default;
|
||||||
|
Expected& operator=(const Expected&) = default;
|
||||||
|
Expected& operator=(Expected&&) = default;
|
||||||
|
|
||||||
|
template <typename... Args, std::enable_if_t<std::is_constructible_v<T, Args&&...>>* = nullptr>
|
||||||
|
constexpr Expected(std::in_place_t, Args&&... args)
|
||||||
|
: impl_base{std::in_place, std::forward<Args>(args)...},
|
||||||
|
ctor_base{detail::default_constructor_tag{}} {}
|
||||||
|
|
||||||
|
template <typename U, typename... Args,
|
||||||
|
std::enable_if_t<std::is_constructible_v<T, std::initializer_list<U>&, Args&&...>>* =
|
||||||
|
nullptr>
|
||||||
|
constexpr Expected(std::in_place_t, std::initializer_list<U> il, Args&&... args)
|
||||||
|
: impl_base{std::in_place, il, std::forward<Args>(args)...},
|
||||||
|
ctor_base{detail::default_constructor_tag{}} {}
|
||||||
|
|
||||||
|
template <typename G = E, std::enable_if_t<std::is_constructible_v<E, const G&>>* = nullptr,
|
||||||
|
std::enable_if_t<!std::is_convertible_v<const G&, E>>* = nullptr>
|
||||||
|
constexpr explicit Expected(const Unexpected<G>& e)
|
||||||
|
: impl_base{unexpect_t{}, e.value()}, ctor_base{detail::default_constructor_tag{}} {}
|
||||||
|
|
||||||
|
template <typename G = E, std::enable_if_t<std::is_constructible_v<E, const G&>>* = nullptr,
|
||||||
|
std::enable_if_t<std::is_convertible_v<const G&, E>>* = nullptr>
|
||||||
|
constexpr Expected(Unexpected<G> const& e)
|
||||||
|
: impl_base{unexpect_t{}, e.value()}, ctor_base{detail::default_constructor_tag{}} {}
|
||||||
|
|
||||||
|
template <typename G = E, std::enable_if_t<std::is_constructible_v<E, G&&>>* = nullptr,
|
||||||
|
std::enable_if_t<!std::is_convertible_v<G&&, E>>* = nullptr>
|
||||||
|
constexpr explicit Expected(Unexpected<G>&& e) noexcept(std::is_nothrow_constructible_v<E, G&&>)
|
||||||
|
: impl_base{unexpect_t{}, std::move(e.value())}, ctor_base{
|
||||||
|
detail::default_constructor_tag{}} {}
|
||||||
|
|
||||||
|
template <typename G = E, std::enable_if_t<std::is_constructible_v<E, G&&>>* = nullptr,
|
||||||
|
std::enable_if_t<std::is_convertible_v<G&&, E>>* = nullptr>
|
||||||
|
constexpr Expected(Unexpected<G>&& e) noexcept(std::is_nothrow_constructible_v<E, G&&>)
|
||||||
|
: impl_base{unexpect_t{}, std::move(e.value())}, ctor_base{
|
||||||
|
detail::default_constructor_tag{}} {}
|
||||||
|
|
||||||
|
template <typename... Args, std::enable_if_t<std::is_constructible_v<E, Args&&...>>* = nullptr>
|
||||||
|
constexpr explicit Expected(unexpect_t, Args&&... args)
|
||||||
|
: impl_base{unexpect_t{}, std::forward<Args>(args)...},
|
||||||
|
ctor_base{detail::default_constructor_tag{}} {}
|
||||||
|
|
||||||
|
template <typename U, typename... Args,
|
||||||
|
std::enable_if_t<std::is_constructible_v<E, std::initializer_list<U>&, Args&&...>>* =
|
||||||
|
nullptr>
|
||||||
|
constexpr explicit Expected(unexpect_t, std::initializer_list<U> il, Args&&... args)
|
||||||
|
: impl_base{unexpect_t{}, il, std::forward<Args>(args)...},
|
||||||
|
ctor_base{detail::default_constructor_tag{}} {}
|
||||||
|
|
||||||
|
template <typename U, typename G,
|
||||||
|
std::enable_if_t<!(std::is_convertible_v<U const&, T> &&
|
||||||
|
std::is_convertible_v<G const&, E>)>* = nullptr,
|
||||||
|
detail::expected_enable_from_other<T, E, U, G, const U&, const G&>* = nullptr>
|
||||||
|
constexpr explicit Expected(const Expected<U, G>& rhs)
|
||||||
|
: ctor_base{detail::default_constructor_tag{}} {
|
||||||
|
if (rhs.has_value()) {
|
||||||
|
this->construct(*rhs);
|
||||||
|
} else {
|
||||||
|
this->construct_error(rhs.error());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U, typename G,
|
||||||
|
std::enable_if_t<(std::is_convertible_v<U const&, T> &&
|
||||||
|
std::is_convertible_v<G const&, E>)>* = nullptr,
|
||||||
|
detail::expected_enable_from_other<T, E, U, G, const U&, const G&>* = nullptr>
|
||||||
|
constexpr Expected(const Expected<U, G>& rhs) : ctor_base{detail::default_constructor_tag{}} {
|
||||||
|
if (rhs.has_value()) {
|
||||||
|
this->construct(*rhs);
|
||||||
|
} else {
|
||||||
|
this->construct_error(rhs.error());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U, typename G,
|
||||||
|
std::enable_if_t<!(std::is_convertible_v<U&&, T> && std::is_convertible_v<G&&, E>)>* =
|
||||||
|
nullptr,
|
||||||
|
detail::expected_enable_from_other<T, E, U, G, U&&, G&&>* = nullptr>
|
||||||
|
constexpr explicit Expected(Expected<U, G>&& 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 <typename U, typename G,
|
||||||
|
std::enable_if_t<(std::is_convertible_v<U&&, T> && std::is_convertible_v<G&&, E>)>* =
|
||||||
|
nullptr,
|
||||||
|
detail::expected_enable_from_other<T, E, U, G, U&&, G&&>* = nullptr>
|
||||||
|
constexpr Expected(Expected<U, G>&& 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 <typename U = T, std::enable_if_t<!std::is_convertible_v<U&&, T>>* = nullptr,
|
||||||
|
detail::expected_enable_forward_value<T, E, U>* = nullptr>
|
||||||
|
constexpr explicit Expected(U&& v) : Expected{std::in_place, std::forward<U>(v)} {}
|
||||||
|
|
||||||
|
template <typename U = T, std::enable_if_t<std::is_convertible_v<U&&, T>>* = nullptr,
|
||||||
|
detail::expected_enable_forward_value<T, E, U>* = nullptr>
|
||||||
|
constexpr Expected(U&& v) : Expected{std::in_place, std::forward<U>(v)} {}
|
||||||
|
|
||||||
|
template <typename U = T, typename G = T,
|
||||||
|
std::enable_if_t<std::is_nothrow_constructible_v<T, U&&>>* = nullptr,
|
||||||
|
std::enable_if_t<(
|
||||||
|
!std::is_same_v<Expected<T, E>, std::remove_cvref_t<U>> &&
|
||||||
|
!std::conjunction_v<std::is_scalar<T>, std::is_same<T, std::remove_cvref_t<U>>> &&
|
||||||
|
std::is_constructible_v<T, U> && std::is_assignable_v<G&, U> &&
|
||||||
|
std::is_nothrow_move_constructible_v<E>)>* = nullptr>
|
||||||
|
Expected& operator=(U&& v) {
|
||||||
|
if (has_value()) {
|
||||||
|
val() = std::forward<U>(v);
|
||||||
|
} else {
|
||||||
|
err().~Unexpected<E>();
|
||||||
|
new (valptr()) T{std::forward<U>(v)};
|
||||||
|
this->m_has_val = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U = T, typename G = T,
|
||||||
|
std::enable_if_t<!std::is_nothrow_constructible_v<T, U&&>>* = nullptr,
|
||||||
|
std::enable_if_t<(
|
||||||
|
!std::is_same_v<Expected<T, E>, std::remove_cvref_t<U>> &&
|
||||||
|
!std::conjunction_v<std::is_scalar<T>, std::is_same<T, std::remove_cvref_t<U>>> &&
|
||||||
|
std::is_constructible_v<T, U> && std::is_assignable_v<G&, U> &&
|
||||||
|
std::is_nothrow_move_constructible_v<E>)>* = nullptr>
|
||||||
|
Expected& operator=(U&& v) {
|
||||||
|
if (has_value()) {
|
||||||
|
val() = std::forward<U>(v);
|
||||||
|
} else {
|
||||||
|
auto tmp = std::move(err());
|
||||||
|
err().~Unexpected<E>();
|
||||||
|
new (valptr()) T{std::forward<U>(v)};
|
||||||
|
this->m_has_val = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename G = E, std::enable_if_t<std::is_nothrow_copy_constructible_v<G> &&
|
||||||
|
std::is_assignable_v<G&, G>>* = nullptr>
|
||||||
|
Expected& operator=(const Unexpected<G>& rhs) {
|
||||||
|
if (!has_value()) {
|
||||||
|
err() = rhs;
|
||||||
|
} else {
|
||||||
|
this->destroy_val();
|
||||||
|
new (errptr()) Unexpected<E>{rhs};
|
||||||
|
this->m_has_val = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename G = E, std::enable_if_t<std::is_nothrow_move_constructible_v<G> &&
|
||||||
|
std::is_move_assignable_v<G>>* = nullptr>
|
||||||
|
Expected& operator=(Unexpected<G>&& rhs) noexcept {
|
||||||
|
if (!has_value()) {
|
||||||
|
err() = std::move(rhs);
|
||||||
|
} else {
|
||||||
|
this->destroy_val();
|
||||||
|
new (errptr()) Unexpected<E>{std::move(rhs)};
|
||||||
|
this->m_has_val = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... Args,
|
||||||
|
std::enable_if_t<std::is_nothrow_constructible_v<T, Args&&...>>* = nullptr>
|
||||||
|
void emplace(Args&&... args) {
|
||||||
|
if (has_value()) {
|
||||||
|
val() = T{std::forward<Args>(args)...};
|
||||||
|
} else {
|
||||||
|
err().~Unexpected<E>();
|
||||||
|
new (valptr()) T{std::forward<Args>(args)...};
|
||||||
|
this->m_has_val = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... Args,
|
||||||
|
std::enable_if_t<!std::is_nothrow_constructible_v<T, Args&&...>>* = nullptr>
|
||||||
|
void emplace(Args&&... args) {
|
||||||
|
if (has_value()) {
|
||||||
|
val() = T{std::forward<Args>(args)...};
|
||||||
|
} else {
|
||||||
|
auto tmp = std::move(err());
|
||||||
|
err().~Unexpected<E>();
|
||||||
|
new (valptr()) T{std::forward<Args>(args)...};
|
||||||
|
this->m_has_val = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U, typename... Args,
|
||||||
|
std::enable_if_t<std::is_nothrow_constructible_v<T, std::initializer_list<U>&,
|
||||||
|
Args&&...>>* = nullptr>
|
||||||
|
void emplace(std::initializer_list<U> il, Args&&... args) {
|
||||||
|
if (has_value()) {
|
||||||
|
T t{il, std::forward<Args>(args)...};
|
||||||
|
val() = std::move(t);
|
||||||
|
} else {
|
||||||
|
err().~Unexpected<E>();
|
||||||
|
new (valptr()) T{il, std::forward<Args>(args)...};
|
||||||
|
this->m_has_val = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U, typename... Args,
|
||||||
|
std::enable_if_t<!std::is_nothrow_constructible_v<T, std::initializer_list<U>&,
|
||||||
|
Args&&...>>* = nullptr>
|
||||||
|
void emplace(std::initializer_list<U> il, Args&&... args) {
|
||||||
|
if (has_value()) {
|
||||||
|
T t{il, std::forward<Args>(args)...};
|
||||||
|
val() = std::move(t);
|
||||||
|
} else {
|
||||||
|
auto tmp = std::move(err());
|
||||||
|
err().~Unexpected<E>();
|
||||||
|
new (valptr()) T{il, std::forward<Args>(args)...};
|
||||||
|
this->m_has_val = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr T* operator->() {
|
||||||
|
return valptr();
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr const T* operator->() const {
|
||||||
|
return valptr();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U = T>
|
||||||
|
constexpr U& operator*() & {
|
||||||
|
return val();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U = T>
|
||||||
|
constexpr const U& operator*() const& {
|
||||||
|
return val();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U = T>
|
||||||
|
constexpr U&& operator*() && {
|
||||||
|
return std::move(val());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U = T>
|
||||||
|
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 <typename U = T>
|
||||||
|
constexpr U& value() & {
|
||||||
|
return val();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U = T>
|
||||||
|
constexpr const U& value() const& {
|
||||||
|
return val();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U = T>
|
||||||
|
constexpr U&& value() && {
|
||||||
|
return std::move(val());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U = T>
|
||||||
|
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 <typename U>
|
||||||
|
constexpr T value_or(U&& v) const& {
|
||||||
|
static_assert(std::is_copy_constructible_v<T> && std::is_convertible_v<U&&, T>,
|
||||||
|
"T must be copy-constructible and convertible from U&&");
|
||||||
|
return bool(*this) ? **this : static_cast<T>(std::forward<U>(v));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U>
|
||||||
|
constexpr T value_or(U&& v) && {
|
||||||
|
static_assert(std::is_move_constructible_v<T> && std::is_convertible_v<U&&, T>,
|
||||||
|
"T must be move-constructible and convertible from U&&");
|
||||||
|
return bool(*this) ? std::move(**this) : static_cast<T>(std::forward<U>(v));
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static_assert(!std::is_reference_v<T>, "T must not be a reference");
|
||||||
|
static_assert(!std::is_same_v<T, std::remove_cv_t<std::in_place_t>>,
|
||||||
|
"T must not be std::in_place_t");
|
||||||
|
static_assert(!std::is_same_v<T, std::remove_cv_t<unexpect_t>>, "T must not be unexpect_t");
|
||||||
|
static_assert(!std::is_same_v<T, std::remove_cv_t<Unexpected<E>>>,
|
||||||
|
"T must not be Unexpected<E>");
|
||||||
|
static_assert(!std::is_reference_v<E>, "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<E>* errptr() {
|
||||||
|
return std::addressof(this->m_unexpect);
|
||||||
|
}
|
||||||
|
|
||||||
|
const Unexpected<E>* errptr() const {
|
||||||
|
return std::addressof(this->m_unexpect);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U = T>
|
||||||
|
constexpr U& val() {
|
||||||
|
return this->m_val;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U = T>
|
||||||
|
constexpr const U& val() const {
|
||||||
|
return this->m_val;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr Unexpected<E>& err() {
|
||||||
|
return this->m_unexpect;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr const Unexpected<E>& err() const {
|
||||||
|
return this->m_unexpect;
|
||||||
|
}
|
||||||
|
|
||||||
|
using impl_base = detail::expected_move_assign_base<T, E>;
|
||||||
|
using ctor_base = detail::expected_default_ctor_base<T, E>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, typename E, typename U, typename F>
|
||||||
|
constexpr bool operator==(const Expected<T, E>& lhs, const Expected<U, F>& rhs) {
|
||||||
|
return (lhs.has_value() != rhs.has_value())
|
||||||
|
? false
|
||||||
|
: (!lhs.has_value() ? lhs.error() == rhs.error() : *lhs == *rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename E, typename U, typename F>
|
||||||
|
constexpr bool operator!=(const Expected<T, E>& lhs, const Expected<U, F>& rhs) {
|
||||||
|
return !operator==(lhs, rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename E, typename U>
|
||||||
|
constexpr bool operator==(const Expected<T, E>& x, const U& v) {
|
||||||
|
return x.has_value() ? *x == v : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename E, typename U>
|
||||||
|
constexpr bool operator==(const U& v, const Expected<T, E>& x) {
|
||||||
|
return x.has_value() ? *x == v : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename E, typename U>
|
||||||
|
constexpr bool operator!=(const Expected<T, E>& x, const U& v) {
|
||||||
|
return !operator==(x, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename E, typename U>
|
||||||
|
constexpr bool operator!=(const U& v, const Expected<T, E>& x) {
|
||||||
|
return !operator==(v, x);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename E>
|
||||||
|
constexpr bool operator==(const Expected<T, E>& x, const Unexpected<E>& e) {
|
||||||
|
return x.has_value() ? false : x.error() == e.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename E>
|
||||||
|
constexpr bool operator==(const Unexpected<E>& e, const Expected<T, E>& x) {
|
||||||
|
return x.has_value() ? false : x.error() == e.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename E>
|
||||||
|
constexpr bool operator!=(const Expected<T, E>& x, const Unexpected<E>& e) {
|
||||||
|
return !operator==(x, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename E>
|
||||||
|
constexpr bool operator!=(const Unexpected<E>& e, const Expected<T, E>& x) {
|
||||||
|
return !operator==(e, x);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Common
|
|
@ -39,13 +39,12 @@ void RomFSFactory::SetPackedUpdate(VirtualFile update_raw_file) {
|
||||||
|
|
||||||
ResultVal<VirtualFile> RomFSFactory::OpenCurrentProcess(u64 current_process_title_id) const {
|
ResultVal<VirtualFile> RomFSFactory::OpenCurrentProcess(u64 current_process_title_id) const {
|
||||||
if (!updatable) {
|
if (!updatable) {
|
||||||
return MakeResult<VirtualFile>(file);
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
const PatchManager patch_manager{current_process_title_id, filesystem_controller,
|
const PatchManager patch_manager{current_process_title_id, filesystem_controller,
|
||||||
content_provider};
|
content_provider};
|
||||||
return MakeResult<VirtualFile>(
|
return patch_manager.PatchRomFS(file, ivfc_offset, ContentRecordType::Program, update_raw);
|
||||||
patch_manager.PatchRomFS(file, ivfc_offset, ContentRecordType::Program, update_raw));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultVal<VirtualFile> RomFSFactory::OpenPatchedRomFS(u64 title_id, ContentRecordType type) const {
|
ResultVal<VirtualFile> RomFSFactory::OpenPatchedRomFS(u64 title_id, ContentRecordType type) const {
|
||||||
|
@ -58,8 +57,7 @@ ResultVal<VirtualFile> RomFSFactory::OpenPatchedRomFS(u64 title_id, ContentRecor
|
||||||
|
|
||||||
const PatchManager patch_manager{title_id, filesystem_controller, content_provider};
|
const PatchManager patch_manager{title_id, filesystem_controller, content_provider};
|
||||||
|
|
||||||
return MakeResult<VirtualFile>(
|
return patch_manager.PatchRomFS(nca->GetRomFS(), nca->GetBaseIVFCOffset(), type);
|
||||||
patch_manager.PatchRomFS(nca->GetRomFS(), nca->GetBaseIVFCOffset(), type));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultVal<VirtualFile> RomFSFactory::OpenPatchedRomFSWithProgramIndex(
|
ResultVal<VirtualFile> RomFSFactory::OpenPatchedRomFSWithProgramIndex(
|
||||||
|
@ -83,7 +81,7 @@ ResultVal<VirtualFile> RomFSFactory::Open(u64 title_id, StorageId storage,
|
||||||
return ResultUnknown;
|
return ResultUnknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
return MakeResult<VirtualFile>(romfs);
|
return romfs;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<NCA> RomFSFactory::GetEntry(u64 title_id, StorageId storage,
|
std::shared_ptr<NCA> RomFSFactory::GetEntry(u64 title_id, StorageId storage,
|
||||||
|
|
|
@ -94,7 +94,7 @@ ResultVal<VirtualDir> SaveDataFactory::Create(SaveDataSpaceId space,
|
||||||
return ResultUnknown;
|
return ResultUnknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
return MakeResult<VirtualDir>(std::move(out));
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultVal<VirtualDir> SaveDataFactory::Open(SaveDataSpaceId space,
|
ResultVal<VirtualDir> SaveDataFactory::Open(SaveDataSpaceId space,
|
||||||
|
@ -115,7 +115,7 @@ ResultVal<VirtualDir> SaveDataFactory::Open(SaveDataSpaceId space,
|
||||||
return ResultUnknown;
|
return ResultUnknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
return MakeResult<VirtualDir>(std::move(out));
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
VirtualDir SaveDataFactory::GetSaveDataSpaceDirectory(SaveDataSpaceId space) const {
|
VirtualDir SaveDataFactory::GetSaveDataSpaceDirectory(SaveDataSpaceId space) const {
|
||||||
|
|
|
@ -25,7 +25,7 @@ SDMCFactory::SDMCFactory(VirtualDir sd_dir_, VirtualDir sd_mod_dir_)
|
||||||
SDMCFactory::~SDMCFactory() = default;
|
SDMCFactory::~SDMCFactory() = default;
|
||||||
|
|
||||||
ResultVal<VirtualDir> SDMCFactory::Open() const {
|
ResultVal<VirtualDir> SDMCFactory::Open() const {
|
||||||
return MakeResult<VirtualDir>(sd_dir);
|
return sd_dir;
|
||||||
}
|
}
|
||||||
|
|
||||||
VirtualDir SDMCFactory::GetSDMCModificationLoadRoot(u64 title_id) const {
|
VirtualDir SDMCFactory::GetSDMCModificationLoadRoot(u64 title_id) const {
|
||||||
|
|
|
@ -74,7 +74,7 @@ private:
|
||||||
// Minimum gyro amplitude to detect if the device is moving
|
// Minimum gyro amplitude to detect if the device is moving
|
||||||
f32 gyro_threshold = 0.0f;
|
f32 gyro_threshold = 0.0f;
|
||||||
|
|
||||||
// Number of invalid secuential data
|
// Number of invalid sequential data
|
||||||
u32 reset_counter = 0;
|
u32 reset_counter = 0;
|
||||||
|
|
||||||
// If the provided data is invalid the device will be autocalibrated
|
// If the provided data is invalid the device will be autocalibrated
|
||||||
|
|
|
@ -859,7 +859,7 @@ ResultVal<VAddr> KPageTable::SetHeapSize(std::size_t size) {
|
||||||
current_heap_addr = heap_region_start + size;
|
current_heap_addr = heap_region_start + size;
|
||||||
}
|
}
|
||||||
|
|
||||||
return MakeResult<VAddr>(heap_region_start);
|
return heap_region_start;
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultVal<VAddr> KPageTable::AllocateAndMapMemory(std::size_t needed_num_pages, std::size_t align,
|
ResultVal<VAddr> KPageTable::AllocateAndMapMemory(std::size_t needed_num_pages, std::size_t align,
|
||||||
|
@ -893,7 +893,7 @@ ResultVal<VAddr> KPageTable::AllocateAndMapMemory(std::size_t needed_num_pages,
|
||||||
|
|
||||||
block_manager->Update(addr, needed_num_pages, state, perm);
|
block_manager->Update(addr, needed_num_pages, state, perm);
|
||||||
|
|
||||||
return MakeResult<VAddr>(addr);
|
return addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultCode KPageTable::LockForDeviceAddressSpace(VAddr addr, std::size_t size) {
|
ResultCode KPageTable::LockForDeviceAddressSpace(VAddr addr, std::size_t size) {
|
||||||
|
|
|
@ -4,11 +4,10 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <new>
|
|
||||||
#include <utility>
|
|
||||||
#include "common/assert.h"
|
#include "common/assert.h"
|
||||||
#include "common/bit_field.h"
|
#include "common/bit_field.h"
|
||||||
#include "common/common_types.h"
|
#include "common/common_types.h"
|
||||||
|
#include "common/expected.h"
|
||||||
|
|
||||||
// All the constants in this file come from http://switchbrew.org/index.php?title=Error_codes
|
// All the constants in this file come from http://switchbrew.org/index.php?title=Error_codes
|
||||||
|
|
||||||
|
@ -155,204 +154,131 @@ constexpr ResultCode ResultSuccess(0);
|
||||||
constexpr ResultCode ResultUnknown(UINT32_MAX);
|
constexpr ResultCode ResultUnknown(UINT32_MAX);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is an optional value type. It holds a `ResultCode` and, if that code is a success code,
|
* This is an optional value type. It holds a `ResultCode` and, if that code is ResultSuccess, it
|
||||||
* also holds a result of type `T`. If the code is an error code then trying to access the inner
|
* also holds a result of type `T`. If the code is an error code (not ResultSuccess), then trying
|
||||||
* value fails, thus ensuring that the ResultCode of functions is always checked properly before
|
* to access the inner value with operator* is undefined behavior and will assert with Unwrap().
|
||||||
* their return value is used. It is similar in concept to the `std::optional` type
|
* Users of this class must be cognizant to check the status of the ResultVal with operator bool(),
|
||||||
* (http://en.cppreference.com/w/cpp/experimental/optional) originally proposed for inclusion in
|
* Code(), Succeeded() or Failed() prior to accessing the inner value.
|
||||||
* C++14, or the `Result` type in Rust (http://doc.rust-lang.org/std/result/index.html).
|
|
||||||
*
|
*
|
||||||
* An example of how it could be used:
|
* An example of how it could be used:
|
||||||
* \code
|
* \code
|
||||||
* ResultVal<int> Frobnicate(float strength) {
|
* ResultVal<int> Frobnicate(float strength) {
|
||||||
* if (strength < 0.f || strength > 1.0f) {
|
* if (strength < 0.f || strength > 1.0f) {
|
||||||
* // Can't frobnicate too weakly or too strongly
|
* // Can't frobnicate too weakly or too strongly
|
||||||
* return ResultCode(ErrorDescription::OutOfRange, ErrorModule::Common,
|
* return ResultCode{ErrorModule::Common, 1};
|
||||||
* ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
|
|
||||||
* } else {
|
* } else {
|
||||||
* // Frobnicated! Give caller a cookie
|
* // Frobnicated! Give caller a cookie
|
||||||
* return MakeResult<int>(42);
|
* return 42;
|
||||||
* }
|
* }
|
||||||
* }
|
* }
|
||||||
* \endcode
|
* \endcode
|
||||||
*
|
*
|
||||||
* \code
|
* \code
|
||||||
* ResultVal<int> frob_result = Frobnicate(0.75f);
|
* auto frob_result = Frobnicate(0.75f);
|
||||||
* if (frob_result) {
|
* if (frob_result) {
|
||||||
* // Frobbed ok
|
* // Frobbed ok
|
||||||
* printf("My cookie is %d\n", *frob_result);
|
* printf("My cookie is %d\n", *frob_result);
|
||||||
* } else {
|
* } else {
|
||||||
* printf("Guess I overdid it. :( Error code: %ux\n", frob_result.code().hex);
|
* printf("Guess I overdid it. :( Error code: %ux\n", frob_result.Code().raw);
|
||||||
* }
|
* }
|
||||||
* \endcode
|
* \endcode
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class ResultVal {
|
class ResultVal {
|
||||||
public:
|
public:
|
||||||
/// Constructs an empty `ResultVal` with the given error code. The code must not be a success
|
constexpr ResultVal() : expected{} {}
|
||||||
/// code.
|
|
||||||
ResultVal(ResultCode error_code = ResultUnknown) : result_code(error_code) {
|
|
||||||
ASSERT(error_code.IsError());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
constexpr ResultVal(ResultCode code) : expected{Common::Unexpected(code)} {}
|
||||||
* Similar to the non-member function `MakeResult`, with the exception that you can manually
|
|
||||||
* specify the success code. `success_code` must not be an error code.
|
|
||||||
*/
|
|
||||||
template <typename... Args>
|
|
||||||
[[nodiscard]] static ResultVal WithCode(ResultCode success_code, Args&&... args) {
|
|
||||||
ResultVal<T> result;
|
|
||||||
result.emplace(success_code, std::forward<Args>(args)...);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
ResultVal(const ResultVal& o) noexcept : result_code(o.result_code) {
|
|
||||||
if (!o.empty()) {
|
|
||||||
new (&object) T(o.object);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ResultVal(ResultVal&& o) noexcept : result_code(o.result_code) {
|
|
||||||
if (!o.empty()) {
|
|
||||||
new (&object) T(std::move(o.object));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
~ResultVal() {
|
|
||||||
if (!empty()) {
|
|
||||||
object.~T();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ResultVal& operator=(const ResultVal& o) noexcept {
|
|
||||||
if (this == &o) {
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
if (!empty()) {
|
|
||||||
if (!o.empty()) {
|
|
||||||
object = o.object;
|
|
||||||
} else {
|
|
||||||
object.~T();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!o.empty()) {
|
|
||||||
new (&object) T(o.object);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result_code = o.result_code;
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
ResultVal& operator=(ResultVal&& o) noexcept {
|
|
||||||
if (this == &o) {
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
if (!empty()) {
|
|
||||||
if (!o.empty()) {
|
|
||||||
object = std::move(o.object);
|
|
||||||
} else {
|
|
||||||
object.~T();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!o.empty()) {
|
|
||||||
new (&object) T(std::move(o.object));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result_code = o.result_code;
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Replaces the current result with a new constructed result value in-place. The code must not
|
|
||||||
* be an error code.
|
|
||||||
*/
|
|
||||||
template <typename... Args>
|
|
||||||
void emplace(ResultCode success_code, Args&&... args) {
|
|
||||||
ASSERT(success_code.IsSuccess());
|
|
||||||
if (!empty()) {
|
|
||||||
object.~T();
|
|
||||||
}
|
|
||||||
new (&object) T(std::forward<Args>(args)...);
|
|
||||||
result_code = success_code;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns true if the `ResultVal` contains an error code and no value.
|
|
||||||
[[nodiscard]] bool empty() const {
|
|
||||||
return result_code.IsError();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns true if the `ResultVal` contains a return value.
|
|
||||||
[[nodiscard]] bool Succeeded() const {
|
|
||||||
return result_code.IsSuccess();
|
|
||||||
}
|
|
||||||
/// Returns true if the `ResultVal` contains an error code and no value.
|
|
||||||
[[nodiscard]] bool Failed() const {
|
|
||||||
return empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] ResultCode Code() const {
|
|
||||||
return result_code;
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] const T& operator*() const {
|
|
||||||
return object;
|
|
||||||
}
|
|
||||||
[[nodiscard]] T& operator*() {
|
|
||||||
return object;
|
|
||||||
}
|
|
||||||
[[nodiscard]] const T* operator->() const {
|
|
||||||
return &object;
|
|
||||||
}
|
|
||||||
[[nodiscard]] T* operator->() {
|
|
||||||
return &object;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the value contained in this `ResultVal`, or the supplied default if it is missing.
|
|
||||||
template <typename U>
|
template <typename U>
|
||||||
[[nodiscard]] T ValueOr(U&& value) const {
|
constexpr ResultVal(U&& val) : expected{std::forward<U>(val)} {}
|
||||||
return !empty() ? object : std::move(value);
|
|
||||||
|
template <typename... Args>
|
||||||
|
constexpr ResultVal(Args&&... args) : expected{std::in_place, std::forward<Args>(args)...} {}
|
||||||
|
|
||||||
|
~ResultVal() = default;
|
||||||
|
|
||||||
|
constexpr ResultVal(const ResultVal&) = default;
|
||||||
|
constexpr ResultVal(ResultVal&&) = default;
|
||||||
|
|
||||||
|
ResultVal& operator=(const ResultVal&) = default;
|
||||||
|
ResultVal& operator=(ResultVal&&) = default;
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr explicit operator bool() const noexcept {
|
||||||
|
return expected.has_value();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Asserts that the result succeeded and returns a reference to it.
|
[[nodiscard]] constexpr ResultCode Code() const {
|
||||||
[[nodiscard]] T& Unwrap() & {
|
return expected.has_value() ? ResultSuccess : expected.error();
|
||||||
ASSERT_MSG(Succeeded(), "Tried to Unwrap empty ResultVal");
|
|
||||||
return **this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] T&& Unwrap() && {
|
[[nodiscard]] constexpr bool Succeeded() const {
|
||||||
|
return expected.has_value();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr bool Failed() const {
|
||||||
|
return !expected.has_value();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr T* operator->() {
|
||||||
|
return std::addressof(expected.value());
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const T* operator->() const {
|
||||||
|
return std::addressof(expected.value());
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr T& operator*() & {
|
||||||
|
return *expected;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const T& operator*() const& {
|
||||||
|
return *expected;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr T&& operator*() && {
|
||||||
|
return *expected;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const T&& operator*() const&& {
|
||||||
|
return *expected;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr T& Unwrap() & {
|
||||||
ASSERT_MSG(Succeeded(), "Tried to Unwrap empty ResultVal");
|
ASSERT_MSG(Succeeded(), "Tried to Unwrap empty ResultVal");
|
||||||
return std::move(**this);
|
return expected.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const T& Unwrap() const& {
|
||||||
|
ASSERT_MSG(Succeeded(), "Tried to Unwrap empty ResultVal");
|
||||||
|
return expected.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr T&& Unwrap() && {
|
||||||
|
ASSERT_MSG(Succeeded(), "Tried to Unwrap empty ResultVal");
|
||||||
|
return std::move(expected.value());
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const T&& Unwrap() const&& {
|
||||||
|
ASSERT_MSG(Succeeded(), "Tried to Unwrap empty ResultVal");
|
||||||
|
return std::move(expected.value());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U>
|
||||||
|
[[nodiscard]] constexpr T ValueOr(U&& v) const& {
|
||||||
|
return expected.value_or(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U>
|
||||||
|
[[nodiscard]] constexpr T ValueOr(U&& v) && {
|
||||||
|
return expected.value_or(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// A union is used to allocate the storage for the value, while allowing us to construct and
|
// TODO: Replace this with std::expected once it is standardized in the STL.
|
||||||
// destruct it at will.
|
Common::Expected<T, ResultCode> expected;
|
||||||
union {
|
|
||||||
T object;
|
|
||||||
};
|
|
||||||
ResultCode result_code;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* This function is a helper used to construct `ResultVal`s. It receives the arguments to construct
|
|
||||||
* `T` with and creates a success `ResultVal` contained the constructed value.
|
|
||||||
*/
|
|
||||||
template <typename T, typename... Args>
|
|
||||||
[[nodiscard]] ResultVal<T> MakeResult(Args&&... args) {
|
|
||||||
return ResultVal<T>::WithCode(ResultSuccess, std::forward<Args>(args)...);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Deducible overload of MakeResult, allowing the template parameter to be ommited if you're just
|
|
||||||
* copy or move constructing.
|
|
||||||
*/
|
|
||||||
template <typename Arg>
|
|
||||||
[[nodiscard]] ResultVal<std::remove_cvref_t<Arg>> MakeResult(Arg&& arg) {
|
|
||||||
return ResultVal<std::remove_cvref_t<Arg>>::WithCode(ResultSuccess, std::forward<Arg>(arg));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check for the success of `source` (which must evaluate to a ResultVal). If it succeeds, unwraps
|
* Check for the success of `source` (which must evaluate to a ResultVal). If it succeeds, unwraps
|
||||||
* the contained value and assigns it to `target`, which can be either an l-value expression or a
|
* the contained value and assigns it to `target`, which can be either an l-value expression or a
|
||||||
|
|
|
@ -226,11 +226,10 @@ ResultVal<FileSys::VirtualFile> VfsDirectoryServiceWrapper::OpenFile(const std::
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mode == FileSys::Mode::Append) {
|
if (mode == FileSys::Mode::Append) {
|
||||||
return MakeResult<FileSys::VirtualFile>(
|
return std::make_shared<FileSys::OffsetVfsFile>(file, 0, file->GetSize());
|
||||||
std::make_shared<FileSys::OffsetVfsFile>(file, 0, file->GetSize()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return MakeResult<FileSys::VirtualFile>(file);
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultVal<FileSys::VirtualDir> VfsDirectoryServiceWrapper::OpenDirectory(const std::string& path_) {
|
ResultVal<FileSys::VirtualDir> VfsDirectoryServiceWrapper::OpenDirectory(const std::string& path_) {
|
||||||
|
@ -240,7 +239,7 @@ ResultVal<FileSys::VirtualDir> VfsDirectoryServiceWrapper::OpenDirectory(const s
|
||||||
// TODO(DarkLordZach): Find a better error code for this
|
// TODO(DarkLordZach): Find a better error code for this
|
||||||
return FileSys::ERROR_PATH_NOT_FOUND;
|
return FileSys::ERROR_PATH_NOT_FOUND;
|
||||||
}
|
}
|
||||||
return MakeResult(dir);
|
return dir;
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultVal<FileSys::EntryType> VfsDirectoryServiceWrapper::GetEntryType(
|
ResultVal<FileSys::EntryType> VfsDirectoryServiceWrapper::GetEntryType(
|
||||||
|
@ -252,12 +251,12 @@ ResultVal<FileSys::EntryType> VfsDirectoryServiceWrapper::GetEntryType(
|
||||||
auto filename = Common::FS::GetFilename(path);
|
auto filename = Common::FS::GetFilename(path);
|
||||||
// TODO(Subv): Some games use the '/' path, find out what this means.
|
// TODO(Subv): Some games use the '/' path, find out what this means.
|
||||||
if (filename.empty())
|
if (filename.empty())
|
||||||
return MakeResult(FileSys::EntryType::Directory);
|
return FileSys::EntryType::Directory;
|
||||||
|
|
||||||
if (dir->GetFile(filename) != nullptr)
|
if (dir->GetFile(filename) != nullptr)
|
||||||
return MakeResult(FileSys::EntryType::File);
|
return FileSys::EntryType::File;
|
||||||
if (dir->GetSubdirectory(filename) != nullptr)
|
if (dir->GetSubdirectory(filename) != nullptr)
|
||||||
return MakeResult(FileSys::EntryType::Directory);
|
return FileSys::EntryType::Directory;
|
||||||
return FileSys::ERROR_PATH_NOT_FOUND;
|
return FileSys::ERROR_PATH_NOT_FOUND;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -270,7 +269,7 @@ ResultVal<FileSys::FileTimeStampRaw> VfsDirectoryServiceWrapper::GetFileTimeStam
|
||||||
if (GetEntryType(path).Failed()) {
|
if (GetEntryType(path).Failed()) {
|
||||||
return FileSys::ERROR_PATH_NOT_FOUND;
|
return FileSys::ERROR_PATH_NOT_FOUND;
|
||||||
}
|
}
|
||||||
return MakeResult(dir->GetFileTimeStamp(Common::FS::GetFilename(path)));
|
return dir->GetFileTimeStamp(Common::FS::GetFilename(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
FileSystemController::FileSystemController(Core::System& system_) : system{system_} {}
|
FileSystemController::FileSystemController(Core::System& system_) : system{system_} {}
|
||||||
|
@ -395,7 +394,7 @@ ResultVal<FileSys::VirtualDir> FileSystemController::OpenSaveDataSpace(
|
||||||
return FileSys::ERROR_ENTITY_NOT_FOUND;
|
return FileSys::ERROR_ENTITY_NOT_FOUND;
|
||||||
}
|
}
|
||||||
|
|
||||||
return MakeResult(save_data_factory->GetSaveDataSpaceDirectory(space));
|
return save_data_factory->GetSaveDataSpaceDirectory(space);
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultVal<FileSys::VirtualDir> FileSystemController::OpenSDMC() const {
|
ResultVal<FileSys::VirtualDir> FileSystemController::OpenSDMC() const {
|
||||||
|
@ -421,7 +420,7 @@ ResultVal<FileSys::VirtualDir> FileSystemController::OpenBISPartition(
|
||||||
return FileSys::ERROR_INVALID_ARGUMENT;
|
return FileSys::ERROR_INVALID_ARGUMENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
return MakeResult<FileSys::VirtualDir>(std::move(part));
|
return part;
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultVal<FileSys::VirtualFile> FileSystemController::OpenBISPartitionStorage(
|
ResultVal<FileSys::VirtualFile> FileSystemController::OpenBISPartitionStorage(
|
||||||
|
@ -437,7 +436,7 @@ ResultVal<FileSys::VirtualFile> FileSystemController::OpenBISPartitionStorage(
|
||||||
return FileSys::ERROR_INVALID_ARGUMENT;
|
return FileSys::ERROR_INVALID_ARGUMENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
return MakeResult<FileSys::VirtualFile>(std::move(part));
|
return part;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 FileSystemController::GetFreeSpaceSize(FileSys::StorageId id) const {
|
u64 FileSystemController::GetFreeSpaceSize(FileSys::StorageId id) const {
|
||||||
|
|
|
@ -26,7 +26,7 @@ ResultVal<ApplicationLaunchProperty> ARPManager::GetLaunchProperty(u64 title_id)
|
||||||
return ERR_NOT_REGISTERED;
|
return ERR_NOT_REGISTERED;
|
||||||
}
|
}
|
||||||
|
|
||||||
return MakeResult<ApplicationLaunchProperty>(iter->second.launch);
|
return iter->second.launch;
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultVal<std::vector<u8>> ARPManager::GetControlProperty(u64 title_id) const {
|
ResultVal<std::vector<u8>> ARPManager::GetControlProperty(u64 title_id) const {
|
||||||
|
@ -39,7 +39,7 @@ ResultVal<std::vector<u8>> ARPManager::GetControlProperty(u64 title_id) const {
|
||||||
return ERR_NOT_REGISTERED;
|
return ERR_NOT_REGISTERED;
|
||||||
}
|
}
|
||||||
|
|
||||||
return MakeResult<std::vector<u8>>(iter->second.control);
|
return iter->second.control;
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultCode ARPManager::Register(u64 title_id, ApplicationLaunchProperty launch,
|
ResultCode ARPManager::Register(u64 title_id, ApplicationLaunchProperty launch,
|
||||||
|
|
|
@ -335,7 +335,7 @@ public:
|
||||||
CASCADE_CODE(result);
|
CASCADE_CODE(result);
|
||||||
|
|
||||||
if (ValidateRegionForMap(page_table, addr, size)) {
|
if (ValidateRegionForMap(page_table, addr, size)) {
|
||||||
return MakeResult<VAddr>(addr);
|
return addr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -371,7 +371,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ValidateRegionForMap(page_table, addr, size)) {
|
if (ValidateRegionForMap(page_table, addr, size)) {
|
||||||
return MakeResult<VAddr>(addr);
|
return addr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -443,14 +443,14 @@ ResultVal<std::vector<MiiInfoElement>> MiiManager::GetDefault(SourceFlag source_
|
||||||
std::vector<MiiInfoElement> result;
|
std::vector<MiiInfoElement> result;
|
||||||
|
|
||||||
if ((source_flag & SourceFlag::Default) == SourceFlag::None) {
|
if ((source_flag & SourceFlag::Default) == SourceFlag::None) {
|
||||||
return MakeResult(std::move(result));
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (std::size_t index = BaseMiiCount; index < DefaultMiiCount; index++) {
|
for (std::size_t index = BaseMiiCount; index < DefaultMiiCount; index++) {
|
||||||
result.emplace_back(BuildDefault(index), Source::Default);
|
result.emplace_back(BuildDefault(index), Source::Default);
|
||||||
}
|
}
|
||||||
|
|
||||||
return MakeResult(std::move(result));
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultCode MiiManager::GetIndex([[maybe_unused]] const MiiInfo& info, u32& index) {
|
ResultCode MiiManager::GetIndex([[maybe_unused]] const MiiInfo& info, u32& index) {
|
||||||
|
|
|
@ -414,7 +414,7 @@ ResultVal<u8> IApplicationManagerInterface::GetApplicationDesiredLanguage(
|
||||||
for (const auto lang : *priority_list) {
|
for (const auto lang : *priority_list) {
|
||||||
const auto supported_flag = GetSupportedLanguageFlag(lang);
|
const auto supported_flag = GetSupportedLanguageFlag(lang);
|
||||||
if (supported_languages == 0 || (supported_languages & supported_flag) == supported_flag) {
|
if (supported_languages == 0 || (supported_languages & supported_flag) == supported_flag) {
|
||||||
return MakeResult(static_cast<u8>(lang));
|
return static_cast<u8>(lang);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -448,7 +448,7 @@ ResultVal<u64> IApplicationManagerInterface::ConvertApplicationLanguageToLanguag
|
||||||
return ERR_APPLICATION_LANGUAGE_NOT_FOUND;
|
return ERR_APPLICATION_LANGUAGE_NOT_FOUND;
|
||||||
}
|
}
|
||||||
|
|
||||||
return MakeResult(static_cast<u64>(*language_code));
|
return static_cast<u64>(*language_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
IApplicationVersionInterface::IApplicationVersionInterface(Core::System& system_)
|
IApplicationVersionInterface::IApplicationVersionInterface(Core::System& system_)
|
||||||
|
|
|
@ -87,7 +87,7 @@ ResultVal<Kernel::KPort*> ServiceManager::GetServicePort(const std::string& name
|
||||||
auto handler = it->second;
|
auto handler = it->second;
|
||||||
port->GetServerPort().SetSessionHandler(std::move(handler));
|
port->GetServerPort().SetSessionHandler(std::move(handler));
|
||||||
|
|
||||||
return MakeResult(port);
|
return port;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -165,7 +165,7 @@ ResultVal<Kernel::KClientSession*> SM::GetServiceImpl(Kernel::HLERequestContext&
|
||||||
|
|
||||||
LOG_DEBUG(Service_SM, "called service={} -> session={}", name, session->GetId());
|
LOG_DEBUG(Service_SM, "called service={} -> session={}", name, session->GetId());
|
||||||
|
|
||||||
return MakeResult(session);
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SM::RegisterService(Kernel::HLERequestContext& ctx) {
|
void SM::RegisterService(Kernel::HLERequestContext& ctx) {
|
||||||
|
|
|
@ -120,40 +120,40 @@ ResultVal<u64> Module::Interface::GetConfigImpl(ConfigItem config_item) const {
|
||||||
return ResultSecureMonitorNotImplemented;
|
return ResultSecureMonitorNotImplemented;
|
||||||
case ConfigItem::ExosphereApiVersion:
|
case ConfigItem::ExosphereApiVersion:
|
||||||
// Get information about the current exosphere version.
|
// Get information about the current exosphere version.
|
||||||
return MakeResult((u64{HLE::ApiVersion::ATMOSPHERE_RELEASE_VERSION_MAJOR} << 56) |
|
return (u64{HLE::ApiVersion::ATMOSPHERE_RELEASE_VERSION_MAJOR} << 56) |
|
||||||
(u64{HLE::ApiVersion::ATMOSPHERE_RELEASE_VERSION_MINOR} << 48) |
|
(u64{HLE::ApiVersion::ATMOSPHERE_RELEASE_VERSION_MINOR} << 48) |
|
||||||
(u64{HLE::ApiVersion::ATMOSPHERE_RELEASE_VERSION_MICRO} << 40) |
|
(u64{HLE::ApiVersion::ATMOSPHERE_RELEASE_VERSION_MICRO} << 40) |
|
||||||
(static_cast<u64>(HLE::ApiVersion::GetTargetFirmware())));
|
(static_cast<u64>(HLE::ApiVersion::GetTargetFirmware()));
|
||||||
case ConfigItem::ExosphereNeedsReboot:
|
case ConfigItem::ExosphereNeedsReboot:
|
||||||
// We are executing, so we aren't in the process of rebooting.
|
// We are executing, so we aren't in the process of rebooting.
|
||||||
return MakeResult(u64{0});
|
return u64{0};
|
||||||
case ConfigItem::ExosphereNeedsShutdown:
|
case ConfigItem::ExosphereNeedsShutdown:
|
||||||
// We are executing, so we aren't in the process of shutting down.
|
// We are executing, so we aren't in the process of shutting down.
|
||||||
return MakeResult(u64{0});
|
return u64{0};
|
||||||
case ConfigItem::ExosphereGitCommitHash:
|
case ConfigItem::ExosphereGitCommitHash:
|
||||||
// Get information about the current exosphere git commit hash.
|
// Get information about the current exosphere git commit hash.
|
||||||
return MakeResult(u64{0});
|
return u64{0};
|
||||||
case ConfigItem::ExosphereHasRcmBugPatch:
|
case ConfigItem::ExosphereHasRcmBugPatch:
|
||||||
// Get information about whether this unit has the RCM bug patched.
|
// Get information about whether this unit has the RCM bug patched.
|
||||||
return MakeResult(u64{0});
|
return u64{0};
|
||||||
case ConfigItem::ExosphereBlankProdInfo:
|
case ConfigItem::ExosphereBlankProdInfo:
|
||||||
// Get whether this unit should simulate a "blanked" PRODINFO.
|
// Get whether this unit should simulate a "blanked" PRODINFO.
|
||||||
return MakeResult(u64{0});
|
return u64{0};
|
||||||
case ConfigItem::ExosphereAllowCalWrites:
|
case ConfigItem::ExosphereAllowCalWrites:
|
||||||
// Get whether this unit should allow writing to the calibration partition.
|
// Get whether this unit should allow writing to the calibration partition.
|
||||||
return MakeResult(u64{0});
|
return u64{0};
|
||||||
case ConfigItem::ExosphereEmummcType:
|
case ConfigItem::ExosphereEmummcType:
|
||||||
// Get what kind of emummc this unit has active.
|
// Get what kind of emummc this unit has active.
|
||||||
return MakeResult(u64{0});
|
return u64{0};
|
||||||
case ConfigItem::ExospherePayloadAddress:
|
case ConfigItem::ExospherePayloadAddress:
|
||||||
// Gets the physical address of the reboot payload buffer, if one exists.
|
// Gets the physical address of the reboot payload buffer, if one exists.
|
||||||
return ResultSecureMonitorNotInitialized;
|
return ResultSecureMonitorNotInitialized;
|
||||||
case ConfigItem::ExosphereLogConfiguration:
|
case ConfigItem::ExosphereLogConfiguration:
|
||||||
// Get the log configuration.
|
// Get the log configuration.
|
||||||
return MakeResult(u64{0});
|
return u64{0};
|
||||||
case ConfigItem::ExosphereForceEnableUsb30:
|
case ConfigItem::ExosphereForceEnableUsb30:
|
||||||
// Get whether usb 3.0 should be force-enabled.
|
// Get whether usb 3.0 should be force-enabled.
|
||||||
return MakeResult(u64{0});
|
return u64{0};
|
||||||
default:
|
default:
|
||||||
return ResultSecureMonitorInvalidArgument;
|
return ResultSecureMonitorInvalidArgument;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1273,15 +1273,15 @@ private:
|
||||||
static ResultVal<ConvertedScaleMode> ConvertScalingModeImpl(NintendoScaleMode mode) {
|
static ResultVal<ConvertedScaleMode> ConvertScalingModeImpl(NintendoScaleMode mode) {
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case NintendoScaleMode::None:
|
case NintendoScaleMode::None:
|
||||||
return MakeResult(ConvertedScaleMode::None);
|
return ConvertedScaleMode::None;
|
||||||
case NintendoScaleMode::Freeze:
|
case NintendoScaleMode::Freeze:
|
||||||
return MakeResult(ConvertedScaleMode::Freeze);
|
return ConvertedScaleMode::Freeze;
|
||||||
case NintendoScaleMode::ScaleToWindow:
|
case NintendoScaleMode::ScaleToWindow:
|
||||||
return MakeResult(ConvertedScaleMode::ScaleToWindow);
|
return ConvertedScaleMode::ScaleToWindow;
|
||||||
case NintendoScaleMode::ScaleAndCrop:
|
case NintendoScaleMode::ScaleAndCrop:
|
||||||
return MakeResult(ConvertedScaleMode::ScaleAndCrop);
|
return ConvertedScaleMode::ScaleAndCrop;
|
||||||
case NintendoScaleMode::PreserveAspectRatio:
|
case NintendoScaleMode::PreserveAspectRatio:
|
||||||
return MakeResult(ConvertedScaleMode::PreserveAspectRatio);
|
return ConvertedScaleMode::PreserveAspectRatio;
|
||||||
default:
|
default:
|
||||||
LOG_ERROR(Service_VI, "Invalid scaling mode specified, mode={}", mode);
|
LOG_ERROR(Service_VI, "Invalid scaling mode specified, mode={}", mode);
|
||||||
return ERR_OPERATION_FAILED;
|
return ERR_OPERATION_FAILED;
|
||||||
|
|
|
@ -433,15 +433,33 @@ Id DescType(EmitContext& ctx, Id sampled_type, Id pointer_type, u32 count) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t FindNextUnusedLocation(const std::bitset<IR::NUM_GENERICS>& used_locations,
|
size_t FindAndSetNextUnusedLocation(std::bitset<IR::NUM_GENERICS>& used_locations,
|
||||||
size_t start_offset) {
|
size_t& start_offset) {
|
||||||
for (size_t location = start_offset; location < used_locations.size(); ++location) {
|
for (size_t location = start_offset; location < used_locations.size(); ++location) {
|
||||||
if (!used_locations.test(location)) {
|
if (!used_locations.test(location)) {
|
||||||
|
start_offset = location;
|
||||||
|
used_locations.set(location);
|
||||||
return location;
|
return location;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw RuntimeError("Unable to get an unused location for legacy attribute");
|
throw RuntimeError("Unable to get an unused location for legacy attribute");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Id DefineLegacyInput(EmitContext& ctx, std::bitset<IR::NUM_GENERICS>& used_locations,
|
||||||
|
size_t& start_offset) {
|
||||||
|
const Id id{DefineInput(ctx, ctx.F32[4], true)};
|
||||||
|
const size_t location = FindAndSetNextUnusedLocation(used_locations, start_offset);
|
||||||
|
ctx.Decorate(id, spv::Decoration::Location, location);
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
Id DefineLegacyOutput(EmitContext& ctx, std::bitset<IR::NUM_GENERICS>& used_locations,
|
||||||
|
size_t& start_offset, std::optional<u32> invocations) {
|
||||||
|
const Id id{DefineOutput(ctx, ctx.F32[4], invocations)};
|
||||||
|
const size_t location = FindAndSetNextUnusedLocation(used_locations, start_offset);
|
||||||
|
ctx.Decorate(id, spv::Decoration::Location, location);
|
||||||
|
return id;
|
||||||
|
}
|
||||||
} // Anonymous namespace
|
} // Anonymous namespace
|
||||||
|
|
||||||
void VectorTypes::Define(Sirit::Module& sirit_ctx, Id base_type, std::string_view name) {
|
void VectorTypes::Define(Sirit::Module& sirit_ctx, Id base_type, std::string_view name) {
|
||||||
|
@ -525,6 +543,64 @@ Id EmitContext::BitOffset16(const IR::Value& offset) {
|
||||||
return OpBitwiseAnd(U32[1], OpShiftLeftLogical(U32[1], Def(offset), Const(3u)), Const(16u));
|
return OpBitwiseAnd(U32[1], OpShiftLeftLogical(U32[1], Def(offset), Const(3u)), Const(16u));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Id EmitContext::InputLegacyAttribute(IR::Attribute attribute) {
|
||||||
|
if (attribute >= IR::Attribute::ColorFrontDiffuseR &&
|
||||||
|
attribute <= IR::Attribute::ColorFrontDiffuseA) {
|
||||||
|
return input_front_color;
|
||||||
|
}
|
||||||
|
if (attribute >= IR::Attribute::ColorFrontSpecularR &&
|
||||||
|
attribute <= IR::Attribute::ColorFrontSpecularA) {
|
||||||
|
return input_front_secondary_color;
|
||||||
|
}
|
||||||
|
if (attribute >= IR::Attribute::ColorBackDiffuseR &&
|
||||||
|
attribute <= IR::Attribute::ColorBackDiffuseA) {
|
||||||
|
return input_back_color;
|
||||||
|
}
|
||||||
|
if (attribute >= IR::Attribute::ColorBackSpecularR &&
|
||||||
|
attribute <= IR::Attribute::ColorBackSpecularA) {
|
||||||
|
return input_back_secondary_color;
|
||||||
|
}
|
||||||
|
if (attribute == IR::Attribute::FogCoordinate) {
|
||||||
|
return input_fog_frag_coord;
|
||||||
|
}
|
||||||
|
if (attribute >= IR::Attribute::FixedFncTexture0S &&
|
||||||
|
attribute <= IR::Attribute::FixedFncTexture9Q) {
|
||||||
|
u32 index =
|
||||||
|
(static_cast<u32>(attribute) - static_cast<u32>(IR::Attribute::FixedFncTexture0S)) / 4;
|
||||||
|
return input_fixed_fnc_textures[index];
|
||||||
|
}
|
||||||
|
throw InvalidArgument("Attribute is not legacy attribute {}", attribute);
|
||||||
|
}
|
||||||
|
|
||||||
|
Id EmitContext::OutputLegacyAttribute(IR::Attribute attribute) {
|
||||||
|
if (attribute >= IR::Attribute::ColorFrontDiffuseR &&
|
||||||
|
attribute <= IR::Attribute::ColorFrontDiffuseA) {
|
||||||
|
return output_front_color;
|
||||||
|
}
|
||||||
|
if (attribute >= IR::Attribute::ColorFrontSpecularR &&
|
||||||
|
attribute <= IR::Attribute::ColorFrontSpecularA) {
|
||||||
|
return output_front_secondary_color;
|
||||||
|
}
|
||||||
|
if (attribute >= IR::Attribute::ColorBackDiffuseR &&
|
||||||
|
attribute <= IR::Attribute::ColorBackDiffuseA) {
|
||||||
|
return output_back_color;
|
||||||
|
}
|
||||||
|
if (attribute >= IR::Attribute::ColorBackSpecularR &&
|
||||||
|
attribute <= IR::Attribute::ColorBackSpecularA) {
|
||||||
|
return output_back_secondary_color;
|
||||||
|
}
|
||||||
|
if (attribute == IR::Attribute::FogCoordinate) {
|
||||||
|
return output_fog_frag_coord;
|
||||||
|
}
|
||||||
|
if (attribute >= IR::Attribute::FixedFncTexture0S &&
|
||||||
|
attribute <= IR::Attribute::FixedFncTexture9Q) {
|
||||||
|
u32 index =
|
||||||
|
(static_cast<u32>(attribute) - static_cast<u32>(IR::Attribute::FixedFncTexture0S)) / 4;
|
||||||
|
return output_fixed_fnc_textures[index];
|
||||||
|
}
|
||||||
|
throw InvalidArgument("Attribute is not legacy attribute {}", attribute);
|
||||||
|
}
|
||||||
|
|
||||||
void EmitContext::DefineCommonTypes(const Info& info) {
|
void EmitContext::DefineCommonTypes(const Info& info) {
|
||||||
void_id = TypeVoid();
|
void_id = TypeVoid();
|
||||||
|
|
||||||
|
@ -1353,22 +1429,26 @@ void EmitContext::DefineInputs(const IR::Program& program) {
|
||||||
}
|
}
|
||||||
size_t previous_unused_location = 0;
|
size_t previous_unused_location = 0;
|
||||||
if (loads.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) {
|
if (loads.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) {
|
||||||
const size_t location = FindNextUnusedLocation(used_locations, previous_unused_location);
|
input_front_color = DefineLegacyInput(*this, used_locations, previous_unused_location);
|
||||||
previous_unused_location = location;
|
}
|
||||||
used_locations.set(location);
|
if (loads.AnyComponent(IR::Attribute::ColorFrontSpecularR)) {
|
||||||
const Id id{DefineInput(*this, F32[4], true)};
|
input_front_secondary_color =
|
||||||
Decorate(id, spv::Decoration::Location, location);
|
DefineLegacyInput(*this, used_locations, previous_unused_location);
|
||||||
input_front_color = id;
|
}
|
||||||
|
if (loads.AnyComponent(IR::Attribute::ColorBackDiffuseR)) {
|
||||||
|
input_back_color = DefineLegacyInput(*this, used_locations, previous_unused_location);
|
||||||
|
}
|
||||||
|
if (loads.AnyComponent(IR::Attribute::ColorBackSpecularR)) {
|
||||||
|
input_back_secondary_color =
|
||||||
|
DefineLegacyInput(*this, used_locations, previous_unused_location);
|
||||||
|
}
|
||||||
|
if (loads.AnyComponent(IR::Attribute::FogCoordinate)) {
|
||||||
|
input_fog_frag_coord = DefineLegacyInput(*this, used_locations, previous_unused_location);
|
||||||
}
|
}
|
||||||
for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) {
|
for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) {
|
||||||
if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) {
|
if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) {
|
||||||
const size_t location =
|
input_fixed_fnc_textures[index] =
|
||||||
FindNextUnusedLocation(used_locations, previous_unused_location);
|
DefineLegacyInput(*this, used_locations, previous_unused_location);
|
||||||
previous_unused_location = location;
|
|
||||||
used_locations.set(location);
|
|
||||||
const Id id{DefineInput(*this, F32[4], true)};
|
|
||||||
Decorate(id, spv::Decoration::Location, location);
|
|
||||||
input_fixed_fnc_textures[index] = id;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (stage == Stage::TessellationEval) {
|
if (stage == Stage::TessellationEval) {
|
||||||
|
@ -1430,22 +1510,29 @@ void EmitContext::DefineOutputs(const IR::Program& program) {
|
||||||
}
|
}
|
||||||
size_t previous_unused_location = 0;
|
size_t previous_unused_location = 0;
|
||||||
if (info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) {
|
if (info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) {
|
||||||
const size_t location = FindNextUnusedLocation(used_locations, previous_unused_location);
|
output_front_color =
|
||||||
previous_unused_location = location;
|
DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
|
||||||
used_locations.set(location);
|
}
|
||||||
const Id id{DefineOutput(*this, F32[4], invocations)};
|
if (info.stores.AnyComponent(IR::Attribute::ColorFrontSpecularR)) {
|
||||||
Decorate(id, spv::Decoration::Location, static_cast<u32>(location));
|
output_front_secondary_color =
|
||||||
output_front_color = id;
|
DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
|
||||||
|
}
|
||||||
|
if (info.stores.AnyComponent(IR::Attribute::ColorBackDiffuseR)) {
|
||||||
|
output_back_color =
|
||||||
|
DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
|
||||||
|
}
|
||||||
|
if (info.stores.AnyComponent(IR::Attribute::ColorBackSpecularR)) {
|
||||||
|
output_back_secondary_color =
|
||||||
|
DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
|
||||||
|
}
|
||||||
|
if (info.stores.AnyComponent(IR::Attribute::FogCoordinate)) {
|
||||||
|
output_fog_frag_coord =
|
||||||
|
DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
|
||||||
}
|
}
|
||||||
for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) {
|
for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) {
|
||||||
if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) {
|
if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) {
|
||||||
const size_t location =
|
output_fixed_fnc_textures[index] =
|
||||||
FindNextUnusedLocation(used_locations, previous_unused_location);
|
DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
|
||||||
previous_unused_location = location;
|
|
||||||
used_locations.set(location);
|
|
||||||
const Id id{DefineOutput(*this, F32[4], invocations)};
|
|
||||||
Decorate(id, spv::Decoration::Location, location);
|
|
||||||
output_fixed_fnc_textures[index] = id;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch (stage) {
|
switch (stage) {
|
||||||
|
|
|
@ -113,6 +113,9 @@ public:
|
||||||
[[nodiscard]] Id BitOffset8(const IR::Value& offset);
|
[[nodiscard]] Id BitOffset8(const IR::Value& offset);
|
||||||
[[nodiscard]] Id BitOffset16(const IR::Value& offset);
|
[[nodiscard]] Id BitOffset16(const IR::Value& offset);
|
||||||
|
|
||||||
|
Id InputLegacyAttribute(IR::Attribute attribute);
|
||||||
|
Id OutputLegacyAttribute(IR::Attribute attribute);
|
||||||
|
|
||||||
Id Const(u32 value) {
|
Id Const(u32 value) {
|
||||||
return Constant(U32[1], value);
|
return Constant(U32[1], value);
|
||||||
}
|
}
|
||||||
|
@ -279,12 +282,20 @@ public:
|
||||||
|
|
||||||
Id input_position{};
|
Id input_position{};
|
||||||
Id input_front_color{};
|
Id input_front_color{};
|
||||||
|
Id input_front_secondary_color{};
|
||||||
|
Id input_back_color{};
|
||||||
|
Id input_back_secondary_color{};
|
||||||
|
Id input_fog_frag_coord{};
|
||||||
std::array<Id, 10> input_fixed_fnc_textures{};
|
std::array<Id, 10> input_fixed_fnc_textures{};
|
||||||
std::array<Id, 32> input_generics{};
|
std::array<Id, 32> input_generics{};
|
||||||
|
|
||||||
Id output_point_size{};
|
Id output_point_size{};
|
||||||
Id output_position{};
|
Id output_position{};
|
||||||
Id output_front_color{};
|
Id output_front_color{};
|
||||||
|
Id output_front_secondary_color{};
|
||||||
|
Id output_back_color{};
|
||||||
|
Id output_back_secondary_color{};
|
||||||
|
Id output_fog_frag_coord{};
|
||||||
std::array<Id, 10> output_fixed_fnc_textures{};
|
std::array<Id, 10> output_fixed_fnc_textures{};
|
||||||
std::array<std::array<GenericElementInfo, 4>, 32> output_generics{};
|
std::array<std::array<GenericElementInfo, 4>, 32> output_generics{};
|
||||||
|
|
||||||
|
|
|
@ -43,23 +43,12 @@ Id AttrPointer(EmitContext& ctx, Id pointer_type, Id vertex, Id base, Args&&...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsFixedFncTexture(IR::Attribute attribute) {
|
bool IsLegacyAttribute(IR::Attribute attribute) {
|
||||||
return attribute >= IR::Attribute::FixedFncTexture0S &&
|
return (attribute >= IR::Attribute::ColorFrontDiffuseR &&
|
||||||
attribute <= IR::Attribute::FixedFncTexture9Q;
|
attribute <= IR::Attribute::ColorBackSpecularA) ||
|
||||||
}
|
attribute == IR::Attribute::FogCoordinate ||
|
||||||
|
(attribute >= IR::Attribute::FixedFncTexture0S &&
|
||||||
u32 FixedFncTextureAttributeIndex(IR::Attribute attribute) {
|
attribute <= IR::Attribute::FixedFncTexture9Q);
|
||||||
if (!IsFixedFncTexture(attribute)) {
|
|
||||||
throw InvalidArgument("Attribute {} is not a FixedFncTexture", attribute);
|
|
||||||
}
|
|
||||||
return (static_cast<u32>(attribute) - static_cast<u32>(IR::Attribute::FixedFncTexture0S)) / 4u;
|
|
||||||
}
|
|
||||||
|
|
||||||
u32 FixedFncTextureAttributeElement(IR::Attribute attribute) {
|
|
||||||
if (!IsFixedFncTexture(attribute)) {
|
|
||||||
throw InvalidArgument("Attribute {} is not a FixedFncTexture", attribute);
|
|
||||||
}
|
|
||||||
return static_cast<u32>(attribute) % 4u;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
|
@ -93,12 +82,16 @@ std::optional<OutAttr> OutputAttrPointer(EmitContext& ctx, IR::Attribute attr) {
|
||||||
return OutputAccessChain(ctx, ctx.output_f32, info.id, index_id);
|
return OutputAccessChain(ctx, ctx.output_f32, info.id, index_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (IsFixedFncTexture(attr)) {
|
if (IsLegacyAttribute(attr)) {
|
||||||
const u32 index{FixedFncTextureAttributeIndex(attr)};
|
if (attr == IR::Attribute::FogCoordinate) {
|
||||||
const u32 element{FixedFncTextureAttributeElement(attr)};
|
return OutputAccessChain(ctx, ctx.output_f32, ctx.OutputLegacyAttribute(attr),
|
||||||
const Id element_id{ctx.Const(element)};
|
ctx.Const(0u));
|
||||||
return OutputAccessChain(ctx, ctx.output_f32, ctx.output_fixed_fnc_textures[index],
|
} else {
|
||||||
element_id);
|
const u32 element{static_cast<u32>(attr) % 4};
|
||||||
|
const Id element_id{ctx.Const(element)};
|
||||||
|
return OutputAccessChain(ctx, ctx.output_f32, ctx.OutputLegacyAttribute(attr),
|
||||||
|
element_id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
switch (attr) {
|
switch (attr) {
|
||||||
case IR::Attribute::PointSize:
|
case IR::Attribute::PointSize:
|
||||||
|
@ -111,14 +104,6 @@ std::optional<OutAttr> OutputAttrPointer(EmitContext& ctx, IR::Attribute attr) {
|
||||||
const Id element_id{ctx.Const(element)};
|
const Id element_id{ctx.Const(element)};
|
||||||
return OutputAccessChain(ctx, ctx.output_f32, ctx.output_position, element_id);
|
return OutputAccessChain(ctx, ctx.output_f32, ctx.output_position, element_id);
|
||||||
}
|
}
|
||||||
case IR::Attribute::ColorFrontDiffuseR:
|
|
||||||
case IR::Attribute::ColorFrontDiffuseG:
|
|
||||||
case IR::Attribute::ColorFrontDiffuseB:
|
|
||||||
case IR::Attribute::ColorFrontDiffuseA: {
|
|
||||||
const u32 element{static_cast<u32>(attr) % 4};
|
|
||||||
const Id element_id{ctx.Const(element)};
|
|
||||||
return OutputAccessChain(ctx, ctx.output_f32, ctx.output_front_color, element_id);
|
|
||||||
}
|
|
||||||
case IR::Attribute::ClipDistance0:
|
case IR::Attribute::ClipDistance0:
|
||||||
case IR::Attribute::ClipDistance1:
|
case IR::Attribute::ClipDistance1:
|
||||||
case IR::Attribute::ClipDistance2:
|
case IR::Attribute::ClipDistance2:
|
||||||
|
@ -341,11 +326,18 @@ Id EmitGetAttribute(EmitContext& ctx, IR::Attribute attr, Id vertex) {
|
||||||
const Id value{ctx.OpLoad(type->id, pointer)};
|
const Id value{ctx.OpLoad(type->id, pointer)};
|
||||||
return type->needs_cast ? ctx.OpBitcast(ctx.F32[1], value) : value;
|
return type->needs_cast ? ctx.OpBitcast(ctx.F32[1], value) : value;
|
||||||
}
|
}
|
||||||
if (IsFixedFncTexture(attr)) {
|
if (IsLegacyAttribute(attr)) {
|
||||||
const u32 index{FixedFncTextureAttributeIndex(attr)};
|
if (attr == IR::Attribute::FogCoordinate) {
|
||||||
const Id attr_id{ctx.input_fixed_fnc_textures[index]};
|
LOG_WARNING(Shader_SPIRV, "Get FogCoordinate Attribute called");
|
||||||
const Id attr_ptr{AttrPointer(ctx, ctx.input_f32, vertex, attr_id, ctx.Const(element))};
|
const Id attr_ptr{AttrPointer(ctx, ctx.input_f32, vertex,
|
||||||
return ctx.OpLoad(ctx.F32[1], attr_ptr);
|
ctx.InputLegacyAttribute(attr), ctx.Const(0u))};
|
||||||
|
return ctx.OpLoad(ctx.F32[1], attr_ptr);
|
||||||
|
} else {
|
||||||
|
const Id element_id{ctx.Const(element)};
|
||||||
|
const Id attr_ptr{AttrPointer(ctx, ctx.input_f32, vertex,
|
||||||
|
ctx.InputLegacyAttribute(attr), element_id)};
|
||||||
|
return ctx.OpLoad(ctx.F32[1], attr_ptr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
switch (attr) {
|
switch (attr) {
|
||||||
case IR::Attribute::PrimitiveId:
|
case IR::Attribute::PrimitiveId:
|
||||||
|
@ -356,13 +348,6 @@ Id EmitGetAttribute(EmitContext& ctx, IR::Attribute attr, Id vertex) {
|
||||||
case IR::Attribute::PositionW:
|
case IR::Attribute::PositionW:
|
||||||
return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_position,
|
return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_position,
|
||||||
ctx.Const(element)));
|
ctx.Const(element)));
|
||||||
case IR::Attribute::ColorFrontDiffuseR:
|
|
||||||
case IR::Attribute::ColorFrontDiffuseG:
|
|
||||||
case IR::Attribute::ColorFrontDiffuseB:
|
|
||||||
case IR::Attribute::ColorFrontDiffuseA: {
|
|
||||||
return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_front_color,
|
|
||||||
ctx.Const(element)));
|
|
||||||
}
|
|
||||||
case IR::Attribute::InstanceId:
|
case IR::Attribute::InstanceId:
|
||||||
if (ctx.profile.support_vertex_instance_id) {
|
if (ctx.profile.support_vertex_instance_id) {
|
||||||
return ctx.OpBitcast(ctx.F32[1], ctx.OpLoad(ctx.U32[1], ctx.instance_id));
|
return ctx.OpBitcast(ctx.F32[1], ctx.OpLoad(ctx.U32[1], ctx.instance_id));
|
||||||
|
|
|
@ -10,16 +10,14 @@
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include "common/alignment.h"
|
|
||||||
#include "common/assert.h"
|
#include "common/assert.h"
|
||||||
#include "common/logging/log.h"
|
#include "common/logging/log.h"
|
||||||
#include "common/math_util.h"
|
#include "common/math_util.h"
|
||||||
#include "common/microprofile.h"
|
#include "common/microprofile.h"
|
||||||
#include "common/scope_exit.h"
|
|
||||||
#include "common/settings.h"
|
#include "common/settings.h"
|
||||||
#include "core/core.h"
|
|
||||||
#include "core/hle/kernel/k_process.h"
|
|
||||||
#include "core/memory.h"
|
#include "core/memory.h"
|
||||||
#include "video_core/engines/kepler_compute.h"
|
#include "video_core/engines/kepler_compute.h"
|
||||||
#include "video_core/engines/maxwell_3d.h"
|
#include "video_core/engines/maxwell_3d.h"
|
||||||
|
|
Loading…
Reference in a new issue