OpenVPN 3 Core Library
Loading...
Searching...
No Matches
openvpn::RCPtr< T > Class Template Reference

The smart pointer class. More...

#include <rc.hpp>

Public Types

typedef T element_type
 

Public Member Functions

 RCPtr () noexcept
 Construct a new RCPtr<T>::RCPtr object.
 
 RCPtr (T *p, const bool add_ref=true) noexcept
 Construct a new RCPtr<T>::RCPtr object.
 
 RCPtr (const RCPtr &rhs) noexcept
 Copy constructor for RCPtr.
 
 RCPtr (RCPtr &&rhs) noexcept
 Construct a new RCPtr object via move.
 
template<typename U >
 RCPtr (const RCPtr< U > &rhs) noexcept
 Construct a new RCPtr<T>::RCPtr object to type T and make it track an object of type U.
 
 ~RCPtr ()
 Destroy the RCPtr<T>::RCPtr object.
 
RCPtroperator= (const RCPtr &rhs) noexcept
 Assigns an existing RCPtr<T> to point to a different T.
 
RCPtroperator= (RCPtr &&rhs) noexcept
 Assigns an existing RCPtr<T> to point to a different T using move.
 
void reset () noexcept
 Points this RCPtr<T> to nullptr safely.
 
void reset (T *rhs) noexcept
 Points this RCPtr to an RC enabled object T.
 
void swap (RCPtr &rhs) noexcept
 swaps the contents of two RCPtr<T>
 
T * get () const noexcept
 Returns the raw pointer to the object T, or nullptr.
 
T & operator* () const noexcept
 Operator returns a ref to the pointed to T.
 
T * operator-> () const noexcept
 Returns the raw pointer to the object T, or nullptr.
 
 operator bool () const noexcept
 Evaluates to true if the internal pointer is not equal to nullptr.
 
bool operator== (const RCPtr &rhs) const
 Evaluates to true if the two RCPtr<T> point to the same object.
 
bool operator!= (const RCPtr &rhs) const
 Evaluates to true if the two RCPtr<T> point to different objects.
 
RCPtr< T > move_strong () noexcept
 Moves ownership of the internal pointer to the returned RCPtr<T>
 
template<typename U >
RCPtr< U > static_pointer_cast () const noexcept
 Returns a "RCPtr<U>" that points to our T object.
 
template<typename U >
RCPtr< U > dynamic_pointer_cast () const noexcept
 Returns a "RCPtr<U>" that points to our T object.
 

Private Attributes

T * px
 Pointer to the controlled object.
 

Detailed Description

template<typename T>
class openvpn::RCPtr< T >

The smart pointer class.

Template Parameters
Tan RC enabled type

Defines a template class called RCPtr that implements a smart pointer for reference counted objects.

RCPtr is a template class, meaning it can be instantiated for any type T that supports reference counting. It keeps track of a pointer to an object of type T, and handles incrementing and decrementing the reference count automatically.

The purpose of RCPtr is to automate reference counting for any reference-counted object (any class that inherits from RC). It provides a safe way to have multiple pointers to an object without worrying about memory leaks or double-frees.

RCPtr has a pointer member variable px that holds a pointer to the T object it references. It overloads operators like * and -> to dereference the pointer and access the referenced object.

The key methods are the constructors and destructor. The constructors increment the reference count, and the destructor decrements it. This ensures the object will stay allocated as long as any RCPtr points to it, and be freed when the last RCPtr is destructed.

Copy and move constructors increment the refcount before assigning px, while move assignment operators decrement the old refcount after reassigning px.

RCPtr is a smart pointer class that automates reference counting for any reference-counted type T, allowing multiple pointer ownership without leaks or double-frees.

Definition at line 118 of file rc.hpp.

Member Typedef Documentation

◆ element_type

template<typename T >
typedef T openvpn::RCPtr< T >::element_type

Definition at line 121 of file rc.hpp.

Constructor & Destructor Documentation

◆ RCPtr() [1/5]

template<typename T >
openvpn::RCPtr< T >::RCPtr ( )
noexcept

Construct a new RCPtr<T>::RCPtr object.

Template Parameters
Tan RC enabled type

The default constructor for the RCPtr class

This constructor initializes the RCPtr class with no referenced object. It sets the internal px pointer to nullptr. The purpose of this default constructor is to allow creating an RCPtr instance that doesn't yet reference anything. This is useful when you need to declare an RCPtr variable but don't have an object to assign it to yet.

Definition at line 167 of file rc.hpp.

Here is the caller graph for this function:

