xrpld
Loading...
Searching...
No Matches
xrpl::SharedIntrusive< T > Class Template Reference

A shared intrusive pointer class that supports weak pointers. More...

#include <IntrusivePointer.h>

Public Member Functions

 SharedIntrusive ()=default
template<CAdoptTag TAdoptTag>
 SharedIntrusive (T *p, TAdoptTag) noexcept
 SharedIntrusive (SharedIntrusive const &rhs)
template<class TT>
 SharedIntrusive (SharedIntrusive< TT > const &rhs)
 SharedIntrusive (SharedIntrusive &&rhs)
template<class TT>
 SharedIntrusive (SharedIntrusive< TT > &&rhs)
SharedIntrusiveoperator= (SharedIntrusive const &rhs)
bool operator!= (std::nullptr_t) const
bool operator== (std::nullptr_t) const
template<class TT>
SharedIntrusiveoperator= (SharedIntrusive< TT > const &rhs)
SharedIntrusiveoperator= (SharedIntrusive &&rhs)
template<class TT>
SharedIntrusiveoperator= (SharedIntrusive< TT > &&rhs)
template<CAdoptTag TAdoptTag = SharedIntrusiveAdoptIncrementStrongTag>
void adopt (T *p)
 Adopt the raw pointer.
 ~SharedIntrusive ()
template<class TT>
 SharedIntrusive (StaticCastTagSharedIntrusive, SharedIntrusive< TT > const &rhs)
 Create a new SharedIntrusive by statically casting the pointer controlled by the rhs param.
template<class TT>
 SharedIntrusive (StaticCastTagSharedIntrusive, SharedIntrusive< TT > &&rhs)
 Create a new SharedIntrusive by statically casting the pointer controlled by the rhs param.
template<class TT>
 SharedIntrusive (DynamicCastTagSharedIntrusive, SharedIntrusive< TT > const &rhs)
 Create a new SharedIntrusive by dynamically casting the pointer controlled by the rhs param.
template<class TT>
 SharedIntrusive (DynamicCastTagSharedIntrusive, SharedIntrusive< TT > &&rhs)
 Create a new SharedIntrusive by dynamically casting the pointer controlled by the rhs param.
T & operator* () const noexcept
T * operator-> () const noexcept
 operator bool () const noexcept
void reset ()
 Set the pointer to null, decrement the strong count, and run the appropriate release action.
T * get () const
 Get the raw pointer.
std::size_t useCount () const
 Return the strong count.

Private Member Functions

T * unsafeGetRawPtr () const
 Return the raw pointer held by this object.
void unsafeReleaseAndStore (T *next)
 Exchange the current raw pointer held by this object with the given pointer.
void unsafeSetRawPtr (T *p)
 Set the raw pointer directly.
T * unsafeExchange (T *p)
 Exchange the raw pointer directly.

Private Attributes

T * ptr_ {nullptr}
 pointer to the type with an intrusive count

Friends

template<class TT>
class SharedIntrusive
template<class TT>
class SharedWeakUnion
template<class TT>
class WeakIntrusive
template<class TT, class... Args>
SharedIntrusive< TT > makeSharedIntrusive (Args &&... args)
 Create a shared intrusive pointer.

Detailed Description

template<class T>
class xrpl::SharedIntrusive< T >

A shared intrusive pointer class that supports weak pointers.

This is meant to be used for SHAMapInnerNodes, but may be useful for other cases. Since the reference counts are stored on the pointee, the pointee is not destroyed until both the strong and weak pointer counts go to zero. When the strong pointer count goes to zero, the "partialDestructor" is called. This can be used to destroy as much of the object as possible while still retaining the reference counts. For example, for SHAMapInnerNodes the children may be reset in that function. Note that std::shared_pointer WILL run the destructor when the strong count reaches zero, but may not free the memory used by the object until the weak count reaches zero. In xrpld, we typically allocate shared pointers with the make_shared function. When that is used, the memory is not reclaimed until the weak count reaches zero.

Definition at line 67 of file IntrusivePointer.h.

Constructor & Destructor Documentation

◆ SharedIntrusive() [1/10]

template<class T>
xrpl::SharedIntrusive< T >::SharedIntrusive ( )
default

◆ SharedIntrusive() [2/10]

template<class T>
template<CAdoptTag TAdoptTag>
xrpl::SharedIntrusive< T >::SharedIntrusive ( T * p,
TAdoptTag  )
noexcept

◆ SharedIntrusive() [3/10]

template<class T>
xrpl::SharedIntrusive< T >::SharedIntrusive ( SharedIntrusive< T > const & rhs)

◆ SharedIntrusive() [4/10]

template<class T>
template<class TT>
xrpl::SharedIntrusive< T >::SharedIntrusive ( SharedIntrusive< TT > const & rhs)

◆ SharedIntrusive() [5/10]

template<class T>
xrpl::SharedIntrusive< T >::SharedIntrusive ( SharedIntrusive< T > && rhs)

◆ SharedIntrusive() [6/10]

template<class T>
template<class TT>
xrpl::SharedIntrusive< T >::SharedIntrusive ( SharedIntrusive< TT > && rhs)

◆ ~SharedIntrusive()

template<class T>
xrpl::SharedIntrusive< T >::~SharedIntrusive ( )

◆ SharedIntrusive() [7/10]

template<class T>
template<class TT>
xrpl::SharedIntrusive< T >::SharedIntrusive ( StaticCastTagSharedIntrusive< T > ,
SharedIntrusive< TT > const & rhs )

