2014-10-22 23:20:01 -04:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 00:38:14 -05:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-10-22 23:20:01 -04:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2015-06-21 08:40:28 -04:00
|
|
|
#include <new>
|
2014-10-22 23:20:01 -04:00
|
|
|
#include <utility>
|
2015-05-06 03:06:12 -04:00
|
|
|
#include "common/assert.h"
|
2014-10-22 23:20:01 -04:00
|
|
|
#include "common/bit_field.h"
|
2015-01-11 00:42:18 -05:00
|
|
|
#include "common/common_funcs.h"
|
|
|
|
#include "common/common_types.h"
|
2014-10-22 23:20:01 -04:00
|
|
|
|
|
|
|
// All the constants in this file come from http://3dbrew.org/wiki/Error_codes
|
|
|
|
|
|
|
|
/// Detailed description of the error. This listing is likely incomplete.
|
|
|
|
enum class ErrorDescription : u32 {
|
|
|
|
Success = 0,
|
2016-04-17 22:58:51 -04:00
|
|
|
WrongPermission = 46,
|
2016-04-24 06:39:59 -04:00
|
|
|
OS_InvalidBufferDescriptor = 48,
|
2016-12-05 13:59:57 -05:00
|
|
|
MaxConnectionsReached = 52,
|
2015-12-31 09:46:32 -05:00
|
|
|
WrongAddress = 53,
|
2016-06-29 08:47:22 -04:00
|
|
|
FS_ArchiveNotMounted = 101,
|
2016-10-17 02:54:48 -04:00
|
|
|
FS_FileNotFound = 112,
|
|
|
|
FS_PathNotFound = 113,
|
2016-11-28 17:52:11 -05:00
|
|
|
FS_GameCardNotInserted = 141,
|
2015-12-28 09:55:38 -05:00
|
|
|
FS_NotFound = 120,
|
2016-10-17 02:54:48 -04:00
|
|
|
FS_FileAlreadyExists = 180,
|
|
|
|
FS_DirectoryAlreadyExists = 185,
|
2015-12-28 09:55:38 -05:00
|
|
|
FS_AlreadyExists = 190,
|
2015-12-28 10:03:09 -05:00
|
|
|
FS_InvalidOpenFlags = 230,
|
2016-10-17 02:54:48 -04:00
|
|
|
FS_DirectoryNotEmpty = 240,
|
2015-12-28 09:55:38 -05:00
|
|
|
FS_NotAFile = 250,
|
2014-12-16 00:33:41 -05:00
|
|
|
FS_NotFormatted = 340, ///< This is used by the FS service when creating a SaveData archive
|
2016-09-17 20:38:01 -04:00
|
|
|
OutofRangeOrMisalignedAddress =
|
|
|
|
513, // TODO(purpasmart): Check if this name fits its actual usage
|
2016-05-28 07:42:40 -04:00
|
|
|
GPU_FirstInitialization = 519,
|
2016-10-18 05:27:51 -04:00
|
|
|
FS_InvalidReadFlag = 700,
|
2015-12-28 13:51:44 -05:00
|
|
|
FS_InvalidPath = 702,
|
2016-10-17 08:23:34 -04:00
|
|
|
FS_WriteBeyondEnd = 705,
|
2016-10-17 02:54:48 -04:00
|
|
|
FS_UnsupportedOpenFlags = 760,
|
|
|
|
FS_UnexpectedFileOrDirectory = 770,
|
2014-10-22 23:20:01 -04:00
|
|
|
InvalidSection = 1000,
|
|
|
|
TooLarge = 1001,
|
|
|
|
NotAuthorized = 1002,
|
|
|
|
AlreadyDone = 1003,
|
|
|
|
InvalidSize = 1004,
|
|
|
|
InvalidEnumValue = 1005,
|
|
|
|
InvalidCombination = 1006,
|
|
|
|
NoData = 1007,
|
|
|
|
Busy = 1008,
|
|
|
|
MisalignedAddress = 1009,
|
|
|
|
MisalignedSize = 1010,
|
|
|
|
OutOfMemory = 1011,
|
|
|
|
NotImplemented = 1012,
|
|
|
|
InvalidAddress = 1013,
|
|
|
|
InvalidPointer = 1014,
|
|
|
|
InvalidHandle = 1015,
|
|
|
|
NotInitialized = 1016,
|
|
|
|
AlreadyInitialized = 1017,
|
|
|
|
NotFound = 1018,
|
|
|
|
CancelRequested = 1019,
|
|
|
|
AlreadyExists = 1020,
|
|
|
|
OutOfRange = 1021,
|
|
|
|
Timeout = 1022,
|
|
|
|
InvalidResultValue = 1023,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
|
|
|
Util = 2,
|
|
|
|
FileServer = 3,
|
|
|
|
LoaderServer = 4,
|
|
|
|
TCB = 5,
|
|
|
|
OS = 6,
|
|
|
|
DBG = 7,
|
|
|
|
DMNT = 8,
|
|
|
|
PDN = 9,
|
|
|
|
GX = 10,
|
|
|
|
I2C = 11,
|
|
|
|
GPIO = 12,
|
|
|
|
DD = 13,
|
|
|
|
CODEC = 14,
|
|
|
|
SPI = 15,
|
|
|
|
PXI = 16,
|
|
|
|
FS = 17,
|
|
|
|
DI = 18,
|
|
|
|
HID = 19,
|
|
|
|
CAM = 20,
|
|
|
|
PI = 21,
|
|
|
|
PM = 22,
|
|
|
|
PM_LOW = 23,
|
|
|
|
FSI = 24,
|
|
|
|
SRV = 25,
|
|
|
|
NDM = 26,
|
|
|
|
NWM = 27,
|
|
|
|
SOC = 28,
|
|
|
|
LDR = 29,
|
|
|
|
ACC = 30,
|
|
|
|
RomFS = 31,
|
|
|
|
AM = 32,
|
|
|
|
HIO = 33,
|
|
|
|
Updater = 34,
|
|
|
|
MIC = 35,
|
|
|
|
FND = 36,
|
|
|
|
MP = 37,
|
|
|
|
MPWL = 38,
|
|
|
|
AC = 39,
|
|
|
|
HTTP = 40,
|
|
|
|
DSP = 41,
|
|
|
|
SND = 42,
|
|
|
|
DLP = 43,
|
|
|
|
HIO_LOW = 44,
|
|
|
|
CSND = 45,
|
|
|
|
SSL = 46,
|
|
|
|
AM_LOW = 47,
|
|
|
|
NEX = 48,
|
|
|
|
Friends = 49,
|
|
|
|
RDT = 50,
|
|
|
|
Applet = 51,
|
|
|
|
NIM = 52,
|
|
|
|
PTM = 53,
|
|
|
|
MIDI = 54,
|
|
|
|
MC = 55,
|
|
|
|
SWC = 56,
|
|
|
|
FatFS = 57,
|
|
|
|
NGC = 58,
|
|
|
|
CARD = 59,
|
|
|
|
CARDNOR = 60,
|
|
|
|
SDMC = 61,
|
|
|
|
BOSS = 62,
|
|
|
|
DBM = 63,
|
|
|
|
Config = 64,
|
|
|
|
PS = 65,
|
|
|
|
CEC = 66,
|
|
|
|
IR = 67,
|
|
|
|
UDS = 68,
|
|
|
|
PL = 69,
|
|
|
|
CUP = 70,
|
|
|
|
Gyroscope = 71,
|
|
|
|
MCU = 72,
|
|
|
|
NS = 73,
|
|
|
|
News = 74,
|
2016-06-30 04:26:53 -04:00
|
|
|
RO = 75,
|
2014-10-22 23:20:01 -04:00
|
|
|
GD = 76,
|
|
|
|
CardSPI = 77,
|
|
|
|
EC = 78,
|
2016-06-30 04:26:53 -04:00
|
|
|
WebBrowser = 79,
|
|
|
|
Test = 80,
|
|
|
|
ENC = 81,
|
|
|
|
PIA = 82,
|
|
|
|
ACT = 83,
|
|
|
|
VCTL = 84,
|
|
|
|
OLV = 85,
|
|
|
|
NEIA = 86,
|
|
|
|
NPNS = 87,
|
|
|
|
|
|
|
|
AVD = 90,
|
|
|
|
L2B = 91,
|
|
|
|
MVD = 92,
|
|
|
|
NFC = 93,
|
|
|
|
UART = 94,
|
|
|
|
SPM = 95,
|
|
|
|
QTM = 96,
|
|
|
|
NFP = 97,
|
2014-10-22 23:20:01 -04:00
|
|
|
|
|
|
|
Application = 254,
|
|
|
|
InvalidResult = 255
|
|
|
|
};
|
|
|
|
|
|
|
|
/// A less specific error cause.
|
|
|
|
enum class ErrorSummary : u32 {
|
|
|
|
Success = 0,
|
|
|
|
NothingHappened = 1,
|
|
|
|
WouldBlock = 2,
|
2016-09-17 20:38:01 -04:00
|
|
|
OutOfResource = 3, ///< There are no more kernel resources (memory, table slots) to
|
|
|
|
///< execute the operation.
|
|
|
|
NotFound = 4, ///< A file or resource was not found.
|
2014-10-22 23:20:01 -04:00
|
|
|
InvalidState = 5,
|
2016-09-17 20:38:01 -04:00
|
|
|
NotSupported = 6, ///< The operation is not supported or not implemented.
|
|
|
|
InvalidArgument = 7, ///< Returned when a passed argument is invalid in the current runtime
|
|
|
|
///< context. (Invalid handle, out-of-bounds pointer or size, etc.)
|
|
|
|
WrongArgument = 8, ///< Returned when a passed argument is in an incorrect format for use
|
|
|
|
///< with the function. (E.g. Invalid enum value)
|
2014-10-22 23:20:01 -04:00
|
|
|
Canceled = 9,
|
|
|
|
StatusChanged = 10,
|
|
|
|
Internal = 11,
|
|
|
|
|
|
|
|
InvalidResult = 63
|
|
|
|
};
|
|
|
|
|
|
|
|
/// The severity of the error.
|
|
|
|
enum class ErrorLevel : u32 {
|
|
|
|
Success = 0,
|
|
|
|
Info = 1,
|
|
|
|
|
|
|
|
Status = 25,
|
|
|
|
Temporary = 26,
|
|
|
|
Permanent = 27,
|
|
|
|
Usage = 28,
|
|
|
|
Reinitialize = 29,
|
|
|
|
Reset = 30,
|
|
|
|
Fatal = 31
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Encapsulates a CTR-OS error code, allowing it to be separated into its constituent fields.
|
|
|
|
union ResultCode {
|
|
|
|
u32 raw;
|
|
|
|
|
|
|
|
BitField<0, 10, ErrorDescription> description;
|
|
|
|
BitField<10, 8, ErrorModule> module;
|
|
|
|
|
|
|
|
BitField<21, 6, ErrorSummary> summary;
|
|
|
|
BitField<27, 5, ErrorLevel> level;
|
|
|
|
|
2016-09-17 20:38:01 -04:00
|
|
|
// The last bit of `level` is checked by apps and the kernel to determine if a result code is an
|
|
|
|
// error
|
2014-10-22 23:20:01 -04:00
|
|
|
BitField<31, 1, u32> is_error;
|
|
|
|
|
2016-09-18 21:01:46 -04:00
|
|
|
explicit ResultCode(u32 raw) : raw(raw) {}
|
2016-09-17 20:38:01 -04:00
|
|
|
ResultCode(ErrorDescription description_, ErrorModule module_, ErrorSummary summary_,
|
|
|
|
ErrorLevel level_)
|
|
|
|
: raw(0) {
|
2016-02-11 12:41:15 -05:00
|
|
|
description.Assign(description_);
|
|
|
|
module.Assign(module_);
|
|
|
|
summary.Assign(summary_);
|
|
|
|
level.Assign(level_);
|
2014-10-22 23:20:01 -04:00
|
|
|
}
|
|
|
|
|
2016-09-17 20:38:01 -04:00
|
|
|
ResultCode& operator=(const ResultCode& o) {
|
|
|
|
raw = o.raw;
|
|
|
|
return *this;
|
|
|
|
}
|
2014-10-22 23:20:01 -04:00
|
|
|
|
|
|
|
bool IsSuccess() const {
|
|
|
|
return is_error == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsError() const {
|
|
|
|
return is_error == 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-27 21:16:19 -05:00
|
|
|
inline bool operator==(const ResultCode& a, const ResultCode& b) {
|
2014-10-22 23:20:01 -04:00
|
|
|
return a.raw == b.raw;
|
|
|
|
}
|
|
|
|
|
2015-02-27 21:16:19 -05:00
|
|
|
inline bool operator!=(const ResultCode& a, const ResultCode& b) {
|
2014-10-22 23:20:01 -04:00
|
|
|
return a.raw != b.raw;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience functions for creating some common kinds of errors:
|
|
|
|
|
|
|
|
/// The default success `ResultCode`.
|
|
|
|
const ResultCode RESULT_SUCCESS(0);
|
|
|
|
|
|
|
|
/// Might be returned instead of a dummy success for unimplemented APIs.
|
|
|
|
inline ResultCode UnimplementedFunction(ErrorModule module) {
|
2016-09-17 20:38:01 -04:00
|
|
|
return ResultCode(ErrorDescription::NotImplemented, module, ErrorSummary::NotSupported,
|
|
|
|
ErrorLevel::Permanent);
|
2014-10-22 23:20:01 -04: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:
|
2016-09-17 20:38:01 -04:00
|
|
|
/// Constructs an empty `ResultVal` with the given error code. The code must not be a success
|
|
|
|
/// code.
|
|
|
|
ResultVal(ResultCode error_code = ResultCode(-1)) : result_code(error_code) {
|
2015-02-19 00:52:36 -05:00
|
|
|
ASSERT(error_code.IsError());
|
2014-10-22 23:20:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar to the non-member function `MakeResult`, with the exception that you can manually
|
|
|
|
* specify the success code. `success_code` must not be an error code.
|
|
|
|
*/
|
|
|
|
template <typename... Args>
|
|
|
|
static ResultVal WithCode(ResultCode success_code, Args&&... args) {
|
|
|
|
ResultVal<T> result;
|
|
|
|
result.emplace(success_code, std::forward<Args>(args)...);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-09-17 20:38:01 -04:00
|
|
|
ResultVal(const ResultVal& o) : result_code(o.result_code) {
|
2014-10-22 23:20:01 -04:00
|
|
|
if (!o.empty()) {
|
2016-03-05 03:10:52 -05:00
|
|
|
new (&object) T(o.object);
|
2014-10-22 23:20:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-17 20:38:01 -04:00
|
|
|
ResultVal(ResultVal&& o) : result_code(o.result_code) {
|
2014-10-22 23:20:01 -04:00
|
|
|
if (!o.empty()) {
|
2016-03-05 03:10:52 -05:00
|
|
|
new (&object) T(std::move(o.object));
|
2014-10-22 23:20:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~ResultVal() {
|
|
|
|
if (!empty()) {
|
2016-03-05 03:10:52 -05:00
|
|
|
object.~T();
|
2014-10-22 23:20:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultVal& operator=(const ResultVal& o) {
|
2015-02-07 13:05:17 -05:00
|
|
|
if (!empty()) {
|
|
|
|
if (!o.empty()) {
|
2016-03-05 03:10:52 -05:00
|
|
|
object = o.object;
|
2014-10-22 23:20:01 -04:00
|
|
|
} else {
|
2016-03-05 03:10:52 -05:00
|
|
|
object.~T();
|
2014-10-22 23:20:01 -04:00
|
|
|
}
|
|
|
|
} else {
|
2015-02-07 13:05:17 -05:00
|
|
|
if (!o.empty()) {
|
2016-03-05 03:10:52 -05:00
|
|
|
new (&object) T(o.object);
|
2014-10-22 23:20:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result_code = o.result_code;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces the current result with a new constructed result value in-place. The code must not
|
|
|
|
* be an error code.
|
|
|
|
*/
|
|
|
|
template <typename... Args>
|
|
|
|
void emplace(ResultCode success_code, Args&&... args) {
|
2015-02-19 00:52:36 -05:00
|
|
|
ASSERT(success_code.IsSuccess());
|
2014-10-22 23:20:01 -04:00
|
|
|
if (!empty()) {
|
2016-03-05 03:10:52 -05:00
|
|
|
object.~T();
|
2014-10-22 23:20:01 -04:00
|
|
|
}
|
2016-03-05 03:10:52 -05:00
|
|
|
new (&object) T(std::forward<Args>(args)...);
|
2014-10-22 23:20:01 -04:00
|
|
|
result_code = success_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the `ResultVal` contains an error code and no value.
|
2016-09-17 20:38:01 -04:00
|
|
|
bool empty() const {
|
|
|
|
return result_code.IsError();
|
|
|
|
}
|
2014-10-22 23:20:01 -04:00
|
|
|
|
|
|
|
/// Returns true if the `ResultVal` contains a return value.
|
2016-09-17 20:38:01 -04:00
|
|
|
bool Succeeded() const {
|
|
|
|
return result_code.IsSuccess();
|
|
|
|
}
|
2014-10-22 23:20:01 -04:00
|
|
|
/// Returns true if the `ResultVal` contains an error code and no value.
|
2016-09-17 20:38:01 -04:00
|
|
|
bool Failed() const {
|
|
|
|
return empty();
|
|
|
|
}
|
2014-10-22 23:20:01 -04:00
|
|
|
|
2016-09-17 20:38:01 -04:00
|
|
|
ResultCode Code() const {
|
|
|
|
return result_code;
|
|
|
|
}
|
2014-10-22 23:20:01 -04:00
|
|
|
|
2016-09-17 20:38:01 -04:00
|
|
|
const T& operator*() const {
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
T& operator*() {
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
const T* operator->() const {
|
|
|
|
return &object;
|
|
|
|
}
|
|
|
|
T* operator->() {
|
|
|
|
return &object;
|
|
|
|
}
|
2014-10-22 23:20:01 -04:00
|
|
|
|
|
|
|
/// Returns the value contained in this `ResultVal`, or the supplied default if it is missing.
|
|
|
|
template <typename U>
|
|
|
|
T ValueOr(U&& value) const {
|
2016-03-05 03:10:52 -05:00
|
|
|
return !empty() ? object : std::move(value);
|
2014-10-22 23:20:01 -04:00
|
|
|
}
|
|
|
|
|
2015-01-11 00:42:18 -05:00
|
|
|
/// Asserts that the result succeeded and returns a reference to it.
|
|
|
|
T& Unwrap() {
|
2015-01-20 20:16:47 -05:00
|
|
|
ASSERT_MSG(Succeeded(), "Tried to Unwrap empty ResultVal");
|
2015-01-11 00:42:18 -05:00
|
|
|
return **this;
|
|
|
|
}
|
|
|
|
|
|
|
|
T&& MoveFrom() {
|
|
|
|
return std::move(Unwrap());
|
|
|
|
}
|
|
|
|
|
2014-10-22 23:20:01 -04:00
|
|
|
private:
|
2016-03-05 03:10:52 -05:00
|
|
|
// A union is used to allocate the storage for the value, while allowing us to construct and
|
|
|
|
// destruct it at will.
|
2016-09-17 20:38:01 -04:00
|
|
|
union {
|
|
|
|
T object;
|
|
|
|
};
|
2014-10-22 23:20:01 -04:00
|
|
|
ResultCode result_code;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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>
|
|
|
|
ResultVal<T> MakeResult(Args&&... args) {
|
|
|
|
return ResultVal<T>::WithCode(RESULT_SUCCESS, std::forward<Args>(args)...);
|
|
|
|
}
|
2015-01-11 00:42:18 -05: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-17 20:38:01 -04:00
|
|
|
#define CASCADE_RESULT(target, source) \
|
|
|
|
auto CONCAT2(check_result_L, __LINE__) = source; \
|
|
|
|
if (CONCAT2(check_result_L, __LINE__).Failed()) \
|
|
|
|
return CONCAT2(check_result_L, __LINE__).Code(); \
|
|
|
|
target = std::move(*CONCAT2(check_result_L, __LINE__))
|