result: Make fully constexpr, add ON_RESULT_INCLUDED

This commit is contained in:
FearlessTobi 2024-01-18 22:39:15 +01:00 committed by Liam
parent c60ab6bbf6
commit 54372fdff5
9 changed files with 57 additions and 29 deletions

View file

@ -12,7 +12,7 @@ void DefaultErrorApplet::Close() const {}
void DefaultErrorApplet::ShowError(Result error, FinishedCallback finished) const {
LOG_CRITICAL(Service_Fatal, "Application requested error display: {:04}-{:04} (raw={:08X})",
error.module.Value(), error.description.Value(), error.raw);
error.GetModule(), error.GetDescription(), error.raw);
}
void DefaultErrorApplet::ShowErrorWithTimestamp(Result error, std::chrono::seconds time,
@ -20,7 +20,7 @@ void DefaultErrorApplet::ShowErrorWithTimestamp(Result error, std::chrono::secon
LOG_CRITICAL(
Service_Fatal,
"Application requested error display: {:04X}-{:04X} (raw={:08X}) with timestamp={:016X}",
error.module.Value(), error.description.Value(), error.raw, time.count());
error.GetModule(), error.GetDescription(), error.raw, time.count());
}
void DefaultErrorApplet::ShowCustomErrorText(Result error, std::string main_text,
@ -28,7 +28,7 @@ void DefaultErrorApplet::ShowCustomErrorText(Result error, std::string main_text
FinishedCallback finished) const {
LOG_CRITICAL(Service_Fatal,
"Application requested custom error with error_code={:04X}-{:04X} (raw={:08X})",
error.module.Value(), error.description.Value(), error.raw);
error.GetModule(), error.GetDescription(), error.raw);
LOG_CRITICAL(Service_Fatal, " Main Text: {}", main_text);
LOG_CRITICAL(Service_Fatal, " Detail Text: {}", detail_text);
}

View file

