A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
simulation-singleton.h
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2007 INRIA
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7
*/
8
#ifndef SIMULATION_SINGLETON_H
9
#define SIMULATION_SINGLETON_H
10
11
/**
12
* \file
13
* \ingroup singleton
14
* ns3::SimulationSingleton declaration and template implementation.
15
*/
16
17
namespace
ns3
18
{
19
20
/**
21
* \ingroup singleton
22
* This singleton class template ensures that the type
23
* for which we want a singleton has a lifetime bounded
24
* by the simulation run lifetime. That it, the underlying
25
* type will be automatically deleted upon a call
26
* to Simulator::Destroy.
27
*
28
* For a singleton with a lifetime bounded by the process,
29
* not the simulation run, see Singleton.
30
*/
31
template
<
typename
T>
32
class
SimulationSingleton
33
{
34
public
:
35
// Delete default constructor, copy constructor and assignment operator to avoid misuse
36
SimulationSingleton
() =
delete
;
37
SimulationSingleton
(
const
SimulationSingleton<T>
&) =
delete
;
38
SimulationSingleton
&
operator=
(
const
SimulationSingleton<T>
&) =
delete
;
39
40
/**
41
* Get a pointer to the singleton instance.
42
*
43
* This instance will be automatically deleted when the
44
* simulation is destroyed by a call to Simulator::Destroy.
45
*
46
* \returns A pointer to the singleton instance.
47
*/
48
static
T*
Get
();
49
50
private
:
51
/**
52
* Get the singleton object, creating a new one if it doesn't exist yet.
53
*
54
* \internal
55
* When a new object is created, this method schedules it's own
56
* destruction using Simulator::ScheduleDestroy().
57
*
58
* \returns The address of the pointer holding the static instance.
59
*/
60
static
T**
GetObject
();
61
62
/** Delete the static instance. */
63
static
void
DeleteObject
();
64
};
65
66
}
// namespace ns3
67
68
/********************************************************************
69
* Implementation of the templates declared above.
70
********************************************************************/
71
72
#include "
simulator.h
"
73
74
namespace
ns3
75
{
76
77
template
<
typename
T>
78
T*
79
SimulationSingleton<T>::Get
()
80
{
81
T** ppobject = GetObject();
82
return
*ppobject;
83
}
84
85
template
<
typename
T>
86
T**
87
SimulationSingleton<T>::GetObject
()
88
{
89
static
T* pobject =
nullptr
;
90
if
(pobject ==
nullptr
)
91
{
92
pobject =
new
T();
93
Simulator::ScheduleDestroy
(&
SimulationSingleton<T>::DeleteObject
);
94
}
95
return
&pobject;
96
}
97
98
template
<
typename
T>
99
void
100
SimulationSingleton<T>::DeleteObject
()
101
{
102
T** ppobject = GetObject();
103
delete
(*ppobject);
104
*ppobject =
nullptr
;
105
}
106
107
}
// namespace ns3
108
109
#endif
/* SIMULATION_SINGLETON_H */
ns3::SimulationSingleton
This singleton class template ensures that the type for which we want a singleton has a lifetime boun...
Definition
simulation-singleton.h:33
ns3::SimulationSingleton::operator=
SimulationSingleton & operator=(const SimulationSingleton< T > &)=delete
ns3::SimulationSingleton::GetObject
static T ** GetObject()
Get the singleton object, creating a new one if it doesn't exist yet.
Definition
simulation-singleton.h:87
ns3::SimulationSingleton::SimulationSingleton
SimulationSingleton()=delete
ns3::SimulationSingleton::SimulationSingleton
SimulationSingleton(const SimulationSingleton< T > &)=delete
ns3::SimulationSingleton::DeleteObject
static void DeleteObject()
Delete the static instance.
Definition
simulation-singleton.h:100
ns3::SimulationSingleton::Get
static T * Get()
Get a pointer to the singleton instance.
Definition
simulation-singleton.h:79
ns3::Simulator::ScheduleDestroy
static EventId ScheduleDestroy(FUNC f, Ts &&... args)
Schedule an event to run at the end of the simulation, when Simulator::Destroy() is called.
Definition
simulator.h:611
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
simulator.h
ns3::Simulator declaration.
src
core
model
simulation-singleton.h
Generated on Wed Oct 9 2024 01:03:23 for ns-3 by
1.11.0