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
22/**
23 * \file
24 * \ingroup singleton
25 * ns3::SimulationSingleton declaration and template implementation.
26 */
27
28namespace ns3
29{
30
31/**
32 * \ingroup singleton
33 * This singleton class template ensures that the type
34 * for which we want a singleton has a lifetime bounded
35 * by the simulation run lifetime. That it, the underlying
36 * type will be automatically deleted upon a call
37 * to Simulator::Destroy.
38 *
39 * For a singleton with a lifetime bounded by the process,
40 * not the simulation run, see Singleton.
41 */
42template <typename T>
44{
45 public:
46 // Delete default constructor, copy constructor and assignment operator to avoid misuse
50
51 /**
52 * Get a pointer to the singleton instance.
53 *
54 * This instance will be automatically deleted when the
55 * simulation is destroyed by a call to Simulator::Destroy.
56 *
57 * \returns A pointer to the singleton instance.
58 */
59 static T* Get();
60
61 private:
62 /**
63 * Get the singleton object, creating a new one if it doesn't exist yet.
64 *
65 * \internal
66 * When a new object is created, this method schedules it's own
67 * destruction using Simulator::ScheduleDestroy().
68 *
69 * \returns The address of the pointer holding the static instance.
70 */
71 static T** GetObject();
72
73 /** Delete the static instance. */
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...
SimulationSingleton & operator=(const SimulationSingleton< T > &)=delete
static T ** GetObject()
Get the singleton object, creating a new one if it doesn't exist yet.
SimulationSingleton(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:622
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Simulator declaration.