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
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 SINGLETON_H
20
#define SINGLETON_H
21
22
/**
23
* \file
24
* \ingroup singleton
25
* ns3::Singleton declaration and template implementation.
26
*/
27
28
namespace
ns3
29
{
30
31
/**
32
* \ingroup core
33
* \defgroup singleton Singleton
34
*
35
* Template class implementing the Singleton design pattern.
36
*/
37
38
/**
39
* \ingroup singleton
40
* \brief A template singleton
41
*
42
* This template class can be used to implement the singleton pattern.
43
* The underlying object will be destroyed automatically when the process
44
* exits.
45
*
46
* For a singleton whose lifetime is bounded by the simulation run,
47
* not the process, see SimulationSingleton.
48
*
49
* To force your `class ExampleS` to be a singleton, inherit from Singleton:
50
* \code
51
* class ExampleS : public Singleton<ExampleS> { ... };
52
* \endcode
53
*
54
* Then, to reach the singleton instance, just do
55
* \code
56
* ExampleS::Get ()->...;
57
* \endcode
58
*
59
* \note
60
* If you call Get() again after the object has
61
* been destroyed, the object will be re-created which will result in a
62
* memory leak as reported by most memory leak checkers. It is up to the
63
* user to ensure that Get() is never called from a static variable
64
* finalizer.
65
*/
66
template
<
typename
T>
67
class
Singleton
68
{
69
public
:
70
// Delete copy constructor and assignment operator to avoid misuse
71
Singleton
(
const
Singleton<T>
&) =
delete
;
72
Singleton
&
operator=
(
const
Singleton<T>
&) =
delete
;
73
74
/**
75
* Get a pointer to the singleton instance.
76
*
77
* The instance will be automatically deleted when
78
* the process exits.
79
*
80
* \return A pointer to the singleton instance.
81
*/
82
static
T*
Get
();
83
84
protected
:
85
/** Constructor. */
86
Singleton
()
87
{
88
}
89
90
/** Destructor. */
91
virtual
~Singleton
()
92
{
93
}
94
};
95
96
}
// namespace ns3
97
98
/********************************************************************
99
* Implementation of the templates declared above.
100
********************************************************************/
101
102
namespace
ns3
103
{
104
105
template
<
typename
T>
106
T*
107
Singleton<T>::Get
()
108
{
109
static
T
object
;
110
return
&
object
;
111
}
112
113
}
// namespace ns3
114
115
#endif
/* SINGLETON_H */
ns3::Singleton
A template singleton.
Definition:
singleton.h:68
ns3::Singleton::Singleton
Singleton(const Singleton< T > &)=delete
ns3::Singleton::operator=
Singleton & operator=(const Singleton< T > &)=delete
ns3::Singleton::Get
static T * Get()
Get a pointer to the singleton instance.
Definition:
singleton.h:107
ns3::Singleton::Singleton
Singleton()
Constructor.
Definition:
singleton.h:86
ns3::Singleton::~Singleton
virtual ~Singleton()
Destructor.
Definition:
singleton.h:91
object
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
core
model
singleton.h
Generated on Tue May 28 2024 23:34:39 for ns-3 by
1.9.6