proxygen
|
#include <DiscriminatedPtr.h>
Public Member Functions | |
DiscriminatedPtr () | |
template<typename T > | |
DiscriminatedPtr (T *ptr) | |
template<typename T > | |
void | set (T *ptr) |
template<typename T > | |
T * | get_nothrow () noexcept |
template<typename T > | |
const T * | get_nothrow () const noexcept |
template<typename T > | |
T * | get () |
template<typename T > | |
const T * | get () const |
bool | empty () const |
template<typename T > | |
bool | hasType () const |
void | clear () |
template<typename T > | |
DiscriminatedPtr & | operator= (T *ptr) |
template<typename V > | |
dptr_detail::VisitorResult< V, Types... >::type | apply (V &&visitor) |
template<typename V > | |
dptr_detail::ConstVisitorResult< V, Types... >::type | apply (V &&visitor) const |
Private Member Functions | |
template<typename T > | |
uint16_t | typeIndex () const |
uint16_t | index () const |
void * | ptr () const |
void | set (void *p, uint16_t v) |
Private Attributes | |
uintptr_t | data_ |
Discriminated pointer.
Given a list of types, a DiscriminatedPtr<Types...> may point to an object of one of the given types, or may be empty. DiscriminatedPtr is type-safe: you may only get a pointer to the type that you put in, otherwise get throws an exception (and get_nothrow returns nullptr)
This pointer does not do any kind of lifetime management – it's not a "smart" pointer. You are responsible for deallocating any memory used to hold pointees, if necessary.
Definition at line 57 of file DiscriminatedPtr.h.
|
inline |
|
inlineexplicit |
Create a DiscriminatedPtr that points to an object of type T. Fails at compile time if T is not a valid type (listed in Types)
Definition at line 74 of file DiscriminatedPtr.h.
References folly::DiscriminatedPtr< Types >::ptr().
|
inline |
Apply a visitor to this object, calling the appropriate overload for the type currently stored in DiscriminatedPtr. Throws invalid_argument if the DiscriminatedPtr is empty.
The visitor must meet the following requirements:
Definition at line 174 of file DiscriminatedPtr.h.
References folly::DiscriminatedPtr< Types >::index(), and folly::DiscriminatedPtr< Types >::ptr().
|
inline |
Definition at line 184 of file DiscriminatedPtr.h.
References folly::DiscriminatedPtr< Types >::index(), and folly::DiscriminatedPtr< Types >::ptr().
|
inline |
Clear this DiscriminatedPtr, making it empty.
Definition at line 147 of file DiscriminatedPtr.h.
References folly::DiscriminatedPtr< Types >::data_.
|
inline |
Return true iff this DiscriminatedPtr is empty.
Definition at line 130 of file DiscriminatedPtr.h.
References folly::DiscriminatedPtr< Types >::index().
|
inline |
Get a pointer to the object that this DiscriminatedPtr points to, if it is of type T. Fails at compile time if T is not a valid type (listed in Types), and throws std::invalid_argument if this DiscriminatedPtr is empty or points to an object of a different type.
Definition at line 112 of file DiscriminatedPtr.h.
References folly::DiscriminatedPtr< Types >::ptr(), folly::T, and UNLIKELY.
|
inline |
Definition at line 120 of file DiscriminatedPtr.h.
References folly::DiscriminatedPtr< Types >::ptr(), folly::T, and UNLIKELY.
|
inlinenoexcept |
Get a pointer to the object that this DiscriminatedPtr points to, if it is of type T. Fails at compile time if T is not a valid type (listed in Types), and returns nullptr if this DiscriminatedPtr is empty or points to an object of a different type.
Definition at line 94 of file DiscriminatedPtr.h.
References LIKELY, folly::DiscriminatedPtr< Types >::ptr(), and folly::T.
|
inlinenoexcept |
Definition at line 100 of file DiscriminatedPtr.h.
References LIKELY, folly::DiscriminatedPtr< Types >::ptr(), and folly::T.
|
inline |
Return true iff the object pointed by this DiscriminatedPtr has type T, false otherwise. Fails at compile time if T is not a valid type (listed in Types...)
Definition at line 140 of file DiscriminatedPtr.h.
References folly::DiscriminatedPtr< Types >::index().
|
inlineprivate |
Definition at line 203 of file DiscriminatedPtr.h.
References folly::DiscriminatedPtr< Types >::data_.
Referenced by folly::DiscriminatedPtr< Types >::apply(), folly::DiscriminatedPtr< Types >::empty(), and folly::DiscriminatedPtr< Types >::hasType().
|
inline |
Assignment operator from a pointer of type T.
Definition at line 155 of file DiscriminatedPtr.h.
References folly::DiscriminatedPtr< Types >::ptr().
|
inlineprivate |
Definition at line 206 of file DiscriminatedPtr.h.
References folly::DiscriminatedPtr< Types >::data_.
Referenced by folly::DiscriminatedPtr< Types >::apply(), folly::DiscriminatedPtr< Types >::DiscriminatedPtr(), folly::DiscriminatedPtr< Types >::get(), folly::DiscriminatedPtr< Types >::get_nothrow(), folly::DiscriminatedPtr< Types >::operator=(), and folly::DiscriminatedPtr< Types >::set().
|
inline |
Set this DiscriminatedPtr to point to an object of type T. Fails at compile time if T is not a valid type (listed in Types)
Definition at line 83 of file DiscriminatedPtr.h.
References folly::DiscriminatedPtr< Types >::ptr().
|
inlineprivate |
Definition at line 210 of file DiscriminatedPtr.h.
References folly::DiscriminatedPtr< Types >::data_, and v.
|
inlineprivate |
Get the 1-based type index of T in Types.
Definition at line 199 of file DiscriminatedPtr.h.
References uint16_t.
|
private |
We store a pointer in the least significant 48 bits of data_, and a type index (0 = empty, or 1-based index in Types) in the most significant 16 bits. We rely on the fact that pointers have their most significant 16 bits clear on x86_64.
Definition at line 223 of file DiscriminatedPtr.h.
Referenced by folly::DiscriminatedPtr< Types >::clear(), folly::DiscriminatedPtr< Types >::index(), folly::DiscriminatedPtr< Types >::ptr(), and folly::DiscriminatedPtr< Types >::set().