The Sparta Modeling Framework
Loading...
Searching...
No Matches
sparta::SpartaSharedPointer< PointerT > Class Template Reference

Used for garbage collection, will delete the object it points to when all objects are finished using it. More...

#include <SpartaSharedPointer.hpp>

Public Types

using element_type = PointerT
 Expected typedef for the resource type.
 

Public Member Functions

 SpartaSharedPointer (PointerT *p=nullptr) noexcept
 Construct a Reference Pointer with the given memory object.
 
constexpr SpartaSharedPointer (std::nullptr_t) noexcept
 Constructor for SpartaSharedPointer<T> ptr = nullptr;.
 
template<class PointerT2 >
 SpartaSharedPointer (const SpartaSharedPointer< PointerT2 > &orig) noexcept
 Construct a reference pointer given another implicitly convertable reference pointer.
 
 SpartaSharedPointer (const SpartaSharedPointer &orig)
 Construct a reference pointer given another reference pointer.
 
 SpartaSharedPointer (SpartaSharedPointer &&orig)
 Take ownership of a reference pointer.
 
 ~SpartaSharedPointer ()
 Detach this shared pointer; if last, delete underlying object.
 
SpartaSharedPointeroperator= (const SpartaSharedPointer &orig)
 Assignment operator.
 
SpartaSharedPointeroperator= (SpartaSharedPointer &&orig)
 Assignment move operator.
 
bool operator! () const
 The not ! operator.
 
 operator bool () const
 Boolean cast.
 
PointerT * operator-> () const
 Dereference the RefPointer (const)
 
PointerT & operator* () const
 Dereference the RefPointer (const)
 
PointerT * get () const
 Get the underlying pointer.
 
void reset (PointerT *p=nullptr)
 Reset this shared pointer.
 
uint32_t use_count () const
 Get the current reference count.
 

Friends

class SpartaSharedPointerAllocator< PointerT >
 
class SpartaWeakPointer< PointerT >
 
template<typename PtrT , typename... Args>
SpartaSharedPointer< PtrT > allocate_sparta_shared_pointer (SpartaSharedPointerAllocator< PtrT > &, Args &&...args)
 

Detailed Description

template<class PointerT>
class sparta::SpartaSharedPointer< PointerT >

Used for garbage collection, will delete the object it points to when all objects are finished using it.

Simple, thread-unsafe class that will delete memory it points to when the last memory reference is deconstructed.

This class was created from the use case of typical model development: lots and lots of small, shared objects flowing through the simulation. Allocation/deallocation of std::shared_ptr is a solution as well as Boost's shared_ptr, but they serve a more general purpose and follow the principles of allocation traits and thread safety. This class does not. Because of the differentiated use case, this class is much faster (up to 6x faster) than using std::shared_ptr.

In addition, the concept of a weak pointer is also supported via the SpartaWeakPointer found nested in this class.

Example usage:

void foo()
{
int * t = new int(0); // Don't do this, BTW
ptr2 = ptr;
*ptr = 5;
sparta_assert(*ptr2 == 5);
sparta_assert(*ptr3 == 10);
sparta_assert(false == wp.expired());
ptr3.reset();
sparta_assert(true == wp.expired());
// Don't delete!
}
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
Used for garbage collection, will delete the object it points to when all objects are finished using ...
Like in STL, create a weak pointer to a SpartaSharedPointer.
bool expired() const noexcept
Has the SpartaSharedPointer that this weak pointer points to expired?
long use_count() const noexcept
The use count of the SpartaSharedPointer. Will be 0 if none left.

This class can be used independently, or more efficiently with sparta::allocate_sparta_shared_pointer<T>. See sparta::SpartaSharedPointerAllocator for more information.

Definition at line 80 of file SpartaSharedPointer.hpp.

Member Typedef Documentation

◆ element_type

template<class PointerT >
using sparta::SpartaSharedPointer< PointerT >::element_type = PointerT

Expected typedef for the resource type.

Definition at line 113 of file SpartaSharedPointer.hpp.

Constructor & Destructor Documentation

◆ SpartaSharedPointer() [1/5]

template<class PointerT >
sparta::SpartaSharedPointer< PointerT >::SpartaSharedPointer ( PointerT *  p = nullptr)
inlineexplicitnoexcept

Construct a Reference Pointer with the given memory object.

Parameters
pThe memory that this reference pointer will take ownership of

Construct a new Reference Pointer and take initial ownership of the given object pointer.

Definition at line 122 of file SpartaSharedPointer.hpp.

◆ SpartaSharedPointer() [2/5]

template<class PointerT >
constexpr sparta::SpartaSharedPointer< PointerT >::SpartaSharedPointer ( std::nullptr_t  )
inlineconstexprnoexcept

Constructor for SpartaSharedPointer<T> ptr = nullptr;.

Parameters
nullptr_t

Definition at line 130 of file SpartaSharedPointer.hpp.

◆ SpartaSharedPointer() [3/5]

template<class PointerT >
template<class PointerT2 >
sparta::SpartaSharedPointer< PointerT >::SpartaSharedPointer ( const SpartaSharedPointer< PointerT2 > &  orig)
inlinenoexcept

