uvco 0.1
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Private Attributes | List of all members
uvco::RefCounted< T > Class Template Reference

#include <internal_utils.h>

Public Member Functions

 RefCounted (const RefCounted &other)=default
 
RefCountedoperator= (const RefCounted &other)=default
 
 RefCounted (RefCounted &&other) noexcept
 
RefCountedoperator= (RefCounted &&other) noexcept
 
virtual ~RefCounted ()=default
 
virtual T * addRef ()
 
virtual void delRef ()
 

Protected Member Functions

 RefCounted ()=default
 

Private Attributes

size_t count_ = 1
 

Detailed Description

template<typename T>
class uvco::RefCounted< T >

RefCounted<T> is an intrusive refcounting approach, which reduces the run-time of low-overhead high frequency promise code (such as buffered channel ping-pong scenarios) by as much as 50% compared to shared_ptr use. However, manual refcounting is required by objects owning a refcounted object.

Use makeRefCounted() to allocate a new reference-counted object, and store it as part of your class. Use addRef() and delRef() to keep track of the current number of references.

This type currently doesn't work well with inheritance: only a class directly inheriting from RefCounted can be managed. This is caused by the current API approach.

Constructor & Destructor Documentation

◆ RefCounted() [1/3]

template<typename T >
uvco::RefCounted< T >::RefCounted ( const RefCounted< T > &  other)
default

◆ RefCounted() [2/3]

template<typename T >
uvco::RefCounted< T >::RefCounted ( RefCounted< T > &&  other)
inlinenoexcept
103{}

◆ ~RefCounted()

template<typename T >
virtual uvco::RefCounted< T >::~RefCounted ( )
virtualdefault

◆ RefCounted() [3/3]

template<typename T >
uvco::RefCounted< T >::RefCounted ( )
protecteddefault

Member Function Documentation

◆ addRef()

template<typename T >
virtual T * uvco::RefCounted< T >::addRef ( )
inlinevirtual

Use in e.g. copy constructors, when creating a new reference to the same object.

109 {
110 ++count_;
111 return static_cast<T *>(this);
112 }
size_t count_
Definition internal_utils.h:128

◆ delRef()

template<typename T >
virtual void uvco::RefCounted< T >::delRef ( )
inlinevirtual

Use in e.g. destructors, when an existing pointer goes out of scope. Once the reference count has dropped to 0, the referred object will be deleted.

117 {
118 --count_;
119 if (count_ == 0) {
120 delete static_cast<T *>(this);
121 }
122 }

◆ operator=() [1/2]

template<typename T >
RefCounted & uvco::RefCounted< T >::operator= ( const RefCounted< T > &  other)
default

◆ operator=() [2/2]

template<typename T >
RefCounted & uvco::RefCounted< T >::operator= ( RefCounted< T > &&  other)
inlinenoexcept
104{}

Member Data Documentation

◆ count_

template<typename T >
size_t uvco::RefCounted< T >::count_ = 1
private

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