Leviathan  0.8.0.0
Leviathan game engine
Leviathan::ReferenceCounted Class Reference

#include <ReferenceCounted.h>

+ Inheritance diagram for Leviathan::ReferenceCounted:

Public Types

using basepointer = boost::intrusive_ptr< ReferenceCounted >
 
using refcountedpointer = boost::intrusive_ptr< ReferenceCounted >
 

Public Member Functions

 ReferenceCounted (const ReferenceCounted &other)=delete
 
ReferenceCountedoperator= (const ReferenceCounted &other)=delete
 
FORCE_INLINE void AddRef ()
 
FORCE_INLINE void Release ()
 removes a reference and deletes the object if reference count reaches zero More...
 
int32_t GetRefCount () const
 Returns the reference count. More...
 

Static Public Member Functions

template<class ActualType >
static boost::intrusive_ptr< ActualType > WrapPtr (ActualType *ptr)
 Creates an intrusive_ptr from raw pointer. More...
 
template<class ActualType , class... Args>
static boost::intrusive_ptr< ActualType > MakeShared (Args &&... args)
 Constructs a new instance and wraps it. More...
 

Protected Member Functions

DLLEXPORT ReferenceCounted ()
 
virtual DLLEXPORT ~ReferenceCounted ()
 

Friends

void intrusive_ptr_add_ref (const ReferenceCounted *obj)
 
void intrusive_ptr_release (const ReferenceCounted *obj)
 

Detailed Description

Reference counted object which will be deleted when all references are gone

Note
Pointers can be used with ReferenceCounted::pointer ptr = new Object();
Todo:
Make sure that all functions using intrusive pointers use the MakeIntrusive function

Definition at line 33 of file ReferenceCounted.h.

Member Typedef Documentation

◆ basepointer

Definition at line 39 of file ReferenceCounted.h.

◆ refcountedpointer

Definition at line 40 of file ReferenceCounted.h.

Constructor & Destructor Documentation

◆ ReferenceCounted() [1/2]

DLLEXPORT Leviathan::ReferenceCounted::ReferenceCounted ( )
inlineprotected

Definition at line 45 of file ReferenceCounted.h.

45 : RefCount(1) {}

◆ ~ReferenceCounted()

virtual DLLEXPORT Leviathan::ReferenceCounted::~ReferenceCounted ( )
inlineprotectedvirtual

Definition at line 46 of file ReferenceCounted.h.

46 {}

◆ ReferenceCounted() [2/2]

Leviathan::ReferenceCounted::ReferenceCounted ( const ReferenceCounted other)
delete

Member Function Documentation

◆ AddRef()

FORCE_INLINE void Leviathan::ReferenceCounted::AddRef ( )
inline

Definition at line 53 of file ReferenceCounted.h.

54  {
55 
57  }
friend void intrusive_ptr_add_ref(const ReferenceCounted *obj)

◆ GetRefCount()

int32_t Leviathan::ReferenceCounted::GetRefCount ( ) const
inline

Returns the reference count.

Todo:
Make sure that the right memory order is used

Definition at line 106 of file ReferenceCounted.h.

107  {
108 
109  return RefCount.load(std::memory_order_acquire);
110  }

◆ MakeShared()

template<class ActualType , class... Args>
static boost::intrusive_ptr<ActualType> Leviathan::ReferenceCounted::MakeShared ( Args &&...  args)
inlinestatic

Constructs a new instance and wraps it.

Note
Doesn't catch any exceptions

Definition at line 93 of file ReferenceCounted.h.

94  {
95 
96  boost::intrusive_ptr<ActualType> ptr(new ActualType(std::forward<Args>(args)...));
97  ptr->Release();
98 
99  return ptr;
100  }

◆ operator=()

ReferenceCounted& Leviathan::ReferenceCounted::operator= ( const ReferenceCounted other)
delete

◆ Release()

FORCE_INLINE void Leviathan::ReferenceCounted::Release ( )
inline

removes a reference and deletes the object if reference count reaches zero

Definition at line 60 of file ReferenceCounted.h.

61  {
62 
64  }
friend void intrusive_ptr_release(const ReferenceCounted *obj)

◆ WrapPtr()

template<class ActualType >
static boost::intrusive_ptr<ActualType> Leviathan::ReferenceCounted::WrapPtr ( ActualType *  ptr)
inlinestatic

Creates an intrusive_ptr from raw pointer.

Releases the reference of the raw pointer (so you don't need to do it manually)

Definition at line 71 of file ReferenceCounted.h.

72  {
73 
74  if(!ptr)
75  return nullptr;
76 
77  boost::intrusive_ptr<ActualType> newptr(ptr);
78  ptr->Release();
79 
80  return newptr;
81  }

Friends And Related Function Documentation

◆ intrusive_ptr_add_ref

void intrusive_ptr_add_ref ( const ReferenceCounted obj)
friend

Definition at line 114 of file ReferenceCounted.h.

115  {
116 
117  obj->RefCount.fetch_add(1, std::memory_order_relaxed);
118  }

◆ intrusive_ptr_release

void intrusive_ptr_release ( const ReferenceCounted obj)
friend

Definition at line 120 of file ReferenceCounted.h.

121  {
122 
123  if(obj->RefCount.fetch_sub(1, std::memory_order_release) == 1) {
124  std::atomic_thread_fence(std::memory_order_acquire);
125  delete obj;
126  }
127  }

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