Construct a reference pointer given another implicitly convertable reference pointer.

Parameters
rThe other reference pointer

The two reference pointers now share the common memory but the newly constructed reference pointer will interpret it as a different class

Definition at line 143 of file SpartaSharedPointer.hpp.

◆ SpartaSharedPointer() [4/5]

template<class PointerT >
sparta::SpartaSharedPointer< PointerT >::SpartaSharedPointer ( const SpartaSharedPointer< PointerT > &  orig)
inline

Construct a reference pointer given another reference pointer.

Parameters
origThe original pointer

The two reference pointers now share the common memory

Definition at line 161 of file SpartaSharedPointer.hpp.

◆ SpartaSharedPointer() [5/5]

template<class PointerT >
sparta::SpartaSharedPointer< PointerT >::SpartaSharedPointer ( SpartaSharedPointer< PointerT > &&  orig)
inline

Take ownership of a reference pointer.

Parameters
origThe original pointer

This SpartaSharedPointer now replaces orig. Orig becomes a nullptr with no references

Definition at line 176 of file SpartaSharedPointer.hpp.

◆ ~SpartaSharedPointer()

template<class PointerT >
sparta::SpartaSharedPointer< PointerT >::~SpartaSharedPointer ( )
inline

Detach this shared pointer; if last, delete underlying object.

Definition at line 184 of file SpartaSharedPointer.hpp.

Member Function Documentation

◆ get()

template<class PointerT >
PointerT * sparta::SpartaSharedPointer< PointerT >::get ( ) const
inline

Get the underlying pointer.

Returns
The underlying pointer

Definition at line 271 of file SpartaSharedPointer.hpp.

◆ operator bool()

template<class PointerT >
sparta::SpartaSharedPointer< PointerT >::operator bool ( ) const
inlineexplicit

Boolean cast.

Returns
Will be true if the pointer is not null

For operations like:

if(refptr) { ... }
// const bool is_good = refptr; // Disallowed

Definition at line 246 of file SpartaSharedPointer.hpp.

◆ operator!()

template<class PointerT >
bool sparta::SpartaSharedPointer< PointerT >::operator! ( ) const
inline

The not ! operator.

Returns
true if the pointer is null, false otherwise

For operations like:

if(!refptr) { ... }

Definition at line 232 of file SpartaSharedPointer.hpp.

◆ operator*()

template<class PointerT >
PointerT & sparta::SpartaSharedPointer< PointerT >::operator* ( ) const
inline

Dereference the RefPointer (const)

Returns
The pointer pointed to by this RefPointer

Definition at line 262 of file SpartaSharedPointer.hpp.

◆ operator->()

template<class PointerT >
PointerT * sparta::SpartaSharedPointer< PointerT >::operator-> ( ) const
inline

Dereference the RefPointer (const)

Returns
The pointer pointed to by this RefPointer

Definition at line 254 of file SpartaSharedPointer.hpp.

◆ operator=() [1/2]

template<class PointerT >
SpartaSharedPointer & sparta::SpartaSharedPointer< PointerT >::operator= ( const SpartaSharedPointer< PointerT > &  orig)
inline

Assignment operator.

Parameters
origThe original pointer
Returns
Reference to this

The two reference pointers now share the common memory

Definition at line 195 of file SpartaSharedPointer.hpp.

◆ operator=() [2/2]

template<class PointerT >
SpartaSharedPointer & sparta::SpartaSharedPointer< PointerT >::operator= ( SpartaSharedPointer< PointerT > &&  orig)
inline

Assignment move operator.

Parameters
origThe original pointer to be moved
Returns
Reference to this

The rvalue is assigned into this class, with this class unlinking itself from it's original pointer

Definition at line 214 of file SpartaSharedPointer.hpp.

◆ reset()

template<class PointerT >
void sparta::SpartaSharedPointer< PointerT >::reset ( PointerT *  p = nullptr)
inline

Reset this shared pointer.

Parameters
pThe new pointer

Definition at line 279 of file SpartaSharedPointer.hpp.

◆ use_count()

template<class PointerT >
uint32_t sparta::SpartaSharedPointer< PointerT >::use_count ( ) const
inline

Get the current reference count.

Returns
The current reference count. 0 if this SpartaSharedPointer points to nullptr

Definition at line 288 of file SpartaSharedPointer.hpp.

Friends And Related Symbol Documentation

◆ SpartaSharedPointer

template<class PointerT >
template<class PointerT2 >
friend class SpartaSharedPointer
friend

Definition at line 84 of file SpartaSharedPointer.hpp.

◆ SpartaSharedPointerAllocator< PointerT >

template<class PointerT >
friend class SpartaSharedPointerAllocator< PointerT >
friend

Definition at line 389 of file SpartaSharedPointer.hpp.

◆ SpartaWeakPointer< PointerT >

template<class PointerT >
friend class SpartaWeakPointer< PointerT >
friend

Definition at line 389 of file SpartaSharedPointer.hpp.


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