Spicy
Public Types | Public Member Functions | Friends | Related Functions | List of all members
hilti::rt::IntrusivePtr< T > Class Template Reference

#include <intrusive-ptr.h>

Public Types

using pointer = T *
 
using const_pointer = const T *
 
using element_type = T
 
using reference = T &
 
using const_reference = const T &
 

Public Member Functions

constexpr IntrusivePtr (std::nullptr_t) noexcept
 
constexpr IntrusivePtr (intrusive_ptr::AdoptRef, pointer raw_ptr) noexcept
 
 IntrusivePtr (intrusive_ptr::NewRef, pointer raw_ptr) noexcept
 
 IntrusivePtr (IntrusivePtr &&other) noexcept
 
 IntrusivePtr (const IntrusivePtr &other) noexcept
 
template<class U , class = std::enable_if_t<std::is_convertible_v<U*, T*>>>
 IntrusivePtr (IntrusivePtr< U > other) noexcept
 
void swap (IntrusivePtr &other) noexcept
 
pointer release () noexcept
 
IntrusivePtroperator= (IntrusivePtr other) noexcept
 
pointer get () const noexcept
 
pointer operator-> () const noexcept
 
reference operator* () const noexcept
 
bool operator! () const noexcept
 
 operator bool () const noexcept
 

Friends

void swap (IntrusivePtr &a, IntrusivePtr &b) noexcept
 

Related Functions

(Note that these are not member functions.)

template<class T , class... Ts>
IntrusivePtr< T > make_intrusive (Ts &&... args)
 
template<class T >
bool operator== (const IntrusivePtr< T > &x, std::nullptr_t)
 
template<class T >
bool operator== (std::nullptr_t, const IntrusivePtr< T > &x)
 
template<class T >
bool operator!= (const IntrusivePtr< T > &x, std::nullptr_t)
 
template<class T >
bool operator!= (std::nullptr_t, const IntrusivePtr< T > &x)
 
template<class T >
bool operator== (const IntrusivePtr< T > &x, const T *y)
 
template<class T >
bool operator== (const T *x, const IntrusivePtr< T > &y)
 
template<class T >
bool operator!= (const IntrusivePtr< T > &x, const T *y)
 
template<class T >
bool operator!= (const T *x, const IntrusivePtr< T > &y)
 
template<class T , class U >
auto operator== (const IntrusivePtr< T > &x, const IntrusivePtr< U > &y) -> decltype(x.get()==y.get())
 
template<class T , class U >
auto operator!= (const IntrusivePtr< T > &x, const IntrusivePtr< U > &y) -> decltype(x.get() !=y.get())
 

Detailed Description

template<class T>
class hilti::rt::IntrusivePtr< T >

An intrusive, reference counting smart pointer implementation. Much like std::shared_ptr, this smart pointer models shared ownership of an object through a pointer. Several IntrusivePtr instances may point to the same object.

The IntrusivePtr requires two free functions associated to T that must be available via argument-dependent lookup: Ref and Unref. The former increments the reference by one whenever a new owner participates in the lifetime of the shared object and the latter decrements the reference count by one. Once the reference count reaches zero, Unref also is responsible for destroying the shared object.

The IntrusivePtr works with any type that offers the two free functions, but most notably can be used with classes derived from #intrusive_ptr::ManagedObject, which provides the functions.

Constructor & Destructor Documentation

◆ IntrusivePtr() [1/2]

template<class T>
constexpr hilti::rt::IntrusivePtr< T >::IntrusivePtr ( intrusive_ptr::AdoptRef  ,
pointer  raw_ptr 
)
inlinenoexcept

Constructs a new intrusive pointer for managing the lifetime of the object pointed to by raw_ptr.

This overload adopts the existing reference from the caller.

Parameters
raw_ptrPointer to the shared object.

◆ IntrusivePtr() [2/2]

template<class T>
hilti::rt::IntrusivePtr< T >::IntrusivePtr ( intrusive_ptr::NewRef  ,
pointer  raw_ptr 
)
inlinenoexcept

Constructs a new intrusive pointer for managing the lifetime of the object pointed to by raw_ptr.

This overload adds a new reference.

Parameters
raw_ptrPointer to the shared object.

Member Function Documentation

◆ release()

template<class T>
pointer hilti::rt::IntrusivePtr< T >::release ( )
inlinenoexcept

Detaches an object from the automated lifetime management and sets this intrusive pointer to nullptr.

Returns
the raw pointer without modifying the reference count.

Friends And Related Function Documentation

◆ make_intrusive()

template<class T , class... Ts>
IntrusivePtr< T > make_intrusive ( Ts &&...  args)
related

Convenience function for creating a reference counted object and wrapping it into an intrusive pointers.

Parameters
argsArguments for constructing the shared object of type T.
Returns
an IntrusivePtr pointing to the new object.
Note
This function assumes that any T starts with a reference count of 1.

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