diff --git a/Source/Core/Common/Result.h b/Source/Core/Common/Result.h index 78d675360bf..db80362da18 100644 --- a/Source/Core/Common/Result.h +++ b/Source/Core/Common/Result.h @@ -7,24 +7,30 @@ namespace Common { -template +template class Result final { public: - Result(ResultCode code) : m_variant{code} {} - Result(const T& t) : m_variant{t} {} - Result(T&& t) : m_variant{std::move(t)} {} - explicit operator bool() const { return Succeeded(); } - bool Succeeded() const { return std::holds_alternative(m_variant); } - // Must only be called when Succeeded() returns false. - ResultCode Error() const { return std::get(m_variant); } + constexpr Result(const Expected& value) : m_variant{value} {} + constexpr Result(Expected&& value) : m_variant{std::move(value)} {} + + constexpr Result(const Unexpected& value) : m_variant{value} {} + constexpr Result(Unexpected&& value) : m_variant{std::move(value)} {} + + constexpr explicit operator bool() const { return Succeeded(); } + constexpr bool Succeeded() const { return std::holds_alternative(m_variant); } + // Must only be called when Succeeded() returns true. - const T& operator*() const { return std::get(m_variant); } - const T* operator->() const { return &std::get(m_variant); } - T& operator*() { return std::get(m_variant); } - T* operator->() { return &std::get(m_variant); } + constexpr const Expected& operator*() const { return std::get(m_variant); } + constexpr const Expected* operator->() const { return &std::get(m_variant); } + constexpr Expected& operator*() { return std::get(m_variant); } + constexpr Expected* operator->() { return &std::get(m_variant); } + + // Must only be called when Succeeded() returns false. + constexpr Unexpected& Error() { return std::get(m_variant); } + constexpr const Unexpected& Error() const { return std::get(m_variant); } private: - std::variant m_variant; + std::variant m_variant; }; } // namespace Common diff --git a/Source/Core/Core/CheatGeneration.cpp b/Source/Core/Core/CheatGeneration.cpp index e6120a0e585..4154b1cfa3f 100644 --- a/Source/Core/Core/CheatGeneration.cpp +++ b/Source/Core/Core/CheatGeneration.cpp @@ -52,7 +52,7 @@ static std::vector ResultToAREntries(u32 addr, const Chea return codes; } -Common::Result +Common::Result Cheats::GenerateActionReplayCode(const Cheats::CheatSearchSessionBase& session, size_t index) { if (index >= session.GetResultCount()) diff --git a/Source/Core/Core/CheatGeneration.h b/Source/Core/Core/CheatGeneration.h index f6fda3a2217..1dfd91a814f 100644 --- a/Source/Core/Core/CheatGeneration.h +++ b/Source/Core/Core/CheatGeneration.h @@ -18,6 +18,6 @@ enum class GenerateActionReplayCodeErrorCode InvalidAddress, }; -Common::Result +Common::Result GenerateActionReplayCode(const Cheats::CheatSearchSessionBase& session, size_t index); } // namespace Cheats diff --git a/Source/Core/Core/CheatSearch.cpp b/Source/Core/Core/CheatSearch.cpp index 40868e5efc9..bc01afa404b 100644 --- a/Source/Core/Core/CheatSearch.cpp +++ b/Source/Core/Core/CheatSearch.cpp @@ -109,11 +109,11 @@ TryReadValueFromEmulatedMemory(const Core::CPUThreadGuard& guard, u32 addr, } template -Common::Result>> -Cheats::NewSearch(const Core::CPUThreadGuard& guard, - const std::vector& memory_ranges, - PowerPC::RequestedAddressSpace address_space, bool aligned, - const std::function& validator) +auto Cheats::NewSearch(const Core::CPUThreadGuard& guard, + const std::vector& memory_ranges, + PowerPC::RequestedAddressSpace address_space, bool aligned, + const std::function& validator) + -> Common::Result>, SearchErrorCode> { if (AchievementManager::GetInstance().IsHardcoreModeActive()) return Cheats::SearchErrorCode::DisabledInHardcoreMode; @@ -162,11 +162,11 @@ Cheats::NewSearch(const Core::CPUThreadGuard& guard, } template -Common::Result>> -Cheats::NextSearch(const Core::CPUThreadGuard& guard, - const std::vector>& previous_results, - PowerPC::RequestedAddressSpace address_space, - const std::function& validator) +auto Cheats::NextSearch( + const Core::CPUThreadGuard& guard, const std::vector>& previous_results, + PowerPC::RequestedAddressSpace address_space, + const std::function& validator) + -> Common::Result>, SearchErrorCode> { if (AchievementManager::GetInstance().IsHardcoreModeActive()) return Cheats::SearchErrorCode::DisabledInHardcoreMode; @@ -335,7 +335,7 @@ Cheats::SearchErrorCode Cheats::CheatSearchSession::RunSearch(const Core::CPU { if (AchievementManager::GetInstance().IsHardcoreModeActive()) return Cheats::SearchErrorCode::DisabledInHardcoreMode; - Common::Result>> result = + Common::Result>, SearchErrorCode> result = Cheats::SearchErrorCode::InvalidParameters; if (m_filter_type == FilterType::CompareAgainstSpecificValue) { diff --git a/Source/Core/Core/CheatSearch.h b/Source/Core/Core/CheatSearch.h index 6c6d5090471..d0e56e6c369 100644 --- a/Source/Core/Core/CheatSearch.h +++ b/Source/Core/Core/CheatSearch.h @@ -116,7 +116,7 @@ std::vector GetValueAsByteVector(const SearchValue& value); // Do a new search across the given memory region in the given address space, only keeping values // for which the given validator returns true. template -Common::Result>> +Common::Result>, SearchErrorCode> NewSearch(const Core::CPUThreadGuard& guard, const std::vector& memory_ranges, PowerPC::RequestedAddressSpace address_space, bool aligned, const std::function& validator); @@ -124,7 +124,7 @@ NewSearch(const Core::CPUThreadGuard& guard, const std::vector& mem // Refresh the values for the given results in the given address space, only keeping values for // which the given validator returns true. template -Common::Result>> +Common::Result>, SearchErrorCode> NextSearch(const Core::CPUThreadGuard& guard, const std::vector>& previous_results, PowerPC::RequestedAddressSpace address_space, const std::function& validator); diff --git a/Source/Core/Core/IOS/FS/FileSystem.h b/Source/Core/Core/IOS/FS/FileSystem.h index 0010f99cfbd..8e8e36dc528 100644 --- a/Source/Core/Core/IOS/FS/FileSystem.h +++ b/Source/Core/Core/IOS/FS/FileSystem.h @@ -49,7 +49,7 @@ enum class ResultCode }; template -using Result = Common::Result; +using Result = Common::Result; using Uid = u32; using Gid = u16; diff --git a/Source/Core/DiscIO/MultithreadedCompressor.h b/Source/Core/DiscIO/MultithreadedCompressor.h index 736d177de53..6019d5472b5 100644 --- a/Source/Core/DiscIO/MultithreadedCompressor.h +++ b/Source/Core/DiscIO/MultithreadedCompressor.h @@ -8,8 +8,6 @@ #include #include #include -#include -#include #include "Common/Assert.h" #include "Common/Event.h" @@ -27,7 +25,7 @@ enum class ConversionResultCode }; template -using ConversionResult = Common::Result; +using ConversionResult = Common::Result; // This class starts a number of compression threads and one output thread. // The set_up_compress_thread_state function is called at the start of each compression thread.