◆ RCPtr() [2/5]

template<typename T >
openvpn::RCPtr< T >::RCPtr ( T *  p,
const bool  add_ref = true 
)
noexcept

Construct a new RCPtr<T>::RCPtr object.

Template Parameters
Tan RC enabled type
Parameters
ppointer to an RC enabled object
add_refbool that determines whether the RC of p is incremented

The RCPtr constructor taking a pointer and bool

This constructor initializes an RCPtr instance to point to a provided object pointer p.

It takes two inputs:

  • p - a pointer to an object of type T that inherits from RC (reference counted).
  • add_ref - a bool indicating if the reference count of p should be incremented.

It does not return anything directly. Its purpose is to construct an RCPtr instance.

The key logic is:

  • The px member is assigned the provided pointer p.
  • If add_ref is true, and px is non-null, the reference count of px is incremented via openvpn::intrusive_ptr_add_ref().

This achieves the goal of constructing an RCPtr that points to the provided object pointer p. If add_ref is true, it will also increment the ref count of p, indicating that RCPtr now owns a count on that object.

Definition at line 195 of file rc.hpp.

◆ RCPtr() [3/5]

template<typename T >
openvpn::RCPtr< T >::RCPtr ( const RCPtr< T > &  rhs)
noexcept

Copy constructor for RCPtr.

Template Parameters
Tan RC enabled type
Parameters
rhsthe RCPtr to be copied

Definition at line 207 of file rc.hpp.

◆ RCPtr() [4/5]

template<typename T >
openvpn::RCPtr< T >::RCPtr ( RCPtr< T > &&  rhs)
noexcept

Construct a new RCPtr object via move.

Template Parameters
Tan RC enabled type
Parameters
rhsobject from which to move

Definition at line 219 of file rc.hpp.

◆ RCPtr() [5/5]

template<typename T >
template<typename U >
openvpn::RCPtr< T >::RCPtr ( const RCPtr< U > &  rhs)
noexcept

Construct a new RCPtr<T>::RCPtr object to type T and make it track an object of type U.

Template Parameters
Tan RC enabled type
Uan RC enabled type
Parameters
rhs"RCPtr<U>" pointing to the object the new RCPtr<T> will reference as well

This achieves the goal of creating an RCPtr<T> that points to the same object as the "RCPtr<U>".

Definition at line 234 of file rc.hpp.

◆ ~RCPtr()

template<typename T >
openvpn::RCPtr< T >::~RCPtr ( )

Destroy the RCPtr<T>::RCPtr object.

Template Parameters
Tan RC enabled type

This achieves the goal of reducing the refcount when the RCPtr is destructed, possibly deleting the object if no other RCPtrs reference it anymore. The key data transformation is decrementing the refcount via openvpn::intrusive_ptr_release().

Definition at line 249 of file rc.hpp.

Member Function Documentation

◆ dynamic_pointer_cast()

template<typename T >
template<typename U >
RCPtr< U > openvpn::RCPtr< T >::dynamic_pointer_cast ( ) const
noexcept

Returns a "RCPtr<U>" that points to our T object.

Template Parameters
Tan RC enabled type
Uan RC enabled type
Returns
"RCPtr<U>" that points to the same object this points to, or nullptr

Performs a dynamic_cast from T * to U * and then wraps the cast pointer in a new "RCPtr<U>", or if the dynamic_cast fails the result will equal nullptr cast to U * in a new "RCPtr<U>".

Definition at line 422 of file rc.hpp.

Here is the caller graph for this function:

◆ get()

template<typename T >
T * openvpn::RCPtr< T >::get ( ) const
noexcept

Returns the raw pointer to the object T, or nullptr.

Template Parameters
Tan RC enabled type
Returns
T* pointer we are tracking, or nullptr.

Definition at line 321 of file rc.hpp.

Here is the caller graph for this function:

◆ move_strong()

template<typename T >
RCPtr< T > openvpn::RCPtr< T >::move_strong ( )
noexcept

Moves ownership of the internal pointer to the returned RCPtr<T>

Template Parameters
Tan RC enabled type
Returns
The new owning RCPtr<T>

Definition at line 391 of file rc.hpp.

◆ operator bool()

template<typename T >
openvpn::RCPtr< T >::operator bool ( ) const
explicitnoexcept

Evaluates to true if the internal pointer is not equal to nullptr.

Template Parameters
Tan RC enabled type
Returns
true if internal pointer is not equal to nullptr
false if internal pointer is equal to nullptr

