A Discrete-Event Network Simulator
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
28namespace ns3
29{
30
42template <typename T>
44{
45 public:
46 // Delete default constructor, copy constructor and assignment operator to avoid misuse
47 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
85namespace ns3
86{
87
88template <typename T>
89T*
91{
92 T** ppobject = GetObject();
93 return *ppobject;
94}
95
96template <typename T>
97T**
99{
100 static T* pobject = nullptr;
101 if (pobject == nullptr)
102 {
103 pobject = new T();
105 }
106 return &pobject;
107}
108
109template <typename T>
110void
112{
113 T** ppobject = GetObject();
114 delete (*ppobject);
115 *ppobject = nullptr;
116}
117
118} // namespace ns3
119
120#endif /* SIMULATION_SINGLETON_H */
This singleton class template ensures that the type for which we want a singleton has a lifetime boun...
static T ** GetObject()
Get the singleton object, creating a new one if it doesn't exist yet.
SimulationSingleton< T > & operator=(const SimulationSingleton< T > &)=delete
static void DeleteObject()
Delete the static instance.
static T * Get()
Get a pointer to the singleton instance.
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
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Simulator declaration.