![]() |
ezEngine Release 26.3
|
Space-efficient pointer storage that embeds flags in unused low-order bits. More...
#include <PointerWithFlags.h>
Public Member Functions | |
| ezPointerWithFlags ()=default | |
| Initializes the pointer and flags with zero. | |
| ezPointerWithFlags (PtrType *pPtr, ezUInt8 uiFlags=0) | |
| Initializes the pointer and flags. | |
| void | SetPtrAndFlags (PtrType *pPtr, ezUInt8 uiFlags) |
| Changes the pointer and flags. | |
| const PtrType * | GetPtr () const |
| Returns the masked off pointer value. | |
| PtrType * | GetPtr () |
| Returns the masked off pointer value. | |
| void | SetPtr (PtrType *pPtr) |
| Changes the pointer value only. Flags stay unchanged. | |
| ezUInt8 | GetFlags () const |
| Returns the flags value only. | |
| void | SetFlags (ezUInt8 uiFlags) |
| Changes only the flags value. The given value must fit into the reserved bits. | |
| operator PtrType * () | |
| Returns the masked off pointer value. | |
| operator const PtrType * () const | |
| Returns the masked off pointer value. | |
| void | operator= (PtrType *pPtr) |
| Changes the pointer value only. Flags stay unchanged. | |
| template<typename = typename std::enable_if<std::is_const<PtrType>::value == false>> | |
| bool | operator== (const PtrType *pPtr) const |
| Compares the pointer part for equality (flags are ignored) | |
| bool | operator== (const ezPointerWithFlags< PtrType, NumFlagBits > &rhs) const |
| EZ_ADD_DEFAULT_OPERATOR_NOTEQUAL (const ezPointerWithFlags< PtrType, NumFlagBits > &) | |
| bool | operator== (PtrType *pPtr) const |
| Compares the pointer part for equality (flags are ignored) | |
| EZ_ADD_DEFAULT_OPERATOR_NOTEQUAL (PtrType *) | |
| bool | operator== (std::nullptr_t) const |
| Compares the pointer part for equality (flags are ignored) | |
| EZ_ADD_DEFAULT_OPERATOR_NOTEQUAL (std::nullptr_t) | |
| operator bool () const | |
| Checks whether the pointer part is not nullptr (flags are ignored) | |
| const PtrType * | operator-> () const |
| Dereferences the pointer. | |
| PtrType * | operator-> () |
| Dereferences the pointer. | |
| const PtrType & | operator* () const |
| Dereferences the pointer. | |
| PtrType & | operator* () |
| Dereferences the pointer. | |
Space-efficient pointer storage that embeds flags in unused low-order bits.
Exploits the fact that aligned pointers have zero bits in their lower positions to store additional flag data without increasing memory usage. The number of available bits depends on alignment: 8-byte aligned pointers provide 3 bits, 4-byte aligned provide 2 bits, etc.
When accessing the pointer, the flag bits are automatically masked off to yield the original pointer value. Flag operations are independent of pointer operations, allowing separate manipulation of the embedded data.
Common use cases include storing object state flags alongside pointers in data structures, reducing memory overhead in pointer-heavy applications like trees or linked lists.