OpenVPN 3 Core Library
Loading...
Searching...
No Matches
openvpn::RCWeak< RCImpl >::Controller Struct Reference

Controller structure for our ptr/weakptr implementation. More...

#include <rc.hpp>

Inheritance diagram for openvpn::RCWeak< RCImpl >::Controller:
[legend]
Collaboration diagram for openvpn::RCWeak< RCImpl >::Controller:
[legend]

Public Types

typedef RCPtr< ControllerPtr
 
- Public Types inherited from openvpn::RC< RCImpl >
typedef RCPtr< RCPtr
 

Public Member Functions

 Controller (RCWeak *parent_arg) noexcept
 Construct a new RCWeak<RCImpl>::Controller object.
 
olong use_count () const noexcept
 Get the use count of the underlying ref count implementation.
 
template<typename PTR >
PTR lock () noexcept
 Used internally to try a to get a pointer to the controlled instance.
 
- Public Member Functions inherited from openvpn::RC< RCImpl >
 RC () noexcept=default
 
virtual ~RC ()=default
 
 RC (const RC &)=delete
 
RCoperator= (const RC &)=delete
 
olong use_count () const noexcept
 Delegates call to RCImpl and returns the result.
 

Public Attributes

RCWeak *const parent
 Pointer to object, dangles (harmlessly) after rc decrements to 0.
 
RCImpl rc
 refcount implementation for 'parent' object
 

Additional Inherited Members

- Static Public Member Functions inherited from openvpn::RC< RCImpl >
static constexpr bool is_thread_safe ()
 Delegates call to RCImpl and returns the result.
 

Detailed Description

template<typename RCImpl>
struct openvpn::RCWeak< RCImpl >::Controller

Controller structure for our ptr/weakptr implementation.

Template Parameters
RCImplThe ref count implementation

For weak-referenceable objects, we must detach the refcount from the object and place it in Controller.

This struct implements 3 essential elements:

  • A pointer to the parent (RCWeak enabled) object.
  • A ref count on Controller via RC<RCImpl> inheritance.
  • A ref count inside Controller via the RCImpl member 'rc'.

They are used as follows.

Controller::rc is the ref count of the object, and the inherited RCImpl is the ref count of the Controller itself. The 'parent' pointer is used to form a Strong (owning) pointer on demand if the Controller::rc refcount is greater than zero, or if not a nullptr Strong is returned.

An RCPtr<> will call intrusive_ptr_add_ref and intrusive_ptr_release helpers which will increment and decrement refcount_, which for RCWeak is an instance of RCWeak<>::ControllerRef. This ControllerRef instance will delegate those operations to the 'controller' member, which is an RCWeak<>::Controller referenced via an RCPtr<> instance inside ControllerRef. This is where the composed 'rc' refcount is modified.

The inherited RCImpl is controlled by the 'Controller::Ptr controller' member in ControllerRef via the member refcount_ and by the RCWeakPtr reaching in and manipulating it. Thus the object itself has 1 refcount on the controller and each weak ptr also holds a refcount on the controller.

When the last RCPtr on the object goes out of scope and decrements Controller::rc to zero, the object will be destroyed. If the only refcount on the controller is the ControllerRef, then the Controller instance will also be destroyed at this time. Otherwise the weak pointers to the controller will control that object's lifespan, but no further Strong pointers to the (now nonexistant) object will be allowed. This test for zero is what allows 'parent' to dangle harmlessly in some cases.

Definition at line 1185 of file rc.hpp.

Member Typedef Documentation

◆ Ptr

template<typename RCImpl >
typedef RCPtr<Controller> openvpn::RCWeak< RCImpl >::Controller::Ptr

Definition at line 1187 of file rc.hpp.

Constructor & Destructor Documentation

◆ Controller()

template<typename RCImpl >
openvpn::RCWeak< RCImpl >::Controller::Controller ( RCWeak parent_arg)
noexcept

Construct a new RCWeak<RCImpl>::Controller object.

Template Parameters
RCImplThe ref count implementation
Parameters
parent_argraw pointer to the RCWeak enabled object instance

Definition at line 1205 of file rc.hpp.

Member Function Documentation

◆ lock()

template<typename RCImpl >
template<typename PTR >
PTR openvpn::RCWeak< RCImpl >::Controller::lock ( )
noexcept

Used internally to try a to get a pointer to the controlled instance.

Template Parameters
RCImplthe injected ref count implementation
PTRexplicit return type
Returns
PTR pointer type referring to either the weakly ref'd object or nullptr

Will return the requested pointer to the object if the ref count could be incremented, or the specified pointer type set to nullptr

Definition at line 1228 of file rc.hpp.

◆ use_count()

template<typename RCImpl >
olong openvpn::RCWeak< RCImpl >::Controller::use_count ( ) const
noexcept

Get the use count of the underlying ref count implementation.

Template Parameters
RCImplthe ref count implementation
Returns
olong ref count

Definition at line 1213 of file rc.hpp.

Member Data Documentation

◆ parent

template<typename RCImpl >
RCWeak* const openvpn::RCWeak< RCImpl >::Controller::parent

Pointer to object, dangles (harmlessly) after rc decrements to 0.

Definition at line 1196 of file rc.hpp.

◆ rc

template<typename RCImpl >
RCImpl openvpn::RCWeak< RCImpl >::Controller::rc

refcount implementation for 'parent' object

Definition at line 1197 of file rc.hpp.


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