Pigweed
 
Loading...
Searching...
No Matches
pw::PackedPtr< T > Class Template Reference

#include <packed_ptr.h>

Public Member Functions

constexpr PackedPtr (T *ptr, uintptr_t packed_value)
 
template<typename T2 , typename = std::enable_if_t<std::is_convertible_v<T2, T>>>
constexpr PackedPtr (const PackedPtr< T2 > &other)
 
template<typename T2 , typename = std::enable_if_t<std::is_convertible_v<T2, T>>>
constexpr PackedPtroperator= (const PackedPtr< T2 > &other)
 
template<typename T2 , typename = std::enable_if_t<std::is_convertible_v<T2, T>>>
constexpr PackedPtr (PackedPtr< T2 > &&other)
 
template<typename T2 , typename = std::enable_if_t<std::is_convertible_v<T2, T>>>
constexpr PackedPtroperator= (PackedPtr< T2 > &&other)
 
constexpr T & operator* () const
 
constexpr T * operator-> () const
 
constexpr T * get () const
 Returns the pointer.
 
constexpr uintptr_t packed_value () const
 Returns the packed_value packed into the unused bits of the pointer.
 
constexpr void set (T *ptr)
 Sets the pointer without changing the packed value.
 
constexpr void set_packed_value (uintptr_t packed_value)
 

Static Public Member Functions

static constexpr size_t NumBits ()
 

Detailed Description

template<typename T>
class pw::PackedPtr< T >

Pointer wrapper that can store extra data in otherwise unused bits.

Pointers to types that have an alignment of more than 1 will always have their least significant bit(s) set to zero. For example, if alignof(T) is 8, than the 3 least significant bits are always 0. In certain contexts where overhead needs to be tightly constrained, reusing these bits and avoiding additional fields may be worth the performance and code size penalties that arise from masking values.

Member Function Documentation

◆ set_packed_value()

template<typename T >
constexpr void pw::PackedPtr< T >::set_packed_value ( uintptr_t  packed_value)
inlineconstexpr

Packs a packed_value into the unused bits of the pointer.

Precondition
The given packed_value must fit in the available bits.

The documentation for this class was generated from the following file: