chore: make yuzu REUSE compliant
[REUSE] is a specification that aims at making file copyright
information consistent, so that it can be both human and machine
readable. It basically requires that all files have a header containing
copyright and licensing information. When this isn't possible, like
when dealing with binary assets, generated files or embedded third-party
dependencies, it is permitted to insert copyright information in the
`.reuse/dep5` file.
Oh, and it also requires that all the licenses used in the project are
present in the `LICENSES` folder, that's why the diff is so huge.
This can be done automatically with `reuse download --all`.
The `reuse` tool also contains a handy subcommand that analyzes the
project and tells whether or not the project is (still) compliant,
`reuse lint`.
Following REUSE has a few advantages over the current approach:
- Copyright information is easy to access for users / downstream
- Files like `dist/license.md` do not need to exist anymore, as
`.reuse/dep5` is used instead
- `reuse lint` makes it easy to ensure that copyright information of
files like binary assets / images is always accurate and up to date
To add copyright information of files that didn't have it I looked up
who committed what and when, for each file. As yuzu contributors do not
have to sign a CLA or similar I couldn't assume that copyright ownership
was of the "yuzu Emulator Project", so I used the name and/or email of
the commit author instead.
[REUSE]: https://reuse.software
Follow-up to 01cf05bc75b1e47beb08937439f3ed9339e7b254
2022-05-15 02:06:02 +02:00
|
|
|
// SPDX-FileCopyrightText: 2014 Citra Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2014-10-23 05:20:01 +02:00
|
|
|
|
|
|
|
#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"
|
2022-10-05 02:05:08 +02:00
|
|
|
#include "common/common_funcs.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,
|
2022-11-15 01:10:04 +01:00
|
|
|
TMA = 12,
|
|
|
|
DMNT = 13,
|
|
|
|
GDS = 14,
|
2017-11-01 00:26:11 +01:00
|
|
|
PM = 15,
|
|
|
|
NS = 16,
|
2022-11-15 01:10:04 +01:00
|
|
|
BSDSockets = 17,
|
2017-11-01 00:26:11 +01:00
|
|
|
HTC = 18,
|
2022-11-15 01:10:04 +01:00
|
|
|
TSC = 19,
|
2018-05-20 00:02:24 +02:00
|
|
|
NCMContent = 20,
|
2017-11-01 00:26:11 +01:00
|
|
|
SM = 21,
|
|
|
|
RO = 22,
|
2022-11-15 01:10:04 +01:00
|
|
|
GC = 23,
|
2017-11-01 00:26:11 +01:00
|
|
|
SDMMC = 24,
|
2018-05-20 00:02:24 +02:00
|
|
|
OVLN = 25,
|
2017-11-01 00:26:11 +01:00
|
|
|
SPL = 26,
|
2022-11-15 01:10:04 +01:00
|
|
|
Socket = 27,
|
|
|
|
HTCLOW = 29,
|
|
|
|
DDSF = 30,
|
|
|
|
HTCFS = 31,
|
|
|
|
Async = 32,
|
|
|
|
Util = 33,
|
|
|
|
TIPC = 35,
|
|
|
|
ANIF = 37,
|
2017-11-01 00:26:11 +01:00
|
|
|
ETHC = 100,
|
|
|
|
I2C = 101,
|
2018-05-20 00:02:24 +02:00
|
|
|
GPIO = 102,
|
|
|
|
UART = 103,
|
2022-11-15 01:10:04 +01:00
|
|
|
CPAD = 104,
|
2017-11-01 00:26:11 +01:00
|
|
|
Settings = 105,
|
2022-11-15 01:10:04 +01:00
|
|
|
FTM = 106,
|
2018-05-20 00:02:24 +02:00
|
|
|
WLAN = 107,
|
|
|
|
XCD = 108,
|
2022-11-15 01:10:04 +01:00
|
|
|
TMP451 = 109,
|
2017-11-01 00:26:11 +01:00
|
|
|
NIFM = 110,
|
2018-05-20 00:02:24 +02:00
|
|
|
Hwopus = 111,
|
2022-11-15 01:10:04 +01:00
|
|
|
LSM6DS3 = 112,
|
2018-05-20 00:02:24 +02:00
|
|
|
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,
|
2022-11-15 01:10:04 +01:00
|
|
|
BH1730FVC = 119,
|
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,
|
2022-11-15 01:10:04 +01:00
|
|
|
USBPD = 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,
|
2022-11-15 01:10:04 +01:00
|
|
|
LA = 144,
|
2018-05-20 00:02:24 +02:00
|
|
|
ETicket = 145,
|
|
|
|
NGC = 146,
|
2017-11-01 00:26:11 +01:00
|
|
|
ERPT = 147,
|
|
|
|
APM = 148,
|
2022-11-15 01:10:04 +01:00
|
|
|
CEC = 149,
|
2018-05-23 14:22:42 +02:00
|
|
|
Profiler = 150,
|
2018-05-20 00:02:24 +02:00
|
|
|
ErrorUpload = 151,
|
2022-11-15 01:10:04 +01:00
|
|
|
LIDBE = 152,
|
2018-05-20 00:02:24 +02:00
|
|
|
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,
|
2022-11-15 01:10:04 +01:00
|
|
|
NetDiag = 160,
|
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,
|
2022-11-15 01:10:04 +01:00
|
|
|
SASBUS = 169,
|
|
|
|
PI = 170,
|
|
|
|
AudioCtrl = 172,
|
|
|
|
LBL = 173,
|
|
|
|
JIT = 175,
|
|
|
|
HDCP = 176,
|
|
|
|
OMM = 177,
|
|
|
|
PDM = 178,
|
|
|
|
OLSC = 179,
|
2018-05-20 00:02:24 +02:00
|
|
|
SREPO = 180,
|
2018-05-23 14:22:42 +02:00
|
|
|
Dauth = 181,
|
2022-11-15 01:10:04 +01:00
|
|
|
STDFU = 182,
|
|
|
|
DBG = 183,
|
|
|
|
DHCPS = 186,
|
|
|
|
SPI = 187,
|
|
|
|
AVM = 188,
|
|
|
|
PWM = 189,
|
|
|
|
RTC = 191,
|
|
|
|
Regulator = 192,
|
|
|
|
LED = 193,
|
|
|
|
SIO = 195,
|
|
|
|
PCM = 196,
|
|
|
|
CLKRST = 197,
|
|
|
|
POWCTL = 198,
|
|
|
|
AudioOld = 201,
|
2018-05-20 00:02:24 +02:00
|
|
|
HID = 202,
|
|
|
|
LDN = 203,
|
2022-11-15 01:10:04 +01:00
|
|
|
CS = 204,
|
2018-05-20 00:02:24 +02:00
|
|
|
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,
|
2022-11-15 01:10:04 +01:00
|
|
|
WEB = 210,
|
|
|
|
LCS = 211,
|
2018-05-20 00:02:24 +02:00
|
|
|
GRC = 212,
|
2022-11-15 01:10:04 +01:00
|
|
|
Repair = 213,
|
|
|
|
Album = 214,
|
|
|
|
RID = 215,
|
2018-05-20 00:02:24 +02:00
|
|
|
Migration = 216,
|
|
|
|
MigrationLdcServ = 217,
|
2022-11-15 01:10:04 +01:00
|
|
|
HIDBUS = 218,
|
|
|
|
ENS = 219,
|
|
|
|
WebSocket = 223,
|
|
|
|
DCDMTP = 227,
|
|
|
|
PGL = 228,
|
|
|
|
Notification = 229,
|
|
|
|
INS = 230,
|
|
|
|
LP2P = 231,
|
|
|
|
RCD = 232,
|
|
|
|
LCM40607 = 233,
|
|
|
|
PRC = 235,
|
|
|
|
TMAHTC = 237,
|
|
|
|
ECTX = 238,
|
|
|
|
MNPP = 239,
|
|
|
|
HSHL = 240,
|
|
|
|
CAPMTP = 242,
|
|
|
|
DP2HDMI = 244,
|
|
|
|
Cradle = 245,
|
|
|
|
SProfile = 246,
|
|
|
|
NDRM = 250,
|
|
|
|
TSPM = 499,
|
|
|
|
DevMenu = 500,
|
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.
|
2022-06-26 05:44:19 +02:00
|
|
|
union Result {
|
2014-10-23 05:20:01 +02:00
|
|
|
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
|
|
|
|
2022-08-01 03:58:13 +02:00
|
|
|
Result() = default;
|
2022-06-26 05:44:19 +02:00
|
|
|
constexpr explicit Result(u32 raw_) : raw(raw_) {}
|
2017-05-21 05:40:13 +02:00
|
|
|
|
2022-06-26 05:44:19 +02:00
|
|
|
constexpr Result(ErrorModule module_, u32 description_)
|
2017-11-01 00:26:11 +01:00
|
|
|
: 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
|
|
|
}
|
2022-10-05 02:05:08 +02:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr bool IsFailure() const {
|
|
|
|
return !IsSuccess();
|
|
|
|
}
|
2022-09-06 02:29:14 +02:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr u32 GetInnerValue() const {
|
|
|
|
return static_cast<u32>(module.Value()) | (description << module.bits);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr bool Includes(Result result) const {
|
|
|
|
return GetInnerValue() == result.GetInnerValue();
|
|
|
|
}
|
2014-10-23 05:20:01 +02:00
|
|
|
};
|
2022-08-01 03:58:13 +02:00
|
|
|
static_assert(std::is_trivial_v<Result>);
|
2014-10-23 05:20:01 +02:00
|
|
|
|
2022-06-26 05:44:19 +02:00
|
|
|
[[nodiscard]] constexpr bool operator==(const Result& a, const Result& b) {
|
2014-10-23 05:20:01 +02:00
|
|
|
return a.raw == b.raw;
|
|
|
|
}
|
|
|
|
|
2022-06-26 05:44:19 +02:00
|
|
|
[[nodiscard]] constexpr bool operator!=(const Result& a, const Result& b) {
|
2021-06-05 12:05:39 +02:00
|
|
|
return !operator==(a, b);
|
2014-10-23 05:20:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience functions for creating some common kinds of errors:
|
|
|
|
|
2022-06-26 05:44:19 +02:00
|
|
|
/// The default success `Result`.
|
|
|
|
constexpr Result 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.
|
|
|
|
*/
|
2022-06-26 05:44:19 +02:00
|
|
|
constexpr Result ResultUnknown(UINT32_MAX);
|
2019-11-12 10:45:21 +01:00
|
|
|
|
2022-05-02 09:15:10 +02:00
|
|
|
/**
|
|
|
|
* A ResultRange defines an inclusive range of error descriptions within an error module.
|
2022-06-26 05:44:19 +02:00
|
|
|
* This can be used to check whether the description of a given Result falls within the range.
|
|
|
|
* The conversion function returns a Result with its description set to description_start.
|
2022-05-02 09:15:10 +02:00
|
|
|
*
|
|
|
|
* An example of how it could be used:
|
|
|
|
* \code
|
|
|
|
* constexpr ResultRange ResultCommonError{ErrorModule::Common, 0, 9999};
|
|
|
|
*
|
2022-06-26 05:44:19 +02:00
|
|
|
* Result Example(int value) {
|
|
|
|
* const Result result = OtherExample(value);
|
2022-05-02 09:15:10 +02:00
|
|
|
*
|
|
|
|
* // This will only evaluate to true if result.module is ErrorModule::Common and
|
|
|
|
* // result.description is in between 0 and 9999 inclusive.
|
|
|
|
* if (ResultCommonError.Includes(result)) {
|
2022-06-26 05:44:19 +02:00
|
|
|
* // This returns Result{ErrorModule::Common, 0};
|
2022-05-02 09:15:10 +02:00
|
|
|
* return ResultCommonError;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* return ResultSuccess;
|
|
|
|
* }
|
|
|
|
* \endcode
|
|
|
|
*/
|
|
|
|
class ResultRange {
|
|
|
|
public:
|
|
|
|
consteval ResultRange(ErrorModule module, u32 description_start, u32 description_end_)
|
|
|
|
: code{module, description_start}, description_end{description_end_} {}
|
|
|
|
|
2022-06-26 05:44:19 +02:00
|
|
|
[[nodiscard]] constexpr operator Result() const {
|
2022-05-02 09:15:10 +02:00
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
2022-06-26 05:44:19 +02:00
|
|
|
[[nodiscard]] constexpr bool Includes(Result other) const {
|
2022-05-02 09:15:10 +02:00
|
|
|
return code.module == other.module && code.description <= other.description &&
|
|
|
|
other.description <= description_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-06-26 05:44:19 +02:00
|
|
|
Result code;
|
2022-05-02 09:15:10 +02:00
|
|
|
u32 description_end;
|
|
|
|
};
|
|
|
|
|
2014-10-23 05:20:01 +02:00
|
|
|
/**
|
2022-06-26 05:44:19 +02:00
|
|
|
* This is an optional value type. It holds a `Result` and, if that code is ResultSuccess, it
|
2021-11-01 15:38:11 +01:00
|
|
|
* also holds a result of type `T`. If the code is an error code (not ResultSuccess), then trying
|
|
|
|
* to access the inner value with operator* is undefined behavior and will assert with Unwrap().
|
|
|
|
* Users of this class must be cognizant to check the status of the ResultVal with operator bool(),
|
|
|
|
* Code(), Succeeded() or Failed() prior to accessing the inner value.
|
2014-10-23 05:20:01 +02:00
|
|
|
*
|
|
|
|
* 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
|
2022-06-26 05:44:19 +02:00
|
|
|
* return Result{ErrorModule::Common, 1};
|
2014-10-23 05:20:01 +02:00
|
|
|
* } else {
|
|
|
|
* // Frobnicated! Give caller a cookie
|
2021-11-02 22:23:19 +01:00
|
|
|
* return 42;
|
2014-10-23 05:20:01 +02:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* \endcode
|
|
|
|
*
|
|
|
|
* \code
|
2021-11-01 15:38:11 +01:00
|
|
|
* auto frob_result = Frobnicate(0.75f);
|
2014-10-23 05:20:01 +02:00
|
|
|
* if (frob_result) {
|
|
|
|
* // Frobbed ok
|
|
|
|
* printf("My cookie is %d\n", *frob_result);
|
|
|
|
* } else {
|
2021-11-01 15:38:11 +01:00
|
|
|
* printf("Guess I overdid it. :( Error code: %ux\n", frob_result.Code().raw);
|
2014-10-23 05:20:01 +02:00
|
|
|
* }
|
|
|
|
* \endcode
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class ResultVal {
|
|
|
|
public:
|
2021-11-01 15:09:37 +01:00
|
|
|
constexpr ResultVal() : expected{} {}
|
|
|
|
|
2022-06-26 05:44:19 +02:00
|
|
|
constexpr ResultVal(Result code) : expected{Common::Unexpected(code)} {}
|
2021-11-01 15:09:37 +01:00
|
|
|
|
2022-05-03 08:00:29 +02:00
|
|
|
constexpr ResultVal(ResultRange range) : expected{Common::Unexpected(range)} {}
|
|
|
|
|
2021-11-01 15:09:37 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-06-26 05:44:19 +02:00
|
|
|
[[nodiscard]] constexpr Result Code() const {
|
2021-11-01 15:09:37 +01:00
|
|
|
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:
|
2022-05-03 08:18:28 +02:00
|
|
|
// TODO (Morph): Replace this with C++23 std::expected.
|
2022-06-26 05:44:19 +02:00
|
|
|
Common::Expected<T, Result> expected;
|
2014-10-23 05:20:01 +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
|
|
|
|
|
|
|
/**
|
2022-06-26 05:44:19 +02:00
|
|
|
* Analogous to CASCADE_RESULT, but for a bare Result. The code will be propagated if
|
2017-06-06 08:13:58 +02:00
|
|
|
* 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
|
|
|
|
2022-10-05 02:05:08 +02:00
|
|
|
#define R_SUCCEEDED(res) (static_cast<Result>(res).IsSuccess())
|
|
|
|
#define R_FAILED(res) (static_cast<Result>(res).IsFailure())
|
2021-05-31 10:15:49 +02:00
|
|
|
|
2022-10-05 02:05:08 +02:00
|
|
|
namespace ResultImpl {
|
|
|
|
template <auto EvaluateResult, class F>
|
|
|
|
class ScopedResultGuard {
|
|
|
|
YUZU_NON_COPYABLE(ScopedResultGuard);
|
|
|
|
YUZU_NON_MOVEABLE(ScopedResultGuard);
|
|
|
|
|
|
|
|
private:
|
|
|
|
Result& m_ref;
|
|
|
|
F m_f;
|
|
|
|
|
|
|
|
public:
|
|
|
|
constexpr ScopedResultGuard(Result& ref, F f) : m_ref(ref), m_f(std::move(f)) {}
|
|
|
|
constexpr ~ScopedResultGuard() {
|
|
|
|
if (EvaluateResult(m_ref)) {
|
|
|
|
m_f();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <auto EvaluateResult>
|
|
|
|
class ResultReferenceForScopedResultGuard {
|
|
|
|
private:
|
|
|
|
Result& m_ref;
|
|
|
|
|
|
|
|
public:
|
|
|
|
constexpr ResultReferenceForScopedResultGuard(Result& r) : m_ref(r) {}
|
|
|
|
constexpr operator Result&() const {
|
|
|
|
return m_ref;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <auto EvaluateResult, typename F>
|
|
|
|
constexpr ScopedResultGuard<EvaluateResult, F> operator+(
|
|
|
|
ResultReferenceForScopedResultGuard<EvaluateResult> ref, F&& f) {
|
|
|
|
return ScopedResultGuard<EvaluateResult, F>(static_cast<Result&>(ref), std::forward<F>(f));
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool EvaluateResultSuccess(const Result& r) {
|
|
|
|
return R_SUCCEEDED(r);
|
|
|
|
}
|
|
|
|
constexpr bool EvaluateResultFailure(const Result& r) {
|
|
|
|
return R_FAILED(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2022-10-13 17:01:28 +02:00
|
|
|
constexpr void UpdateCurrentResultReference(T result_reference, Result result) = delete;
|
|
|
|
// Intentionally not defined
|
2022-10-05 02:05:08 +02:00
|
|
|
|
|
|
|
template <>
|
|
|
|
constexpr void UpdateCurrentResultReference<Result&>(Result& result_reference, Result result) {
|
|
|
|
result_reference = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2022-10-13 17:01:28 +02:00
|
|
|
constexpr void UpdateCurrentResultReference<const Result>(Result result_reference, Result result) {}
|
2022-10-05 02:05:08 +02:00
|
|
|
} // namespace ResultImpl
|
|
|
|
|
|
|
|
#define DECLARE_CURRENT_RESULT_REFERENCE_AND_STORAGE(COUNTER_VALUE) \
|
2022-10-29 10:46:24 +02:00
|
|
|
[[maybe_unused]] constexpr bool CONCAT2(HasPrevRef_, COUNTER_VALUE) = \
|
2022-10-05 02:05:08 +02:00
|
|
|
std::same_as<decltype(__TmpCurrentResultReference), Result&>; \
|
2022-10-29 10:46:24 +02:00
|
|
|
[[maybe_unused]] Result CONCAT2(PrevRef_, COUNTER_VALUE) = __TmpCurrentResultReference; \
|
|
|
|
[[maybe_unused]] Result CONCAT2(__tmp_result_, COUNTER_VALUE) = ResultSuccess; \
|
|
|
|
Result& __TmpCurrentResultReference = CONCAT2(HasPrevRef_, COUNTER_VALUE) \
|
|
|
|
? CONCAT2(PrevRef_, COUNTER_VALUE) \
|
|
|
|
: CONCAT2(__tmp_result_, COUNTER_VALUE)
|
2022-10-05 02:05:08 +02:00
|
|
|
|
|
|
|
#define ON_RESULT_RETURN_IMPL(...) \
|
|
|
|
static_assert(std::same_as<decltype(__TmpCurrentResultReference), Result&>); \
|
2022-10-29 10:46:24 +02:00
|
|
|
auto CONCAT2(RESULT_GUARD_STATE_, __COUNTER__) = \
|
2022-10-05 02:05:08 +02:00
|
|
|
ResultImpl::ResultReferenceForScopedResultGuard<__VA_ARGS__>( \
|
|
|
|
__TmpCurrentResultReference) + \
|
|
|
|
[&]()
|
|
|
|
|
|
|
|
#define ON_RESULT_FAILURE_2 ON_RESULT_RETURN_IMPL(ResultImpl::EvaluateResultFailure)
|
|
|
|
|
|
|
|
#define ON_RESULT_FAILURE \
|
|
|
|
DECLARE_CURRENT_RESULT_REFERENCE_AND_STORAGE(__COUNTER__); \
|
|
|
|
ON_RESULT_FAILURE_2
|
|
|
|
|
|
|
|
#define ON_RESULT_SUCCESS_2 ON_RESULT_RETURN_IMPL(ResultImpl::EvaluateResultSuccess)
|
|
|
|
|
|
|
|
#define ON_RESULT_SUCCESS \
|
|
|
|
DECLARE_CURRENT_RESULT_REFERENCE_AND_STORAGE(__COUNTER__); \
|
|
|
|
ON_RESULT_SUCCESS_2
|
|
|
|
|
|
|
|
constexpr inline Result __TmpCurrentResultReference = ResultSuccess;
|
|
|
|
|
|
|
|
/// Returns a result.
|
|
|
|
#define R_RETURN(res_expr) \
|
|
|
|
{ \
|
|
|
|
const Result _tmp_r_throw_rc = (res_expr); \
|
|
|
|
ResultImpl::UpdateCurrentResultReference<decltype(__TmpCurrentResultReference)>( \
|
|
|
|
__TmpCurrentResultReference, _tmp_r_throw_rc); \
|
|
|
|
return _tmp_r_throw_rc; \
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns ResultSuccess()
|
|
|
|
#define R_SUCCEED() R_RETURN(ResultSuccess)
|
|
|
|
|
|
|
|
/// Throws a result.
|
|
|
|
#define R_THROW(res_expr) R_RETURN(res_expr)
|
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)) { \
|
2022-10-05 02:05:08 +02:00
|
|
|
R_THROW(res); \
|
2021-05-31 10:15:49 +02:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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); \
|
2022-10-05 02:05:08 +02:00
|
|
|
if (R_FAILED(_tmp_r_try_rc)) { \
|
|
|
|
R_THROW(_tmp_r_try_rc); \
|
2021-05-31 10:15:49 +02:00
|
|
|
} \
|
|
|
|
}
|
2022-10-05 02:05:08 +02:00
|
|
|
|
|
|
|
/// Evaluates a boolean expression, and succeeds if that expression is true.
|
|
|
|
#define R_SUCCEED_IF(expr) R_UNLESS(!(expr), ResultSuccess)
|