WPILibC++ 2024.3.2
wpi::PointerUnion< PTs > Class Template Reference

A discriminated union of two or more pointer types, with the discriminator in the low bit of the pointer. More...

#include </home/runner/work/allwpilib/allwpilib/wpiutil/src/main/native/thirdparty/llvm/include/wpi/PointerUnion.h>

Inheritance diagram for wpi::PointerUnion< PTs >:
wpi::pointer_union_detail::PointerUnionMembers< PointerUnion< PTs... >, PointerIntPair< void *, pointer_union_detail::bitsRequired(sizeof...(PTs)), int, pointer_union_detail::PointerUnionUIntTraits< PTs... > >, 0, PTs... >

Public Member Functions

 PointerUnion ()=default
 
 PointerUnion (std::nullptr_t)
 
bool isNull () const
 Test if the pointer held in the union is null, regardless of which type it is. More...
 
 operator bool () const
 
template<typename T >
bool is () const
 Test if the Union currently holds the type matching T. More...
 
template<typename T >
get () const
 Returns the value of the specified pointer type. More...
 
template<typename T >
dyn_cast () const
 Returns the current pointer if it is of the specified pointer type, otherwise returns null. More...
 
First const * getAddrOfPtr1 () const
 If the union is set to the first pointer type get an address pointing to it. More...
 
First * getAddrOfPtr1 ()
 If the union is set to the first pointer type get an address pointing to it. More...
 
const PointerUnionoperator= (std::nullptr_t)
 Assignment from nullptr which just clears the union. More...
 
void * getOpaqueValue () const
 

Static Public Member Functions

static PointerUnion getFromOpaqueValue (void *VP)
 

Friends

struct CastInfoPointerUnionImpl< PTs... >
 This is needed to give the CastInfo implementation below access to protected members. More...
 

Detailed Description

template<typename... PTs>
class wpi::PointerUnion< PTs >

A discriminated union of two or more pointer types, with the discriminator in the low bit of the pointer.

This implementation is extremely efficient in space due to leveraging the low bits of the pointer, while exposing a natural and type-safe API.

Common use patterns would be something like this: PointerUnion<int*, float*> P; P = (int*)0; printf("%d %d", P.is<int*>(), P.is<float*>()); // prints "1 0" X = P.get<int*>(); // ok. Y = P.get<float*>(); // runtime assertion failure. Z = P.get<double*>(); // compile time failure. P = (float*)0; Y = P.get<float*>(); // ok. X = P.get<int*>(); // runtime assertion failure. PointerUnion<int*, int*> Q; // compile time failure.

Constructor & Destructor Documentation

◆ PointerUnion() [1/2]

template<typename... PTs>
wpi::PointerUnion< PTs >::PointerUnion ( )
default

◆ PointerUnion() [2/2]

template<typename... PTs>
wpi::PointerUnion< PTs >::PointerUnion ( std::nullptr_t  )
inline

Member Function Documentation

◆ dyn_cast()

template<typename... PTs>
template<typename T >
T wpi::PointerUnion< PTs >::dyn_cast ( ) const
inline

Returns the current pointer if it is of the specified pointer type, otherwise returns null.

◆ get()

template<typename... PTs>
template<typename T >
T wpi::PointerUnion< PTs >::get ( ) const
inline

Returns the value of the specified pointer type.

If the specified pointer type is incorrect, assert.

◆ getAddrOfPtr1() [1/2]

template<typename... PTs>
First * wpi::PointerUnion< PTs >::getAddrOfPtr1 ( )
inline

If the union is set to the first pointer type get an address pointing to it.

◆ getAddrOfPtr1() [2/2]

template<typename... PTs>
First const * wpi::PointerUnion< PTs >::getAddrOfPtr1 ( ) const
inline

If the union is set to the first pointer type get an address pointing to it.

◆ getFromOpaqueValue()

template<typename... PTs>
static PointerUnion wpi::PointerUnion< PTs >::getFromOpaqueValue ( void *  VP)
inlinestatic

◆ getOpaqueValue()

template<typename... PTs>
void * wpi::PointerUnion< PTs >::getOpaqueValue ( ) const
inline

◆ is()

template<typename... PTs>
template<typename T >
bool wpi::PointerUnion< PTs >::is ( ) const
inline

Test if the Union currently holds the type matching T.

◆ isNull()

template<typename... PTs>
bool wpi::PointerUnion< PTs >::isNull ( ) const
inline

Test if the pointer held in the union is null, regardless of which type it is.

◆ operator bool()

template<typename... PTs>
wpi::PointerUnion< PTs >::operator bool ( ) const
inlineexplicit

◆ operator=()

template<typename... PTs>
const PointerUnion & wpi::PointerUnion< PTs >::operator= ( std::nullptr_t  )
inline

Assignment from nullptr which just clears the union.

Friends And Related Function Documentation

◆ CastInfoPointerUnionImpl< PTs... >

template<typename... PTs>
friend struct CastInfoPointerUnionImpl< PTs... >
friend

This is needed to give the CastInfo implementation below access to protected members.

Refer to its definition for further details.


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