2014-10-23 05:20:01 +02:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 06:38:14 +01:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-10-23 05:20:01 +02:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2015-05-06 09:06:12 +02:00
|
|
|
#include "common/assert.h"
|
2014-10-23 05:20:01 +02:00
|
|
|
#include "common/bit_field.h"
|
2015-01-11 06:42:18 +01:00
|
|
|
#include "common/common_types.h"
|
2021-11-01 15:09:37 +01:00
|
|
|
#include "common/expected.h"
|
2014-10-23 05:20:01 +02:00
|
|
|
|
2017-11-01 00:26:11 +01:00
|
|
|
// All the constants in this file come from http://switchbrew.org/index.php?title=Error_codes
|
2014-10-23 05:20:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Identifies the module which caused the error. Error codes can be propagated through a call
|
|
|
|
* chain, meaning that this doesn't always correspond to the module where the API call made is
|
|
|
|
* contained.
|
|
|
|
*/
|
|
|
|
enum class ErrorModule : u32 {
|
|
|
|
Common = 0,
|
|
|
|
Kernel = 1,
|
2017-11-01 00:26:11 +01:00
|
|
|
FS = 2,
|
2018-05-23 14:22:42 +02:00
|
|
|
OS = 3, // used for Memory, Thread, Mutex, Nvidia
|
|
|
|
HTCS = 4,
|
2017-11-01 00:26:11 +01:00
|
|
|
NCM = 5,
|
|
|
|
DD = 6,
|
|
|
|
LR = 8,
|
|
|
|
Loader = 9,
|
|
|
|
CMIF = 10,
|
|
|
|
HIPC = 11,
|
|
|
|
PM = 15,
|
|
|
|
NS = 16,
|
|
|
|
HTC = 18,
|
2018-05-20 00:02:24 +02:00
|
|
|
NCMContent = 20,
|
2017-11-01 00:26:11 +01:00
|
|
|
SM = 21,
|
|
|
|
RO = 22,
|
|
|
|
SDMMC = 24,
|
2018-05-20 00:02:24 +02:00
|
|
|
OVLN = 25,
|
2017-11-01 00:26:11 +01:00
|
|
|
SPL = 26,
|
|
|
|
ETHC = 100,
|
|
|
|
I2C = 101,
|
2018-05-20 00:02:24 +02:00
|
|
|
GPIO = 102,
|
|
|
|
UART = 103,
|
2017-11-01 00:26:11 +01:00
|
|
|
Settings = 105,
|
2018-05-20 00:02:24 +02:00
|
|
|
WLAN = 107,
|
|
|
|
XCD = 108,
|
2017-11-01 00:26:11 +01:00
|
|
|
NIFM = 110,
|
2018-05-20 00:02:24 +02:00
|
|
|
Hwopus = 111,
|
|
|
|
Bluetooth = 113,
|
|
|
|
VI = 114,
|
|
|
|
NFP = 115,
|
|
|
|
Time = 116,
|
2017-11-01 00:26:11 +01:00
|
|
|
FGM = 117,
|
2018-05-23 14:22:42 +02:00
|
|
|
OE = 118,
|
2018-05-20 00:02:24 +02:00
|
|
|
PCIe = 120,
|
2017-11-01 00:26:11 +01:00
|
|
|
Friends = 121,
|
2018-05-20 00:02:24 +02:00
|
|
|
BCAT = 122,
|
2021-05-16 07:46:30 +02:00
|
|
|
SSLSrv = 123,
|
2017-11-01 00:26:11 +01:00
|
|
|
Account = 124,
|
2018-05-20 00:02:24 +02:00
|
|
|
News = 125,
|
2017-11-01 00:26:11 +01:00
|
|
|
Mii = 126,
|
2018-05-20 00:02:24 +02:00
|
|
|
NFC = 127,
|
2017-11-01 00:26:11 +01:00
|
|
|
AM = 128,
|
|
|
|
PlayReport = 129,
|
2018-05-20 00:02:24 +02:00
|
|
|
AHID = 130,
|
|
|
|
Qlaunch = 132,
|
2017-11-01 00:26:11 +01:00
|
|
|
PCV = 133,
|
|
|
|
OMM = 134,
|
2018-05-20 00:02:24 +02:00
|
|
|
BPC = 135,
|
|
|
|
PSM = 136,
|
2017-11-01 00:26:11 +01:00
|
|
|
NIM = 137,
|
|
|
|
PSC = 138,
|
2018-05-20 00:02:24 +02:00
|
|
|
TC = 139,
|
2017-11-01 00:26:11 +01:00
|
|
|
USB = 140,
|
2018-05-20 00:02:24 +02:00
|
|
|
NSD = 141,
|
|
|
|
PCTL = 142,
|
2017-11-01 00:26:11 +01:00
|
|
|
BTM = 143,
|
2018-05-20 00:02:24 +02:00
|
|
|
ETicket = 145,
|
|
|
|
NGC = 146,
|
2017-11-01 00:26:11 +01:00
|
|
|
ERPT = 147,
|
|
|
|
APM = 148,
|
2018-05-23 14:22:42 +02:00
|
|
|
Profiler = 150,
|
2018-05-20 00:02:24 +02:00
|
|
|
ErrorUpload = 151,
|
|
|
|
Audio = 153,
|
2017-11-01 00:26:11 +01:00
|
|
|
NPNS = 154,
|
2018-05-20 00:02:24 +02:00
|
|
|
NPNSHTTPSTREAM = 155,
|
2017-11-01 00:26:11 +01:00
|
|
|
ARP = 157,
|
2018-05-23 14:22:42 +02:00
|
|
|
SWKBD = 158,
|
|
|
|
BOOT = 159,
|
2018-05-20 00:02:24 +02:00
|
|
|
NFCMifare = 161,
|
2017-11-01 00:26:11 +01:00
|
|
|
UserlandAssert = 162,
|
2018-05-20 00:02:24 +02:00
|
|
|
Fatal = 163,
|
|
|
|
NIMShop = 164,
|
|
|
|
SPSM = 165,
|
|
|
|
BGTC = 167,
|
2017-11-01 00:26:11 +01:00
|
|
|
UserlandCrash = 168,
|
2018-05-20 00:02:24 +02:00
|
|
|
SREPO = 180,
|
2018-05-23 14:22:42 +02:00
|
|
|
Dauth = 181,
|
2018-05-20 00:02:24 +02:00
|
|
|
HID = 202,
|
|
|
|
LDN = 203,
|
|
|
|
Irsensor = 205,
|
2017-11-01 00:26:11 +01:00
|
|
|
Capture = 206,
|
2018-05-20 00:02:24 +02:00
|
|
|
Manu = 208,
|
2018-05-23 14:22:42 +02:00
|
|
|
ATK = 209,
|
2018-05-20 00:02:24 +02:00
|
|
|
GRC = 212,
|
|
|
|
Migration = 216,
|
|
|
|
MigrationLdcServ = 217,
|
2017-11-01 00:26:11 +01:00
|
|
|
GeneralWebApplet = 800,
|
|
|
|
WifiWebAuthApplet = 809,
|
|
|
|
WhitelistedApplet = 810,
|
|
|
|
ShopN = 811,
|
2014-10-23 05:20:01 +02:00
|
|
|
};
|
|
|
|
|
2019-03-10 23:29:28 +01:00
|
|
|
/// Encapsulates a Horizon OS error code, allowing it to be separated into its constituent fields.
|
2014-10-23 05:20:01 +02:00
|
|
|
union ResultCode {
|
|
|
|
u32 raw;
|
|
|
|
|
2017-11-01 00:26:11 +01:00
|
|
|
BitField<0, 9, ErrorModule> module;
|
|
|
|
BitField<9, 13, u32> description;
|
2014-10-23 05:20:01 +02:00
|
|
|
|
2020-11-25 21:21:03 +01:00
|
|
|
constexpr explicit ResultCode(u32 raw_) : raw(raw_) {}
|
2017-05-21 05:40:13 +02:00
|
|
|
|
2017-11-01 00:26:11 +01:00
|
|
|
constexpr ResultCode(ErrorModule module_, u32 description_)
|
|
|
|
: raw(module.FormatValue(module_) | description.FormatValue(description_)) {}
|
2014-10-23 05:20:01 +02:00
|
|
|
|
2021-06-05 12:05:39 +02:00
|
|
|
[[nodiscard]] constexpr bool IsSuccess() const {
|
2018-02-19 06:30:30 +01:00
|
|
|
return raw == 0;
|
2014-10-23 05:20:01 +02:00
|
|
|
}
|
|
|
|
|
2021-06-05 12:05:39 +02:00
|
|
|
[[nodiscard]] constexpr bool IsError() const {
|
|
|
|
return !IsSuccess();
|
2014-10-23 05:20:01 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-05 12:05:39 +02:00
|
|
|
[[nodiscard]] constexpr bool operator==(const ResultCode& a, const ResultCode& b) {
|
2014-10-23 05:20:01 +02:00
|
|
|
return a.raw == b.raw;
|
|
|
|
}
|
|
|
|
|
2021-06-05 12:05:39 +02:00
|
|
|
[[nodiscard]] constexpr bool operator!=(const ResultCode& a, const ResultCode& b) {
|
|
|
|
return !operator==(a, b);
|
2014-10-23 05:20:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience functions for creating some common kinds of errors:
|
|
|
|
|
|
|
|
/// The default success `ResultCode`.
|
2021-05-21 07:05:04 +02:00
|
|
|
constexpr ResultCode ResultSuccess(0);
|
2014-10-23 05:20:01 +02:00
|
|
|
|
2019-11-12 10:45:21 +01:00
|
|
|
/**
|
|
|
|
* Placeholder result code used for unknown error codes.
|
|
|
|
*
|
|
|
|
* @note This should only be used when a particular error code
|
|
|
|
* is not known yet.
|
|
|
|
*/
|
2021-05-21 07:06:31 +02:00
|
|
|
constexpr ResultCode ResultUnknown(UINT32_MAX);
|
2019-11-12 10:45:21 +01:00
|
|
|
|
2014-10-23 05:20:01 +02:00
|
|
|
/**
|
|
|
|
* This is an optional value type. It holds a `ResultCode` and, if that code is a success code,
|
|
|
|
* also holds a result of type `T`. If the code is an error code then trying to access the inner
|
|
|
|
* value fails, thus ensuring that the ResultCode of functions is always checked properly before
|
|
|
|
* their return value is used. It is similar in concept to the `std::optional` type
|
|
|
|
* (http://en.cppreference.com/w/cpp/experimental/optional) originally proposed for inclusion in
|
|
|
|
* C++14, or the `Result` type in Rust (http://doc.rust-lang.org/std/result/index.html).
|
|
|
|
*
|
|
|
|
* An example of how it could be used:
|
|
|
|
* \code
|
|
|
|
* ResultVal<int> Frobnicate(float strength) {
|
|
|
|
* if (strength < 0.f || strength > 1.0f) {
|
|
|
|
* // Can't frobnicate too weakly or too strongly
|
|
|
|
* return ResultCode(ErrorDescription::OutOfRange, ErrorModule::Common,
|
|
|
|
* ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
|
|
|
|
* } else {
|
|
|
|
* // Frobnicated! Give caller a cookie
|
|
|
|
* return MakeResult<int>(42);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* \endcode
|
|
|
|
*
|
|
|
|
* \code
|
|
|
|
* ResultVal<int> frob_result = Frobnicate(0.75f);
|
|
|
|
* if (frob_result) {
|
|
|
|
* // Frobbed ok
|
|
|
|
* printf("My cookie is %d\n", *frob_result);
|
|
|
|
* } else {
|
|
|
|
* printf("Guess I overdid it. :( Error code: %ux\n", frob_result.code().hex);
|
|
|
|
* }
|
|
|
|
* \endcode
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class ResultVal {
|
|
|
|
public:
|
2021-11-01 15:09:37 +01:00
|
|
|
constexpr ResultVal() : expected{} {}
|
|
|
|
|
|
|
|
constexpr ResultVal(ResultCode code) : expected{Common::Unexpected(code)} {}
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
constexpr ResultVal(U&& val) : expected{std::forward<U>(val)} {}
|
2014-10-23 05:20:01 +02:00
|
|
|
|
|
|
|
template <typename... Args>
|
2021-11-01 15:09:37 +01:00
|
|
|
constexpr ResultVal(Args&&... args) : expected{std::in_place, std::forward<Args>(args)...} {}
|
2014-10-23 05:20:01 +02:00
|
|
|
|
2021-11-01 15:09:37 +01:00
|
|
|
~ResultVal() = default;
|
2014-10-23 05:20:01 +02:00
|
|
|
|
2021-11-01 15:09:37 +01:00
|
|
|
constexpr ResultVal(const ResultVal&) = default;
|
|
|
|
constexpr ResultVal(ResultVal&&) = default;
|
2014-10-23 05:20:01 +02:00
|
|
|
|
2021-11-01 15:09:37 +01:00
|
|
|
ResultVal& operator=(const ResultVal&) = default;
|
|
|
|
ResultVal& operator=(ResultVal&&) = default;
|
2014-10-23 05:20:01 +02:00
|
|
|
|
2021-11-01 15:09:37 +01:00
|
|
|
[[nodiscard]] constexpr explicit operator bool() const noexcept {
|
|
|
|
return expected.has_value();
|
2014-10-23 05:20:01 +02:00
|
|
|
}
|
|
|
|
|
2021-11-01 15:09:37 +01:00
|
|
|
[[nodiscard]] constexpr ResultCode Code() const {
|
|
|
|
return expected.has_value() ? ResultSuccess : expected.error();
|
2021-10-28 09:52:21 +02:00
|
|
|
}
|
|
|
|
|
2021-11-01 15:09:37 +01:00
|
|
|
[[nodiscard]] constexpr bool Succeeded() const {
|
|
|
|
return expected.has_value();
|
2014-10-23 05:20:01 +02:00
|
|
|
}
|
|
|
|
|
2021-11-01 15:09:37 +01:00
|
|
|
[[nodiscard]] constexpr bool Failed() const {
|
|
|
|
return !expected.has_value();
|
2016-09-18 02:38:01 +02:00
|
|
|
}
|
2014-10-23 05:20:01 +02:00
|
|
|
|
2021-11-01 15:09:37 +01:00
|
|
|
[[nodiscard]] constexpr T* operator->() {
|
|
|
|
return std::addressof(expected.value());
|
2016-09-18 02:38:01 +02:00
|
|
|
}
|
2021-11-01 15:09:37 +01:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr const T* operator->() const {
|
|
|
|
return std::addressof(expected.value());
|
2016-09-18 02:38:01 +02:00
|
|
|
}
|
2014-10-23 05:20:01 +02:00
|
|
|
|
2021-11-01 15:09:37 +01:00
|
|
|
[[nodiscard]] constexpr T& operator*() & {
|
|
|
|
return *expected;
|
2016-09-18 02:38:01 +02:00
|
|
|
}
|
2014-10-23 05:20:01 +02:00
|
|
|
|
2021-11-01 15:09:37 +01:00
|
|
|
[[nodiscard]] constexpr const T& operator*() const& {
|
|
|
|
return *expected;
|
2016-09-18 02:38:01 +02:00
|
|
|
}
|
2021-11-01 15:09:37 +01:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr T&& operator*() && {
|
|
|
|
return *expected;
|
2016-09-18 02:38:01 +02:00
|
|
|
}
|
2021-11-01 15:09:37 +01:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr const T&& operator*() const&& {
|
|
|
|
return *expected;
|
2016-09-18 02:38:01 +02:00
|
|
|
}
|
2021-11-01 15:09:37 +01:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr T& Unwrap() & {
|
|
|
|
ASSERT_MSG(Succeeded(), "Tried to Unwrap empty ResultVal");
|
|
|
|
return expected.value();
|
2016-09-18 02:38:01 +02:00
|
|
|
}
|
2014-10-23 05:20:01 +02:00
|
|
|
|
2021-11-01 15:09:37 +01:00
|
|
|
[[nodiscard]] constexpr const T& Unwrap() const& {
|
|
|
|
ASSERT_MSG(Succeeded(), "Tried to Unwrap empty ResultVal");
|
|
|
|
return expected.value();
|
2014-10-23 05:20:01 +02:00
|
|
|
}
|
|
|
|
|
2021-11-01 15:09:37 +01:00
|
|
|
[[nodiscard]] constexpr T&& Unwrap() && {
|
2015-01-21 02:16:47 +01:00
|
|
|
ASSERT_MSG(Succeeded(), "Tried to Unwrap empty ResultVal");
|
2021-11-01 15:09:37 +01:00
|
|
|
return std::move(expected.value());
|
2015-01-11 06:42:18 +01:00
|
|
|
}
|
|
|
|
|
2021-11-01 15:09:37 +01:00
|
|
|
[[nodiscard]] constexpr const T&& Unwrap() const&& {
|
2017-06-19 03:49:46 +02:00
|
|
|
ASSERT_MSG(Succeeded(), "Tried to Unwrap empty ResultVal");
|
2021-11-01 15:09:37 +01:00
|
|
|
return std::move(expected.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
[[nodiscard]] constexpr T ValueOr(U&& v) const& {
|
|
|
|
return expected.value_or(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
[[nodiscard]] constexpr T ValueOr(U&& v) && {
|
|
|
|
return expected.value_or(v);
|
2017-06-19 03:49:46 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 05:20:01 +02:00
|
|
|
private:
|
2021-11-01 15:09:37 +01:00
|
|
|
// TODO: Replace this with std::expected once it is standardized in the STL.
|
|
|
|
Common::Expected<T, ResultCode> expected;
|
2014-10-23 05:20:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is a helper used to construct `ResultVal`s. It receives the arguments to construct
|
|
|
|
* `T` with and creates a success `ResultVal` contained the constructed value.
|
|
|
|
*/
|
|
|
|
template <typename T, typename... Args>
|
2021-06-05 12:05:39 +02:00
|
|
|
[[nodiscard]] ResultVal<T> MakeResult(Args&&... args) {
|
2021-11-01 15:09:37 +01:00
|
|
|
return ResultVal<T>{std::forward<Args>(args)...};
|
2014-10-23 05:20:01 +02:00
|
|
|
}
|
2015-01-11 06:42:18 +01:00
|
|
|
|
2017-06-06 08:13:58 +02:00
|
|
|
/**
|
|
|
|
* Deducible overload of MakeResult, allowing the template parameter to be ommited if you're just
|
|
|
|
* copy or move constructing.
|
|
|
|
*/
|
2021-11-01 15:09:37 +01:00
|
|
|
template <typename T>
|
|
|
|
[[nodiscard]] ResultVal<std::remove_cvref_t<T>> MakeResult(T&& val) {
|
|
|
|
return ResultVal<std::remove_cvref_t<T>>{std::forward<T>(val)};
|
2017-06-06 08:13:58 +02:00
|
|
|
}
|
|
|
|
|
2015-01-11 06:42:18 +01:00
|
|
|
/**
|
|
|
|
* Check for the success of `source` (which must evaluate to a ResultVal). If it succeeds, unwraps
|
|
|
|
* the contained value and assigns it to `target`, which can be either an l-value expression or a
|
|
|
|
* variable declaration. If it fails the return code is returned from the current function. Thus it
|
|
|
|
* can be used to cascade errors out, achieving something akin to exception handling.
|
|
|
|
*/
|
2016-09-18 02:38:01 +02:00
|
|
|
#define CASCADE_RESULT(target, source) \
|
|
|
|
auto CONCAT2(check_result_L, __LINE__) = source; \
|
2020-08-14 15:04:44 +02:00
|
|
|
if (CONCAT2(check_result_L, __LINE__).Failed()) { \
|
2016-09-18 02:38:01 +02:00
|
|
|
return CONCAT2(check_result_L, __LINE__).Code(); \
|
2020-08-14 15:04:44 +02:00
|
|
|
} \
|
2016-09-18 02:38:01 +02:00
|
|
|
target = std::move(*CONCAT2(check_result_L, __LINE__))
|
2017-06-06 08:13:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Analogous to CASCADE_RESULT, but for a bare ResultCode. The code will be propagated if
|
|
|
|
* non-success, or discarded otherwise.
|
|
|
|
*/
|
|
|
|
#define CASCADE_CODE(source) \
|
2020-08-14 15:04:44 +02:00
|
|
|
do { \
|
|
|
|
auto CONCAT2(check_result_L, __LINE__) = source; \
|
|
|
|
if (CONCAT2(check_result_L, __LINE__).IsError()) { \
|
|
|
|
return CONCAT2(check_result_L, __LINE__); \
|
|
|
|
} \
|
|
|
|
} while (false)
|
2021-05-31 10:15:49 +02:00
|
|
|
|
|
|
|
#define R_SUCCEEDED(res) (res.IsSuccess())
|
|
|
|
|
|
|
|
/// Evaluates a boolean expression, and succeeds if that expression is true.
|
2021-05-21 07:05:04 +02:00
|
|
|
#define R_SUCCEED_IF(expr) R_UNLESS(!(expr), ResultSuccess)
|
2021-05-31 10:15:49 +02:00
|
|
|
|
|
|
|
/// Evaluates a boolean expression, and returns a result unless that expression is true.
|
|
|
|
#define R_UNLESS(expr, res) \
|
|
|
|
{ \
|
|
|
|
if (!(expr)) { \
|
|
|
|
if (res.IsError()) { \
|
|
|
|
LOG_ERROR(Kernel, "Failed with result: {}", res.raw); \
|
|
|
|
} \
|
|
|
|
return res; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Evaluates an expression that returns a result, and returns the result if it would fail.
|
|
|
|
#define R_TRY(res_expr) \
|
|
|
|
{ \
|
|
|
|
const auto _tmp_r_try_rc = (res_expr); \
|
|
|
|
if (_tmp_r_try_rc.IsError()) { \
|
|
|
|
return _tmp_r_try_rc; \
|
|
|
|
} \
|
|
|
|
}
|