JUCE  v5.1.1-3-g1a0b28c73
JUCE API
SharedResourcePointer< SharedObjectType > Class Template Reference

A smart-pointer that automatically creates and manages the lifetime of a shared static instance of a class. More...

#include <juce_SharedResourcePointer.h>

Collaboration diagram for SharedResourcePointer< SharedObjectType >:

Classes

struct  SharedObjectHolder
 

Public Member Functions

 SharedResourcePointer ()
 Creates an instance of the shared object. More...
 
 SharedResourcePointer (const SharedResourcePointer &)
 
 ~SharedResourcePointer ()
 Destructor. More...
 
SharedObjectType & get () const noexcept
 Returns the shared object. More...
 
SharedObjectType & getObject () const noexcept
 Returns the object that this pointer references. More...
 
int getReferenceCount () const noexcept
 Returns the number of SharedResourcePointers that are currently holding the shared object. More...
 
 operator SharedObjectType * () const noexcept
 Returns the shared object. More...
 
SharedObjectType * operator-> () const noexcept
 Returns the shared object. More...
 

Private Member Functions

void initialise ()
 
SharedResourcePointeroperator= (const SharedResourcePointer &)=delete
 

Static Private Member Functions

static SharedObjectHoldergetSharedObjectHolder () noexcept
 

Private Attributes

SharedObjectType * sharedObject
 

Detailed Description

template<typename SharedObjectType>
class SharedResourcePointer< SharedObjectType >

A smart-pointer that automatically creates and manages the lifetime of a shared static instance of a class.

The SharedObjectType template type indicates the class to use for the shared object - the only requirements on this class are that it must have a public default constructor and destructor.

The SharedResourcePointer offers a pattern that differs from using a singleton or static instance of an object, because it uses reference-counting to make sure that the underlying shared object is automatically created/destroyed according to the number of SharedResourcePointer objects that exist. When the last one is deleted, the underlying object is also immediately destroyed. This allows you to use scoping to manage the lifetime of a shared resource.

Note: the construction/deletion of the shared object must not involve any code that makes recursive calls to a SharedResourcePointer, or you'll cause a deadlock.

Example:

// An example of a class that contains the shared data you want to use.
struct MySharedData
{
// There's no need to ever create an instance of this class directly yourself,
// but it does need a public constructor that does the initialisation.
MySharedData()
{
sharedStuff = generateHeavyweightStuff();
}
Array<SomeKindOfData> sharedStuff;
};
struct DataUserClass
{
DataUserClass()
{
// Multiple instances of the DataUserClass will all have the same
// shared common instance of MySharedData referenced by their sharedData
// member variables.
useSharedStuff (sharedData->sharedStuff);
}
// By keeping this pointer as a member variable, the shared resource
// is guaranteed to be available for as long as the DataUserClass object.
};

Constructor & Destructor Documentation

◆ SharedResourcePointer() [1/2]

template<typename SharedObjectType>
SharedResourcePointer< SharedObjectType >::SharedResourcePointer ( )
inline

Creates an instance of the shared object.

If other SharedResourcePointer objects for this type already exist, then this one will simply point to the same shared object that they are already using. Otherwise, if this is the first SharedResourcePointer to be created, then a shared object will be created automatically.

References SharedResourcePointer< SharedObjectType >::initialise().

◆ SharedResourcePointer() [2/2]

template<typename SharedObjectType>
SharedResourcePointer< SharedObjectType >::SharedResourcePointer ( const SharedResourcePointer< SharedObjectType > &  )
inline

◆ ~SharedResourcePointer()

template<typename SharedObjectType>
SharedResourcePointer< SharedObjectType >::~SharedResourcePointer ( )
inline

Destructor.

If no other SharedResourcePointer objects exist, this will also delete the shared object to which it refers.

References SharedResourcePointer< SharedObjectType >::getSharedObjectHolder().

Member Function Documentation

◆ get()

template<typename SharedObjectType>
SharedObjectType& SharedResourcePointer< SharedObjectType >::get ( ) const
inlinenoexcept

Returns the shared object.

References SharedResourcePointer< SharedObjectType >::sharedObject.

◆ getObject()

template<typename SharedObjectType>
SharedObjectType& SharedResourcePointer< SharedObjectType >::getObject ( ) const
inlinenoexcept

Returns the object that this pointer references.

The pointer returned may be a nullptr, of course.

References SharedResourcePointer< SharedObjectType >::sharedObject.

◆ getReferenceCount()

template<typename SharedObjectType>
int SharedResourcePointer< SharedObjectType >::getReferenceCount ( ) const
inlinenoexcept

Returns the number of SharedResourcePointers that are currently holding the shared object.

References SharedResourcePointer< SharedObjectType >::getSharedObjectHolder(), and SharedResourcePointer< SharedObjectType >::SharedObjectHolder::refCount.

◆ getSharedObjectHolder()

◆ initialise()

◆ operator SharedObjectType *()

template<typename SharedObjectType>
SharedResourcePointer< SharedObjectType >::operator SharedObjectType * ( ) const
inlinenoexcept

Returns the shared object.

References SharedResourcePointer< SharedObjectType >::sharedObject.

◆ operator->()

template<typename SharedObjectType>
SharedObjectType* SharedResourcePointer< SharedObjectType >::operator-> ( ) const
inlinenoexcept

Returns the shared object.

References SharedResourcePointer< SharedObjectType >::sharedObject.

◆ operator=()

template<typename SharedObjectType>
SharedResourcePointer& SharedResourcePointer< SharedObjectType >::operator= ( const SharedResourcePointer< SharedObjectType > &  )
privatedelete

Member Data Documentation

◆ sharedObject


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