A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
 
Singletonoperator= (const Singleton< T > &)=delete
 

Static Public Member Functions

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

Protected Member Functions

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

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> { ... };
A template singleton.
Definition: singleton.h:68

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 67 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 86 of file singleton.h.

◆ ~Singleton()

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

Destructor.

Definition at line 91 of file singleton.h.

Member Function Documentation

◆ Get()

template<typename T >
T * ns3::Singleton< T >::Get
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 107 of file singleton.h.

◆ operator=()

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

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