result.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // SPDX-FileCopyrightText: 2014 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include "common/assert.h"
  5. #include "common/bit_field.h"
  6. #include "common/common_funcs.h"
  7. #include "common/common_types.h"
  8. #include "common/expected.h"
  9. // All the constants in this file come from http://switchbrew.org/index.php?title=Error_codes
  10. /**
  11. * Identifies the module which caused the error. Error codes can be propagated through a call
  12. * chain, meaning that this doesn't always correspond to the module where the API call made is
  13. * contained.
  14. */
  15. enum class ErrorModule : u32 {
  16. Common = 0,
  17. Kernel = 1,
  18. FS = 2,
  19. OS = 3, // used for Memory, Thread, Mutex, Nvidia
  20. HTCS = 4,
  21. NCM = 5,
  22. DD = 6,
  23. LR = 8,
  24. Loader = 9,
  25. CMIF = 10,
  26. HIPC = 11,
  27. TMA = 12,
  28. DMNT = 13,
  29. GDS = 14,
  30. PM = 15,
  31. NS = 16,
  32. BSDSockets = 17,
  33. HTC = 18,
  34. TSC = 19,
  35. NCMContent = 20,
  36. SM = 21,
  37. RO = 22,
  38. GC = 23,
  39. SDMMC = 24,
  40. OVLN = 25,
  41. SPL = 26,
  42. Socket = 27,
  43. HTCLOW = 29,
  44. DDSF = 30,
  45. HTCFS = 31,
  46. Async = 32,
  47. Util = 33,
  48. TIPC = 35,
  49. ANIF = 37,
  50. ETHC = 100,
  51. I2C = 101,
  52. GPIO = 102,
  53. UART = 103,
  54. CPAD = 104,
  55. Settings = 105,
  56. FTM = 106,
  57. WLAN = 107,
  58. XCD = 108,
  59. TMP451 = 109,
  60. NIFM = 110,
  61. HwOpus = 111,
  62. LSM6DS3 = 112,
  63. Bluetooth = 113,
  64. VI = 114,
  65. NFP = 115,
  66. Time = 116,
  67. FGM = 117,
  68. OE = 118,
  69. BH1730FVC = 119,
  70. PCIe = 120,
  71. Friends = 121,
  72. BCAT = 122,
  73. SSLSrv = 123,
  74. Account = 124,
  75. News = 125,
  76. Mii = 126,
  77. NFC = 127,
  78. AM = 128,
  79. PlayReport = 129,
  80. AHID = 130,
  81. Qlaunch = 132,
  82. PCV = 133,
  83. USBPD = 134,
  84. BPC = 135,
  85. PSM = 136,
  86. NIM = 137,
  87. PSC = 138,
  88. TC = 139,
  89. USB = 140,
  90. NSD = 141,
  91. PCTL = 142,
  92. BTM = 143,
  93. LA = 144,
  94. ETicket = 145,
  95. NGC = 146,
  96. ERPT = 147,
  97. APM = 148,
  98. CEC = 149,
  99. Profiler = 150,
  100. ErrorUpload = 151,
  101. LIDBE = 152,
  102. Audio = 153,
  103. NPNS = 154,
  104. NPNSHTTPSTREAM = 155,
  105. ARP = 157,
  106. SWKBD = 158,
  107. BOOT = 159,
  108. NetDiag = 160,
  109. NFCMifare = 161,
  110. UserlandAssert = 162,
  111. Fatal = 163,
  112. NIMShop = 164,
  113. SPSM = 165,
  114. BGTC = 167,
  115. UserlandCrash = 168,
  116. SASBUS = 169,
  117. PI = 170,
  118. AudioCtrl = 172,
  119. LBL = 173,
  120. JIT = 175,
  121. HDCP = 176,
  122. OMM = 177,
  123. PDM = 178,
  124. OLSC = 179,
  125. SREPO = 180,
  126. Dauth = 181,
  127. STDFU = 182,
  128. DBG = 183,
  129. DHCPS = 186,
  130. SPI = 187,
  131. AVM = 188,
  132. PWM = 189,
  133. RTC = 191,
  134. Regulator = 192,
  135. LED = 193,
  136. SIO = 195,
  137. PCM = 196,
  138. CLKRST = 197,
  139. POWCTL = 198,
  140. AudioOld = 201,
  141. HID = 202,
  142. LDN = 203,
  143. CS = 204,
  144. Irsensor = 205,
  145. Capture = 206,
  146. Manu = 208,
  147. ATK = 209,
  148. WEB = 210,
  149. LCS = 211,
  150. GRC = 212,
  151. Repair = 213,
  152. Album = 214,
  153. RID = 215,
  154. Migration = 216,
  155. MigrationLdcServ = 217,
  156. HIDBUS = 218,
  157. ENS = 219,
  158. WebSocket = 223,
  159. DCDMTP = 227,
  160. PGL = 228,
  161. Notification = 229,
  162. INS = 230,
  163. LP2P = 231,
  164. RCD = 232,
  165. LCM40607 = 233,
  166. PRC = 235,
  167. TMAHTC = 237,
  168. ECTX = 238,
  169. MNPP = 239,
  170. HSHL = 240,
  171. CAPMTP = 242,
  172. DP2HDMI = 244,
  173. Cradle = 245,
  174. SProfile = 246,
  175. NDRM = 250,
  176. TSPM = 499,
  177. DevMenu = 500,
  178. GeneralWebApplet = 800,
  179. WifiWebAuthApplet = 809,
  180. WhitelistedApplet = 810,
  181. ShopN = 811,
  182. };
  183. /// Encapsulates a Horizon OS error code, allowing it to be separated into its constituent fields.
  184. union Result {
  185. u32 raw;
  186. BitField<0, 9, ErrorModule> module;
  187. BitField<9, 13, u32> description;
  188. Result() = default;
  189. constexpr explicit Result(u32 raw_) : raw(raw_) {}
  190. constexpr Result(ErrorModule module_, u32 description_)
  191. : raw(module.FormatValue(module_) | description.FormatValue(description_)) {}
  192. [[nodiscard]] constexpr bool IsSuccess() const {
  193. return raw == 0;
  194. }
  195. [[nodiscard]] constexpr bool IsError() const {
  196. return !IsSuccess();
  197. }
  198. [[nodiscard]] constexpr bool IsFailure() const {
  199. return !IsSuccess();
  200. }
  201. [[nodiscard]] constexpr u32 GetInnerValue() const {
  202. return static_cast<u32>(module.Value()) | (description << module.bits);
  203. }
  204. [[nodiscard]] constexpr bool Includes(Result result) const {
  205. return GetInnerValue() == result.GetInnerValue();
  206. }
  207. };
  208. static_assert(std::is_trivial_v<Result>);
  209. [[nodiscard]] constexpr bool operator==(const Result& a, const Result& b) {
  210. return a.raw == b.raw;
  211. }
  212. [[nodiscard]] constexpr bool operator!=(const Result& a, const Result& b) {
  213. return !operator==(a, b);
  214. }
  215. // Convenience functions for creating some common kinds of errors:
  216. /// The default success `Result`.
  217. constexpr Result ResultSuccess(0);
  218. /**
  219. * Placeholder result code used for unknown error codes.
  220. *
  221. * @note This should only be used when a particular error code
  222. * is not known yet.
  223. */
  224. constexpr Result ResultUnknown(UINT32_MAX);
  225. /**
  226. * A ResultRange defines an inclusive range of error descriptions within an error module.
  227. * This can be used to check whether the description of a given Result falls within the range.
  228. * The conversion function returns a Result with its description set to description_start.
  229. *
  230. * An example of how it could be used:
  231. * \code
  232. * constexpr ResultRange ResultCommonError{ErrorModule::Common, 0, 9999};
  233. *
  234. * Result Example(int value) {
  235. * const Result result = OtherExample(value);
  236. *
  237. * // This will only evaluate to true if result.module is ErrorModule::Common and
  238. * // result.description is in between 0 and 9999 inclusive.
  239. * if (ResultCommonError.Includes(result)) {
  240. * // This returns Result{ErrorModule::Common, 0};
  241. * return ResultCommonError;
  242. * }
  243. *
  244. * return ResultSuccess;
  245. * }
  246. * \endcode
  247. */
  248. class ResultRange {
  249. public:
  250. consteval ResultRange(ErrorModule module, u32 description_start, u32 description_end_)
  251. : code{module, description_start}, description_end{description_end_} {}
  252. [[nodiscard]] constexpr operator Result() const {
  253. return code;
  254. }
  255. [[nodiscard]] constexpr bool Includes(Result other) const {
  256. return code.module == other.module && code.description <= other.description &&
  257. other.description <= description_end;
  258. }
  259. private:
  260. Result code;
  261. u32 description_end;
  262. };
  263. #define R_SUCCEEDED(res) (static_cast<Result>(res).IsSuccess())
  264. #define R_FAILED(res) (static_cast<Result>(res).IsFailure())
  265. namespace ResultImpl {
  266. template <auto EvaluateResult, class F>
  267. class ScopedResultGuard {
  268. YUZU_NON_COPYABLE(ScopedResultGuard);
  269. YUZU_NON_MOVEABLE(ScopedResultGuard);
  270. private:
  271. Result& m_ref;
  272. F m_f;
  273. public:
  274. constexpr ScopedResultGuard(Result& ref, F f) : m_ref(ref), m_f(std::move(f)) {}
  275. constexpr ~ScopedResultGuard() {
  276. if (EvaluateResult(m_ref)) {
  277. m_f();
  278. }
  279. }
  280. };
  281. template <auto EvaluateResult>
  282. class ResultReferenceForScopedResultGuard {
  283. private:
  284. Result& m_ref;
  285. public:
  286. constexpr ResultReferenceForScopedResultGuard(Result& r) : m_ref(r) {}
  287. constexpr operator Result&() const {
  288. return m_ref;
  289. }
  290. };
  291. template <auto EvaluateResult, typename F>
  292. constexpr ScopedResultGuard<EvaluateResult, F> operator+(
  293. ResultReferenceForScopedResultGuard<EvaluateResult> ref, F&& f) {
  294. return ScopedResultGuard<EvaluateResult, F>(static_cast<Result&>(ref), std::forward<F>(f));
  295. }
  296. constexpr bool EvaluateResultSuccess(const Result& r) {
  297. return R_SUCCEEDED(r);
  298. }
  299. constexpr bool EvaluateResultFailure(const Result& r) {
  300. return R_FAILED(r);
  301. }
  302. template <typename T>
  303. constexpr void UpdateCurrentResultReference(T result_reference, Result result) = delete;
  304. // Intentionally not defined
  305. template <>
  306. constexpr void UpdateCurrentResultReference<Result&>(Result& result_reference, Result result) {
  307. result_reference = result;
  308. }
  309. template <>
  310. constexpr void UpdateCurrentResultReference<const Result>(Result result_reference, Result result) {}
  311. } // namespace ResultImpl
  312. #define DECLARE_CURRENT_RESULT_REFERENCE_AND_STORAGE(COUNTER_VALUE) \
  313. [[maybe_unused]] constexpr bool CONCAT2(HasPrevRef_, COUNTER_VALUE) = \
  314. std::same_as<decltype(__TmpCurrentResultReference), Result&>; \
  315. [[maybe_unused]] Result CONCAT2(PrevRef_, COUNTER_VALUE) = __TmpCurrentResultReference; \
  316. [[maybe_unused]] Result CONCAT2(__tmp_result_, COUNTER_VALUE) = ResultSuccess; \
  317. Result& __TmpCurrentResultReference = CONCAT2(HasPrevRef_, COUNTER_VALUE) \
  318. ? CONCAT2(PrevRef_, COUNTER_VALUE) \
  319. : CONCAT2(__tmp_result_, COUNTER_VALUE)
  320. #define ON_RESULT_RETURN_IMPL(...) \
  321. static_assert(std::same_as<decltype(__TmpCurrentResultReference), Result&>); \
  322. auto CONCAT2(RESULT_GUARD_STATE_, __COUNTER__) = \
  323. ResultImpl::ResultReferenceForScopedResultGuard<__VA_ARGS__>( \
  324. __TmpCurrentResultReference) + \
  325. [&]()
  326. #define ON_RESULT_FAILURE_2 ON_RESULT_RETURN_IMPL(ResultImpl::EvaluateResultFailure)
  327. #define ON_RESULT_FAILURE \
  328. DECLARE_CURRENT_RESULT_REFERENCE_AND_STORAGE(__COUNTER__); \
  329. ON_RESULT_FAILURE_2
  330. #define ON_RESULT_SUCCESS_2 ON_RESULT_RETURN_IMPL(ResultImpl::EvaluateResultSuccess)
  331. #define ON_RESULT_SUCCESS \
  332. DECLARE_CURRENT_RESULT_REFERENCE_AND_STORAGE(__COUNTER__); \
  333. ON_RESULT_SUCCESS_2
  334. constexpr inline Result __TmpCurrentResultReference = ResultSuccess;
  335. /// Returns a result.
  336. #define R_RETURN(res_expr) \
  337. { \
  338. const Result _tmp_r_throw_rc = (res_expr); \
  339. ResultImpl::UpdateCurrentResultReference<decltype(__TmpCurrentResultReference)>( \
  340. __TmpCurrentResultReference, _tmp_r_throw_rc); \
  341. return _tmp_r_throw_rc; \
  342. }
  343. /// Returns ResultSuccess()
  344. #define R_SUCCEED() R_RETURN(ResultSuccess)
  345. /// Throws a result.
  346. #define R_THROW(res_expr) R_RETURN(res_expr)
  347. /// Evaluates a boolean expression, and returns a result unless that expression is true.
  348. #define R_UNLESS(expr, res) \
  349. { \
  350. if (!(expr)) { \
  351. R_THROW(res); \
  352. } \
  353. }
  354. /// Evaluates an expression that returns a result, and returns the result if it would fail.
  355. #define R_TRY(res_expr) \
  356. { \
  357. const auto _tmp_r_try_rc = (res_expr); \
  358. if (R_FAILED(_tmp_r_try_rc)) { \
  359. R_THROW(_tmp_r_try_rc); \
  360. } \
  361. }
  362. /// Evaluates a boolean expression, and succeeds if that expression is true.
  363. #define R_SUCCEED_IF(expr) R_UNLESS(!(expr), ResultSuccess)