2019-12-09 11:57:37 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-12-09 11:57:37 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
2020-02-23 07:05:14 +00:00
|
|
|
#include <vapours/common.hpp>
|
|
|
|
#include <vapours/assert.hpp>
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
namespace ams {
|
|
|
|
|
2022-03-10 09:15:45 +00:00
|
|
|
const char *GetResultName(int module, int description);
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
namespace result::impl {
|
|
|
|
|
2022-03-10 09:15:45 +00:00
|
|
|
#if defined(AMS_AUTO_GENERATE_RESULT_NAMES)
|
|
|
|
struct DummyNameHolder {
|
|
|
|
static constexpr bool Exists = false;
|
|
|
|
static constexpr const char *Name = "unknown";
|
|
|
|
};
|
|
|
|
|
|
|
|
template<int Module>
|
|
|
|
struct ResultNameSpaceExistsImpl {
|
|
|
|
static constexpr bool Exists = false;
|
|
|
|
|
|
|
|
template<int Description>
|
|
|
|
using NameHolder = DummyNameHolder;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
class ResultTraits {
|
|
|
|
public:
|
|
|
|
using BaseType = u32;
|
|
|
|
static_assert(std::is_same<BaseType, ::Result>::value, "std::is_same<BaseType, ::Result>::value");
|
|
|
|
static constexpr BaseType SuccessValue = BaseType();
|
|
|
|
static constexpr BaseType ModuleBits = 9;
|
|
|
|
static constexpr BaseType DescriptionBits = 13;
|
|
|
|
static constexpr BaseType ReservedBits = 10;
|
|
|
|
static_assert(ModuleBits + DescriptionBits + ReservedBits == sizeof(BaseType) * CHAR_BIT, "ModuleBits + DescriptionBits + ReservedBits == sizeof(BaseType) * CHAR_BIT");
|
2020-12-03 19:13:35 +00:00
|
|
|
private:
|
|
|
|
static constexpr ALWAYS_INLINE BaseType GetBitsValue(BaseType v, int ofs, int num) {
|
|
|
|
return (v >> ofs) & ~(~BaseType() << num);
|
|
|
|
}
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
2020-02-23 07:05:14 +00:00
|
|
|
static constexpr ALWAYS_INLINE BaseType MakeValue(BaseType module, BaseType description) {
|
2019-12-09 11:57:37 +00:00
|
|
|
return (module) | (description << ModuleBits);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<BaseType module, BaseType description>
|
|
|
|
struct MakeStaticValue : public std::integral_constant<BaseType, MakeValue(module, description)> {
|
|
|
|
static_assert(module < (1 << ModuleBits), "Invalid Module");
|
|
|
|
static_assert(description < (1 << DescriptionBits), "Invalid Description");
|
|
|
|
};
|
|
|
|
|
2020-02-23 07:05:14 +00:00
|
|
|
static constexpr ALWAYS_INLINE BaseType GetModuleFromValue(BaseType value) {
|
2020-12-03 19:13:35 +00:00
|
|
|
return GetBitsValue(value, 0, ModuleBits);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 07:05:14 +00:00
|
|
|
static constexpr ALWAYS_INLINE BaseType GetDescriptionFromValue(BaseType value) {
|
2020-12-03 19:13:35 +00:00
|
|
|
return GetBitsValue(value, ModuleBits, DescriptionBits);
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr ALWAYS_INLINE BaseType GetReservedFromValue(BaseType value) {
|
|
|
|
return GetBitsValue(value, ModuleBits + DescriptionBits, ReservedBits);
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr ALWAYS_INLINE BaseType MaskReservedFromValue(BaseType value) {
|
|
|
|
return value & ~(~(~BaseType() << ReservedBits) << (ModuleBits + DescriptionBits));
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr ALWAYS_INLINE BaseType MergeValueWithReserved(BaseType value, BaseType reserved) {
|
|
|
|
return (value << 0) | (reserved << (ModuleBits + DescriptionBits));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Use CRTP for Results. */
|
|
|
|
template<typename Self>
|
|
|
|
class ResultBase {
|
|
|
|
public:
|
|
|
|
using BaseType = typename ResultTraits::BaseType;
|
|
|
|
static constexpr BaseType SuccessValue = ResultTraits::SuccessValue;
|
|
|
|
public:
|
2020-01-31 00:51:35 +00:00
|
|
|
constexpr ALWAYS_INLINE BaseType GetModule() const { return ResultTraits::GetModuleFromValue(static_cast<const Self *>(this)->GetValue()); }
|
|
|
|
constexpr ALWAYS_INLINE BaseType GetDescription() const { return ResultTraits::GetDescriptionFromValue(static_cast<const Self *>(this)->GetValue()); }
|
2019-12-09 11:57:37 +00:00
|
|
|
};
|
|
|
|
|
2020-12-03 19:13:35 +00:00
|
|
|
class ResultInternalAccessor;
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class ResultSuccess;
|
|
|
|
|
|
|
|
class Result final : public result::impl::ResultBase<Result> {
|
2020-12-03 19:13:35 +00:00
|
|
|
friend class result::impl::ResultInternalAccessor;
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
|
|
|
using Base = typename result::impl::ResultBase<Result>;
|
|
|
|
private:
|
2021-10-01 22:12:38 +00:00
|
|
|
typename Base::BaseType m_value;
|
2019-12-09 11:57:37 +00:00
|
|
|
private:
|
|
|
|
/* TODO: Maybe one-day, the result constructor. */
|
|
|
|
public:
|
|
|
|
Result() { /* ... */ }
|
|
|
|
|
|
|
|
/* TODO: It sure would be nice to make this private. */
|
2021-10-01 22:12:38 +00:00
|
|
|
constexpr ALWAYS_INLINE Result(typename Base::BaseType v) : m_value(v) { static_assert(std::is_same<typename Base::BaseType, ::Result>::value); }
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2020-01-31 00:51:35 +00:00
|
|
|
constexpr ALWAYS_INLINE operator ResultSuccess() const;
|
2021-10-01 22:12:38 +00:00
|
|
|
static constexpr ALWAYS_INLINE bool CanAccept(Result) { return true; }
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2021-10-01 22:12:38 +00:00
|
|
|
constexpr ALWAYS_INLINE bool IsSuccess() const { return m_value == Base::SuccessValue; }
|
2020-01-31 00:51:35 +00:00
|
|
|
constexpr ALWAYS_INLINE bool IsFailure() const { return !this->IsSuccess(); }
|
|
|
|
constexpr ALWAYS_INLINE typename Base::BaseType GetModule() const { return Base::GetModule(); }
|
|
|
|
constexpr ALWAYS_INLINE typename Base::BaseType GetDescription() const { return Base::GetDescription(); }
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2021-10-01 22:12:38 +00:00
|
|
|
constexpr ALWAYS_INLINE typename Base::BaseType GetInnerValue() const { return ::ams::result::impl::ResultTraits::MaskReservedFromValue(m_value); }
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE typename Base::BaseType GetValue() const { return m_value; }
|
2019-12-09 11:57:37 +00:00
|
|
|
};
|
|
|
|
static_assert(sizeof(Result) == sizeof(Result::Base::BaseType), "sizeof(Result) == sizeof(Result::Base::BaseType)");
|
|
|
|
static_assert(std::is_trivially_destructible<Result>::value, "std::is_trivially_destructible<Result>::value");
|
|
|
|
|
2022-03-10 09:15:45 +00:00
|
|
|
ALWAYS_INLINE const char *GetResultName(const Result &result) {
|
|
|
|
return GetResultName(result.GetModule(), result.GetDescription());
|
|
|
|
}
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
namespace result::impl {
|
|
|
|
|
2020-12-03 19:13:35 +00:00
|
|
|
class ResultInternalAccessor {
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
2020-01-31 00:51:35 +00:00
|
|
|
static constexpr ALWAYS_INLINE Result MakeResult(ResultTraits::BaseType value) {
|
2019-12-09 11:57:37 +00:00
|
|
|
return Result(value);
|
|
|
|
}
|
2020-12-03 19:13:35 +00:00
|
|
|
|
|
|
|
static constexpr ALWAYS_INLINE ResultTraits::BaseType GetReserved(Result result) {
|
2021-10-01 22:12:38 +00:00
|
|
|
return ResultTraits::GetReservedFromValue(result.m_value);
|
2020-12-03 19:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr ALWAYS_INLINE Result MergeReserved(Result result, ResultTraits::BaseType reserved) {
|
2021-10-01 22:12:38 +00:00
|
|
|
return Result(ResultTraits::MergeValueWithReserved(ResultTraits::MaskReservedFromValue(result.m_value), reserved));
|
2020-12-03 19:13:35 +00:00
|
|
|
}
|
2019-12-09 11:57:37 +00:00
|
|
|
};
|
|
|
|
|
2020-01-31 00:51:35 +00:00
|
|
|
constexpr ALWAYS_INLINE Result MakeResult(ResultTraits::BaseType value) {
|
2020-12-03 19:13:35 +00:00
|
|
|
return ResultInternalAccessor::MakeResult(value);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class ResultSuccess final : public result::impl::ResultBase<ResultSuccess> {
|
|
|
|
public:
|
|
|
|
using Base = typename result::impl::ResultBase<ResultSuccess>;
|
|
|
|
public:
|
2021-10-01 22:12:38 +00:00
|
|
|
constexpr ALWAYS_INLINE operator Result() const { return result::impl::MakeResult(Base::SuccessValue); }
|
|
|
|
static constexpr ALWAYS_INLINE bool CanAccept(Result result) { return result.IsSuccess(); }
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2020-01-31 00:51:35 +00:00
|
|
|
constexpr ALWAYS_INLINE bool IsSuccess() const { return true; }
|
|
|
|
constexpr ALWAYS_INLINE bool IsFailure() const { return !this->IsSuccess(); }
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2020-01-31 00:51:35 +00:00
|
|
|
constexpr ALWAYS_INLINE typename Base::BaseType GetValue() const { return Base::SuccessValue; }
|
2019-12-09 11:57:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace result::impl {
|
|
|
|
|
2020-02-23 07:05:14 +00:00
|
|
|
NORETURN NOINLINE void OnResultAssertion(const char *file, int line, const char *func, const char *expr, Result result);
|
|
|
|
NORETURN NOINLINE void OnResultAssertion(Result result);
|
|
|
|
NORETURN NOINLINE void OnResultAbort(const char *file, int line, const char *func, const char *expr, Result result);
|
|
|
|
NORETURN NOINLINE void OnResultAbort(Result result);
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-31 00:51:35 +00:00
|
|
|
constexpr ALWAYS_INLINE Result::operator ResultSuccess() const {
|
2019-12-09 11:57:37 +00:00
|
|
|
if (!ResultSuccess::CanAccept(*this)) {
|
2020-02-23 07:05:14 +00:00
|
|
|
result::impl::OnResultAbort(*this);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace result::impl {
|
|
|
|
|
|
|
|
template<ResultTraits::BaseType _Module, ResultTraits::BaseType _Description>
|
|
|
|
class ResultErrorBase : public ResultBase<ResultErrorBase<_Module, _Description>> {
|
|
|
|
public:
|
|
|
|
using Base = typename result::impl::ResultBase<ResultErrorBase<_Module, _Description>>;
|
|
|
|
static constexpr typename Base::BaseType Module = _Module;
|
|
|
|
static constexpr typename Base::BaseType Description = _Description;
|
|
|
|
static constexpr typename Base::BaseType Value = ResultTraits::MakeStaticValue<Module, Description>::value;
|
|
|
|
static_assert(Value != Base::SuccessValue, "Value != Base::SuccessValue");
|
|
|
|
public:
|
2021-10-01 22:12:38 +00:00
|
|
|
constexpr ALWAYS_INLINE operator Result() const { return MakeResult(Value); }
|
2021-11-07 01:19:34 +00:00
|
|
|
constexpr ALWAYS_INLINE operator ResultSuccess() const {
|
|
|
|
OnResultAbort(Value);
|
|
|
|
__builtin_unreachable();
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2020-01-31 00:51:35 +00:00
|
|
|
constexpr ALWAYS_INLINE bool IsSuccess() const { return false; }
|
|
|
|
constexpr ALWAYS_INLINE bool IsFailure() const { return !this->IsSuccess(); }
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2020-01-31 00:51:35 +00:00
|
|
|
constexpr ALWAYS_INLINE typename Base::BaseType GetValue() const { return Value; }
|
2019-12-09 11:57:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<ResultTraits::BaseType _Module, ResultTraits::BaseType DescStart, ResultTraits::BaseType DescEnd>
|
|
|
|
class ResultErrorRangeBase {
|
2021-10-01 22:12:38 +00:00
|
|
|
private:
|
|
|
|
/* NOTE: GCC does not optimize the module/description comparisons into one check (as of 10/1/2021) */
|
|
|
|
/* and so this optimizes result comparisons to get the same codegen as Nintendo does. */
|
|
|
|
static constexpr bool UseDirectValueComparison = true;
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
|
|
|
static constexpr ResultTraits::BaseType Module = _Module;
|
|
|
|
static constexpr ResultTraits::BaseType DescriptionStart = DescStart;
|
|
|
|
static constexpr ResultTraits::BaseType DescriptionEnd = DescEnd;
|
|
|
|
static_assert(DescriptionStart <= DescriptionEnd, "DescriptionStart <= DescriptionEnd");
|
|
|
|
static constexpr typename ResultTraits::BaseType StartValue = ResultTraits::MakeStaticValue<Module, DescriptionStart>::value;
|
|
|
|
static constexpr typename ResultTraits::BaseType EndValue = ResultTraits::MakeStaticValue<Module, DescriptionEnd>::value;
|
|
|
|
public:
|
2021-10-01 22:12:38 +00:00
|
|
|
static constexpr ALWAYS_INLINE bool Includes(Result result) {
|
|
|
|
if constexpr (UseDirectValueComparison) {
|
|
|
|
const auto inner_value = result.GetInnerValue();
|
|
|
|
if constexpr (StartValue == EndValue) {
|
|
|
|
return inner_value == StartValue;
|
|
|
|
} else {
|
|
|
|
return StartValue <= inner_value && inner_value <= EndValue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return result.GetModule() == Module && DescriptionStart <= result.GetDescription() && result.GetDescription() <= DescriptionEnd;
|
|
|
|
}
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
#if defined(ATMOSPHERE_BOARD_NINTENDO_NX) && defined(ATMOSPHERE_ARCH_ARM64) && defined(ATMOSPHERE_IS_STRATOSPHERE)
|
|
|
|
namespace diag::impl {
|
|
|
|
|
|
|
|
void FatalErrorByResultForNx(Result result) noexcept NORETURN;
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Macros for defining new results. */
|
2022-03-10 09:15:45 +00:00
|
|
|
#if defined(AMS_AUTO_GENERATE_RESULT_NAMES)
|
|
|
|
#define R_DEFINE_NAMESPACE_RESULT_MODULE(nmspc, value) \
|
|
|
|
namespace nmspc { \
|
|
|
|
\
|
|
|
|
namespace result_impl { \
|
|
|
|
static constexpr inline ::ams::result::impl::ResultTraits::BaseType ResultModuleId = value; \
|
|
|
|
\
|
|
|
|
template<int Description> \
|
|
|
|
struct ResultNameHolderImpl { static constexpr bool Exists = false; }; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
namespace ams::result::impl { \
|
|
|
|
\
|
|
|
|
template<> struct ResultNameSpaceExistsImpl<value> { \
|
|
|
|
static constexpr bool Exists = true; \
|
|
|
|
\
|
|
|
|
template<int Description> \
|
|
|
|
using NameHolder = nmspc::result_impl::ResultNameHolderImpl<Description>; \
|
|
|
|
}; \
|
|
|
|
\
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define R_DEFINE_NAMESPACE_RESULT_MODULE(nmspc, value) \
|
|
|
|
namespace nmspc { \
|
|
|
|
\
|
|
|
|
namespace result_impl { \
|
|
|
|
static constexpr inline ::ams::result::impl::ResultTraits::BaseType ResultModuleId = value; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define R_CURRENT_NAMESPACE_RESULT_MODULE result_impl::ResultModuleId
|
2019-12-09 11:57:37 +00:00
|
|
|
#define R_NAMESPACE_MODULE_ID(nmspc) nmspc::R_CURRENT_NAMESPACE_RESULT_MODULE
|
|
|
|
|
|
|
|
#define R_MAKE_NAMESPACE_RESULT(nmspc, desc) static_cast<::ams::Result>(::ams::result::impl::ResultTraits::MakeValue(R_NAMESPACE_MODULE_ID(nmspc), desc))
|
|
|
|
|
2022-03-10 09:15:45 +00:00
|
|
|
#if defined(AMS_AUTO_GENERATE_RESULT_NAMES)
|
|
|
|
#define R_DEFINE_ERROR_RESULT_NAME_HOLDER_IMPL(name, desc_start, desc_end) \
|
|
|
|
template<> struct result_impl::ResultNameHolderImpl<desc_start> { static constexpr bool Exists = true; static constexpr const char *Name = #name; };
|
|
|
|
#else
|
|
|
|
#define R_DEFINE_ERROR_RESULT_NAME_HOLDER_IMPL(name, desc_start, desc_end)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define R_DEFINE_ERROR_RESULT_CLASS_IMPL(name, desc_start, desc_end) \
|
2019-12-09 11:57:37 +00:00
|
|
|
class Result##name final : public ::ams::result::impl::ResultErrorBase<R_CURRENT_NAMESPACE_RESULT_MODULE, desc_start>, public ::ams::result::impl::ResultErrorRangeBase<R_CURRENT_NAMESPACE_RESULT_MODULE, desc_start, desc_end> {}
|
|
|
|
|
2022-03-10 09:15:45 +00:00
|
|
|
#define R_DEFINE_ERROR_RESULT_IMPL(name, desc_start, desc_end) \
|
|
|
|
R_DEFINE_ERROR_RESULT_NAME_HOLDER_IMPL(name, desc_start, desc_end) \
|
|
|
|
R_DEFINE_ERROR_RESULT_CLASS_IMPL(name, desc_start, desc_end)
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
#define R_DEFINE_ABSTRACT_ERROR_RESULT_IMPL(name, desc_start, desc_end) \
|
|
|
|
class Result##name final : public ::ams::result::impl::ResultErrorRangeBase<R_CURRENT_NAMESPACE_RESULT_MODULE, desc_start, desc_end> {}
|
|
|
|
|
|
|
|
#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)
|
|
|
|
|
2022-03-10 09:15:45 +00:00
|
|
|
|
|
|
|
#define R_DEFINE_ERROR_RESULT_NS(ns, name, desc) namespace ns { R_DEFINE_ERROR_RESULT_CLASS_IMPL(name, desc, desc); } R_DEFINE_ERROR_RESULT_NAME_HOLDER_IMPL(name, desc, desc)
|
|
|
|
#define R_DEFINE_ERROR_RANGE_NS(ns, name, start, end) namespace ns { R_DEFINE_ERROR_RESULT_CLASS_IMPL(name, start, end); } R_DEFINE_ERROR_RESULT_NAME_HOLDER_IMPL(name, start, end)
|
|
|
|
|
|
|
|
#define R_DEFINE_ABSTRACT_ERROR_RESULT_NS(ns, name, desc) namespace ns { R_DEFINE_ABSTRACT_ERROR_RESULT_IMPL(name, desc, desc); }
|
|
|
|
#define R_DEFINE_ABSTRACT_ERROR_RANGE_NS(ns, name, start, end) namespace ns { R_DEFINE_ABSTRACT_ERROR_RESULT_IMPL(name, start, end); }
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
/* Remove libnx macros, replace with our own. */
|
|
|
|
#ifndef R_SUCCEEDED
|
|
|
|
#error "R_SUCCEEDED not defined."
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef R_SUCCEEDED
|
|
|
|
|
|
|
|
#ifndef R_FAILED
|
|
|
|
#error "R_FAILED not defined"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef R_FAILED
|
|
|
|
|
|
|
|
#define R_SUCCEEDED(res) (static_cast<::ams::Result>(res).IsSuccess())
|
|
|
|
#define R_FAILED(res) (static_cast<::ams::Result>(res).IsFailure())
|
|
|
|
|
|
|
|
|
2022-02-14 22:45:32 +00:00
|
|
|
/* NOTE: The following are experimental and cannot be safely used yet. */
|
|
|
|
/* =================================================================== */
|
|
|
|
constinit inline ::ams::Result __TmpCurrentResultReference = ::ams::ResultSuccess();
|
|
|
|
|
|
|
|
namespace ams::result::impl {
|
|
|
|
|
|
|
|
template<auto EvaluateResult, class F>
|
|
|
|
class ScopedResultGuard {
|
|
|
|
NON_COPYABLE(ScopedResultGuard);
|
|
|
|
NON_MOVEABLE(ScopedResultGuard);
|
|
|
|
private:
|
|
|
|
Result &m_ref;
|
|
|
|
F m_f;
|
|
|
|
public:
|
|
|
|
constexpr ALWAYS_INLINE ScopedResultGuard(Result &ref, F f) : m_ref(ref), m_f(std::move(f)) { }
|
|
|
|
constexpr ALWAYS_INLINE ~ScopedResultGuard() { if (EvaluateResult(m_ref)) { m_f(); } }
|
|
|
|
};
|
|
|
|
|
|
|
|
template<auto EvaluateResult>
|
|
|
|
class ResultReferenceForScopedResultGuard {
|
|
|
|
private:
|
|
|
|
Result &m_ref;
|
|
|
|
public:
|
|
|
|
constexpr ALWAYS_INLINE ResultReferenceForScopedResultGuard(Result &r) : m_ref(r) { /* ... */ }
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE operator Result &() const { return m_ref; }
|
|
|
|
};
|
|
|
|
|
|
|
|
template<auto EvaluateResult, typename F>
|
|
|
|
constexpr ALWAYS_INLINE ScopedResultGuard<EvaluateResult, F> operator+(ResultReferenceForScopedResultGuard<EvaluateResult> ref, F&& f) {
|
|
|
|
return ScopedResultGuard<EvaluateResult, F>(static_cast<Result &>(ref), std::forward<F>(f));
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE bool EvaluateResultSuccess(const ::ams::Result &r) { return R_SUCCEEDED(r); }
|
|
|
|
constexpr ALWAYS_INLINE bool EvaluateResultFailure(const ::ams::Result &r) { return R_FAILED(r); }
|
|
|
|
|
|
|
|
template<typename R>
|
|
|
|
constexpr ALWAYS_INLINE bool EvaluateResultIncludedImplForSuccessCompatibility(const ::ams::Result &r) {
|
|
|
|
if constexpr (std::same_as<R, ::ams::ResultSuccess>) {
|
|
|
|
return R_SUCCEEDED(r);
|
|
|
|
} else {
|
|
|
|
return R::Includes(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Rs>
|
|
|
|
constexpr ALWAYS_INLINE bool EvaluateAnyResultIncludes(const ::ams::Result &r) { return (EvaluateResultIncludedImplForSuccessCompatibility<Rs>(r) || ...); }
|
|
|
|
|
|
|
|
template<typename... Rs>
|
|
|
|
constexpr ALWAYS_INLINE bool EvaluateResultNotIncluded(const ::ams::Result &r) { return !EvaluateAnyResultIncludes<Rs...>(r); }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#define AMS_DECLARE_CURRENT_RESULT_REFERENCE_AND_STORAGE(COUNTER_VALUE) \
|
|
|
|
[[maybe_unused]] constexpr bool HasPrevRef_##COUNTER_VALUE = std::same_as<decltype(__TmpCurrentResultReference), Result &>; \
|
|
|
|
[[maybe_unused]] auto &PrevRef_##COUNTER_VALUE = __TmpCurrentResultReference; \
|
|
|
|
[[maybe_unused]] Result __tmp_result_##COUNTER_VALUE = ResultSuccess(); \
|
|
|
|
::ams::Result &__TmpCurrentResultReference = HasPrevRef_##COUNTER_VALUE ? PrevRef_##COUNTER_VALUE : __tmp_result_##COUNTER_VALUE
|
|
|
|
|
|
|
|
#define ON_RESULT_RETURN_IMPL(...) \
|
|
|
|
static_assert(std::same_as<decltype(__TmpCurrentResultReference), Result &>); \
|
|
|
|
auto ANONYMOUS_VARIABLE(RESULT_GUARD_STATE_) = ::ams::result::impl::ResultReferenceForScopedResultGuard<__VA_ARGS__>(__TmpCurrentResultReference) + [&]() ALWAYS_INLINE_LAMBDA
|
|
|
|
|
|
|
|
#define ON_RESULT_FAILURE_2 ON_RESULT_RETURN_IMPL(::ams::result::impl::EvaluateResultFailure)
|
|
|
|
|
|
|
|
#define ON_RESULT_FAILURE \
|
|
|
|
AMS_DECLARE_CURRENT_RESULT_REFERENCE_AND_STORAGE(__COUNTER__); \
|
|
|
|
ON_RESULT_FAILURE_2
|
|
|
|
|
|
|
|
#define ON_RESULT_SUCCESS_2 ON_RESULT_RETURN_IMPL(::ams::result::impl::EvaluateResultSuccess)
|
|
|
|
|
|
|
|
#define ON_RESULT_SUCCESS \
|
|
|
|
AMS_DECLARE_CURRENT_RESULT_REFERENCE_AND_STORAGE(__COUNTER__); \
|
|
|
|
ON_RESULT_SUCCESS_2
|
|
|
|
|
|
|
|
#define ON_RESULT_INCLUDED_2(...) ON_RESULT_RETURN_IMPL(::ams::result::impl::EvaluateAnyResultIncludes<__VA_ARGS__>)
|
|
|
|
|
|
|
|
#define ON_RESULT_INCLUDED(...) \
|
|
|
|
AMS_DECLARE_CURRENT_RESULT_REFERENCE_AND_STORAGE(__COUNTER__); \
|
|
|
|
ON_RESULT_INCLUDED_2(__VA_ARGS__)
|
|
|
|
|
|
|
|
#define ON_RESULT_NOT_INCLUDED_2(...) ON_RESULT_RETURN_IMPL(::ams::result::impl::EvaluateResultNotIncluded<__VA_ARGS__>)
|
|
|
|
|
|
|
|
#define ON_RESULT_NOT_INCLUDED(...) \
|
|
|
|
AMS_DECLARE_CURRENT_RESULT_REFERENCE_AND_STORAGE(__COUNTER__); \
|
|
|
|
ON_RESULT_NOT_INCLUDED_2(__VA_ARGS__)
|
|
|
|
|
|
|
|
#define ON_RESULT_FAILURE_BESIDES(...) ON_RESULT_NOT_INCLUDED(::ams::ResultSuccess, ## __VA_ARGS__)
|
|
|
|
|
|
|
|
#define ON_RESULT_FAILURE_BESIDES_2(...) ON_RESULT_NOT_INCLUDED_2(::ams::ResultSuccess, ## __VA_ARGS__)
|
|
|
|
|
|
|
|
/* =================================================================== */
|
|
|
|
|
|
|
|
/// Returns a result.
|
2022-03-26 21:48:33 +00:00
|
|
|
#define R_RETURN(res_expr) \
|
2022-03-06 20:08:20 +00:00
|
|
|
{ \
|
2022-02-14 22:45:32 +00:00
|
|
|
const ::ams::Result _tmp_r_throw_rc = (res_expr); \
|
|
|
|
if constexpr (std::same_as<decltype(__TmpCurrentResultReference), ::ams::Result &>) { __TmpCurrentResultReference = _tmp_r_throw_rc; } \
|
|
|
|
return _tmp_r_throw_rc; \
|
2022-03-06 20:08:20 +00:00
|
|
|
}
|
2022-02-14 22:45:32 +00:00
|
|
|
|
|
|
|
/// Returns ResultSuccess()
|
2022-03-26 21:48:33 +00:00
|
|
|
#define R_SUCCEED() R_RETURN(::ams::ResultSuccess())
|
|
|
|
|
|
|
|
/// Throws a result.
|
|
|
|
#define R_THROW(res_expr) R_RETURN(res_expr)
|
2022-02-14 22:45:32 +00:00
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
/// Evaluates an expression that returns a result, and returns the result if it would fail.
|
2022-02-14 22:45:32 +00:00
|
|
|
#define R_TRY(res_expr) \
|
2022-03-06 20:08:20 +00:00
|
|
|
{ \
|
2022-02-14 22:45:32 +00:00
|
|
|
if (const auto _tmp_r_try_rc = (res_expr); R_FAILED(_tmp_r_try_rc)) { \
|
|
|
|
R_THROW(_tmp_r_try_rc); \
|
|
|
|
} \
|
2022-03-06 20:08:20 +00:00
|
|
|
}
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
#if defined(ATMOSPHERE_BOARD_NINTENDO_NX) && defined(ATMOSPHERE_IS_STRATOSPHERE) && !defined(AMS_ENABLE_DETAILED_ASSERTIONS) && !defined(AMS_BUILD_FOR_DEBUGGING) && !defined(AMS_BUILD_FOR_AUDITING)
|
|
|
|
#define AMS_CALL_ON_RESULT_ASSERTION_IMPL(cond, val) do { ::ams::diag::impl::FatalErrorByResultForNx(val); AMS_INFINITE_LOOP(); AMS_ASSUME(false); } while (false)
|
|
|
|
#define AMS_CALL_ON_RESULT_ABORT_IMPL(cond, val) do { ::ams::diag::impl::FatalErrorByResultForNx(val); AMS_INFINITE_LOOP(); AMS_ASSUME(false); } while (false)
|
2022-03-10 09:15:45 +00:00
|
|
|
#elif defined(ATMOSPHERE_OS_HORIZON)
|
|
|
|
#define AMS_CALL_ON_RESULT_ASSERTION_IMPL(cond, val) AMS_CALL_ASSERT_FAIL_IMPL(::ams::diag::AssertionType_Assert, "ams::Result::IsSuccess()", "Failed: %s\n Module: %d\n Description: %d\n InnerValue: 0x%08" PRIX32, cond, val.GetModule(), val.GetDescription(), static_cast<::ams::Result>(val).GetInnerValue())
|
2023-03-11 22:14:33 +00:00
|
|
|
#define AMS_CALL_ON_RESULT_ABORT_IMPL(cond, val) AMS_CALL_ABORT_IMPL("ams::Result::IsSuccess()", "Failed: %s\n Module: %d\n Description: %d\n InnerValue: 0x%08" PRIX32, cond, static_cast<::ams::Result>(val).GetModule(), static_cast<::ams::Result>(val).GetDescription(), static_cast<::ams::Result>(val).GetInnerValue())
|
2020-02-23 07:05:14 +00:00
|
|
|
#else
|
2023-03-11 22:14:33 +00:00
|
|
|
#define AMS_CALL_ON_RESULT_ASSERTION_IMPL(cond, val) AMS_CALL_ASSERT_FAIL_IMPL(::ams::diag::AssertionType_Assert, "ams::Result::IsSuccess()", "Failed: %s\n Module: %d\n Description: %d\n InnerValue: 0x%08" PRIX32 "\n Name: %s", cond, val.GetModule(), val.GetDescription(), static_cast<::ams::Result>(val).GetInnerValue(), ::ams::GetResultName(static_cast<::ams::Result>(val)))
|
|
|
|
#define AMS_CALL_ON_RESULT_ABORT_IMPL(cond, val) AMS_CALL_ABORT_IMPL("ams::Result::IsSuccess()", "Failed: %s\n Module: %d\n Description: %d\n InnerValue: 0x%08" PRIX32 "\n Name: %s", cond, static_cast<::ams::Result>(val).GetModule(), static_cast<::ams::Result>(val).GetDescription(), static_cast<::ams::Result>(val).GetInnerValue(), ::ams::GetResultName(static_cast<::ams::Result>(val)))
|
2020-02-23 07:05:14 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/// Evaluates an expression that returns a result, and asserts the result if it would fail.
|
|
|
|
#ifdef AMS_ENABLE_ASSERTIONS
|
2022-02-14 22:45:32 +00:00
|
|
|
#define R_ASSERT(res_expr) \
|
2022-03-06 20:08:20 +00:00
|
|
|
{ \
|
2022-02-14 22:45:32 +00:00
|
|
|
if (const auto _tmp_r_assert_rc = (res_expr); AMS_UNLIKELY(R_FAILED(_tmp_r_assert_rc))) { \
|
|
|
|
AMS_CALL_ON_RESULT_ASSERTION_IMPL(#res_expr, _tmp_r_assert_rc); \
|
|
|
|
} \
|
2022-03-06 20:08:20 +00:00
|
|
|
}
|
2020-02-23 07:05:14 +00:00
|
|
|
#else
|
|
|
|
#define R_ASSERT(res_expr) AMS_UNUSED((res_expr));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/// Evaluates an expression that returns a result, and aborts if the result would fail.
|
2022-02-14 22:45:32 +00:00
|
|
|
#define R_ABORT_UNLESS(res_expr) \
|
2022-03-06 20:08:20 +00:00
|
|
|
{ \
|
2022-02-14 22:45:32 +00:00
|
|
|
if (const auto _tmp_r_abort_rc = (res_expr); AMS_UNLIKELY(R_FAILED(_tmp_r_abort_rc))) { \
|
|
|
|
AMS_CALL_ON_RESULT_ABORT_IMPL(#res_expr, _tmp_r_abort_rc); \
|
|
|
|
} \
|
2022-03-06 20:08:20 +00:00
|
|
|
}
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
/// Evaluates a boolean expression, and returns a result unless that expression is true.
|
|
|
|
#define R_UNLESS(expr, res) \
|
2022-03-06 20:08:20 +00:00
|
|
|
{ \
|
2022-02-14 22:45:32 +00:00
|
|
|
if (!(expr)) { \
|
|
|
|
R_THROW(res); \
|
|
|
|
} \
|
2022-03-06 20:08:20 +00:00
|
|
|
}
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2020-03-08 08:06:23 +00:00
|
|
|
/// Evaluates a boolean expression, and succeeds if that expression is true.
|
|
|
|
#define R_SUCCEED_IF(expr) R_UNLESS(!(expr), ResultSuccess())
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
/// Helpers for pattern-matching on a result expression, if the result would fail.
|
2022-02-14 22:45:32 +00:00
|
|
|
#define R_CURRENT_RESULT _tmp_r_try_catch_current_result
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
#define R_TRY_CATCH(res_expr) \
|
|
|
|
{ \
|
2020-02-23 07:05:14 +00:00
|
|
|
const auto R_CURRENT_RESULT = (res_expr); \
|
2022-03-06 20:08:20 +00:00
|
|
|
if (R_FAILED(R_CURRENT_RESULT)) { \
|
2019-12-09 11:57:37 +00:00
|
|
|
if (false)
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
#define R_CATCH(...) \
|
2022-02-14 22:45:32 +00:00
|
|
|
} else if (::ams::result::impl::EvaluateAnyResultIncludes<__VA_ARGS__>(R_CURRENT_RESULT)) { \
|
2019-12-09 11:57:37 +00:00
|
|
|
if (true)
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
#define R_CATCH_MODULE(__module__) \
|
2020-04-27 17:33:51 +00:00
|
|
|
} else if ((R_CURRENT_RESULT).GetModule() == ::ams::R_NAMESPACE_MODULE_ID(__module__)) { \
|
|
|
|
if (true)
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
#define R_CONVERT(catch_type, convert_type) \
|
2022-02-14 22:45:32 +00:00
|
|
|
R_CATCH(catch_type) { R_THROW(static_cast<::ams::Result>(convert_type)); }
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
#define R_CATCH_ALL() \
|
2019-12-09 11:57:37 +00:00
|
|
|
} else if (R_FAILED(R_CURRENT_RESULT)) { \
|
|
|
|
if (true)
|
|
|
|
|
|
|
|
#define R_CONVERT_ALL(convert_type) \
|
2022-02-14 22:45:32 +00:00
|
|
|
R_CATCH_ALL() { R_THROW(static_cast<::ams::Result>(convert_type)); }
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
#define R_CATCH_RETHROW(catch_type) \
|
|
|
|
R_CONVERT(catch_type, R_CURRENT_RESULT)
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
#define R_END_TRY_CATCH \
|
2019-12-09 11:57:37 +00:00
|
|
|
else if (R_FAILED(R_CURRENT_RESULT)) { \
|
2022-03-06 20:08:20 +00:00
|
|
|
R_THROW(R_CURRENT_RESULT); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
}
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
#define R_END_TRY_CATCH_WITH_ASSERT \
|
|
|
|
else { \
|
2019-12-09 11:57:37 +00:00
|
|
|
R_ASSERT(R_CURRENT_RESULT); \
|
2022-03-06 20:08:20 +00:00
|
|
|
} \
|
|
|
|
} \
|
|
|
|
}
|
2020-02-23 07:05:14 +00:00
|
|
|
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
#define R_END_TRY_CATCH_WITH_ABORT_UNLESS \
|
|
|
|
else { \
|
2020-02-23 07:05:14 +00:00
|
|
|
R_ABORT_UNLESS(R_CURRENT_RESULT); \
|
2022-03-06 20:08:20 +00:00
|
|
|
} \
|
|
|
|
} \
|
|
|
|
}
|
2022-02-14 22:45:32 +00:00
|
|
|
|
|
|
|
|