Definition at line 356 of file rc.hpp.

◆ operator!=()

template<typename T >
bool openvpn::RCPtr< T >::operator!= ( const RCPtr< T > &  rhs) const

Evaluates to true if the two RCPtr<T> point to different objects.

Template Parameters
Tan RC enabled type
Parameters
rhsother RCPtr<T>
Returns
true if *this and rhs point to different instances or do not both equal nullptr
false if *this and rhs point to the same instance or both equal nullptr

Definition at line 381 of file rc.hpp.

◆ operator*()

template<typename T >
T & openvpn::RCPtr< T >::operator* ( ) const
noexcept

Operator returns a ref to the pointed to T.

Template Parameters
Tan RC enabled type
Returns
T& reference to the object this RCPtr points to

Operator returns a ref to the pointed to T, or if the RCPtr does not point to a valid T, undefined behavior due to dereference of invalid pointer. This is identical to the behavior of a C ptr or the STL smart pointers.

Definition at line 335 of file rc.hpp.

◆ operator->()

template<typename T >
T * openvpn::RCPtr< T >::operator-> ( ) const
noexcept

Returns the raw pointer to the object T, or nullptr.

Template Parameters
Tan RC enabled type
Returns
T* pointer we are tracking, or nullptr.

Definition at line 345 of file rc.hpp.

◆ operator=() [1/2]

template<typename T >
RCPtr< T > & openvpn::RCPtr< T >::operator= ( const RCPtr< T > &  rhs)
noexcept

Assigns an existing RCPtr<T> to point to a different T.

Template Parameters
Tan RC enabled type
Parameters
rhsother RCPtr<T>
Returns
reference to this

Assigns an existing RCPtr<T> to point to a different T, which is already controlled by another RCPtr<T>. Reduces the refcount on the current T.

Definition at line 264 of file rc.hpp.

◆ operator=() [2/2]

template<typename T >
RCPtr< T > & openvpn::RCPtr< T >::operator= ( RCPtr< T > &&  rhs)
noexcept

Assigns an existing RCPtr<T> to point to a different T using move.

Template Parameters
Tan RC enabled type
Parameters
rhsother RCPtr<T>
Returns
reference to this

Assigns an existing RCPtr<T> to point to a different T using move, by stealing the guts of another RCPtr.

Definition at line 280 of file rc.hpp.

◆ operator==()

template<typename T >
bool openvpn::RCPtr< T >::operator== ( const RCPtr< T > &  rhs) const

Evaluates to true if the two RCPtr<T> point to the same object.

Note
Does not check equality of the pointed two object, rather checks for identity.
Template Parameters
Tan RC enabled type
Parameters
rhsother RCPtr<T>
Returns
true if *this and rhs point to the same instance or both equal nullptr
false if *this and rhs point to different instances or do not both equal nullptr

Definition at line 369 of file rc.hpp.

◆ reset() [1/2]

template<typename T >
void openvpn::RCPtr< T >::reset ( )
noexcept

Points this RCPtr<T> to nullptr safely.

Template Parameters
Tan RC enabled type

Definition at line 290 of file rc.hpp.

Here is the caller graph for this function:

◆ reset() [2/2]

template<typename T >
void openvpn::RCPtr< T >::reset ( T *  rhs)
noexcept

Points this RCPtr to an RC enabled object T.

Note
It's critical that the object in question be allocated via new.
Template Parameters
Tan RC enabled type
Parameters
rhspointer to the object that will be managed by this pointer

Definition at line 301 of file rc.hpp.

◆ static_pointer_cast()

template<typename T >
template<typename U >
RCPtr< U > openvpn::RCPtr< T >::static_pointer_cast ( ) const
noexcept

Returns a "RCPtr<U>" that points to our T object.

Template Parameters
Tan RC enabled type
Uan RC enabled type
Returns
"RCPtr<U>" that points to the same object this points to

Performs a static_cast from T * to U * and then wraps the cast pointer in a new "RCPtr<U>"

Definition at line 407 of file rc.hpp.

◆ swap()

template<typename T >
void openvpn::RCPtr< T >::swap ( RCPtr< T > &  rhs)
noexcept

swaps the contents of two RCPtr<T>

Template Parameters
Tan RC enabled type
Parameters
rhsthe other RCPtr<T>

Definition at line 311 of file rc.hpp.

Here is the caller graph for this function:

Member Data Documentation

◆ px

template<typename T >
T* openvpn::RCPtr< T >::px
private

Pointer to the controlled object.

Definition at line 153 of file rc.hpp.


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