@ -189,14 +189,14 @@ enum class ErrorModule : u32 {
union Result {
u32 raw;
BitField<0, 9, ErrorModule> module;
BitField<9, 13, u32> description;
using Module = BitField<0, 9, ErrorModule>;
using Description = BitField<9, 13, u32>;
Result() = default;
constexpr explicit Result(u32 raw_) : raw(raw_) {}
constexpr Result(ErrorModule module_, u32 description_)
: raw(module.FormatValue(module_) | description.FormatValue(description_)) {}
: raw(Module::FormatValue(module_) | Description::FormatValue(description_)) {}
[[nodiscard]] constexpr bool IsSuccess() const {
return raw == 0;
@ -211,7 +211,15 @@ union Result {
}
[[nodiscard]] constexpr u32 GetInnerValue() const {
return static_cast<u32>(module.Value()) | (description << module.bits);
return raw;
}
[[nodiscard]] constexpr ErrorModule GetModule() const {
return Module::ExtractValue(raw);
}
[[nodiscard]] constexpr u32 GetDescription() const {
return Description::ExtractValue(raw);
}
[[nodiscard]] constexpr bool Includes(Result result) const {
@ -274,8 +282,9 @@ public:
}
[[nodiscard]] constexpr bool Includes(Result other) const {
return code.module == other.module && code.description <= other.description &&
other.description <= description_end;
return code.GetModule() == other.GetModule() &&
code.GetDescription() <= other.GetDescription() &&
other.GetDescription() <= description_end;
}
private:
@ -330,6 +339,16 @@ constexpr bool EvaluateResultFailure(const Result& r) {
return R_FAILED(r);
}
template <auto... R>
constexpr bool EvaluateAnyResultIncludes(const Result& r) {
return ((r == R) || ...);
}
template <auto... R>
constexpr bool EvaluateResultNotIncluded(const Result& r) {
return !EvaluateAnyResultIncludes<R...>(r);
}
template <typename T>
constexpr void UpdateCurrentResultReference(T result_reference, Result result) = delete;
// Intentionally not defined
@ -371,6 +390,13 @@ constexpr void UpdateCurrentResultReference<const Result>(Result result_referenc
DECLARE_CURRENT_RESULT_REFERENCE_AND_STORAGE(__COUNTER__); \
ON_RESULT_SUCCESS_2
#define ON_RESULT_INCLUDED_2(...) \
ON_RESULT_RETURN_IMPL(ResultImpl::EvaluateAnyResultIncludes<__VA_ARGS__>)
#define ON_RESULT_INCLUDED(...) \
DECLARE_CURRENT_RESULT_REFERENCE_AND_STORAGE(__COUNTER__); \
ON_RESULT_INCLUDED_2(__VA_ARGS__)
constexpr inline Result __TmpCurrentResultReference = ResultSuccess;
/// Returns a result.

View file

@ -27,8 +27,8 @@ struct ErrorCode {
static constexpr ErrorCode FromResult(Result result) {
return {
.error_category{2000 + static_cast<u32>(result.module.Value())},
.error_number{result.description.Value()},
.error_category{2000 + static_cast<u32>(result.GetModule())},
.error_number{result.GetDescription()},
};
}

View file

@ -139,7 +139,8 @@ private:
ctx.WriteBufferC(performance_buffer.data(), performance_buffer.size(), 1);
}
} else {
LOG_ERROR(Service_Audio, "RequestUpdate failed error 0x{:02X}!", result.description);
LOG_ERROR(Service_Audio, "RequestUpdate failed error 0x{:02X}!",
result.GetDescription());
}
IPC::ResponseBuilder rb{ctx, 2};

View file

@ -202,14 +202,14 @@ Result IAlbumAccessorService::TranslateResult(Result in_result) {
}
if ((in_result.raw & 0x3801ff) == ResultUnknown1024.raw) {
if (in_result.description - 0x514 < 100) {
if (in_result.GetDescription() - 0x514 < 100) {
return ResultInvalidFileData;
}
if (in_result.description - 0x5dc < 100) {
if (in_result.GetDescription() - 0x5dc < 100) {
return ResultInvalidFileData;
}
if (in_result.description - 0x578 < 100) {
if (in_result.GetDescription() - 0x578 < 100) {
if (in_result == ResultFileCountLimit) {
return ResultUnknown22;
}
@ -244,9 +244,10 @@ Result IAlbumAccessorService::TranslateResult(Result in_result) {
return ResultUnknown1024;
}
if (in_result.module == ErrorModule::FS) {
if ((in_result.description >> 0xc < 0x7d) || (in_result.description - 1000 < 2000) ||
(((in_result.description - 3000) >> 3) < 0x271)) {
if (in_result.GetModule() == ErrorModule::FS) {
if ((in_result.GetDescription() >> 0xc < 0x7d) ||
(in_result.GetDescription() - 1000 < 2000) ||
(((in_result.GetDescription() - 3000) >> 3) < 0x271)) {
// TODO: Translate FS error
return in_result;
}

View file

@ -73,8 +73,8 @@ static void GenerateErrorReport(Core::System& system, Result error_code, const F
"Program entry point: 0x{:16X}\n"
"\n",
Common::g_scm_branch, Common::g_scm_desc, title_id, error_code.raw,
2000 + static_cast<u32>(error_code.module.Value()),
static_cast<u32>(error_code.description.Value()), info.set_flags, info.program_entry_point);
2000 + static_cast<u32>(error_code.GetModule()),
static_cast<u32>(error_code.GetDescription()), info.set_flags, info.program_entry_point);
if (info.backtrace_size != 0x0) {
crash_report += "Registers:\n";
for (size_t i = 0; i < info.registers.size(); i++) {

View file

@ -301,7 +301,7 @@ Result NfcInterface::TranslateResultToServiceError(Result result) const {
return result;
}
if (result.module != ErrorModule::NFC) {
if (result.GetModule() != ErrorModule::NFC) {
return result;
}

View file

@ -68,8 +68,8 @@ json GetReportCommonData(u64 title_id, Result result, const std::string& timesta
auto out = json{
{"title_id", fmt::format("{:016X}", title_id)},
{"result_raw", fmt::format("{:08X}", result.raw)},
{"result_module", fmt::format("{:08X}", static_cast<u32>(result.module.Value()))},
{"result_description", fmt::format("{:08X}", result.description.Value())},
{"result_module", fmt::format("{:08X}", static_cast<u32>(result.GetModule()))},
{"result_description", fmt::format("{:08X}", result.GetDescription())},
{"timestamp", timestamp},
};

View file

@ -25,8 +25,8 @@ void QtErrorDisplay::ShowError(Result error, FinishedCallback finished) const {
callback = std::move(finished);
emit MainWindowDisplayError(
tr("Error Code: %1-%2 (0x%3)")
.arg(static_cast<u32>(error.module.Value()) + 2000, 4, 10, QChar::fromLatin1('0'))
.arg(error.description, 4, 10, QChar::fromLatin1('0'))
.arg(static_cast<u32>(error.GetModule()) + 2000, 4, 10, QChar::fromLatin1('0'))
.arg(error.GetDescription(), 4, 10, QChar::fromLatin1('0'))
.arg(error.raw, 8, 16, QChar::fromLatin1('0')),
tr("An error has occurred.\nPlease try again or contact the developer of the software."));
}
@ -38,8 +38,8 @@ void QtErrorDisplay::ShowErrorWithTimestamp(Result error, std::chrono::seconds t
const QDateTime date_time = QDateTime::fromSecsSinceEpoch(time.count());
emit MainWindowDisplayError(
tr("Error Code: %1-%2 (0x%3)")
.arg(static_cast<u32>(error.module.Value()) + 2000, 4, 10, QChar::fromLatin1('0'))
.arg(error.description, 4, 10, QChar::fromLatin1('0'))
.arg(static_cast<u32>(error.GetModule()) + 2000, 4, 10, QChar::fromLatin1('0'))
.arg(error.GetDescription(), 4, 10, QChar::fromLatin1('0'))
.arg(error.raw, 8, 16, QChar::fromLatin1('0')),
tr("An error occurred on %1 at %2.\nPlease try again or contact the developer of the "
"software.")
@ -53,8 +53,8 @@ void QtErrorDisplay::ShowCustomErrorText(Result error, std::string dialog_text,
callback = std::move(finished);
emit MainWindowDisplayError(
tr("Error Code: %1-%2 (0x%3)")
.arg(static_cast<u32>(error.module.Value()) + 2000, 4, 10, QChar::fromLatin1('0'))
.arg(error.description, 4, 10, QChar::fromLatin1('0'))
.arg(static_cast<u32>(error.GetModule()) + 2000, 4, 10, QChar::fromLatin1('0'))
.arg(error.GetDescription(), 4, 10, QChar::fromLatin1('0'))
.arg(error.raw, 8, 16, QChar::fromLatin1('0')),
tr("An error has occurred.\n\n%1\n\n%2")
.arg(QString::fromStdString(dialog_text))