From 4242c4a3252a47d2880202b077cd00ce616c6365 Mon Sep 17 00:00:00 2001 From: Jan Laupetin Date: Sat, 4 Apr 2026 14:47:10 +0200 Subject: [PATCH] refactor: use std::expected instead of custom implementation --- src/ModMan/Context/FastFileContext.cpp | 10 +- src/ModMan/Context/FastFileContext.h | 6 +- src/ModMan/Web/Binds/UnlinkingBinds.cpp | 6 +- .../Techset/CommonShaderArgCreator.cpp | 176 +++++++++-------- .../Techset/CommonShaderArgCreator.h | 25 ++- .../TechniqueShaderScopeSequences.cpp | 6 +- src/Utils/Utils/Result.h | 185 ------------------ src/ZoneLoading/ZoneLoading.cpp | 12 +- src/ZoneLoading/ZoneLoading.h | 6 +- 9 files changed, 124 insertions(+), 308 deletions(-) delete mode 100644 src/Utils/Utils/Result.h diff --git a/src/ModMan/Context/FastFileContext.cpp b/src/ModMan/Context/FastFileContext.cpp index 1027b488..cc3a45e6 100644 --- a/src/ModMan/Context/FastFileContext.cpp +++ b/src/ModMan/Context/FastFileContext.cpp @@ -50,11 +50,11 @@ void FastFileContext::Destroy() m_loaded_zones.clear(); } -result::Expected FastFileContext::LoadFastFile(const std::string& path) +std::expected FastFileContext::LoadFastFile(const std::string& path) { auto zone = ZoneLoading::LoadZone(path, std::make_unique(fs::path(path).filename().replace_extension().string())); if (!zone) - return result::Unexpected(std::move(zone.error())); + return std::unexpected(std::move(zone.error())); auto loadedZone = std::make_unique(std::move(*zone), path); @@ -69,7 +69,7 @@ result::Expected FastFileContext::LoadFastFile(const s return result; } -result::Expected FastFileContext::UnloadZone(const std::string& zoneName) +std::expected FastFileContext::UnloadZone(const std::string& zoneName) { { std::lock_guard lock(m_zone_lock); @@ -83,9 +83,9 @@ result::Expected FastFileContext::UnloadZone(const std::s { m_loaded_zones.erase(existingZone); ui::NotifyZoneUnloaded(zoneName); - return NoResult(); + return {}; } } - return result::Unexpected(std::format("No zone with name {} loaded", zoneName)); + return std::unexpected(std::format("No zone with name {} loaded", zoneName)); } diff --git a/src/ModMan/Context/FastFileContext.h b/src/ModMan/Context/FastFileContext.h index e769e34f..842b5e5f 100644 --- a/src/ModMan/Context/FastFileContext.h +++ b/src/ModMan/Context/FastFileContext.h @@ -1,8 +1,8 @@ #pragma once -#include "Utils/Result.h" #include "Zone/Zone.h" +#include #include #include #include @@ -21,8 +21,8 @@ class FastFileContext public: void Destroy(); - result::Expected LoadFastFile(const std::string& path); - result::Expected UnloadZone(const std::string& zoneName); + std::expected LoadFastFile(const std::string& path); + std::expected UnloadZone(const std::string& zoneName); std::vector> m_loaded_zones; std::shared_mutex m_zone_lock; diff --git a/src/ModMan/Web/Binds/UnlinkingBinds.cpp b/src/ModMan/Web/Binds/UnlinkingBinds.cpp index d226e629..6ed609eb 100644 --- a/src/ModMan/Web/Binds/UnlinkingBinds.cpp +++ b/src/ModMan/Web/Binds/UnlinkingBinds.cpp @@ -50,7 +50,7 @@ namespace double m_last_progress; }; - result::Expected UnlinkZoneInDbThread(const std::string& zoneName) + std::expected UnlinkZoneInDbThread(const std::string& zoneName) { const auto& context = ModManContext::Get().m_fast_file; const auto existingZone = std::ranges::find_if(context.m_loaded_zones, @@ -60,7 +60,7 @@ namespace }); if (existingZone == context.m_loaded_zones.end()) - return result::Unexpected(std::format("No zone with name {} loaded", zoneName)); + return std::unexpected(std::format("No zone with name {} loaded", zoneName)); const auto& loadedZone = *existingZone->get(); @@ -75,7 +75,7 @@ namespace *loadedZone.m_zone, outputFolderPathStr, outputFolderOutputPath, searchPaths, std::make_unique(zoneName)); objWriter->DumpZone(dumpingContext); - return NoResult(); + return {}; } void UnlinkZone(webview::webview& wv, std::string id, std::string zoneName) // NOLINT(performance-unnecessary-value-param) Copy is made for thread safety diff --git a/src/ObjCompiling/Techset/CommonShaderArgCreator.cpp b/src/ObjCompiling/Techset/CommonShaderArgCreator.cpp index e277b578..c20890a7 100644 --- a/src/ObjCompiling/Techset/CommonShaderArgCreator.cpp +++ b/src/ObjCompiling/Techset/CommonShaderArgCreator.cpp @@ -72,7 +72,7 @@ namespace DetermineSupportedArgumentTypes(); } - result::Expected EnterShader(const techset::CommonTechniqueShaderType shaderType, const std::string& name) override + std::expected EnterShader(const techset::CommonTechniqueShaderType shaderType, const std::string& name) override { m_shader_type = shaderType; m_shader_name = name; @@ -88,23 +88,23 @@ namespace } if (!maybeShader) - return result::Unexpected("Failed to load shader"); + return std::unexpected("Failed to load shader"); m_bin = *maybeShader; - return NoResult{}; + return {}; } - result::Expected LeaveShader() override + std::expected LeaveShader() override { m_bin = {}; return AutoCreateMissingArgs(); } - result::Expected AcceptShaderConstantArgument(const techset::CommonShaderArgCreatorDestination& destination, - const techset::CommonCodeConstSource codeConstSource, - const unsigned sourceIndex) override + std::expected AcceptShaderConstantArgument(const techset::CommonShaderArgCreatorDestination& destination, + const techset::CommonCodeConstSource codeConstSource, + const unsigned sourceIndex) override { techset::CommonShaderArgDestination commonDestination{}; bool isTransposed; @@ -113,32 +113,32 @@ namespace if (!FindDestinationForConstant(commonDestination, isTransposed, rowCount, errorMessage, destination)) { if (!errorMessage.empty()) - return result::Unexpected(std::move(errorMessage)); + return std::unexpected(std::move(errorMessage)); - return result::Unexpected(std::format("Could not find constant shader input with name {}", destination.m_argument_name)); + return std::unexpected(std::format("Could not find constant shader input with name {}", destination.m_argument_name)); } return AcceptShaderConstantArgument(commonDestination, isTransposed, rowCount, codeConstSource, sourceIndex); } - result::Expected AcceptShaderSamplerArgument(const techset::CommonShaderArgCreatorDestination& destination, - const techset::CommonCodeSamplerSource codeSamplerSource) override + std::expected AcceptShaderSamplerArgument(const techset::CommonShaderArgCreatorDestination& destination, + const techset::CommonCodeSamplerSource codeSamplerSource) override { techset::CommonShaderArgDestination commonDestination{}; std::string errorMessage; if (!FindDestinationForSampler(commonDestination, errorMessage, destination)) { if (!errorMessage.empty()) - return result::Unexpected(std::move(errorMessage)); + return std::unexpected(std::move(errorMessage)); - return result::Unexpected(std::format("Could not find sampler shader input with name {}", destination.m_argument_name)); + return std::unexpected(std::format("Could not find sampler shader input with name {}", destination.m_argument_name)); } return AcceptShaderSamplerArgument(commonDestination, codeSamplerSource); } - result::Expected AcceptShaderLiteralArgument(const techset::CommonShaderArgCreatorDestination& destination, - const std::array& literalValue) override + std::expected AcceptShaderLiteralArgument(const techset::CommonShaderArgCreatorDestination& destination, + const std::array& literalValue) override { techset::CommonShaderArgDestination commonDestination{}; bool isTransposed; @@ -147,9 +147,9 @@ namespace if (!FindDestinationForConstant(commonDestination, isTransposed, rowCount, errorMessage, destination)) { if (!errorMessage.empty()) - return result::Unexpected(std::move(errorMessage)); + return std::unexpected(std::move(errorMessage)); - return result::Unexpected(std::format("Could not find constant shader input with name {}", destination.m_argument_name)); + return std::unexpected(std::format("Could not find constant shader input with name {}", destination.m_argument_name)); } techset::CommonShaderArgumentType argumentType{ @@ -158,16 +158,17 @@ namespace }; if (!IsArgumentTypeSupported(argumentType)) - return result::Unexpected(std::format("{} constants are unsupported", ShaderTypeName(argumentType.m_shader_type))); + return std::unexpected(std::format("{} constants are unsupported", ShaderTypeName(argumentType.m_shader_type))); techset::CommonShaderArgValue value{.literal_value = literalValue}; m_args.emplace_back(argumentType, commonDestination, value); - return NoResult{}; + + return {}; } - result::Expected AcceptShaderMaterialArgument(const techset::CommonShaderArgCreatorDestination& destination, - const unsigned nameHash) override + std::expected AcceptShaderMaterialArgument(const techset::CommonShaderArgCreatorDestination& destination, + const unsigned nameHash) override { techset::CommonShaderArgumentType argumentType{ .m_shader_type = m_shader_type, @@ -181,39 +182,40 @@ namespace if (!FindDestinationForConstant(commonDestination, isTransposed, rowCount, errorMessage, destination)) { if (!errorMessage.empty()) - return result::Unexpected(std::move(errorMessage)); + return std::unexpected(std::move(errorMessage)); argumentType.m_value_type = techset::CommonShaderValueType::MATERIAL_SAMPLER; if (!FindDestinationForSampler(commonDestination, errorMessage, destination)) { if (!errorMessage.empty()) - return result::Unexpected(std::move(errorMessage)); + return std::unexpected(std::move(errorMessage)); - return result::Unexpected(std::format("Could not find shader input with name {}", destination.m_argument_name)); + return std::unexpected(std::format("Could not find shader input with name {}", destination.m_argument_name)); } } if (!IsArgumentTypeSupported(argumentType)) { - return result::Unexpected( + return std::unexpected( std::format("{} {} are unsupported", ShaderTypeName(argumentType.m_shader_type), ArgTypeName(argumentType.m_value_type))); } techset::CommonShaderArgValue value{.name_hash = nameHash}; m_args.emplace_back(argumentType, commonDestination, value); - return NoResult{}; + + return {}; } - result::Expected AcceptShaderMaterialArgument(const techset::CommonShaderArgCreatorDestination& destination, - const std::string& nameValue) override + std::expected AcceptShaderMaterialArgument(const techset::CommonShaderArgCreatorDestination& destination, + const std::string& nameValue) override { // All game's call R_HashString here which has the same implementation in all games return AcceptShaderMaterialArgument(destination, djb2_xor_nocase(nameValue.c_str(), 0)); } - result::Expected FinalizePass(techset::CommonTechnique& technique, techset::CommonPass& pass) override + std::expected FinalizePass(techset::CommonTechnique& technique, techset::CommonPass& pass) override { std::ranges::sort(m_args, [this](const techset::CommonShaderArg& arg0, const techset::CommonShaderArg& arg1) -> bool @@ -247,15 +249,15 @@ namespace m_args = std::vector(); m_sampler_flags = 0; - return NoResult{}; + return {}; } protected: - result::Expected AcceptShaderConstantArgument(const techset::CommonShaderArgDestination& commonDestination, - const bool isTransposed, - const unsigned rowCount, - const techset::CommonCodeConstSource codeConstSource, - const unsigned sourceIndex) + std::expected AcceptShaderConstantArgument(const techset::CommonShaderArgDestination& commonDestination, + const bool isTransposed, + const unsigned rowCount, + const techset::CommonCodeConstSource codeConstSource, + const unsigned sourceIndex) { techset::CommonShaderArgumentType argumentType{ .m_shader_type = m_shader_type, @@ -263,11 +265,11 @@ namespace }; if (!IsArgumentTypeSupported(argumentType)) - return result::Unexpected(std::format("{} constants are unsupported", ShaderTypeName(argumentType.m_shader_type))); + return std::unexpected(std::format("{} constants are unsupported", ShaderTypeName(argumentType.m_shader_type))); const auto maybeInfo = m_common_code_source_infos.GetInfoForCodeConstSource(codeConstSource); if (!maybeInfo) - return result::Unexpected("Could not find info for code constant"); + return std::unexpected("Could not find info for code constant"); const auto isMatrix = maybeInfo->transposedMatrix.has_value(); techset::CommonShaderArgCodeConstValue value{ @@ -279,7 +281,7 @@ namespace if (isMatrix) { if (sourceIndex >= 4) - return result::Unexpected(std::format("Index for matrix code const is out of bounds: {} (must be < 4)", sourceIndex)); + return std::unexpected(std::format("Index for matrix code const is out of bounds: {} (must be < 4)", sourceIndex)); value.m_index = isTransposed ? *maybeInfo->transposedMatrix : codeConstSource; value.m_first_row = sourceIndex; @@ -288,7 +290,7 @@ namespace { const auto arrayCount = std::max(maybeInfo->arrayCount, 1u); if (sourceIndex >= arrayCount) - return result::Unexpected(std::format("Index for code const is out of bounds: {} (must be < {})", sourceIndex, arrayCount)); + return std::unexpected(std::format("Index for code const is out of bounds: {} (must be < {})", sourceIndex, arrayCount)); value.m_index = codeConstSource + static_cast(sourceIndex); value.m_first_row = 0; @@ -298,11 +300,11 @@ namespace if (maybeInfo->techFlags && (!maybeInfo->techFlagShaderType || *maybeInfo->techFlagShaderType == m_shader_type)) m_tech_flags |= *maybeInfo->techFlags; - return NoResult{}; + return {}; } - result::Expected AcceptShaderSamplerArgument(const techset::CommonShaderArgDestination& commonDestination, - const techset::CommonCodeSamplerSource codeSamplerSource) + std::expected AcceptShaderSamplerArgument(const techset::CommonShaderArgDestination& commonDestination, + const techset::CommonCodeSamplerSource codeSamplerSource) { techset::CommonShaderArgumentType argumentType{ .m_shader_type = m_shader_type, @@ -310,11 +312,11 @@ namespace }; if (!IsArgumentTypeSupported(argumentType)) - return result::Unexpected(std::format("{} samplers are unsupported", ShaderTypeName(argumentType.m_shader_type))); + return std::unexpected(std::format("{} samplers are unsupported", ShaderTypeName(argumentType.m_shader_type))); const auto maybeInfo = m_common_code_source_infos.GetInfoForCodeSamplerSource(codeSamplerSource); if (!maybeInfo) - return result::Unexpected("Could not find info for code sampler"); + return std::unexpected("Could not find info for code sampler"); m_args.emplace_back(argumentType, commonDestination, techset::CommonShaderArgValue{.code_sampler_source = codeSamplerSource}); if (maybeInfo->techFlags) @@ -322,7 +324,7 @@ namespace if (maybeInfo->customSamplerIndex) m_sampler_flags |= (1 << *maybeInfo->customSamplerIndex); - return NoResult{}; + return {}; } [[nodiscard]] bool IsArgumentTypeSupported(const techset::CommonShaderArgumentType& argumentType) const @@ -369,7 +371,7 @@ namespace std::string& errorMessage, const techset::CommonShaderArgCreatorDestination& input) = 0; - virtual result::Expected AutoCreateMissingArgs() = 0; + virtual std::expected AutoCreateMissingArgs() = 0; techset::ITechniqueShaderLoader& m_shader_loader; techset::CommonCodeSourceInfos& m_common_code_source_infos; @@ -392,7 +394,7 @@ namespace { } - result::Expected EnterShader(const techset::CommonTechniqueShaderType shaderType, const std::string& name) override + std::expected EnterShader(const techset::CommonTechniqueShaderType shaderType, const std::string& name) override { auto result = BaseCommonShaderArgCreator::EnterShader(shaderType, name); if (!result) @@ -400,14 +402,14 @@ namespace m_shader_info = d3d9::ShaderAnalyser::GetShaderInfo(m_bin.m_shader_bin, m_bin.m_shader_bin_size); if (!m_shader_info) - return result::Unexpected(std::format("Failed to analyse dx9 shader {}", name)); + return std::unexpected(std::format("Failed to analyse dx9 shader {}", name)); m_arg_added = std::vector(m_shader_info->m_constants.size(), false); - return NoResult{}; + return {}; } - result::Expected LeaveShader() override + std::expected LeaveShader() override { auto result = BaseCommonShaderArgCreator::LeaveShader(); m_shader_info = nullptr; @@ -473,7 +475,7 @@ namespace return true; } - result::Expected AutoCreateMissingArgs() override + std::expected AutoCreateMissingArgs() override { const auto argCount = m_shader_info->m_constants.size(); for (size_t argIndex = 0; argIndex < argCount; argIndex++) @@ -499,11 +501,11 @@ namespace } } - return NoResult{}; + return {}; } private: - result::Expected AutoCreateConstantArg(const d3d9::ShaderConstant& shaderArg) + std::expected AutoCreateConstantArg(const d3d9::ShaderConstant& shaderArg) { if (!IsArgumentTypeSupported( techset::CommonShaderArgumentType{.m_shader_type = m_shader_type, .m_value_type = techset::CommonShaderValueType::CODE_CONST})) @@ -511,7 +513,7 @@ namespace con::warn("Shader {} uses unsupported argument type \"{} constant\". This may cause unstable behaviour.", m_shader_name, ShaderTypeName(m_shader_type)); - return NoResult{}; + return {}; } const auto maybeCodeConst = m_common_code_source_infos.GetCodeConstSourceForAccessor(shaderArg.m_name); @@ -519,24 +521,24 @@ namespace { // Some variables are simply not added as args for some reason if (m_common_code_source_infos.IsArgAccessorIgnored(shaderArg.m_name)) - return NoResult{}; + return {}; - return result::Unexpected(std::format("Missing assignment to shader constant {}", shaderArg.m_name)); + return std::unexpected(std::format("Missing assignment to shader constant {}", shaderArg.m_name)); } const auto constInfo = m_common_code_source_infos.GetInfoForCodeConstSource(*maybeCodeConst); if (!constInfo) - return result::Unexpected(std::format("Missing info for code const {}", shaderArg.m_name)); + return std::unexpected(std::format("Missing info for code const {}", shaderArg.m_name)); const auto elementSize = ElementSizeForArg(shaderArg); const auto elementCount = utils::Align(shaderArg.m_register_count, elementSize) / elementSize; const auto infoArrayCount = std::max(constInfo->arrayCount, 1); if (elementCount > infoArrayCount) { - return result::Unexpected(std::format("Could not auto create argument for constant {} as it has more elements ({}) than the code constant ({})", - shaderArg.m_name, - elementCount, - infoArrayCount)); + return std::unexpected(std::format("Could not auto create argument for constant {} as it has more elements ({}) than the code constant ({})", + shaderArg.m_name, + elementCount, + infoArrayCount)); } techset::CommonShaderArgDestination commonDestination; @@ -552,7 +554,7 @@ namespace if (constInfo->techFlags && (!constInfo->techFlagShaderType || *constInfo->techFlagShaderType == m_shader_type)) m_tech_flags |= *constInfo->techFlags; - return NoResult{}; + return {}; } [[nodiscard]] static unsigned ElementSizeForArg(const d3d9::ShaderConstant& arg) @@ -567,7 +569,7 @@ namespace } } - result::Expected AutoCreateSamplerArg(const d3d9::ShaderConstant& shaderArg) + std::expected AutoCreateSamplerArg(const d3d9::ShaderConstant& shaderArg) { if (!IsArgumentTypeSupported( techset::CommonShaderArgumentType{.m_shader_type = m_shader_type, .m_value_type = techset::CommonShaderValueType::CODE_SAMPLER})) @@ -575,16 +577,16 @@ namespace con::warn("Shader {} uses unsupported argument type \"{} sampler\". This may cause unstable behaviour.", m_shader_name, ShaderTypeName(m_shader_type)); - return NoResult{}; + return {}; } const auto maybeCodeSampler = m_common_code_source_infos.GetCodeSamplerSourceForAccessor(shaderArg.m_name); if (!maybeCodeSampler) - return result::Unexpected(std::format("Missing assignment to shader texture {}", shaderArg.m_name)); + return std::unexpected(std::format("Missing assignment to shader texture {}", shaderArg.m_name)); const auto samplerInfo = m_common_code_source_infos.GetInfoForCodeSamplerSource(*maybeCodeSampler); if (!samplerInfo) - return result::Unexpected(std::format("Missing info for code sampler {}", shaderArg.m_name)); + return std::unexpected(std::format("Missing info for code sampler {}", shaderArg.m_name)); techset::CommonShaderArgDestination commonDestination; commonDestination.dx9.m_destination_register = shaderArg.m_register_index; @@ -609,7 +611,7 @@ namespace { } - result::Expected EnterShader(const techset::CommonTechniqueShaderType shaderType, const std::string& name) override + std::expected EnterShader(const techset::CommonTechniqueShaderType shaderType, const std::string& name) override { auto result = BaseCommonShaderArgCreator::EnterShader(shaderType, name); if (!result) @@ -617,14 +619,14 @@ namespace m_shader_info = d3d11::ShaderAnalyser::GetShaderInfo(m_bin.m_shader_bin, m_bin.m_shader_bin_size); if (!m_shader_info) - return result::Unexpected(std::format("Failed to analyse dx11 shader {}", name)); + return std::unexpected(std::format("Failed to analyse dx11 shader {}", name)); CountShaderArgs(); - return NoResult{}; + return {}; } - result::Expected LeaveShader() override + std::expected LeaveShader() override { auto result = BaseCommonShaderArgCreator::LeaveShader(); m_shader_info = nullptr; @@ -777,7 +779,7 @@ namespace return true; } - result::Expected AutoCreateMissingArgs() override + std::expected AutoCreateMissingArgs() override { size_t usedConstantCount = 0; size_t textureCount = 0; @@ -791,7 +793,7 @@ namespace return boundResource.m_type == d3d11::BoundResourceType::CBUFFER && boundResource.m_name == buffer.m_name; }); if (bufferBinding == m_shader_info->m_bound_resources.end()) - return result::Unexpected(std::format("Failed to find binding for constant buffer {}", buffer.m_name)); + return std::unexpected(std::format("Failed to find binding for constant buffer {}", buffer.m_name)); for (const auto& variable : buffer.m_variables) { @@ -829,7 +831,7 @@ namespace return std::move(result); } - return NoResult{}; + return {}; } private: @@ -857,7 +859,7 @@ namespace m_texture_arg_added = std::vector(textureCount, false); } - result::Expected AutoCreateConstantArg(const d3d11::ConstantBufferVariable& variable, const size_t bufferIndex) + std::expected AutoCreateConstantArg(const d3d11::ConstantBufferVariable& variable, const size_t bufferIndex) { if (!IsArgumentTypeSupported( techset::CommonShaderArgumentType{.m_shader_type = m_shader_type, .m_value_type = techset::CommonShaderValueType::CODE_CONST})) @@ -865,7 +867,7 @@ namespace con::warn("Shader {} uses unsupported argument type \"{} constant\". This may cause unstable behaviour.", m_shader_name, ShaderTypeName(m_shader_type)); - return NoResult{}; + return {}; } const auto maybeCodeConst = m_common_code_source_infos.GetCodeConstSourceForAccessor(variable.m_name); @@ -873,24 +875,24 @@ namespace { // Some variables are simply not added as args for some reason if (m_common_code_source_infos.IsArgAccessorIgnored(variable.m_name)) - return NoResult{}; + return {}; - return result::Unexpected(std::format("Missing assignment to shader constant {}", variable.m_name)); + return std::unexpected(std::format("Missing assignment to shader constant {}", variable.m_name)); } const auto constInfo = m_common_code_source_infos.GetInfoForCodeConstSource(*maybeCodeConst); if (!constInfo) - return result::Unexpected(std::format("Missing info for code const {}", variable.m_name)); + return std::unexpected(std::format("Missing info for code const {}", variable.m_name)); const auto variableElementCount = std::max(variable.m_element_count, 1); const auto variableElementSize = variable.m_size / variableElementCount; const auto infoArrayCount = std::max(constInfo->arrayCount, 1); if (variableElementCount > infoArrayCount) { - return result::Unexpected(std::format("Could not auto create argument for constant {} as it has more elements ({}) than the code constant ({})", - variable.m_name, - variableElementCount, - infoArrayCount)); + return std::unexpected(std::format("Could not auto create argument for constant {} as it has more elements ({}) than the code constant ({})", + variable.m_name, + variableElementCount, + infoArrayCount)); } techset::CommonShaderArgDestination commonDestination; @@ -908,10 +910,10 @@ namespace if (constInfo->techFlags && (!constInfo->techFlagShaderType || *constInfo->techFlagShaderType == m_shader_type)) m_tech_flags |= *constInfo->techFlags; - return NoResult{}; + return {}; } - result::Expected AutoCreateSamplerArg(const d3d11::BoundResource& textureResource, const unsigned samplerBindPoint) + std::expected AutoCreateSamplerArg(const d3d11::BoundResource& textureResource, const unsigned samplerBindPoint) { if (!IsArgumentTypeSupported( techset::CommonShaderArgumentType{.m_shader_type = m_shader_type, .m_value_type = techset::CommonShaderValueType::CODE_SAMPLER})) @@ -919,16 +921,16 @@ namespace con::warn("Shader {} uses unsupported argument type \"{} sampler\". This may cause unstable behaviour.", m_shader_name, ShaderTypeName(m_shader_type)); - return NoResult{}; + return {}; } const auto maybeCodeSampler = m_common_code_source_infos.GetCodeSamplerSourceForAccessor(textureResource.m_name); if (!maybeCodeSampler) - return result::Unexpected(std::format("Missing assignment to shader texture {}", textureResource.m_name)); + return std::unexpected(std::format("Missing assignment to shader texture {}", textureResource.m_name)); const auto samplerInfo = m_common_code_source_infos.GetInfoForCodeSamplerSource(*maybeCodeSampler); if (!samplerInfo) - return result::Unexpected(std::format("Missing info for code sampler {}", textureResource.m_name)); + return std::unexpected(std::format("Missing info for code sampler {}", textureResource.m_name)); techset::CommonShaderArgDestination commonDestination; commonDestination.dx11.m_location.texture_index = textureResource.m_bind_point; diff --git a/src/ObjCompiling/Techset/CommonShaderArgCreator.h b/src/ObjCompiling/Techset/CommonShaderArgCreator.h index 80ba54de..39b5702b 100644 --- a/src/ObjCompiling/Techset/CommonShaderArgCreator.h +++ b/src/ObjCompiling/Techset/CommonShaderArgCreator.h @@ -2,9 +2,9 @@ #include "Asset/AssetCreationContext.h" #include "Techset/CommonTechnique.h" -#include "Utils/Result.h" #include +#include #include #include #include @@ -38,21 +38,20 @@ namespace techset CommonShaderArgCreator() = default; virtual ~CommonShaderArgCreator() = default; - virtual result::Expected EnterShader(CommonTechniqueShaderType shaderType, const std::string& name) = 0; - virtual result::Expected LeaveShader() = 0; + virtual std::expected EnterShader(CommonTechniqueShaderType shaderType, const std::string& name) = 0; + virtual std::expected LeaveShader() = 0; - virtual result::Expected + virtual std::expected AcceptShaderConstantArgument(const CommonShaderArgCreatorDestination& destination, CommonCodeConstSource codeConstSource, unsigned sourceIndex) = 0; - virtual result::Expected AcceptShaderSamplerArgument(const CommonShaderArgCreatorDestination& destination, - CommonCodeSamplerSource codeSamplerSource) = 0; - virtual result::Expected AcceptShaderLiteralArgument(const CommonShaderArgCreatorDestination& destination, - const std::array& literalValue) = 0; - virtual result::Expected AcceptShaderMaterialArgument(const CommonShaderArgCreatorDestination& destination, - unsigned nameHash) = 0; - virtual result::Expected AcceptShaderMaterialArgument(const CommonShaderArgCreatorDestination& destination, - const std::string& nameValue) = 0; + virtual std::expected AcceptShaderSamplerArgument(const CommonShaderArgCreatorDestination& destination, + CommonCodeSamplerSource codeSamplerSource) = 0; + virtual std::expected AcceptShaderLiteralArgument(const CommonShaderArgCreatorDestination& destination, + const std::array& literalValue) = 0; + virtual std::expected AcceptShaderMaterialArgument(const CommonShaderArgCreatorDestination& destination, unsigned nameHash) = 0; + virtual std::expected AcceptShaderMaterialArgument(const CommonShaderArgCreatorDestination& destination, + const std::string& nameValue) = 0; - virtual result::Expected FinalizePass(techset::CommonTechnique& technique, CommonPass& pass) = 0; + virtual std::expected FinalizePass(techset::CommonTechnique& technique, CommonPass& pass) = 0; static std::unique_ptr CreateDx9(ITechniqueShaderLoader& shaderLoader, AssetCreationContext& context, CommonCodeSourceInfos& commonCodeSourceInfos); diff --git a/src/ObjCompiling/Techset/Parsing/Sequence/TechniqueShaderScopeSequences.cpp b/src/ObjCompiling/Techset/Parsing/Sequence/TechniqueShaderScopeSequences.cpp index 17dbd95c..44cdf737 100644 --- a/src/ObjCompiling/Techset/Parsing/Sequence/TechniqueShaderScopeSequences.cpp +++ b/src/ObjCompiling/Techset/Parsing/Sequence/TechniqueShaderScopeSequences.cpp @@ -241,7 +241,7 @@ namespace techset } } - result::Expected shaderCreatorResult(NoResult{}); + std::expected shaderCreatorResult; if (isSampler) shaderCreatorResult = state->m_shader_arg_creator.AcceptShaderSamplerArgument(destination, samplerSource); else @@ -287,7 +287,7 @@ namespace techset SequenceResult& result, const CommonShaderArgCreatorDestination& destination) { - result::Expected shaderCreatorResult(NoResult{}); + std::expected shaderCreatorResult; if (result.HasNextCapture(CAPTURE_MATERIAL_HASH)) { shaderCreatorResult = state->m_shader_arg_creator.AcceptShaderMaterialArgument( @@ -300,7 +300,7 @@ namespace techset } if (!shaderCreatorResult.has_value()) - throw ParsingException(result.NextCapture(CAPTURE_FIRST_TOKEN).GetPos(), std::move(shaderCreatorResult.error())); + throw ParsingException(result.NextCapture(CAPTURE_FIRST_TOKEN).GetPos(), std::move(shaderCreatorResult).error()); } }; } // namespace techset diff --git a/src/Utils/Utils/Result.h b/src/Utils/Utils/Result.h deleted file mode 100644 index b5d870c0..00000000 --- a/src/Utils/Utils/Result.h +++ /dev/null @@ -1,185 +0,0 @@ -#pragma once - -#include -#include - -using NoResult = std::monostate; - -// Can be replaced by std::expected with c++23 -namespace result -{ - template class Unexpected - { - public: - Unexpected(TError result) - : m_data(std::move(result)) - { - } - - constexpr std::add_lvalue_reference_t value() & - { - return m_data; - } - - constexpr std::add_const_t> value() const& - { - return m_data; - } - - constexpr std::add_rvalue_reference_t value() && - { - return std::move(m_data); - } - - constexpr std::add_const_t> value() const&& - { - return std::move(m_data); - } - - constexpr std::add_lvalue_reference_t operator*() & - { - return m_data; - } - - constexpr std::add_const_t> operator*() const& - { - return m_data; - } - - constexpr std::add_rvalue_reference_t operator*() && - { - return std::move(m_data); - } - - constexpr std::add_const_t> operator*() const&& - { - return std::move(m_data); - } - - constexpr std::add_pointer_t operator->() - { - return m_data; - } - - constexpr std::add_const_t> operator->() const - { - return m_data; - } - - private: - TError m_data; - }; - - template class Expected - { - public: - Expected(TResult result) - : m_data(std::variant(std::in_place_index<0>, std::move(result))) - { - } - - Expected(Unexpected unexpected) - : m_data(std::variant(std::in_place_index<1>, std::move(*unexpected))) - { - } - - constexpr operator bool() const noexcept - { - return m_data.index() == 0; - } - - constexpr bool has_value() const noexcept - { - return m_data.index() == 0; - } - - constexpr std::add_lvalue_reference_t value() & - { - return std::get<0>(m_data); - } - - constexpr std::add_const_t> value() const& - { - return std::get<0>(m_data); - } - - constexpr std::add_rvalue_reference_t value() && - { - return std::move(std::get<0>(m_data)); - } - - constexpr std::add_const_t> value() const&& - { - return std::move(std::get<0>(m_data)); - } - - constexpr std::add_lvalue_reference_t operator*() & - { - return std::get<0>(m_data); - } - - constexpr std::add_const_t> operator*() const& - { - return std::get<0>(m_data); - } - - constexpr std::add_rvalue_reference_t operator*() && - { - return std::move(std::get<0>(m_data)); - } - - constexpr std::add_const_t> operator*() const&& - { - return std::move(std::get<0>(m_data)); - } - - constexpr std::add_pointer_t operator->() - { - return std::get<0>(m_data); - } - - constexpr std::add_const_t> operator->() const - { - return std::get<0>(m_data); - } - - constexpr std::add_lvalue_reference_t error() & - { - return std::get<1>(m_data); - } - - constexpr std::add_const_t> error() const& - { - return std::get<1>(m_data); - } - - constexpr std::add_rvalue_reference_t error() && - { - return std::move(std::get<1>(m_data)); - } - - constexpr std::add_const_t> error() const&& - { - return std::move(std::get<1>(m_data)); - } - - private: - explicit Expected(std::variant data) - : m_data(std::move(data)) - { - } - - std::variant m_data; - }; - -#define ENSURE_RESULT_VAR(var) \ - if (!(var)) \ - return (var); -#define ENSURE_RESULT(expr) \ - { \ - const auto result = (expr); \ - if (!result) \ - return result; \ - } - -} // namespace result diff --git a/src/ZoneLoading/ZoneLoading.cpp b/src/ZoneLoading/ZoneLoading.cpp index 71e2ae0a..6ce60fb7 100644 --- a/src/ZoneLoading/ZoneLoading.cpp +++ b/src/ZoneLoading/ZoneLoading.cpp @@ -11,19 +11,19 @@ using namespace std::string_literals; namespace fs = std::filesystem; -result::Expected, std::string> ZoneLoading::LoadZone(const std::string& path, - std::optional> progressCallback) +std::expected, std::string> ZoneLoading::LoadZone(const std::string& path, + std::optional> progressCallback) { auto zoneName = fs::path(path).filename().replace_extension().string(); std::ifstream file(path, std::fstream::in | std::fstream::binary); if (!file.is_open()) - return result::Unexpected(std::format("Could not open file '{}'.", path)); + return std::unexpected(std::format("Could not open file '{}'.", path)); ZoneHeader header{}; file.read(reinterpret_cast(&header), sizeof(header)); if (file.gcount() != sizeof(header)) - return result::Unexpected(std::format("Failed to read zone header from file '{}'.", path)); + return std::unexpected(std::format("Failed to read zone header from file '{}'.", path)); std::unique_ptr zoneLoader; for (auto game = 0u; game < static_cast(GameId::COUNT); game++) @@ -37,14 +37,14 @@ result::Expected, std::string> ZoneLoading::LoadZone(const } if (!zoneLoader) - return result::Unexpected(std::format("Could not create factory for zone '{}'.", zoneName)); + return std::unexpected(std::format("Could not create factory for zone '{}'.", zoneName)); auto loadedZone = zoneLoader->LoadZone(file); file.close(); if (!loadedZone) - return result::Unexpected("Loading zone failed."s); + return std::unexpected("Loading zone failed."s); return std::move(loadedZone); } diff --git a/src/ZoneLoading/ZoneLoading.h b/src/ZoneLoading/ZoneLoading.h index 3c86ac8e..85d9dfc0 100644 --- a/src/ZoneLoading/ZoneLoading.h +++ b/src/ZoneLoading/ZoneLoading.h @@ -1,14 +1,14 @@ #pragma once #include "Utils/ProgressCallback.h" -#include "Utils/Result.h" #include "Zone/Zone.h" +#include #include class ZoneLoading { public: - static result::Expected, std::string> LoadZone(const std::string& path, - std::optional> progressCallback); + static std::expected, std::string> LoadZone(const std::string& path, + std::optional> progressCallback); };