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 * 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
17namespace 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 */
31template <typename T>
33{
34 public:
35 // Delete default constructor, copy constructor and assignment operator to avoid misuse
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
74namespace ns3
75{
76
77template <typename T>
78T*
80{
81 T** ppobject = GetObject();
82 return *ppobject;
83}
84
85template <typename T>
86T**
88{
89 static T* pobject = nullptr;
90 if (pobject == nullptr)
91 {
92 pobject = new T();
94 }
95 return &pobject;
96}
97
98template <typename T>
99void
101{
102 T** ppobject = GetObject();
103 delete (*ppobject);
104 *ppobject = nullptr;
105}
106
107} // namespace ns3
108
109#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:611
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Simulator declaration.