15#ifndef WPIUTIL_WPI_POINTERUNION_H
16#define WPIUTIL_WPI_POINTERUNION_H
31template <
typename T,
typename... Us>
struct TypesAreDistinct;
32template <
typename T,
typename... Us>
34 : std::integral_constant<bool, !std::disjunction_v<std::is_same<T, Us>...> &&
35 TypesAreDistinct<Us...>::value> {};
48template <
typename... Ts>
50 : std::integral_constant<bool, detail::TypesAreDistinct<Ts...>::value> {};
63template <
typename T,
typename U,
typename... Us>
65 : std::integral_constant<size_t, 1 + FirstIndexOfType<T, Us...>::value> {};
66template <
typename T,
typename... Us>
72template <
size_t I,
typename... Ts>
73using TypeAtIndex = std::tuple_element_t<I, std::tuple<Ts...>>;
75namespace pointer_union_detail {
100 template <
typename Derived,
typename ValTy,
int I,
typename ...Types>
103 template <
typename Derived,
typename ValTy,
int I>
113 template <
typename Derived,
typename ValTy,
int I,
typename Type,
122 :
Base(ValTy(const_cast<void *>(
126 using Base::operator=;
131 return static_cast<Derived &
>(*this);
138template <
typename... PTs>
struct CastInfoPointerUnionImpl;
156template <
typename... PTs>
159 PointerUnion<PTs...>,
161 void *, pointer_union_detail::bitsRequired(sizeof...(PTs)), int,
162 pointer_union_detail::PointerUnionUIntTraits<PTs...>>,
165 "PointerUnion alternative types cannot be repeated");
172 using Base =
typename PointerUnion::PointerUnionMembers;
187 bool isNull()
const {
return !this->Val.getPointer(); }
189 explicit operator bool()
const {
return !
isNull(); }
195 template <
typename T>
inline bool is()
const {
return isa<T>(*
this); }
200 template <
typename T>
inline T
get()
const {
201 assert(isa<T>(*
this) &&
"Invalid accessor called");
202 return cast<T>(*
this);
208 return wpi::dyn_cast_if_present<T>(*
this);
220 assert(isa<First>(*
this) &&
"Val is not the first pointer");
223 this->Val.getPointer() &&
224 "Can't get the address because PointerLikeTypeTraits changes the ptr");
225 return const_cast<First *
>(
226 reinterpret_cast<const First *
>(this->Val.getAddrOfPointer()));
231 this->Val.initWithPointer(
nullptr);
236 using Base::operator=;
246template <
typename ...PTs>
251template <
typename ...PTs>
256template <
typename ...PTs>
279 assert(isPossible<To>(
F) &&
"cast to an incompatible type!");
285template <
typename To,
typename... PTs>
288 CastInfo<To, PointerUnion<PTs...>>> {
293 return Impl::template isPossible<To>(f);
296 static To
doCast(
From &f) {
return Impl::template doCast<To>(f); }
301template <
typename To,
typename... PTs>
304 CastInfo<To, PointerUnion<PTs...>>> {
309template <
typename ...PTs>
334 return Union(FirstInfo::getTombstoneKey());
This file defines DenseMapInfo traits for DenseMap.
This file defines the PointerIntPair class.
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:163
static PointerUnion getFromOpaqueValue(void *VP)
Definition: PointerUnion.h:239
T dyn_cast() const
Returns the current pointer if it is of the specified pointer type, otherwise returns null.
Definition: PointerUnion.h:207
void * getOpaqueValue() const
Definition: PointerUnion.h:238
First * getAddrOfPtr1()
If the union is set to the first pointer type get an address pointing to it.
Definition: PointerUnion.h:219
PointerUnion(std::nullptr_t)
Definition: PointerUnion.h:182
First const * getAddrOfPtr1() const
If the union is set to the first pointer type get an address pointing to it.
Definition: PointerUnion.h:213
const PointerUnion & operator=(std::nullptr_t)
Assignment from nullptr which just clears the union.
Definition: PointerUnion.h:230
T get() const
Returns the value of the specified pointer type.
Definition: PointerUnion.h:200
bool isNull() const
Test if the pointer held in the union is null, regardless of which type it is.
Definition: PointerUnion.h:187
bool is() const
Test if the Union currently holds the type matching T.
Definition: PointerUnion.h:195
PointerUnionMembers()=default
ValTy Val
Definition: PointerUnion.h:106
PointerUnionMembers()=default
PointerUnionMembers(Type V)
Definition: PointerUnion.h:121
Derived & operator=(Type V)
Definition: PointerUnion.h:127
Definition: PointerUnion.h:101
Provide PointerLikeTypeTraits for void* that is used by PointerUnion for the template arguments.
Definition: PointerUnion.h:93
static constexpr int NumLowBitsAvailable
Definition: PointerUnion.h:97
static void * getAsVoidPointer(void *P)
Definition: PointerUnion.h:95
static void * getFromVoidPointer(void *P)
Definition: PointerUnion.h:96
detail namespace with internal helper functions
Definition: xchar.h:20
type
Definition: core.h:556
static constexpr const unit_t< compound_unit< charge::coulomb, inverse< substance::mol > > > F(N_A *e)
Faraday constant.
constexpr int lowBitsAvailable()
Definition: PointerUnion.h:82
constexpr int bitsRequired(unsigned n)
Determine the number of bits required to store integers with values < n.
Definition: PointerUnion.h:78
Definition: ntcore_cpp.h:26
constexpr bool operator<(const unexpected< E > &lhs, const unexpected< E > &rhs)
Definition: expected:184
constexpr bool operator==(const unexpected< E > &lhs, const unexpected< E > &rhs)
Definition: expected:176
constexpr bool operator!=(const unexpected< E > &lhs, const unexpected< E > &rhs)
Definition: expected:180
static To doCast(From &f)
Definition: PointerUnion.h:296
static bool isPossible(From &f)
Definition: PointerUnion.h:292
static To castFailed()
Definition: PointerUnion.h:298
This struct provides a method for customizing the way a cast is performed.
Definition: Casting.h:476
We can't (at least, at this moment with C++14) declare CastInfo as a friend of PointerUnion like this...
Definition: PointerUnion.h:271
static To doCast(From &F)
Definition: PointerUnion.h:278
static bool isPossible(From &F)
Definition: PointerUnion.h:274
Provides a cast trait that strips const from types to make it easier to implement a const-version of ...
Definition: Casting.h:388
This cast trait just provides the default implementation of doCastIfPossible to make CastInfo special...
Definition: Casting.h:309
static Union getTombstoneKey()
Definition: PointerUnion.h:333
static unsigned getHashValue(const Union &UnionVal)
Definition: PointerUnion.h:337
static bool isEqual(const Union &LHS, const Union &RHS)
Definition: PointerUnion.h:342
static Union getEmptyKey()
Definition: PointerUnion.h:331
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: DenseMapInfo.h:50
Find the first index where a type appears in a list of types.
Definition: PointerUnion.h:62
static PointerUnion< PTs... > getFromVoidPointer(void *P)
Definition: PointerUnion.h:315
static void * getAsVoidPointer(const PointerUnion< PTs... > &P)
Definition: PointerUnion.h:311
A traits type that is used to handle pointer types and things that are just wrappers for pointers as ...
Definition: PointerLikeTypeTraits.h:25
Determine if all types in Ts are distinct.
Definition: PointerUnion.h:50
Definition: PointerUnion.h:35
Find the first type in a list of types.
Definition: PointerUnion.h:87
T type
Definition: PointerUnion.h:88
std::tuple_element_t< I, std::tuple< Ts... > > TypeAtIndex
Find the type at a given index in a list of types.
Definition: PointerUnion.h:73