Loading...
Searching...
No Matches
juce::SharedResourcePointer< SharedObjectType > Class Template Reference

## 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.
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.
SharedResourcePointer<MySharedData> sharedData;
};
juce::Array
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:68

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

- juce_core/memory/juce_SharedResourcePointer.h

## Public Member Functions

| SharedResourcePointer ()=default
| Creates an instance of the shared object.
| SharedResourcePointer (const SharedResourcePointer &)=default
| Copy constructor.
| SharedResourcePointer (SharedResourcePointer &&) noexcept=default
| Move constructor.
| ~SharedResourcePointer ()=default
| Destructor.
| operator SharedObjectType * () const noexcept
| Returns a pointer to the shared object.
SharedObjectType & | get () const noexcept
| Returns a reference to the shared object.
SharedObjectType & | getObject () const noexcept
| Returns a reference to the shared object.
SharedObjectType * | operator-> () const noexcept
| Returns a pointer to the shared object.
SharedObjectType & | operator* () const noexcept
| Returns a reference to the shared object.

## Static Public Member Functions

static std::optional< SharedResourcePointer > | getSharedObjectWithoutCreating ()
| Returns the SharedResourcePointer if one already exists, or a null optional otherwise.

## Constructors and Destructors

## SharedResourcePointer() [1/3]

template<typename SharedObjectType>

juce::SharedResourcePointer< SharedObjectType >::SharedResourcePointer | ( |  | ) |

default

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.

Referenced by getSharedObjectWithoutCreating(), SharedResourcePointer(), SharedResourcePointer(), and ~SharedResourcePointer().

## SharedResourcePointer() [2/3]

template<typename SharedObjectType>

juce::SharedResourcePointer< SharedObjectType >::SharedResourcePointer | ( | const SharedResourcePointer< SharedObjectType > & |  | ) |

default

Copy constructor.

References SharedResourcePointer().

## SharedResourcePointer() [3/3]

template<typename SharedObjectType>

juce::SharedResourcePointer< SharedObjectType >::SharedResourcePointer | ( | SharedResourcePointer< SharedObjectType > && |  | ) |

default noexcept

Move constructor.

References SharedResourcePointer().

## ~SharedResourcePointer()

template<typename SharedObjectType>

juce::SharedResourcePointer< SharedObjectType >::~SharedResourcePointer | ( |  | ) |

default

Destructor.

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

References SharedResourcePointer().

## Member Functions

## operator SharedObjectType *()

template<typename SharedObjectType>

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

inline noexcept

Returns a pointer to the shared object.

## get()

template<typename SharedObjectType>

SharedObjectType & juce::SharedResourcePointer< SharedObjectType >::get | ( |  | ) | const

inline noexcept

Returns a reference to the shared object.

## getObject()

template<typename SharedObjectType>

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

inline noexcept

Returns a reference to the shared object.

## operator->()

template<typename SharedObjectType>

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

inline noexcept

Returns a pointer to the shared object.

## operator*()

template<typename SharedObjectType>

SharedObjectType & juce::SharedResourcePointer< SharedObjectType >::operator* | ( |  | ) | const

inline noexcept

Returns a reference to the shared object.

## getSharedObjectWithoutCreating()

template<typename SharedObjectType>

std::optional< SharedResourcePointer > juce::SharedResourcePointer< SharedObjectType >::getSharedObjectWithoutCreating | ( |  | ) |

inline static

Returns the SharedResourcePointer if one already exists, or a null optional otherwise.

References SharedResourcePointer().

### Purchase

Get JUCE

### Discover

What's New in JUCE Features

### Learn

Documentaion Tutorials Made with JUCE Resources

### Support

JUCE Forum Newsletter Archive

### About

Contact Us JUCE Legal JUCE Licensing FAQ

### Events

Audio Developer Conference
© Raw Material Software Limited