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 34 of file ReferenceCounted.h.

Member Typedef Documentation

◆ basepointer

Definition at line 40 of file ReferenceCounted.h.

◆ refcountedpointer

Definition at line 41 of file ReferenceCounted.h.

Constructor & Destructor Documentation

◆ ReferenceCounted() [1/2]

DLLEXPORT Leviathan::ReferenceCounted::ReferenceCounted ( )
inlineprotected

Definition at line 46 of file ReferenceCounted.h.

46 : RefCount(1) {}

◆ ~ReferenceCounted()

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

Definition at line 47 of file ReferenceCounted.h.

47 {}

◆ ReferenceCounted() [2/2]

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

Member Function Documentation

◆ AddRef()

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

Definition at line 54 of file ReferenceCounted.h.

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 103 of file ReferenceCounted.h.

104  {
105  return RefCount.load(std::memory_order_acquire);
106  }

◆ 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 91 of file ReferenceCounted.h.

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

◆ 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  {
63  }
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 70 of file ReferenceCounted.h.

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

Friends And Related Function Documentation

◆ intrusive_ptr_add_ref

void intrusive_ptr_add_ref ( const ReferenceCounted obj)
friend

Definition at line 110 of file ReferenceCounted.h.

111  {
112  obj->RefCount.fetch_add(1, std::memory_order_relaxed);
113  }

◆ intrusive_ptr_release

void intrusive_ptr_release ( const ReferenceCounted obj)
friend

Definition at line 115 of file ReferenceCounted.h.

116  {
117  if(obj->RefCount.fetch_sub(1, std::memory_order_release) == 1) {
118  std::atomic_thread_fence(std::memory_order_acquire);
119  delete obj;
120  }
121  }

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