L4Re - L4 Runtime Environment
cxx::Ref_ptr< T, CNT > Class Template Reference

A reference-counting pointer with automatic cleanup. More...

+ Inheritance diagram for cxx::Ref_ptr< T, CNT >:
+ Collaboration diagram for cxx::Ref_ptr< T, CNT >:

Public Member Functions

 Ref_ptr () throw ()
 Default constructor creates a pointer with no managed object.
 
 Ref_ptr (Wp const &o) throw ()
 Create a shared pointer from a weak pointer. More...
 
 Ref_ptr (decltype(nullptr) n) noexcept
 allow creation from nullptr
 
template<typename X >
 Ref_ptr (X *o) throw ()
 Create a shared pointer from a raw pointer. More...
 
 Ref_ptr (T *o, bool d) throw ()
 Create a shared pointer from a raw pointer without creating a new reference. More...
 
T * get () const throw ()
 Return a raw pointer to the object this shared pointer points to. More...
 
T * ptr () const throw ()
 Return a raw pointer to the object this shared pointer points to. More...
 
T * release () throw ()
 Release the shared pointer without removing the reference. More...
 

Detailed Description

template<typename T = void, template< typename X > class CNT = Default_ref_counter>
class cxx::Ref_ptr< T, CNT >

A reference-counting pointer with automatic cleanup.

Template Parameters
TType of object the pointer points to.
CNTType of management class that manages the life time of the object.

This pointer is similar to the standard C++-11 shared_ptr but it does the reference counting directly in the object being pointed to, so that no additional management structures need to be allocated from the heap.

Classes that use this pointer type must implement two functions:

int remove_ref()

is called when a reference is removed and must return 0 when there are no further references to the object.

void add_ref()

is called when another ref_ptr to the object is created.

Ref_obj provides a simple implementation of this interface from which classes may inherit.

Examples:
tmpfs/lib/src/fs.cc.

Definition at line 80 of file ref_ptr.

Constructor & Destructor Documentation

◆ Ref_ptr() [1/3]

template<typename T = void, template< typename X > class CNT = Default_ref_counter>
cxx::Ref_ptr< T, CNT >::Ref_ptr ( Wp const &  o)
throw (
)
inline

Create a shared pointer from a weak pointer.

Increases references.

Definition at line 101 of file ref_ptr.

◆ Ref_ptr() [2/3]

template<typename T = void, template< typename X > class CNT = Default_ref_counter>
template<typename X >
cxx::Ref_ptr< T, CNT >::Ref_ptr ( X *  o)
throw (
)
inlineexplicit

Create a shared pointer from a raw pointer.

In contrast to C++11 shared_ptr it is safe to use this constructor multiple times and have the same reference counter.

Definition at line 115 of file ref_ptr.

◆ Ref_ptr() [3/3]

template<typename T = void, template< typename X > class CNT = Default_ref_counter>
cxx::Ref_ptr< T, CNT >::Ref_ptr ( T *  o,
bool  d 
)
throw (
)
inline

Create a shared pointer from a raw pointer without creating a new reference.

Parameters
oPointer to the object.
dDummy parameter to select this constructor at compile time. The value may be true or false.

This is the counterpart to release().

Definition at line 138 of file ref_ptr.

Member Function Documentation

◆ get()

template<typename T = void, template< typename X > class CNT = Default_ref_counter>
T* cxx::Ref_ptr< T, CNT >::get ( ) const
throw (
)
inline

Return a raw pointer to the object this shared pointer points to.

This does not release the pointer or decrease the reference count.

Definition at line 145 of file ref_ptr.

◆ ptr()

template<typename T = void, template< typename X > class CNT = Default_ref_counter>
T* cxx::Ref_ptr< T, CNT >::ptr ( ) const
throw (
)
inline

Return a raw pointer to the object this shared pointer points to.

This does not release the pointer or decrease the reference count.

Definition at line 151 of file ref_ptr.

◆ release()

template<typename T = void, template< typename X > class CNT = Default_ref_counter>
T* cxx::Ref_ptr< T, CNT >::release ( )
throw (
)
inline

Release the shared pointer without removing the reference.

Returns
A raw pointer to the managed object.

Definition at line 162 of file ref_ptr.


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