From 08336566e5c7ea03d50055719fb54be5ec86874a Mon Sep 17 00:00:00 2001 From: Fuzzy2319 Date: Sun, 4 Oct 2026 21:27:00 +0200 Subject: [PATCH 1/3] add:err header modifications from nnheaders#74 --- include/nn/err.h | 54 +-- include/nn/err/detail/err_MessageKind.h | 8 + include/nn/err/err_ApplicationErrorArg.h | 31 ++ include/nn/err/err_ErrorCode.h | 23 ++ .../nn/err/err_ErrorMessageDatabaseVersion.h | 13 + include/nn/err/err_ErrorResultVariant.h | 34 ++ include/nn/err/err_ResultBacktrace.h | 13 + include/nn/err/err_SystemErrorArg.h | 37 ++ include/nn/err/err_detail.h | 60 +++ include/nn/fs/fs_Mount.h | 5 + include/nn/settings.h | 4 + include/vapours/results/results_common.hpp | 344 ++++++++++-------- 12 files changed, 451 insertions(+), 175 deletions(-) create mode 100644 include/nn/err/detail/err_MessageKind.h create mode 100644 include/nn/err/err_ApplicationErrorArg.h create mode 100644 include/nn/err/err_ErrorCode.h create mode 100644 include/nn/err/err_ErrorMessageDatabaseVersion.h create mode 100644 include/nn/err/err_ErrorResultVariant.h create mode 100644 include/nn/err/err_ResultBacktrace.h create mode 100644 include/nn/err/err_SystemErrorArg.h create mode 100644 include/nn/err/err_detail.h diff --git a/include/nn/err.h b/include/nn/err.h index eee3a08..1488fb0 100644 --- a/include/nn/err.h +++ b/include/nn/err.h @@ -1,30 +1,40 @@ #pragma once +#include +#include +#include +#include +#include +#include +#include #include +#include namespace nn::err { -enum ErrorCodeCategoryType : uint32_t { - unk1, - unk2, -}; +class EulaData; // TODO +class ErrorViewerJumpDestination {}; // TODO -class ApplicationErrorArg { -public: - ApplicationErrorArg(); - ApplicationErrorArg(uint32_t error_code, const char* dialog_message, - const char* fullscreen_message, - const nn::settings::LanguageCode& languageCode); - void SetApplicationErrorCodeNumber(uint32_t error_code); - void SetDialogMessage(const char* message); - void SetFullScreenMessage(const char* message); - - uint64_t unk; - uint32_t error_code; - nn::settings::LanguageCode language_code; - char dialog_message[2048]; - char fullscreen_message[2048]; -}; - -uint32_t MakeErrorCode(ErrorCodeCategoryType err_category_type, uint32_t errorCodeNumber); +ErrorCode ConvertResultToErrorCode(const Result& result); +bool CreateErrorViewerStartupParamForRecordedError(void*, std::uint64_t*, std::uint64_t, + const char*, const char*, time::PosixTime); +void ExecuteJump(ErrorViewerJumpDestination destination); +void GetErrorCodeString(char* outErrorCodeString, std::size_t errorCodeStrBufferSize, + ErrorCode errorCode); +void* GetErrorMessageDatabaseVersion(ErrorMessageDatabaseVersion* outVersion); +ErrorCode MakeErrorCode(std::uint32_t category, std::uint32_t number); void ShowApplicationError(const ApplicationErrorArg& arg); +void ShowError(Result result); +void ShowError(ErrorCode errorCode); +void ShowError(const ErrorResultVariant& errorResultVariant); +void ShowError(Result result, ResultBacktrace& backtrace); +void ShowErrorRecord(Result result, time::PosixTime timestamp); +void ShowErrorRecord(ErrorCode errorCode, time::PosixTime timestamp); +void ShowErrorRecord(const void*, std::uint64_t); +void ShowErrorWithoutJump(Result result); +void ShowErrorWithoutJump(ErrorCode errorCode); +void ShowEula(settings::system::RegionCode regionCode); +void ShowSystemError(const SystemErrorArg& arg); +void ShowSystemUpdateEula(settings::system::RegionCode regionCode, EulaData& data); +void ShowUnacceptableAddOnContentVersionError(); +void ShowUnacceptableApplicationVersionError(); } // namespace nn::err diff --git a/include/nn/err/detail/err_MessageKind.h b/include/nn/err/detail/err_MessageKind.h new file mode 100644 index 0000000..28a3033 --- /dev/null +++ b/include/nn/err/detail/err_MessageKind.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +namespace nn::err::detail { +// What does Flv stand for? +enum class MessageKind : std::uint8_t { DialogueMessage, DialogueButton, FlvMessage, FlvButton }; +} // namespace nn::err::detail diff --git a/include/nn/err/err_ApplicationErrorArg.h b/include/nn/err/err_ApplicationErrorArg.h new file mode 100644 index 0000000..680fc35 --- /dev/null +++ b/include/nn/err/err_ApplicationErrorArg.h @@ -0,0 +1,31 @@ +#pragma once + +#include + +namespace nn::err { +class ApplicationErrorArg { +public: + ApplicationErrorArg(); + ApplicationErrorArg(std::uint32_t errorCode, const char* dialogMessage, + const char* fullScreenMessage, const settings::LanguageCode& languageCode); + + std::uint32_t GetApplicationErrorCodeNumber() const; + const char* GetDialogMessage() const; + const char* GetFullScreenMessage() const; + settings::LanguageCode GetLanguageCode() const; + + void SetApplicationErrorCodeNumber(std::uint32_t errorCode); + void SetDialogMessage(const char* message); + void SetFullScreenMessage(const char* message); + void SetLanguageCode(const settings::LanguageCode& languageCode); + +private: + std::int8_t _0 = 2; + std::int8_t _1 = 1; + std::int8_t padding[6]; + std::uint32_t m_ErrorCode = 0; + settings::LanguageCode m_LanguageCode; + char m_DialogMessage[2048]; + char m_FullScreenMessage[2048]; +}; +} // namespace nn::err diff --git a/include/nn/err/err_ErrorCode.h b/include/nn/err/err_ErrorCode.h new file mode 100644 index 0000000..db0250c --- /dev/null +++ b/include/nn/err/err_ErrorCode.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +namespace nn::err { +class ErrorCode { +public: + static ErrorCode GetInvalidErrorCode(); + + ErrorCode() {} + ErrorCode(std::uint32_t category, std::uint32_t number) + : m_Category(category), m_Number(number) {} + + bool IsValid() const; + + std::uint32_t GetCategory() const { return m_Category; } + std::uint32_t GetNumber() const { return m_Number; } + +private: + std::uint32_t m_Category = 0; + std::uint32_t m_Number = 0; +}; +} // namespace nn::err diff --git a/include/nn/err/err_ErrorMessageDatabaseVersion.h b/include/nn/err/err_ErrorMessageDatabaseVersion.h new file mode 100644 index 0000000..f9885bd --- /dev/null +++ b/include/nn/err/err_ErrorMessageDatabaseVersion.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +namespace nn::err { +class ErrorMessageDatabaseVersion { +public: +private: + std::int32_t m_Version; +}; + +static_assert(sizeof(ErrorMessageDatabaseVersion) == 4); +} // namespace nn::err diff --git a/include/nn/err/err_ErrorResultVariant.h b/include/nn/err/err_ErrorResultVariant.h new file mode 100644 index 0000000..100c136 --- /dev/null +++ b/include/nn/err/err_ErrorResultVariant.h @@ -0,0 +1,34 @@ +#pragma once + +#include +#include + +namespace nn::err { +class ErrorResultVariant { +public: + enum class State : std::uint32_t { + Undefined, + Result, + ErrorCode, + }; + + ErrorResultVariant(); + ErrorResultVariant(const Result& result); + ErrorResultVariant(const ErrorCode& errorCode); + + State GetState() const; + + operator ErrorCode() const; + operator Result() const; + + void operator=(const Result& result); + void operator=(const ErrorCode& errorCode); + +private: + State m_State = State::Undefined; + union { + Result m_Result; + ErrorCode m_ErrorCode; + } m_Value; +}; +} // namespace nn::err diff --git a/include/nn/err/err_ResultBacktrace.h b/include/nn/err/err_ResultBacktrace.h new file mode 100644 index 0000000..498fc39 --- /dev/null +++ b/include/nn/err/err_ResultBacktrace.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +namespace nn::err { +class ResultBacktrace { +public: + static void Make(ResultBacktrace* outBackTrace, const Result*, std::int32_t); + +private: + // TODO +}; +} // namespace nn::err diff --git a/include/nn/err/err_SystemErrorArg.h b/include/nn/err/err_SystemErrorArg.h new file mode 100644 index 0000000..cdd22e8 --- /dev/null +++ b/include/nn/err/err_SystemErrorArg.h @@ -0,0 +1,37 @@ +#pragma once + +#include +#include + +namespace nn::err { +class SystemErrorArg { +public: + SystemErrorArg(); + SystemErrorArg(ErrorCode errorCode, const char* dialogMessage, const char* fullScreenMessage, + const settings::LanguageCode& languageCode); + + void SetErrorCode(ErrorCode errorCode); + void SetDialogMessage(const char* message); + void SetFullScreenMessage(const char* message); + void SetLanguageCode(const settings::LanguageCode& languageCode); + ErrorCode GetErrorCode() const; + const char* GetDialogMessage() const; + const char* GetFullScreenMessage() const; + settings::LanguageCode GetLanguageCode() const; + void GetStartupParam() const; + +private: + std::int8_t _0 = 1; + std::int8_t _1 = 0; + std::int8_t _2 = 0; + std::int8_t _3 = 0; + std::int8_t _4 = 0; + std::int8_t _5 = 0; + std::int8_t _6 = 0; + std::int8_t _7 = 0; + ErrorCode m_ErrorCode; + settings::LanguageCode m_LanguageCode; + char m_DialogMessage[2048]; + char m_FullScreenMessage[2048]; +}; +} // namespace nn::err diff --git a/include/nn/err/err_detail.h b/include/nn/err/err_detail.h new file mode 100644 index 0000000..8e2dcae --- /dev/null +++ b/include/nn/err/err_detail.h @@ -0,0 +1,60 @@ +#pragma once + +#include +#include +#include +#include + +namespace nn::ns { +// TODO: move +class ApplicationErrorCodeCategory { +public: + const char* GetCategory() const { return m_Category; } + +private: + char m_Category[8]; +}; +} // namespace nn::ns + +namespace nn::err { +class ErrorMessageDatabaseVersion; +} // namespace nn::err + +namespace nn::err::detail { +bool CategoryExists(std::uint32_t category); +bool DefaultErrorMessageDataExists(std::uint32_t category); +bool ErrorMessageDataExists(ErrorCode errorCode); +bool IsApplicationErrorCodeString(const char* errorCodeString); +void MakeApplicationErrorCodeString( + char* outErrorCodeString, std::size_t errorCodeStringBufferSize, + const ns::ApplicationErrorCodeCategory& applicationErrorCodeCategory, + std::uint32_t errorCodeNumber); +void MakeErrorCodeString(char* outErrorCodeString, std::size_t errorCodeStringBufferSize, + ErrorCode errorCode); +void MakeErrorInfoCommonFilePath(char* outErrorInfoCommonFilePath, + std::size_t errorInfoCommonFilePathBufferSize, + ErrorCode errorCode); +void MakeErrorInfoDirectoryPath(char* outErrorInfoDirectoryPath, + std::size_t errorInfoDirectoryPathBufferSize, ErrorCode errorCode); +void MakeErrorInfoMessageFilePath(char* outErrorInfoMessageFilePath, + std::size_t errorInfoMessageFilePathBufferSize, + ErrorCode errorCode, settings::LanguageCode languageCode, + MessageKind messageKind); +void MakeErrorInfoModuleDirectoryPath(char* outErrorInfoModuleDirectoryPath, + std::size_t errorInfoModuleDirectoryPathBufferSize, + std::uint32_t errorCodeCategory); +void ParseApplicationErrorCodeString( + ns::ApplicationErrorCodeCategory* outApplicationErrorCodeCategory, + std::uint32_t* outErrorCodeCategoryNumber, const char* errorCodeString); +void ParseErrorCodeString(ErrorCode* outErrorCode, const char* errorCodeString); +void* ReadMessageFile(char16_t* outBuffer, std::int32_t* outMessageLength, + std::size_t messageBufferSize, ErrorCode errorCode, + settings::LanguageCode languageCode, MessageKind messageKind); +void ReadMessageFile(char16_t* outBuffer, std::size_t bufferSize, const char* errorCodeString, + const settings::LanguageCode& languageCode); +void ReadVersion(ErrorMessageDatabaseVersion* outVersion); +bool TryParseApplicationErrorCodeString( + ns::ApplicationErrorCodeCategory* outApplicationErrorCodeCategory, + std::uint32_t* outErrorCodeCategoryNumber, const char* errorCodeString); +bool TryParseErrorCodeString(ErrorCode* outErrorCode, const char* errorCodeString); +} // namespace nn::err::detail diff --git a/include/nn/fs/fs_Mount.h b/include/nn/fs/fs_Mount.h index f65d594..cfdc0d7 100644 --- a/include/nn/fs/fs_Mount.h +++ b/include/nn/fs/fs_Mount.h @@ -3,6 +3,10 @@ #include #include +namespace nn::ncm { +enum class SystemDataId : std::uint64_t { Err = 0x10000000000801 }; // TODO: move +} // namespace nn::ncm + namespace nn::fs { /* @@ -12,6 +16,7 @@ namespace nn::fs { bool MountSdCardForDebug(const char* mount); Result MountSdCard(const char* mountPoint); +Result MountSystemData(const char* mountPoint, ncm::SystemDataId id); bool IsSdCardInserted(); Result FormatSdCard(); Result FormatSdCardDryRun(); diff --git a/include/nn/settings.h b/include/nn/settings.h index ba99ee1..c2cb6ca 100644 --- a/include/nn/settings.h +++ b/include/nn/settings.h @@ -53,6 +53,10 @@ struct FirmwareVersion { } }; +struct RegionCode { + std::int32_t code; +}; + Result GetFirmwareVersion(FirmwareVersion*); } // namespace system diff --git a/include/vapours/results/results_common.hpp b/include/vapours/results/results_common.hpp index e124af0..47aa010 100644 --- a/include/vapours/results/results_common.hpp +++ b/include/vapours/results/results_common.hpp @@ -17,198 +17,236 @@ * see /include/vapours/LICENSE */ #pragma once -#include #include +#include #include namespace nn { - namespace result::detail { - - class ResultTraits { - public: - using BaseType = std::uint32_t; - static const BaseType SuccessValue = BaseType(); - static const int ModuleBits = 9; - static const int DescriptionBits = 13; - static const int ReservedBits = 10; - static_assert(ModuleBits + DescriptionBits + ReservedBits == sizeof(BaseType) * CHAR_BIT, "ModuleBits + DescriptionBits + ReservedBits == sizeof(BaseType) * CHAR_BIT"); - private: - static BaseType GetBitsValue(BaseType v, int ofs, int num) noexcept { - return (v >> ofs) & ~(~BaseType() << num); - } - public: - template - struct MakeInnerValueStatic : public std::integral_constant { - static_assert(M < (1 << ModuleBits), "Invalid Module"); - static_assert(D < (1 << DescriptionBits), "Invalid Description"); - }; - - static BaseType MakeInnerValue(int m, int d) noexcept { - return (static_cast(m) << 0) | (static_cast(d) << ModuleBits); - } - - static BaseType GetModuleFromValue(BaseType value) noexcept { - return GetBitsValue(value, 0, ModuleBits); - } - - static BaseType GetDescriptionFromValue(BaseType value) noexcept { - return GetBitsValue(value, ModuleBits, DescriptionBits); - } - }; - - /* Use CRTP for Results. */ - template - class ResultBase { - public: - using BaseType = typename ResultTraits::BaseType; - static const BaseType SuccessValue = ResultTraits::SuccessValue; - - int GetModule() const noexcept { - return static_cast(ResultTraits::GetModuleFromValue(static_cast(*this).GetInnerValueForDebug())); - } - - int GetDescription() const noexcept { - return static_cast(ResultTraits::GetDescriptionFromValue(static_cast(*this).GetInnerValueForDebug())); - } - }; - - class ResultInternalAccessor; - +namespace result::detail { + +class ResultTraits { +public: + using BaseType = std::uint32_t; + static const BaseType SuccessValue = BaseType(); + static const int ModuleBits = 9; + static const int DescriptionBits = 13; + static const int ReservedBits = 10; + static_assert(ModuleBits + DescriptionBits + ReservedBits == sizeof(BaseType) * CHAR_BIT, + "ModuleBits + DescriptionBits + ReservedBits == sizeof(BaseType) * CHAR_BIT"); + +private: + static BaseType GetBitsValue(BaseType v, int ofs, int num) noexcept { + return (v >> ofs) & ~(~BaseType() << num); } - class ResultSuccess; +public: + template + struct MakeInnerValueStatic : public std::integral_constant { + static_assert(M < (1 << ModuleBits), "Invalid Module"); + static_assert(D < (1 << DescriptionBits), "Invalid Description"); + }; - class Result : public result::detail::ResultBase { - friend class result::detail::ResultInternalAccessor; - private: - using Base = typename result::detail::ResultBase; - private: - BaseType m_value; - private: - explicit Result(BaseType v) noexcept : m_value(v) { /* ... */ } - public: - Result() noexcept { /* ... */ } + static BaseType MakeInnerValue(int m, int d) noexcept { + return (static_cast(m) << 0) | (static_cast(d) << ModuleBits); + } - BaseType GetInnerValueForDebug() const noexcept { return m_value; } + static BaseType GetModuleFromValue(BaseType value) noexcept { + return GetBitsValue(value, 0, ModuleBits); + } - bool IsSuccess() const noexcept { return m_value == SuccessValue; } - bool IsFailure() const noexcept { return !IsSuccess(); } + static BaseType GetDescriptionFromValue(BaseType value) noexcept { + return GetBitsValue(value, ModuleBits, DescriptionBits); + } - operator ResultSuccess() const noexcept; - static bool CanAccept(Result result) noexcept; + static BaseType GetInnerValue(BaseType value) noexcept { + return GetBitsValue(value, 0, ModuleBits + DescriptionBits); + } +}; + +/* Use CRTP for Results. */ +template +class ResultBase { +public: + using BaseType = typename ResultTraits::BaseType; + static const BaseType SuccessValue = ResultTraits::SuccessValue; + + __attribute__((always_inline)) BaseType GetModule() const noexcept { + return ResultTraits::GetModuleFromValue( + static_cast(*this).GetInnerValueForDebug()); + } - int GetModule() const noexcept { return Base::GetModule(); } - int GetDescription() const noexcept { return Base::GetDescription(); } - }; - static_assert(sizeof(Result) == sizeof(result::detail::ResultTraits::BaseType), "sizeof(Result) == sizeof(detail::ResultTraits::BaseType)"); - static_assert(std::is_trivially_destructible::value, "std::is_trivially_destructible::value"); + __attribute__((always_inline)) BaseType GetDescription() const noexcept { + return ResultTraits::GetDescriptionFromValue( + static_cast(*this).GetInnerValueForDebug()); + } - inline bool Result::CanAccept(Result result) noexcept { - (void)(result); - return true; + __attribute__((always_inline)) BaseType GetValue() const noexcept { + return ResultTraits::GetInnerValue(static_cast(*this).GetInnerValueForDebug()); } +}; - namespace result::detail { +class ResultInternalAccessor; - class ResultInternalAccessor { - public: - static Result ConstructResult(ResultTraits::BaseType v) noexcept { return Result(v); } - }; +} // namespace result::detail - inline Result ConstructResult(ResultTraits::BaseType v) noexcept { return ResultInternalAccessor::ConstructResult(v); } +class ResultSuccess; - } +class Result : public result::detail::ResultBase { + friend class result::detail::ResultInternalAccessor; - class ResultSuccess : public result::detail::ResultBase { - public: - using Base = typename result::detail::ResultBase; - public: - operator Result() const noexcept { return result::detail::ConstructResult(SuccessValue); } +private: + using Base = typename result::detail::ResultBase; - BaseType GetInnerValueForDebug() const noexcept { return SuccessValue; } +private: + BaseType m_value; - bool IsSuccess() const noexcept { return true; } +private: + explicit Result(BaseType v) noexcept : m_value(v) { /* ... */ } - static bool CanAccept(Result result) noexcept { return result.IsSuccess(); } +public: + Result() noexcept { /* ... */ } - int GetModule() const noexcept { return Base::GetModule(); } - int GetDescription() const noexcept { return Base::GetDescription(); } - }; + BaseType GetInnerValueForDebug() const noexcept { return m_value; } - namespace result::detail { + bool IsSuccess() const noexcept { return m_value == SuccessValue; } + bool IsFailure() const noexcept { return !IsSuccess(); } - __attribute__((noreturn)) void OnUnhandledResult(Result result) noexcept; + bool operator==(Result other) const noexcept { return GetValue() == other.GetValue(); } + bool operator!=(Result other) const noexcept { return !operator==(other); } + operator ResultSuccess() const noexcept; + static bool CanAccept(Result result) noexcept; + __attribute__((always_inline)) BaseType GetModule() const noexcept { return Base::GetModule(); } + __attribute__((always_inline)) BaseType GetDescription() const noexcept { + return Base::GetDescription(); } + __attribute__((always_inline)) BaseType GetValue() const noexcept { return Base::GetValue(); } +}; +static_assert(sizeof(Result) == sizeof(result::detail::ResultTraits::BaseType), + "sizeof(Result) == sizeof(detail::ResultTraits::BaseType)"); +static_assert(std::is_trivially_destructible::value, + "std::is_trivially_destructible::value"); + +inline bool Result::CanAccept(Result result) noexcept { + (void)(result); + return true; +} - inline Result::operator ResultSuccess() const noexcept { - if (!ResultSuccess::CanAccept(*this)) { - result::detail::OnUnhandledResult(*this); - } - return ResultSuccess(); - } +namespace result::detail { + +class ResultInternalAccessor { +public: + static Result ConstructResult(ResultTraits::BaseType v) noexcept { return Result(v); } +}; + +inline Result ConstructResult(ResultTraits::BaseType v) noexcept { + return ResultInternalAccessor::ConstructResult(v); +} - namespace result::detail { - - template - class ErrorResultBase : public ResultBase> { - private: - using Base = ResultBase>; - public: - static constexpr int Module = Module_; - static constexpr int Description = Description_; - static constexpr typename Base::BaseType InnerValue = ResultTraits::MakeInnerValueStatic::value; - static_assert(InnerValue != Base::SuccessValue, "InnerValue != Base::SuccessValue"); - public: - operator Result() const noexcept { return ConstructResult(InnerValue); } - operator ResultSuccess() const noexcept { OnUnhandledResult(*this); } - - bool IsSuccess() const noexcept { return false; } - - typename Base::BaseType GetInnerValueForDebug() const noexcept { return InnerValue; } - }; - - template - class ErrorRange { - public: - static const int Module = Module_; - static const int DescriptionStart = DescStart; - static const int DescriptionEnd = DescEnd; - static_assert(DescriptionStart < DescriptionEnd, "DescriptionStart < DescriptionEnd"); - public: - static bool Includes(Result result) noexcept { - return result.GetModule() == Module && DescriptionStart <= result.GetDescription() && result.GetDescription() < DescriptionEnd; - } - - friend bool operator <=(Result result, ErrorRange) noexcept { - return Includes(result); - } - }; +} // namespace result::detail +class ResultSuccess : public result::detail::ResultBase { +public: + using Base = typename result::detail::ResultBase; + +public: + operator Result() const noexcept { return result::detail::ConstructResult(SuccessValue); } + + BaseType GetInnerValueForDebug() const noexcept { return SuccessValue; } + + bool IsSuccess() const noexcept { return true; } + + static bool CanAccept(Result result) noexcept { return result.IsSuccess(); } + + __attribute__((always_inline)) int GetModule() const noexcept { return Base::GetModule(); } + __attribute__((always_inline)) int GetDescription() const noexcept { + return Base::GetDescription(); } + __attribute__((always_inline)) int GetValue() const noexcept { return Base::GetValue(); } +}; + +namespace result::detail { + +__attribute__((noreturn)) void OnUnhandledResult(Result result) noexcept; } +inline Result::operator ResultSuccess() const noexcept { + if (!ResultSuccess::CanAccept(*this)) { + result::detail::OnUnhandledResult(*this); + } + return ResultSuccess(); +} + +namespace result::detail { + +template +class ErrorResultBase : public ResultBase> { +private: + using Base = ResultBase>; + +public: + static constexpr int Module = Module_; + static constexpr int Description = Description_; + static constexpr typename Base::BaseType InnerValue = + ResultTraits::MakeInnerValueStatic::value; + static_assert(InnerValue != Base::SuccessValue, "InnerValue != Base::SuccessValue"); + +public: + operator Result() const noexcept { return ConstructResult(InnerValue); } + operator ResultSuccess() const noexcept { OnUnhandledResult(*this); } + + bool IsSuccess() const noexcept { return false; } + + typename Base::BaseType GetInnerValueForDebug() const noexcept { return InnerValue; } +}; + +template +class ErrorRange { +public: + static const int Module = Module_; + static const int DescriptionStart = DescStart; + static const int DescriptionEnd = DescEnd; + static_assert(DescriptionStart < DescriptionEnd, "DescriptionStart < DescriptionEnd"); + +public: + static bool Includes(Result result) noexcept { + return result.GetModule() == Module && DescriptionStart <= result.GetDescription() && + result.GetDescription() < DescriptionEnd; + } + + friend bool operator<=(Result result, ErrorRange) noexcept { return Includes(result); } +}; + +} // namespace result::detail + +} // namespace nn + /* Macros for defining new results. */ -#define R_DEFINE_NAMESPACE_RESULT_MODULE(value) namespace detail::result { using ResultModuleId = std::integral_constant; } +#define R_DEFINE_NAMESPACE_RESULT_MODULE(value) \ + namespace detail::result { \ + using ResultModuleId = std::integral_constant; \ + } #define R_CURRENT_NAMESPACE_RESULT_MODULE detail::result::ResultModuleId::value #define R_NAMESPACE_MODULE_ID(nmspc) nmspc::R_CURRENT_NAMESPACE_RESULT_MODULE -#define R_DEFINE_ERROR_RESULT_IMPL(name, desc_start, desc_end) \ - class Result##name : \ - public ::nn::result::detail::ErrorResultBase, \ - public ::nn::result::detail::ErrorRange \ - {} - -#define R_DEFINE_ABSTRACT_ERROR_RESULT_IMPL(name, desc_start, desc_end) \ - class Result##name : \ - public ::nn::result::detail::ErrorRange \ - {} +#define R_DEFINE_ERROR_RESULT_IMPL(name, desc_start, desc_end) \ + class Result##name \ + : public ::nn::result::detail::ErrorResultBase, \ + public ::nn::result::detail::ErrorRange {} +#define R_DEFINE_ABSTRACT_ERROR_RESULT_IMPL(name, desc_start, desc_end) \ + class Result##name \ + : public ::nn::result::detail::ErrorRange {} -#define R_DEFINE_ERROR_RESULT(name, desc) R_DEFINE_ERROR_RESULT_IMPL(name, desc, desc) +#define R_DEFINE_ERROR_RESULT(name, desc) R_DEFINE_ERROR_RESULT_IMPL(name, desc, desc) #define R_DEFINE_ERROR_RANGE(name, start, end) R_DEFINE_ERROR_RESULT_IMPL(name, start, end) -#define R_DEFINE_ABSTRACT_ERROR_RESULT(name, desc) R_DEFINE_ABSTRACT_ERROR_RESULT_IMPL(name, desc, desc) -#define R_DEFINE_ABSTRACT_ERROR_RANGE(name, start, end) R_DEFINE_ABSTRACT_ERROR_RESULT_IMPL(name, start, end) +#define R_DEFINE_ABSTRACT_ERROR_RESULT(name, desc) \ + R_DEFINE_ABSTRACT_ERROR_RESULT_IMPL(name, desc, desc) +#define R_DEFINE_ABSTRACT_ERROR_RANGE(name, start, end) \ + R_DEFINE_ABSTRACT_ERROR_RESULT_IMPL(name, start, end) From 92b1942748bea2f55ecdc7c4d4f3c0613cf6a2d7 Mon Sep 17 00:00:00 2001 From: Fuzzy2319 Date: Sun, 4 Oct 2026 21:53:48 +0200 Subject: [PATCH 2/3] fix:clang-tidy --- include/nn/err/err_ErrorResultVariant.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/nn/err/err_ErrorResultVariant.h b/include/nn/err/err_ErrorResultVariant.h index 100c136..c6af17d 100644 --- a/include/nn/err/err_ErrorResultVariant.h +++ b/include/nn/err/err_ErrorResultVariant.h @@ -21,8 +21,8 @@ class ErrorResultVariant { operator ErrorCode() const; operator Result() const; - void operator=(const Result& result); - void operator=(const ErrorCode& errorCode); + ErrorResultVariant& operator=(const Result& result); + ErrorResultVariant& operator=(const ErrorCode& errorCode); private: State m_State = State::Undefined; From 16eb4520979e06dac5009ec46ca499a90ad1c12b Mon Sep 17 00:00:00 2001 From: Fuzzy2319 Date: Sat, 17 Oct 2026 10:43:21 +0200 Subject: [PATCH 3/3] fix:requested changes --- include/vapours/results/results_common.hpp | 355 ++++++++++----------- 1 file changed, 164 insertions(+), 191 deletions(-) diff --git a/include/vapours/results/results_common.hpp b/include/vapours/results/results_common.hpp index 47aa010..6795f45 100644 --- a/include/vapours/results/results_common.hpp +++ b/include/vapours/results/results_common.hpp @@ -17,236 +17,209 @@ * see /include/vapours/LICENSE */ #pragma once -#include #include +#include #include namespace nn { -namespace result::detail { - -class ResultTraits { -public: - using BaseType = std::uint32_t; - static const BaseType SuccessValue = BaseType(); - static const int ModuleBits = 9; - static const int DescriptionBits = 13; - static const int ReservedBits = 10; - static_assert(ModuleBits + DescriptionBits + ReservedBits == sizeof(BaseType) * CHAR_BIT, - "ModuleBits + DescriptionBits + ReservedBits == sizeof(BaseType) * CHAR_BIT"); - -private: - static BaseType GetBitsValue(BaseType v, int ofs, int num) noexcept { - return (v >> ofs) & ~(~BaseType() << num); - } - -public: - template - struct MakeInnerValueStatic : public std::integral_constant { - static_assert(M < (1 << ModuleBits), "Invalid Module"); - static_assert(D < (1 << DescriptionBits), "Invalid Description"); - }; - - static BaseType MakeInnerValue(int m, int d) noexcept { - return (static_cast(m) << 0) | (static_cast(d) << ModuleBits); - } - - static BaseType GetModuleFromValue(BaseType value) noexcept { - return GetBitsValue(value, 0, ModuleBits); - } - - static BaseType GetDescriptionFromValue(BaseType value) noexcept { - return GetBitsValue(value, ModuleBits, DescriptionBits); - } - - static BaseType GetInnerValue(BaseType value) noexcept { - return GetBitsValue(value, 0, ModuleBits + DescriptionBits); - } -}; - -/* Use CRTP for Results. */ -template -class ResultBase { -public: - using BaseType = typename ResultTraits::BaseType; - static const BaseType SuccessValue = ResultTraits::SuccessValue; - - __attribute__((always_inline)) BaseType GetModule() const noexcept { - return ResultTraits::GetModuleFromValue( - static_cast(*this).GetInnerValueForDebug()); - } + namespace result::detail { + + class ResultTraits { + public: + using BaseType = std::uint32_t; + static const BaseType SuccessValue = BaseType(); + static const int ModuleBits = 9; + static const int DescriptionBits = 13; + static const int ReservedBits = 10; + static_assert(ModuleBits + DescriptionBits + ReservedBits == sizeof(BaseType) * CHAR_BIT, "ModuleBits + DescriptionBits + ReservedBits == sizeof(BaseType) * CHAR_BIT"); + private: + static BaseType GetBitsValue(BaseType v, int ofs, int num) noexcept { + return (v >> ofs) & ~(~BaseType() << num); + } + public: + template + struct MakeInnerValueStatic : public std::integral_constant { + static_assert(M < (1 << ModuleBits), "Invalid Module"); + static_assert(D < (1 << DescriptionBits), "Invalid Description"); + }; + + static BaseType MakeInnerValue(int m, int d) noexcept { + return (static_cast(m) << 0) | (static_cast(d) << ModuleBits); + } + + static BaseType GetModuleFromValue(BaseType value) noexcept { + return GetBitsValue(value, 0, ModuleBits); + } + + static BaseType GetDescriptionFromValue(BaseType value) noexcept { + return GetBitsValue(value, ModuleBits, DescriptionBits); + } + + static BaseType GetInnerValue(BaseType value) noexcept { + return GetBitsValue(value, 0, ModuleBits + DescriptionBits); + } + }; + + /* Use CRTP for Results. */ + template + class ResultBase { + public: + using BaseType = typename ResultTraits::BaseType; + static const BaseType SuccessValue = ResultTraits::SuccessValue; + + __attribute__((always_inline)) BaseType GetModule() const noexcept { + return ResultTraits::GetModuleFromValue(static_cast(*this).GetInnerValueForDebug()); + } + + __attribute__((always_inline)) BaseType GetDescription() const noexcept { + return ResultTraits::GetDescriptionFromValue(static_cast(*this).GetInnerValueForDebug()); + } + + __attribute__((always_inline)) BaseType GetValue() const noexcept { + return ResultTraits::GetInnerValue(static_cast(*this).GetInnerValueForDebug()); + } + }; + + class ResultInternalAccessor; - __attribute__((always_inline)) BaseType GetDescription() const noexcept { - return ResultTraits::GetDescriptionFromValue( - static_cast(*this).GetInnerValueForDebug()); } - __attribute__((always_inline)) BaseType GetValue() const noexcept { - return ResultTraits::GetInnerValue(static_cast(*this).GetInnerValueForDebug()); - } -}; - -class ResultInternalAccessor; - -} // namespace result::detail - -class ResultSuccess; + class ResultSuccess; -class Result : public result::detail::ResultBase { - friend class result::detail::ResultInternalAccessor; + class Result : public result::detail::ResultBase { + friend class result::detail::ResultInternalAccessor; + private: + using Base = typename result::detail::ResultBase; + private: + BaseType m_value; + private: + explicit Result(BaseType v) noexcept : m_value(v) { /* ... */ } + public: + Result() noexcept { /* ... */ } -private: - using Base = typename result::detail::ResultBase; + BaseType GetInnerValueForDebug() const noexcept { return m_value; } -private: - BaseType m_value; + bool IsSuccess() const noexcept { return m_value == SuccessValue; } + bool IsFailure() const noexcept { return !IsSuccess(); } -private: - explicit Result(BaseType v) noexcept : m_value(v) { /* ... */ } + bool operator==(Result other) const noexcept { return GetValue() == other.GetValue(); } + bool operator!=(Result other) const noexcept { return !operator==(other); } + operator ResultSuccess() const noexcept; + static bool CanAccept(Result result) noexcept; -public: - Result() noexcept { /* ... */ } - - BaseType GetInnerValueForDebug() const noexcept { return m_value; } - - bool IsSuccess() const noexcept { return m_value == SuccessValue; } - bool IsFailure() const noexcept { return !IsSuccess(); } - - bool operator==(Result other) const noexcept { return GetValue() == other.GetValue(); } - bool operator!=(Result other) const noexcept { return !operator==(other); } - operator ResultSuccess() const noexcept; - static bool CanAccept(Result result) noexcept; + __attribute__((always_inline)) BaseType GetModule() const noexcept { return Base::GetModule(); } + __attribute__((always_inline)) BaseType GetDescription() const noexcept { return Base::GetDescription(); } + __attribute__((always_inline)) BaseType GetValue() const noexcept { return Base::GetValue(); } + }; + static_assert(sizeof(Result) == sizeof(result::detail::ResultTraits::BaseType), "sizeof(Result) == sizeof(detail::ResultTraits::BaseType)"); + static_assert(std::is_trivially_destructible::value, "std::is_trivially_destructible::value"); - __attribute__((always_inline)) BaseType GetModule() const noexcept { return Base::GetModule(); } - __attribute__((always_inline)) BaseType GetDescription() const noexcept { - return Base::GetDescription(); + inline bool Result::CanAccept(Result result) noexcept { + (void)(result); + return true; } - __attribute__((always_inline)) BaseType GetValue() const noexcept { return Base::GetValue(); } -}; -static_assert(sizeof(Result) == sizeof(result::detail::ResultTraits::BaseType), - "sizeof(Result) == sizeof(detail::ResultTraits::BaseType)"); -static_assert(std::is_trivially_destructible::value, - "std::is_trivially_destructible::value"); - -inline bool Result::CanAccept(Result result) noexcept { - (void)(result); - return true; -} -namespace result::detail { + namespace result::detail { -class ResultInternalAccessor { -public: - static Result ConstructResult(ResultTraits::BaseType v) noexcept { return Result(v); } -}; + class ResultInternalAccessor { + public: + static Result ConstructResult(ResultTraits::BaseType v) noexcept { return Result(v); } + }; -inline Result ConstructResult(ResultTraits::BaseType v) noexcept { - return ResultInternalAccessor::ConstructResult(v); -} - -} // namespace result::detail + inline Result ConstructResult(ResultTraits::BaseType v) noexcept { return ResultInternalAccessor::ConstructResult(v); } -class ResultSuccess : public result::detail::ResultBase { -public: - using Base = typename result::detail::ResultBase; + } -public: - operator Result() const noexcept { return result::detail::ConstructResult(SuccessValue); } + class ResultSuccess : public result::detail::ResultBase { + public: + using Base = typename result::detail::ResultBase; + public: + operator Result() const noexcept { return result::detail::ConstructResult(SuccessValue); } - BaseType GetInnerValueForDebug() const noexcept { return SuccessValue; } + BaseType GetInnerValueForDebug() const noexcept { return SuccessValue; } - bool IsSuccess() const noexcept { return true; } + bool IsSuccess() const noexcept { return true; } - static bool CanAccept(Result result) noexcept { return result.IsSuccess(); } + static bool CanAccept(Result result) noexcept { return result.IsSuccess(); } - __attribute__((always_inline)) int GetModule() const noexcept { return Base::GetModule(); } - __attribute__((always_inline)) int GetDescription() const noexcept { - return Base::GetDescription(); - } - __attribute__((always_inline)) int GetValue() const noexcept { return Base::GetValue(); } -}; - -namespace result::detail { + int GetModule() const noexcept { return Base::GetModule(); } + int GetDescription() const noexcept { return Base::GetDescription(); } + }; -__attribute__((noreturn)) void OnUnhandledResult(Result result) noexcept; + namespace result::detail { -} + __attribute__((noreturn)) void OnUnhandledResult(Result result) noexcept; -inline Result::operator ResultSuccess() const noexcept { - if (!ResultSuccess::CanAccept(*this)) { - result::detail::OnUnhandledResult(*this); } - return ResultSuccess(); -} -namespace result::detail { - -template -class ErrorResultBase : public ResultBase> { -private: - using Base = ResultBase>; - -public: - static constexpr int Module = Module_; - static constexpr int Description = Description_; - static constexpr typename Base::BaseType InnerValue = - ResultTraits::MakeInnerValueStatic::value; - static_assert(InnerValue != Base::SuccessValue, "InnerValue != Base::SuccessValue"); - -public: - operator Result() const noexcept { return ConstructResult(InnerValue); } - operator ResultSuccess() const noexcept { OnUnhandledResult(*this); } - - bool IsSuccess() const noexcept { return false; } - - typename Base::BaseType GetInnerValueForDebug() const noexcept { return InnerValue; } -}; - -template -class ErrorRange { -public: - static const int Module = Module_; - static const int DescriptionStart = DescStart; - static const int DescriptionEnd = DescEnd; - static_assert(DescriptionStart < DescriptionEnd, "DescriptionStart < DescriptionEnd"); - -public: - static bool Includes(Result result) noexcept { - return result.GetModule() == Module && DescriptionStart <= result.GetDescription() && - result.GetDescription() < DescriptionEnd; + inline Result::operator ResultSuccess() const noexcept { + if (!ResultSuccess::CanAccept(*this)) { + result::detail::OnUnhandledResult(*this); + } + return ResultSuccess(); } - friend bool operator<=(Result result, ErrorRange) noexcept { return Includes(result); } -}; + namespace result::detail { + + template + class ErrorResultBase : public ResultBase> { + private: + using Base = ResultBase>; + public: + static constexpr int Module = Module_; + static constexpr int Description = Description_; + static constexpr typename Base::BaseType InnerValue = ResultTraits::MakeInnerValueStatic::value; + static_assert(InnerValue != Base::SuccessValue, "InnerValue != Base::SuccessValue"); + public: + operator Result() const noexcept { return ConstructResult(InnerValue); } + operator ResultSuccess() const noexcept { OnUnhandledResult(*this); } + + bool IsSuccess() const noexcept { return false; } + + typename Base::BaseType GetInnerValueForDebug() const noexcept { return InnerValue; } + }; + + template + class ErrorRange { + public: + static const int Module = Module_; + static const int DescriptionStart = DescStart; + static const int DescriptionEnd = DescEnd; + static_assert(DescriptionStart < DescriptionEnd, "DescriptionStart < DescriptionEnd"); + public: + static bool Includes(Result result) noexcept { + return result.GetModule() == Module && DescriptionStart <= result.GetDescription() && result.GetDescription() < DescriptionEnd; + } + + friend bool operator <=(Result result, ErrorRange) noexcept { + return Includes(result); + } + }; -} // namespace result::detail + } -} // namespace nn +} /* Macros for defining new results. */ -#define R_DEFINE_NAMESPACE_RESULT_MODULE(value) \ - namespace detail::result { \ - using ResultModuleId = std::integral_constant; \ - } +#define R_DEFINE_NAMESPACE_RESULT_MODULE(value) namespace detail::result { using ResultModuleId = std::integral_constant; } #define R_CURRENT_NAMESPACE_RESULT_MODULE detail::result::ResultModuleId::value #define R_NAMESPACE_MODULE_ID(nmspc) nmspc::R_CURRENT_NAMESPACE_RESULT_MODULE -#define R_DEFINE_ERROR_RESULT_IMPL(name, desc_start, desc_end) \ - class Result##name \ - : public ::nn::result::detail::ErrorResultBase, \ - public ::nn::result::detail::ErrorRange {} +#define R_DEFINE_ERROR_RESULT_IMPL(name, desc_start, desc_end) \ + class Result##name : \ + public ::nn::result::detail::ErrorResultBase, \ + public ::nn::result::detail::ErrorRange \ + {} + +#define R_DEFINE_ABSTRACT_ERROR_RESULT_IMPL(name, desc_start, desc_end) \ + class Result##name : \ + public ::nn::result::detail::ErrorRange \ + {} -#define R_DEFINE_ABSTRACT_ERROR_RESULT_IMPL(name, desc_start, desc_end) \ - class Result##name \ - : public ::nn::result::detail::ErrorRange {} -#define R_DEFINE_ERROR_RESULT(name, desc) R_DEFINE_ERROR_RESULT_IMPL(name, desc, desc) +#define R_DEFINE_ERROR_RESULT(name, desc) R_DEFINE_ERROR_RESULT_IMPL(name, desc, desc) #define R_DEFINE_ERROR_RANGE(name, start, end) R_DEFINE_ERROR_RESULT_IMPL(name, start, end) -#define R_DEFINE_ABSTRACT_ERROR_RESULT(name, desc) \ - R_DEFINE_ABSTRACT_ERROR_RESULT_IMPL(name, desc, desc) -#define R_DEFINE_ABSTRACT_ERROR_RANGE(name, start, end) \ - R_DEFINE_ABSTRACT_ERROR_RESULT_IMPL(name, start, end) +#define R_DEFINE_ABSTRACT_ERROR_RESULT(name, desc) R_DEFINE_ABSTRACT_ERROR_RESULT_IMPL(name, desc, desc) +#define R_DEFINE_ABSTRACT_ERROR_RANGE(name, start, end) R_DEFINE_ABSTRACT_ERROR_RESULT_IMPL(name, start, end)