7#ifndef PXR_BASE_TF_POINTER_AND_BITS_H
8#define PXR_BASE_TF_POINTER_AND_BITS_H
17PXR_NAMESPACE_OPEN_SCOPE
20constexpr bool Tf_IsPow2(uintptr_t val) {
21 return val && !(val & (val - 1));
41 template <
typename U,
bool = false>
43 static constexpr uintptr_t value =
alignof(U);
46 struct _AlignOf<U, true> {
47 static constexpr uintptr_t value =
alignof(
void*);
53 static constexpr uintptr_t _GetAlign()
noexcept {
54 return _AlignOf<T, std::is_abstract<T>::value>::value;
57 static constexpr bool _SupportsAtLeastOneBit()
noexcept {
58 return _GetAlign() > 1 && Tf_IsPow2(_GetAlign());
65 static_assert(_SupportsAtLeastOneBit(),
66 "T's alignment does not support any bits");
71 : _ptrAndBits(_Combine(p, bits))
73 static_assert(_SupportsAtLeastOneBit(),
74 "T's alignment does not support any bits");
77 constexpr uintptr_t GetMaxValue()
const {
78 return _GetAlign() - 1;
81 constexpr uintptr_t GetNumBitsValues()
const {
102 template <
class Integral>
103 constexpr Integral
BitsAs() const noexcept {
105 ARCH_PRAGMA_FORCING_TO_BOOL
107 ARCH_PRAGMA_MAYBE_UNINITIALIZED
108 return static_cast<Integral
>(_GetBits());
113 template <
class Integral>
115 _SetBits(
static_cast<uintptr_t
>(val));
119 void Set(T *ptr)
noexcept {
124 template <
class Integral>
125 void Set(T *ptr, Integral val)
noexcept {
126 _ptrAndBits = _Combine(ptr, val);
130 constexpr T *
Get() const noexcept {
140 return _AsInt(_ptrAndBits);
145 ::std::swap(_ptrAndBits, other._ptrAndBits);
149 constexpr uintptr_t _GetBitMask() const noexcept {
150 return GetMaxValue();
154 constexpr T *_Combine(T *p, uintptr_t bits)
const noexcept {
155 return _AsPtr(_AsInt(p) | (bits & _GetBitMask()));
160 constexpr uintptr_t _AsInt(T *p)
const noexcept {
166 constexpr T *_AsPtr(uintptr_t i)
const noexcept {
171 constexpr T *_GetPtr() const noexcept {
172 return _AsPtr(_AsInt(_ptrAndBits) & ~_GetBitMask());
176 void _SetPtr(T *p)
noexcept {
177 _ptrAndBits = _Combine(p, _GetBits());
181 constexpr uintptr_t _GetBits() const noexcept {
182 return _AsInt(_ptrAndBits) & _GetBitMask();
186 void _SetBits(uintptr_t bits)
noexcept {
187 _ptrAndBits = _Combine(_GetPtr(), bits);
194PXR_NAMESPACE_CLOSE_SCOPE
This class stores a T * and a small integer in the space of a T *.
void Set(T *ptr) noexcept
Set the pointer value to ptr.
TfPointerAndBits & operator=(T *ptr) noexcept
Assignment. Leaves bits unmodified.
void SetBits(Integral val) noexcept
Set the stored bits. No static range checking is performed.
constexpr T & operator*() const noexcept
Dereference.
constexpr T * operator->() const noexcept
Indirection.
constexpr TfPointerAndBits() noexcept
Constructor.
void Set(T *ptr, Integral val) noexcept
Set the pointer value to ptr and the bits to val.
constexpr uintptr_t GetLiteral() const noexcept
Retrieve the raw underlying value.
void Swap(TfPointerAndBits &other) noexcept
Swap this PointerAndBits with other.
constexpr T * Get() const noexcept
Retrieve the pointer.
constexpr Integral BitsAs() const noexcept
Retrieve the stored bits as the integral type Integral.
constexpr TfPointerAndBits(T *p, uintptr_t bits=0) noexcept
Constructor. Set the pointer to p, and the bits to bits.
Pragmas for controlling compiler-specific behaviors.