OpenVPN 3 Core Library
|
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. | |
RCPtr & | operator= (const RCPtr &rhs) noexcept |
Assigns an existing RCPtr<T> to point to a different T. | |
RCPtr & | operator= (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. | |
The smart pointer class.
T | an 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.
typedef T openvpn::RCPtr< T >::element_type |
|
noexcept |
Construct a new RCPtr<T>::RCPtr object.
T | an 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.
|
noexcept |
Construct a new RCPtr<T>::RCPtr object.
T | an RC enabled type |
p | pointer to an RC enabled object |
add_ref | bool 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:
It does not return anything directly. Its purpose is to construct an RCPtr instance.
The key logic is:
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.
|
noexcept |
|
noexcept |
|
noexcept |
Construct a new RCPtr<T>::RCPtr object to type T and make it track an object of type U.
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>".
openvpn::RCPtr< T >::~RCPtr | ( | ) |
Destroy the RCPtr<T>::RCPtr object.
T | an 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().
|
noexcept |
Returns a "RCPtr<U>" that points to our T object.
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.
|
noexcept |
|
noexcept |
|
explicitnoexcept |
bool openvpn::RCPtr< T >::operator!= | ( | const RCPtr< T > & | rhs | ) | const |
Evaluates to true if the two RCPtr<T> point to different objects.
T | an RC enabled type |
rhs | other RCPtr<T> |
|
noexcept |
Operator returns a ref to the pointed to T.
T | an RC enabled type |
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.
|
noexcept |
|
noexcept |
Assigns an existing RCPtr<T> to point to a different T.
T | an RC enabled type |
rhs | other RCPtr<T> |
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.
|
noexcept |
bool openvpn::RCPtr< T >::operator== | ( | const RCPtr< T > & | rhs | ) | const |
Evaluates to true if the two RCPtr<T> point to the same object.
T | an RC enabled type |
rhs | other RCPtr<T> |
|
noexcept |
|
noexcept |
|
noexcept |
Returns a "RCPtr<U>" that points to our T object.
Performs a static_cast from T * to U * and then wraps the cast pointer in a new "RCPtr<U>"
|
noexcept |
|
private |