iceoryx_hoofs 2.0.5
Loading...
Searching...
No Matches
Public Member Functions | List of all members
iox::cxx::unique_ptr< T > Class Template Reference

The unique_ptr class is a heap-less unique ptr implementation, unlike the STL. More...

#include <iceoryx_hoofs/cxx/unique_ptr.hpp>

Public Member Functions

 unique_ptr (function_ref< void(T *)> &&deleter) noexcept
 unique_ptr Creates an empty unique ptr that owns nothing. Can be passed ownership later via reset.
 
 unique_ptr (T *const ptr, function_ref< void(T *)> &&deleter) noexcept
 unique_ptr Creates a unique pointer that takes ownership of an object.
 
 unique_ptr (const unique_ptr &other)=delete
 
unique_ptroperator= (const unique_ptr &)=delete
 
 unique_ptr (unique_ptr &&rhs) noexcept
 
unique_ptroperator= (unique_ptr &&rhs) noexcept
 
 ~unique_ptr () noexcept
 
unique_ptr< T > & operator= (std::nullptr_t) noexcept
 
T * operator-> () noexcept
 operator -> Transparent access to the managed object.
 
const T * operator-> () const noexcept
 operator -> Transparent access to the managed object.
 
 operator bool () const noexcept
 operator bool Returns true if it points to something.
 
T * get () noexcept
 get Retrieve the underlying raw pointer.
 
const T * get () const noexcept
 get Retrieve the underlying raw pointer.
 
T * release () noexcept
 release Release ownership of the underlying pointer.
 
void reset (T *const ptr=nullptr) noexcept
 reset Reset the unique pointer to take ownership of the given pointer.
 
void swap (unique_ptr &other) noexcept
 swap Swaps object ownership with another unique_ptr (incl. deleters)
 

Detailed Description

template<typename T>
class iox::cxx::unique_ptr< T >

The unique_ptr class is a heap-less unique ptr implementation, unlike the STL.

To avoid using the heap, deleters are not managed by the pointer itself, and instead must be provided as function references ('cxx:function_ref'). The functions must exist at least as long as the pointers that use them.

Also unlike the STL implementation, the deleters are not encoded in the unique_ptr type, allowing unique_ptr instances with different deleters to be stored in the same containers.

Constructor & Destructor Documentation

◆ unique_ptr()

template<typename T >
iox::cxx::unique_ptr< T >::unique_ptr ( T *const  ptr,
function_ref< void(T *)> &&  deleter 
)
noexcept

unique_ptr Creates a unique pointer that takes ownership of an object.

A deleter must always be provided as no default can be provided given that no heap is used. The unique_ptr must know how to delete the managed object when the pointer goes out of scope.

Parameters
ptrThe raw pointer to the object to be managed.
deleterThe deleter function for cleaning up the managed object. As cxx:function_ref used for the deleter is non-owning the user needs to care about the lifetime of the callable!

◆ ~unique_ptr()

template<typename T >
iox::cxx::unique_ptr< T >::~unique_ptr ( )
noexcept

Automatically deletes the managed object on destruction.

Member Function Documentation

◆ get() [1/2]

template<typename T >
const T * iox::cxx::unique_ptr< T >::get ( ) const
noexcept

get Retrieve the underlying raw pointer.

The unique_ptr retains ownership, therefore the "borrowed" pointer must not be deleted.

Returns
Pointer to managed object or nullptr if none owned.

◆ get() [2/2]

template<typename T >
T * iox::cxx::unique_ptr< T >::get ( )
noexcept

get Retrieve the underlying raw pointer.

The unique_ptr retains ownership, therefore the "borrowed" pointer must not be deleted.

Returns
Pointer to managed object or nullptr if none owned.

◆ operator->() [1/2]

template<typename T >
const T * iox::cxx::unique_ptr< T >::operator-> ( ) const
noexcept

operator -> Transparent access to the managed object.

Returns

◆ operator->() [2/2]

template<typename T >
T * iox::cxx::unique_ptr< T >::operator-> ( )
noexcept

operator -> Transparent access to the managed object.

Returns

◆ release()

template<typename T >
T * iox::cxx::unique_ptr< T >::release ( )
noexcept

release Release ownership of the underlying pointer.

Returns
Pointer to the managed object or nullptr if none owned.

◆ reset()

template<typename T >
void iox::cxx::unique_ptr< T >::reset ( T *const  ptr = nullptr)
noexcept

reset Reset the unique pointer to take ownership of the given pointer.

Any previously owned objects will be deleted. If no pointer given then points to nullptr.

Parameters
ptrPointer to object to take ownership on.

◆ swap()

template<typename T >
void iox::cxx::unique_ptr< T >::swap ( unique_ptr< T > &  other)
noexcept

swap Swaps object ownership with another unique_ptr (incl. deleters)

Parameters
otherThe unique_ptr with which to swap owned objects.

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