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 { namespace impl {
#define AMS_UTIL_OFFSET_OF_STANDARD_COMPLIANT 0
#if AMS_UTIL_OFFSET_OF_STANDARD_COMPLIANT
template<size_t MaxDepth> template<size_t MaxDepth>
struct OffsetOfUnionHolder { struct OffsetOfUnionHolder {
template<typename ParentType, typename MemberType, size_t Offset> 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> template<typename T>
struct GetMemberPointerTraits; struct GetMemberPointerTraits;