Create a new SharedIntrusive by statically casting the pointer controlled by the rhs param.

◆ SharedIntrusive() [8/10]

template<class T>
template<class TT>
xrpl::SharedIntrusive< T >::SharedIntrusive ( StaticCastTagSharedIntrusive< T > ,
SharedIntrusive< TT > && rhs )

Create a new SharedIntrusive by statically casting the pointer controlled by the rhs param.

◆ SharedIntrusive() [9/10]

template<class T>
template<class TT>
xrpl::SharedIntrusive< T >::SharedIntrusive ( DynamicCastTagSharedIntrusive< T > ,
SharedIntrusive< TT > const & rhs )

Create a new SharedIntrusive by dynamically casting the pointer controlled by the rhs param.

◆ SharedIntrusive() [10/10]

template<class T>
template<class TT>
xrpl::SharedIntrusive< T >::SharedIntrusive ( DynamicCastTagSharedIntrusive< T > ,
SharedIntrusive< TT > && rhs )

Create a new SharedIntrusive by dynamically casting the pointer controlled by the rhs param.

Member Function Documentation

◆ operator=() [1/4]

template<class T>
SharedIntrusive & xrpl::SharedIntrusive< T >::operator= ( SharedIntrusive< T > const & rhs)

◆ operator!=()

template<class T>
bool xrpl::SharedIntrusive< T >::operator!= ( std::nullptr_t ) const

◆ operator==()

template<class T>
bool xrpl::SharedIntrusive< T >::operator== ( std::nullptr_t ) const

◆ operator=() [2/4]

template<class T>
template<class TT>
SharedIntrusive & xrpl::SharedIntrusive< T >::operator= ( SharedIntrusive< TT > const & rhs)

◆ operator=() [3/4]

template<class T>
SharedIntrusive & xrpl::SharedIntrusive< T >::operator= ( SharedIntrusive< T > && rhs)

◆ operator=() [4/4]

template<class T>
template<class TT>
SharedIntrusive & xrpl::SharedIntrusive< T >::operator= ( SharedIntrusive< TT > && rhs)

◆ adopt()

template<class T>
template<CAdoptTag TAdoptTag = SharedIntrusiveAdoptIncrementStrongTag>
void xrpl::SharedIntrusive< T >::adopt ( T * p)

Adopt the raw pointer.

The strong reference may or may not be incremented, depending on the TAdoptTag

◆ operator*()

template<class T>
T & xrpl::SharedIntrusive< T >::operator* ( ) const
noexcept

◆ operator->()

template<class T>
T * xrpl::SharedIntrusive< T >::operator-> ( ) const
noexcept

◆ operator bool()

template<class T>
xrpl::SharedIntrusive< T >::operator bool ( ) const
explicitnoexcept

◆ reset()

template<class T>
void xrpl::SharedIntrusive< T >::reset ( )

Set the pointer to null, decrement the strong count, and run the appropriate release action.

◆ get()

template<class T>
T * xrpl::SharedIntrusive< T >::get ( ) const
nodiscard

Get the raw pointer.

◆ useCount()

template<class T>
std::size_t xrpl::SharedIntrusive< T >::useCount ( ) const
nodiscard

Return the strong count.

◆ unsafeGetRawPtr()

template<class T>
T * xrpl::SharedIntrusive< T >::unsafeGetRawPtr ( ) const
nodiscardprivate

Return the raw pointer held by this object.

◆ unsafeReleaseAndStore()

template<class T>
void xrpl::SharedIntrusive< T >::unsafeReleaseAndStore ( T * next)
private

Exchange the current raw pointer held by this object with the given pointer.

Decrement the strong count of the raw pointer previously held by this object and run the appropriate release action.

◆ unsafeSetRawPtr()

template<class T>
void xrpl::SharedIntrusive< T >::unsafeSetRawPtr ( T * p)
private

Set the raw pointer directly.

This is wrapped in a function so the class can support both atomic and non-atomic pointers in a future patch.

◆ unsafeExchange()

template<class T>
T * xrpl::SharedIntrusive< T >::unsafeExchange ( T * p)
private

Exchange the raw pointer directly.

This sets the raw pointer to the given value and returns the previous value. This is wrapped in a function so the class can support both atomic and non-atomic pointers in a future patch.

◆ SharedIntrusive

template<class T>
template<class TT>
friend class SharedIntrusive
friend

Definition at line 174 of file IntrusivePointer.h.

◆ SharedWeakUnion

template<class T>
template<class TT>
friend class SharedWeakUnion
friend

Definition at line 177 of file IntrusivePointer.h.

◆ WeakIntrusive

template<class T>
template<class TT>
friend class WeakIntrusive
friend

Definition at line 180 of file IntrusivePointer.h.

◆ makeSharedIntrusive

template<class T>
template<class TT, class... Args>
SharedIntrusive< TT > makeSharedIntrusive ( Args &&... args)
friend

Create a shared intrusive pointer.

Note: unlike std::shared_ptr, where there is an advantage of allocating the pointer and control block together, there is no benefit for intrusive pointers.

Definition at line 445 of file IntrusivePointer.h.

Member Data Documentation

◆ ptr_

template<class T>
T* xrpl::SharedIntrusive< T >::ptr_ {nullptr}
private

pointer to the type with an intrusive count

Definition at line 209 of file IntrusivePointer.h.