A Discrete-Event Network Simulator
API
ns3::Singleton< T > Class Template Reference

A template singleton. More...

#include "singleton.h"

+ Inheritance diagram for ns3::Singleton< T >:

Public Member Functions

 Singleton (const Singleton< T > &)=delete
 
Singleton< T > & operator= (const Singleton< T > &)=delete
 

Static Public Member Functions

static T * Get (void)
 Get a pointer to the singleton instance. More...
 

Protected Member Functions

 Singleton ()
 Constructor. More...
 
virtual ~Singleton ()
 Destructor. More...
 

Detailed Description

template<typename T>
class ns3::Singleton< T >

A template singleton.

This template class can be used to implement the singleton pattern. The underlying object will be destroyed automatically when the process exits.

For a singleton whose lifetime is bounded by the simulation run, not the process, see SimulationSingleton.

To force your class ExampleS to be a singleton, inherit from Singleton:

class ExampleS : public Singleton<ExampleS> { ... };
Singleton()
Constructor.
Definition: singleton.h:79

Then, to reach the singleton instance, just do

ExampleS::Get ()->...;
Note
If you call Get() again after the object has been destroyed, the object will be re-created which will result in a memory leak as reported by most memory leak checkers. It is up to the user to ensure that Get() is never called from a static variable finalizer.

Definition at line 60 of file singleton.h.

Constructor & Destructor Documentation

◆ Singleton() [1/2]

template<typename T >
ns3::Singleton< T >::Singleton ( const Singleton< T > &  )
delete

◆ Singleton() [2/2]

template<typename T >
ns3::Singleton< T >::Singleton ( )
inlineprotected

Constructor.

Definition at line 75 of file singleton.h.

◆ ~Singleton()

template<typename T >
virtual ns3::Singleton< T >::~Singleton ( )
inlineprotectedvirtual

Destructor.

Definition at line 75 of file singleton.h.

Member Function Documentation

◆ Get()

template<typename T >
T * ns3::Singleton< T >::Get ( void  )
static

Get a pointer to the singleton instance.

The instance will be automatically deleted when the process exits.

Returns
A pointer to the singleton instance.

Definition at line 100 of file singleton.h.

◆ operator=()

template<typename T >
Singleton<T>& ns3::Singleton< T >::operator= ( const Singleton< T > &  )
delete

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