// SPDX-License-Identifier: BSD-2-Clause // author: Max Kellermann #pragma once #include "OffsetPointer.hxx" #include template constexpr T * OffsetCast(U *p, std::ptrdiff_t offset) { return reinterpret_cast(OffsetPointer(p, offset)); } template constexpr T * OffsetCast(const U *p, std::ptrdiff_t offset) { return reinterpret_cast(OffsetPointer(p, offset)); } template constexpr std::ptrdiff_t ContainerAttributeOffset(const C *null_c, const A C::*p) { return std::ptrdiff_t((const char *)&(null_c->*p) - (const char *)null_c); } template constexpr std::ptrdiff_t ContainerAttributeOffset(const A C::*p) { return ContainerAttributeOffset(nullptr, p); } /** * Cast the given pointer to a struct member to its parent structure. */ template constexpr C & ContainerCast(A &a, const A C::*member) { return *OffsetCast(&a, -ContainerAttributeOffset(member)); } /** * Cast the given pointer to a struct member to its parent structure. */ template constexpr const C & ContainerCast(const A &a, const A C::*member) { return *OffsetCast(&a, -ContainerAttributeOffset(member)); }