1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-10 06:01:52 +00:00

util: faster constexpr offsetof calc, at the cost of standards compliance

This commit is contained in:
Michael Scire 2021-01-08 04:07:42 -08:00
parent a6e3a93c07
commit b8072b1398

View file

@ -23,6 +23,10 @@ namespace ams::util {
namespace impl {
#define AMS_UTIL_OFFSET_OF_STANDARD_COMPLIANT 0
#if AMS_UTIL_OFFSET_OF_STANDARD_COMPLIANT
template<size_t MaxDepth>
struct OffsetOfUnionHolder {
template<typename ParentType, typename MemberType, size_t Offset>
@ -101,6 +105,20 @@ namespace ams::util {
}
};
#else
template<typename ParentType, typename MemberType>
struct OffsetOfCalculator {
static constexpr std::ptrdiff_t OffsetOf(MemberType ParentType::*member) {
constexpr TYPED_STORAGE(ParentType) Holder = {};
const auto *parent = GetPointer(Holder);
const auto *target = std::addressof(parent->*member);
return static_cast<const uint8_t *>(static_cast<const void *>(target)) - static_cast<const uint8_t *>(static_cast<const void *>(parent));
}
};
#endif
template<typename T>
struct GetMemberPointerTraits;