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
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 2 as
6
* published by the Free Software Foundation;
7
*
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
12
*
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
*
17
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18
*/
19
#ifndef SIMULATION_SINGLETON_H
20
#define SIMULATION_SINGLETON_H
21
28
namespace
ns3
29
{
30
42
template
<
typename
T>
43
class
SimulationSingleton
44
{
45
public
:
46
// Delete default constructor, copy constructor and assignment operator to avoid misuse
47
SimulationSingleton<T>
() =
delete
;
48
SimulationSingleton<T>
(
const
SimulationSingleton<T>
&) =
delete
;
49
SimulationSingleton<T>
&
operator=
(
const
SimulationSingleton<T>
&) =
delete
;
50
59
static
T*
Get
();
60
61
private
:
71
static
T**
GetObject
();
72
74
static
void
DeleteObject
();
75
};
76
77
}
// namespace ns3
78
79
/********************************************************************
80
* Implementation of the templates declared above.
81
********************************************************************/
82
83
#include "
simulator.h
"
84
85
namespace
ns3
86
{
87
88
template
<
typename
T>
89
T*
90
SimulationSingleton<T>::Get
()
91
{
92
T** ppobject = GetObject();
93
return
*ppobject;
94
}
95
96
template
<
typename
T>
97
T**
98
SimulationSingleton<T>::GetObject
()
99
{
100
static
T* pobject =
nullptr
;
101
if
(pobject ==
nullptr
)
102
{
103
pobject =
new
T();
104
Simulator::ScheduleDestroy
(&
SimulationSingleton<T>::DeleteObject
);
105
}
106
return
&pobject;
107
}
108
109
template
<
typename
T>
110
void
111
SimulationSingleton<T>::DeleteObject
()
112
{
113
T** ppobject = GetObject();
114
delete
(*ppobject);
115
*ppobject =
nullptr
;
116
}
117
118
}
// namespace ns3
119
120
#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:44
ns3::SimulationSingleton::GetObject
static T ** GetObject()
Get the singleton object, creating a new one if it doesn't exist yet.
Definition:
simulation-singleton.h:98
ns3::SimulationSingleton::operator=
SimulationSingleton< T > & operator=(const SimulationSingleton< T > &)=delete
ns3::SimulationSingleton::DeleteObject
static void DeleteObject()
Delete the static instance.
Definition:
simulation-singleton.h:111
ns3::SimulationSingleton::Get
static T * Get()
Get a pointer to the singleton instance.
Definition:
simulation-singleton.h:90
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:625
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 Sun Jul 2 2023 18:21:32 for ns-3 by
1.9.6