OpenShot Audio Library | OpenShotAudio 0.3.2
Loading...
Searching...
No Matches
juce::SharedResourcePointer< SharedObjectType > Class Template Reference

#include <juce_SharedResourcePointer.h>

Public Member Functions

 SharedResourcePointer ()
 
 SharedResourcePointer (const SharedResourcePointer &)
 
 ~SharedResourcePointer ()
 
 operator SharedObjectType * () const noexcept
 
SharedObjectTypeget () const noexcept
 
SharedObjectTypegetObject () const noexcept
 
SharedObjectTypeoperator-> () const noexcept
 
int getReferenceCount () const noexcept
 

Detailed Description

template<typename SharedObjectType>
class juce::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.
{
// 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.
{
}
};
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.
SharedResourcePointer<MySharedData> sharedData;
};

Definition at line 81 of file juce_SharedResourcePointer.h.

Constructor & Destructor Documentation

◆ SharedResourcePointer() [1/2]

juce::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.

Definition at line 90 of file juce_SharedResourcePointer.h.

◆ SharedResourcePointer() [2/2]

Definition at line 95 of file juce_SharedResourcePointer.h.

◆ ~SharedResourcePointer()

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

Definition at line 104 of file juce_SharedResourcePointer.h.

Member Function Documentation

◆ get()

Returns the shared object.

Definition at line 117 of file juce_SharedResourcePointer.h.

◆ getObject()

SharedObjectType & juce::SharedResourcePointer< SharedObjectType >::getObject ( ) const
inlinenoexcept

Returns the object that this pointer references. The pointer returned may be a nullptr, of course.

Definition at line 122 of file juce_SharedResourcePointer.h.

◆ getReferenceCount()

int juce::SharedResourcePointer< SharedObjectType >::getReferenceCount ( ) const
inlinenoexcept

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

Definition at line 128 of file juce_SharedResourcePointer.h.

◆ operator SharedObjectType *()

juce::SharedResourcePointer< SharedObjectType >::operator SharedObjectType * ( ) const
inlinenoexcept

Returns the shared object.

Definition at line 114 of file juce_SharedResourcePointer.h.

◆ operator->()

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

Returns the shared object.

Definition at line 125 of file juce_SharedResourcePointer.h.


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