Mbed OS Reference
Loading...
Searching...
No Matches
SingletonPtr< T > Struct Template Reference

Utility class for creating and using a singleton. More...

#include <SingletonPtr.h>

Public Member Functions

T * get () const
 Get a pointer to the underlying singleton. More...
 
T * operator-> () const
 Get a pointer to the underlying singleton. More...
 
T & operator* () const
 Get a reference to the underlying singleton. More...
 
T * get_no_init () const
 Get a pointer to the underlying singleton. More...
 
void destroy ()
 Destroy the underlying singleton. More...
 

Detailed Description

template<class T>
struct SingletonPtr< T >

Utility class for creating and using a singleton.

Note
Synchronization level: Thread safe
: This class is lazily initialized on first use. This class has a constexpr default constructor so if it is not used as a non-local variable it will be garbage collected.
: This class would normally be used in a static standalone context. It does not call the destructor of the wrapped object when it is destroyed, effectively ensuring linker exclusion of the destructor for static objects. If used in another context, such as a member of a normal class wanting "initialize on first-use" semantics on a member, care should be taken to call the destroy method manually if necessary.
: If used as a sub-object of a class, that class's own constructor must be constexpr to achieve its exclusion by the linker when unused. That will require explicit initialization of its other members.
: More detail on initialization: Formerly, SingletonPtr had no constructor, so was "zero-initialized" when non-local. So if enclosed in another class with no constructor, the whole thing would be zero-initialized, and linker-excludable. Having no constructor meant SingletonPtr was not constexpr, which limited applicability in other contexts. With its new constexpr constructor, it is now "constant-initialized" when non-local. This achieves the same effect as a standalone non-local object, but as a sub-object linker exclusion is now only achieved if the outer object is itself using a constexpr constructor to get constant-initialization. Otherwise, the outer object will be neither zero-initialized nor constant-initialized, so will be "dynamic-initialized", and likely to be left in by the linker.

Definition at line 88 of file SingletonPtr.h.

Member Function Documentation

◆ get()

T * get ( ) const

Get a pointer to the underlying singleton.

Returns
A pointer to the singleton

Definition at line 101 of file SingletonPtr.h.

◆ operator->()

T * operator-> ( ) const

Get a pointer to the underlying singleton.

Returns
A pointer to the singleton

Definition at line 124 of file SingletonPtr.h.

◆ operator*()

T & operator* ( ) const

Get a reference to the underlying singleton.

Returns
A reference to the singleton

Definition at line 134 of file SingletonPtr.h.

◆ get_no_init()

T * get_no_init ( ) const

Get a pointer to the underlying singleton.

Gets a pointer without initialization - can be used as an optimization when it is known that initialization must have already occurred.

Returns
A pointer to the singleton, or NULL if not initialized.

Definition at line 149 of file SingletonPtr.h.

◆ destroy()

void destroy ( )

Destroy the underlying singleton.

The underlying singleton is never automatically destroyed; this is a potential optimization to avoid destructors being pulled into an embedded image on the exit path, which should never occur. The destructor can be manually invoked via this call.

Unlike construction, this is not thread-safe. After this call, no further operations on the object are permitted.

Is a no-op if the object has not been constructed.

Definition at line 167 of file SingletonPtr.h.