ORIGINAL
Loading...
Searching...
No Matches
original::singleton< TYPE > Class Template Reference

Thread-safe singleton pattern implementation with ownership management. More...

#include <singleton.h>

Collaboration diagram for original::singleton< TYPE >:
Collaboration graph

Public Member Functions

 singleton (const singleton &)=delete
 Deleted copy constructor to prevent copying.
 
singletonoperator= (const singleton &)=delete
 Deleted copy assignment operator to prevent copying.
 
 singleton (singleton &&)=delete
 Deleted move constructor to prevent moving.
 
singletonoperator= (singleton &&)=delete
 Deleted move assignment operator to prevent moving.
 

Static Public Member Functions

static bool exist ()
 Checks if the singleton instance exists.
 
template<typename... Args>
static void init (Args &&... args)
 Initializes the singleton instance with provided arguments.
 
static TYPE & instance ()
 Provides access to the singleton instance.
 
static void clear ()
 Clears the singleton instance.
 
template<typename... Args>
static void reset (Args &&... args)
 Resets the singleton instance with new arguments.
 

Detailed Description

template<typename TYPE>
class original::singleton< TYPE >

Thread-safe singleton pattern implementation with ownership management.

Template Parameters
TYPEType of the singleton instance

Provides a global point of access to a single instance of TYPE while ensuring proper initialization, destruction, and ownership semantics. Uses ownerPtr for exclusive ownership and automatic cleanup.

Key Features:

  • Lazy initialization with thread safety
  • Exclusive ownership using ownerPtr
  • Controlled initialization and destruction
  • Prevention of copy and move operations
  • Exception-safe operations
Note
This implementation is not thread-safe for simultaneous init()/reset() calls. For thread-safe usage, ensure proper synchronization at the application level.

Member Function Documentation

◆ clear()

template<typename TYPE >
void original::singleton< TYPE >::clear ( )
static

Clears the singleton instance.

Postcondition
instance_ becomes nullptr, existing instance is destroyed
Note
Safe to call even if instance doesn't exist

◆ exist()

template<typename TYPE >
bool original::singleton< TYPE >::exist ( )
static

Checks if the singleton instance exists.

Returns
true if instance is initialized, false otherwise

◆ init()

template<typename TYPE >
template<typename ... Args>
void original::singleton< TYPE >::init ( Args &&... args)
static

Initializes the singleton instance with provided arguments.

Template Parameters
ArgsArgument types for TYPE constructor
Parameters
argsArguments to forward to TYPE constructor
Exceptions
valueErrorif instance already exists
singleton<MyClass>::init(arg1, arg2); // Initialize with constructor arguments
static void init(Args &&... args)
Initializes the singleton instance with provided arguments.
Definition singleton.h:122

◆ instance()

template<typename TYPE >
TYPE & original::singleton< TYPE >::instance ( )
static

Provides access to the singleton instance.

Returns
Reference to the singleton instance of TYPE
Exceptions
nullPointerErrorif instance not initialized
auto& instance = singleton<MyClass>::instance(); // Access the instance
instance.someMethod();
static TYPE & instance()
Provides access to the singleton instance.
Definition singleton.h:131

◆ reset()

template<typename TYPE >
template<typename ... Args>
void original::singleton< TYPE >::reset ( Args &&... args)
static

Resets the singleton instance with new arguments.

Template Parameters
ArgsArgument types for TYPE constructor
Parameters
argsArguments to forward to TYPE constructor
Note
Destroys existing instance and creates a new one
singleton<MyClass>::reset(newArg1, newArg2); // Replace existing instance
static void reset(Args &&... args)
Resets the singleton instance with new arguments.
Definition singleton.h:147

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