2019-12-09 12:57:37 +01:00
|
|
|
/*
|
2020-01-24 11:10:40 +01:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2019-12-09 12:57:37 +01: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 08:05:14 +01:00
|
|
|
#include <vapours/common.hpp>
|
|
|
|
#include <vapours/assert.hpp>
|
2019-12-09 12:57:37 +01:00
|
|
|
|
|
|
|
namespace ams {
|
|
|
|
|
|
|
|
namespace result::impl {
|
|
|
|
|
|
|
|
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");
|
|
|
|
public:
|
2020-02-23 08:05:14 +01:00
|
|
|
static constexpr ALWAYS_INLINE BaseType MakeValue(BaseType module, BaseType description) {
|
2019-12-09 12:57:37 +01: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 08:05:14 +01:00
|
|
|
static constexpr ALWAYS_INLINE BaseType GetModuleFromValue(BaseType value) {
|
2019-12-09 12:57:37 +01:00
|
|
|
return value & ~(~BaseType() << ModuleBits);
|
|
|
|
}
|
|
|
|
|
2020-02-23 08:05:14 +01:00
|
|
|
static constexpr ALWAYS_INLINE BaseType GetDescriptionFromValue(BaseType value) {
|
2019-12-09 12:57:37 +01:00
|
|
|
return ((value >> ModuleBits) & ~(~BaseType() << DescriptionBits));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* 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 01:51:35 +01: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 12:57:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class ResultConstructor;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class ResultSuccess;
|
|
|
|
|
|
|
|
class Result final : public result::impl::ResultBase<Result> {
|
|
|
|
friend class ResultConstructor;
|
|
|
|
public:
|
|
|
|
using Base = typename result::impl::ResultBase<Result>;
|
|
|
|
private:
|
|
|
|
typename Base::BaseType value;
|
|
|
|
private:
|
|
|
|
/* TODO: Maybe one-day, the result constructor. */
|
|
|
|
public:
|
|
|
|
Result() { /* ... */ }
|
|
|
|
|
|
|
|
/* TODO: It sure would be nice to make this private. */
|
|
|
|
constexpr Result(typename Base::BaseType v) : value(v) { static_assert(std::is_same<typename Base::BaseType, ::Result>::value); }
|
|
|
|
|
2020-01-31 01:51:35 +01:00
|
|
|
constexpr ALWAYS_INLINE operator ResultSuccess() const;
|
2020-08-17 23:20:24 +02:00
|
|
|
NX_CONSTEXPR bool CanAccept(Result) { return true; }
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-01-31 01:51:35 +01:00
|
|
|
constexpr ALWAYS_INLINE bool IsSuccess() const { return this->GetValue() == Base::SuccessValue; }
|
|
|
|
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 12:57:37 +01:00
|
|
|
|
2020-01-31 01:51:35 +01:00
|
|
|
constexpr ALWAYS_INLINE typename Base::BaseType GetValue() const { return this->value; }
|
2019-12-09 12:57:37 +01: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");
|
|
|
|
|
|
|
|
namespace result::impl {
|
|
|
|
|
|
|
|
class ResultConstructor {
|
|
|
|
public:
|
2020-01-31 01:51:35 +01:00
|
|
|
static constexpr ALWAYS_INLINE Result MakeResult(ResultTraits::BaseType value) {
|
2019-12-09 12:57:37 +01:00
|
|
|
return Result(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-31 01:51:35 +01:00
|
|
|
constexpr ALWAYS_INLINE Result MakeResult(ResultTraits::BaseType value) {
|
2019-12-09 12:57:37 +01:00
|
|
|
return ResultConstructor::MakeResult(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class ResultSuccess final : public result::impl::ResultBase<ResultSuccess> {
|
|
|
|
public:
|
|
|
|
using Base = typename result::impl::ResultBase<ResultSuccess>;
|
|
|
|
public:
|
|
|
|
constexpr operator Result() const { return result::impl::MakeResult(Base::SuccessValue); }
|
|
|
|
NX_CONSTEXPR bool CanAccept(Result result) { return result.IsSuccess(); }
|
|
|
|
|
2020-01-31 01:51:35 +01:00
|
|
|
constexpr ALWAYS_INLINE bool IsSuccess() const { return true; }
|
|
|
|
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 12:57:37 +01:00
|
|
|
|
2020-01-31 01:51:35 +01:00
|
|
|
constexpr ALWAYS_INLINE typename Base::BaseType GetValue() const { return Base::SuccessValue; }
|
2019-12-09 12:57:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace result::impl {
|
|
|
|
|
2020-02-23 08:05:14 +01: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 12:57:37 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-31 01:51:35 +01:00
|
|
|
constexpr ALWAYS_INLINE Result::operator ResultSuccess() const {
|
2019-12-09 12:57:37 +01:00
|
|
|
if (!ResultSuccess::CanAccept(*this)) {
|
2020-02-23 08:05:14 +01:00
|
|
|
result::impl::OnResultAbort(*this);
|
2019-12-09 12:57:37 +01: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:
|
|
|
|
constexpr operator Result() const { return MakeResult(Value); }
|
2020-02-23 08:05:14 +01:00
|
|
|
constexpr operator ResultSuccess() const { OnResultAbort(Value); }
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-01-31 01:51:35 +01:00
|
|
|
constexpr ALWAYS_INLINE bool IsSuccess() const { return false; }
|
|
|
|
constexpr ALWAYS_INLINE bool IsFailure() const { return !this->IsSuccess(); }
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-01-31 01:51:35 +01:00
|
|
|
constexpr ALWAYS_INLINE typename Base::BaseType GetValue() const { return Value; }
|
2019-12-09 12:57:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template<ResultTraits::BaseType _Module, ResultTraits::BaseType DescStart, ResultTraits::BaseType DescEnd>
|
|
|
|
class ResultErrorRangeBase {
|
|
|
|
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:
|
2020-12-01 02:16:00 +01:00
|
|
|
static constexpr bool Includes(Result result) {
|
|
|
|
return result.GetModule() == Module && DescriptionStart <= result.GetDescription() && result.GetDescription() <= DescriptionEnd;
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Macros for defining new results. */
|
|
|
|
#define R_DEFINE_NAMESPACE_RESULT_MODULE(value) namespace impl::result { static constexpr inline ::ams::result::impl::ResultTraits::BaseType ResultModuleId = value; }
|
|
|
|
#define R_CURRENT_NAMESPACE_RESULT_MODULE impl::result::ResultModuleId
|
|
|
|
#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))
|
|
|
|
|
|
|
|
#define R_DEFINE_ERROR_RESULT_IMPL(name, desc_start, desc_end) \
|
|
|
|
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> {}
|
|
|
|
|
|
|
|
#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)
|
|
|
|
|
|
|
|
/* 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())
|
|
|
|
|
|
|
|
|
|
|
|
/// Evaluates an expression that returns a result, and returns the result if it would fail.
|
|
|
|
#define R_TRY(res_expr) \
|
|
|
|
({ \
|
2020-02-23 08:05:14 +01:00
|
|
|
const auto _tmp_r_try_rc = (res_expr); \
|
2019-12-09 12:57:37 +01:00
|
|
|
if (R_FAILED(_tmp_r_try_rc)) { \
|
|
|
|
return _tmp_r_try_rc; \
|
|
|
|
} \
|
|
|
|
})
|
|
|
|
|
2020-02-23 08:05:14 +01:00
|
|
|
#ifdef AMS_ENABLE_DEBUG_PRINT
|
|
|
|
#define AMS_CALL_ON_RESULT_ASSERTION_IMPL(cond, val) ::ams::result::impl::OnResultAssertion(__FILE__, __LINE__, __PRETTY_FUNCTION__, cond, val)
|
|
|
|
#define AMS_CALL_ON_RESULT_ABORT_IMPL(cond, val) ::ams::result::impl::OnResultAbort(__FILE__, __LINE__, __PRETTY_FUNCTION__, cond, val)
|
|
|
|
#else
|
|
|
|
#define AMS_CALL_ON_RESULT_ASSERTION_IMPL(cond, val) ::ams::result::impl::OnResultAssertion("", 0, "", "", val)
|
|
|
|
#define AMS_CALL_ON_RESULT_ABORT_IMPL(cond, val) ::ams::result::impl::OnResultAbort("", 0, "", "", val)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/// Evaluates an expression that returns a result, and asserts the result if it would fail.
|
|
|
|
#ifdef AMS_ENABLE_ASSERTIONS
|
2019-12-09 12:57:37 +01:00
|
|
|
#define R_ASSERT(res_expr) \
|
|
|
|
({ \
|
2020-02-23 08:05:14 +01:00
|
|
|
const auto _tmp_r_assert_rc = (res_expr); \
|
|
|
|
if (AMS_UNLIKELY(R_FAILED(_tmp_r_assert_rc))) { \
|
|
|
|
AMS_CALL_ON_RESULT_ASSERTION_IMPL(#res_expr, _tmp_r_assert_rc); \
|
|
|
|
} \
|
|
|
|
})
|
|
|
|
#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.
|
|
|
|
#define R_ABORT_UNLESS(res_expr) \
|
|
|
|
({ \
|
|
|
|
const auto _tmp_r_abort_rc = (res_expr); \
|
|
|
|
if (AMS_UNLIKELY(R_FAILED(_tmp_r_abort_rc))) { \
|
|
|
|
AMS_CALL_ON_RESULT_ABORT_IMPL(#res_expr, _tmp_r_abort_rc); \
|
2019-12-09 12:57:37 +01:00
|
|
|
} \
|
|
|
|
})
|
|
|
|
|
|
|
|
/// Evaluates a boolean expression, and returns a result unless that expression is true.
|
|
|
|
#define R_UNLESS(expr, res) \
|
|
|
|
({ \
|
|
|
|
if (!(expr)) { \
|
|
|
|
return static_cast<::ams::Result>(res); \
|
|
|
|
} \
|
|
|
|
})
|
|
|
|
|
2020-03-08 09:06:23 +01:00
|
|
|
/// Evaluates a boolean expression, and succeeds if that expression is true.
|
|
|
|
#define R_SUCCEED_IF(expr) R_UNLESS(!(expr), ResultSuccess())
|
|
|
|
|
2019-12-09 12:57:37 +01:00
|
|
|
/// Helpers for pattern-matching on a result expression, if the result would fail.
|
|
|
|
#define R_CURRENT_RESULT _tmp_r_current_result
|
|
|
|
|
|
|
|
#define R_TRY_CATCH(res_expr) \
|
|
|
|
({ \
|
2020-02-23 08:05:14 +01:00
|
|
|
const auto R_CURRENT_RESULT = (res_expr); \
|
2019-12-09 12:57:37 +01:00
|
|
|
if (R_FAILED(R_CURRENT_RESULT)) { \
|
|
|
|
if (false)
|
|
|
|
|
|
|
|
namespace ams::result::impl {
|
|
|
|
|
|
|
|
template<typename... Rs>
|
2020-02-23 08:05:14 +01:00
|
|
|
constexpr ALWAYS_INLINE bool AnyIncludes(Result result) {
|
2019-12-09 12:57:37 +01:00
|
|
|
return (Rs::Includes(result) || ...);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#define R_CATCH(...) \
|
|
|
|
} else if (::ams::result::impl::AnyIncludes<__VA_ARGS__>(R_CURRENT_RESULT)) { \
|
|
|
|
if (true)
|
|
|
|
|
2020-04-27 19:33:51 +02:00
|
|
|
#define R_CATCH_MODULE(__module__) \
|
|
|
|
} else if ((R_CURRENT_RESULT).GetModule() == ::ams::R_NAMESPACE_MODULE_ID(__module__)) { \
|
|
|
|
if (true)
|
|
|
|
|
2019-12-09 12:57:37 +01:00
|
|
|
#define R_CONVERT(catch_type, convert_type) \
|
|
|
|
R_CATCH(catch_type) { return static_cast<::ams::Result>(convert_type); }
|
|
|
|
|
|
|
|
#define R_CATCH_ALL() \
|
|
|
|
} else if (R_FAILED(R_CURRENT_RESULT)) { \
|
|
|
|
if (true)
|
|
|
|
|
|
|
|
#define R_CONVERT_ALL(convert_type) \
|
|
|
|
R_CATCH_ALL() { return static_cast<::ams::Result>(convert_type); }
|
|
|
|
|
|
|
|
#define R_CATCH_RETHROW(catch_type) \
|
|
|
|
R_CONVERT(catch_type, R_CURRENT_RESULT)
|
|
|
|
|
|
|
|
#define R_END_TRY_CATCH \
|
|
|
|
else if (R_FAILED(R_CURRENT_RESULT)) { \
|
|
|
|
return R_CURRENT_RESULT; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define R_END_TRY_CATCH_WITH_ASSERT \
|
|
|
|
else { \
|
|
|
|
R_ASSERT(R_CURRENT_RESULT); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
})
|
2020-02-23 08:05:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
#define R_END_TRY_CATCH_WITH_ABORT_UNLESS \
|
|
|
|
else { \
|
|
|
|
R_ABORT_UNLESS(R_CURRENT_RESULT); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
})
|