From 87d8a9c98626be491e87e4b9fad84b862d8aa0c9 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 15 Aug 2018 05:38:37 -0400 Subject: [PATCH] loader: Make ResultStatus directly compatible with fmt We can make the enum class type compatible with fmt by providing an overload of operator<<. While we're at it, perform proper bounds checking. If something exceeds the array, it should be a hard fail, because it's, without a doubt, a programmer error in this case. --- src/core/file_sys/card_image.cpp | 7 +++++-- src/core/loader/loader.cpp | 12 ++++-------- src/core/loader/loader.h | 4 ++-- src/yuzu/main.cpp | 5 ++++- src/yuzu_cmd/yuzu.cpp | 4 +++- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/core/file_sys/card_image.cpp b/src/core/file_sys/card_image.cpp index 8e05b9d0e..060948f9e 100644 --- a/src/core/file_sys/card_image.cpp +++ b/src/core/file_sys/card_image.cpp @@ -4,11 +4,14 @@ #include #include -#include + +#include + #include "common/logging/log.h" #include "core/file_sys/card_image.h" #include "core/file_sys/partition_filesystem.h" #include "core/file_sys/vfs_offset.h" +#include "core/loader/loader.h" namespace FileSys { @@ -142,7 +145,7 @@ Loader::ResultStatus XCI::AddNCAFromPartition(XCIPartition part) { const u16 error_id = static_cast(nca->GetStatus()); LOG_CRITICAL(Loader, "Could not load NCA {}/{}, failed with error code {:04X} ({})", partition_names[static_cast(part)], nca->GetName(), error_id, - Loader::GetMessageForResultStatus(nca->GetStatus())); + nca->GetStatus()); } } diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp index b143f043c..5e07a3f10 100644 --- a/src/core/loader/loader.cpp +++ b/src/core/loader/loader.cpp @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #include +#include #include #include "common/logging/log.h" #include "common/string_util.h" @@ -119,14 +120,9 @@ constexpr std::array RESULT_MESSAGES{ "There is no control data available.", }; -std::string GetMessageForResultStatus(ResultStatus status) { - return GetMessageForResultStatus(static_cast(status)); -} - -std::string GetMessageForResultStatus(u16 status) { - if (status >= 36) - return ""; - return RESULT_MESSAGES[status]; +std::ostream& operator<<(std::ostream& os, ResultStatus status) { + os << RESULT_MESSAGES.at(static_cast(status)); + return os; } /** diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h index 6dffe451a..b74cfbf8a 100644 --- a/src/core/loader/loader.h +++ b/src/core/loader/loader.h @@ -5,6 +5,7 @@ #pragma once #include +#include #include #include #include @@ -94,8 +95,7 @@ enum class ResultStatus : u16 { ErrorNoControl, }; -std::string GetMessageForResultStatus(ResultStatus status); -std::string GetMessageForResultStatus(u16 status); +std::ostream& operator<<(std::ostream& os, ResultStatus status); /// Interface for loading an application class AppLoader : NonCopyable { diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index 94fb8ae6a..4bbea3f3c 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -6,7 +6,10 @@ #include #include #include + +#include #include + #define QT_NO_OPENGL #include #include @@ -454,7 +457,7 @@ bool GMainWindow::LoadROM(const QString& filename) { "While attempting to load the ROM requested, an error occured. Please " "refer to the yuzu wiki for more information or the yuzu discord for " "additional help.\n\nError Code: {:04X}-{:04X}\nError Description: {}", - loader_id, error_id, Loader::GetMessageForResultStatus(error_id)))); + loader_id, error_id, static_cast(error_id)))); } else { QMessageBox::critical( this, tr("Error while loading ROM!"), diff --git a/src/yuzu_cmd/yuzu.cpp b/src/yuzu_cmd/yuzu.cpp index e44a98311..9095cf27d 100644 --- a/src/yuzu_cmd/yuzu.cpp +++ b/src/yuzu_cmd/yuzu.cpp @@ -7,6 +7,8 @@ #include #include +#include + #include "common/common_paths.h" #include "common/logging/backend.h" #include "common/logging/filter.h" @@ -194,7 +196,7 @@ int main(int argc, char** argv) { "While attempting to load the ROM requested, an error occured. Please " "refer to the yuzu wiki for more information or the yuzu discord for " "additional help.\n\nError Code: {:04X}-{:04X}\nError Description: {}", - loader_id, error_id, Loader::GetMessageForResultStatus(error_id)); + loader_id, error_id, static_cast(error_id)); } }