delpi  0.0.1
DElta-complete LP solver
Loading...
Searching...
No Matches
delpi::intrusive_ptr< T > Class Template Reference

Pointer to a generic object that supports intrusive reference counting. More...

#include <intrusive_ptr.hpp>

Public Types

typedef T element_type
 The type this intrusive_ptr points to.

Public Member Functions

 intrusive_ptr (T *p, const bool add_ref=true)
 Construct a new intrusive_ptr object wrapping the given pointer p.
 intrusive_ptr (const intrusive_ptr &ip)
 Construct a new intrusive_ptr object from another intrusive_ptr object.
 intrusive_ptr (intrusive_ptr &&ip) noexcept
 Construct a new intrusive_ptr object from another intrusive_ptr object using move semantics.
 ~intrusive_ptr ()
 Destruct the intrusive_ptr object, releasing the owned pointer.
intrusive_ptr & operator= (const intrusive_ptr &ip)
 Copy assignment operator.
intrusive_ptr & operator= (intrusive_ptr &&ip) noexcept
 Move assignment operator.
intrusive_ptr & operator= (T *ptr)
 Copy assignment operator.
void reset ()
 Release the owned pointer, decrementing the reference count and setting ptr_ to null.
void swap (intrusive_ptr &ip) noexcept
 Exchanges the owned pointer between two intrusive_ptr objects.
void attach (T *ptr)
 Sets the owned pointer to the given pointer ptr without incrementing the reference count.
T * detach ()
 Surrenders the reference held by an intrusive_ptr pointer.
 operator bool_ () const noexcept
 Implicit bool conversion.

Protected Attributes

T * ptr_
 Raw pointer to the data.

Detailed Description

template<class T>
class delpi::intrusive_ptr< T >

Pointer to a generic object that supports intrusive reference counting.

Implementation based on the EASTL intrusive_ptr.

Template Parameters
Tclass the intrusive_ptr is pointing to. Must have AddRef and Release methods.

Definition at line 22 of file intrusive_ptr.hpp.

Constructor & Destructor Documentation

◆ intrusive_ptr() [1/3]

template<class T>
delpi::intrusive_ptr< T >::intrusive_ptr ( T * p,
const bool add_ref = true )
inlineexplicit

Construct a new intrusive_ptr object wrapping the given pointer p.

If add_ref is true, the reference count of the object is incremented immediately.

intrusive_ptr<MyObject> str(new MyObject());
Parameters
ppointer to the object
add_refwhether to increment the reference count

Definition at line 39 of file intrusive_ptr.hpp.

◆ intrusive_ptr() [2/3]

template<class T>
delpi::intrusive_ptr< T >::intrusive_ptr ( const intrusive_ptr< T > & ip)
inline

Construct a new intrusive_ptr object from another intrusive_ptr object.

AddRef is immediately run on the object. The source intrusive_ptr object maintains its AddRef on the pointer.

intrusive_ptr<MyObject> my_object_ptr_1;
intrusive_ptr<MyObject> my_object_ptr_2(my_object_ptr_1);
Parameters
ipintrusive_ptr object to copy

Definition at line 53 of file intrusive_ptr.hpp.

◆ intrusive_ptr() [3/3]

template<class T>
delpi::intrusive_ptr< T >::intrusive_ptr ( intrusive_ptr< T > && ip)
inlinenoexcept

Construct a new intrusive_ptr object from another intrusive_ptr object using move semantics.

Parameters
ipintrusive_ptr object to move

Definition at line 60 of file intrusive_ptr.hpp.

Member Function Documentation

◆ attach()

template<class T>
void delpi::intrusive_ptr< T >::attach ( T * ptr)
inline

Sets the owned pointer to the given pointer ptr without incrementing the reference count.

The intrusive_ptr eventually only does a Release() on the object. Useful for assuming a reference that someone else has handed you and making sure it is always released.

Parameters
ptrpointer to the object

Definition at line 131 of file intrusive_ptr.hpp.

◆ detach()

template<class T>
T * delpi::intrusive_ptr< T >::detach ( )
inline

Surrenders the reference held by an intrusive_ptr pointer.

It returns the current reference and nulls the pointer without decrementing the reference count. Therefore, if the returned pointer is non-null it must be eventually released by the caller.

Returns
the current reference

Definition at line 144 of file intrusive_ptr.hpp.

◆ operator bool_()

template<class T>
delpi::intrusive_ptr< T >::operator bool_ ( ) const
inlinenoexcept

Implicit bool conversion.

Allows the intrusive_ptr to be used as a boolean in checks.

intrusive_ptr<MyObject> ptr = new MyObject();
if (ptr) ptr->DoSomething();
Note
The implementation does not use the builtin bool() operator. That is because boolean are implicitally converted to short, int, float, etc. Hence, you can have ambiguous code such as if (ptr == 1) // True.

Definition at line 163 of file intrusive_ptr.hpp.

◆ operator=() [1/3]

template<class T>
intrusive_ptr & delpi::intrusive_ptr< T >::operator= ( const intrusive_ptr< T > & ip)
inline

Copy assignment operator.

AddRef is immediately run on the object. The source ip maintains its reference to the pointer. If this pointer was already in use, the old object is released at the end.

Parameters
ipintrusive_ptr object to copy
Returns
reference to this intrusive_ptr object

Definition at line 76 of file intrusive_ptr.hpp.

◆ operator=() [2/3]

template<class T>
intrusive_ptr & delpi::intrusive_ptr< T >::operator= ( intrusive_ptr< T > && ip)
inlinenoexcept

Move assignment operator.

Parameters
ipintrusive_ptr object to move
Returns
reference to this intrusive_ptr object

Definition at line 86 of file intrusive_ptr.hpp.

◆ operator=() [3/3]

template<class T>
intrusive_ptr & delpi::intrusive_ptr< T >::operator= ( T * ptr)
inline

Copy assignment operator.

AddRef is immediately run on the object. If this pointer was already in use, the old object is released at the end.

Parameters
ptrraw pointer to the object
Returns
reference to this intrusive_ptr object

Definition at line 100 of file intrusive_ptr.hpp.

◆ swap()

template<class T>
void delpi::intrusive_ptr< T >::swap ( intrusive_ptr< T > & ip)
inlinenoexcept

Exchanges the owned pointer between two intrusive_ptr objects.

Note
The reference count is not modified.
Parameters
ipintrusive_ptr object to swap with

Definition at line 122 of file intrusive_ptr.hpp.


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