A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
simulation-singleton.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #ifndef SIMULATION_SINGLETON_H
21 #define SIMULATION_SINGLETON_H
22 
23 namespace ns3 {
24 
32 template <typename T>
34 {
35 public:
42  static T *Get (void);
43 private:
44  static T **GetObject (void);
45  static void DeleteObject (void);
46 };
47 
48 } // namespace ns3
49 
50 
51 #include "simulator.h"
52 
53 namespace ns3 {
54 
55 template <typename T>
56 T *
58 {
59  T ** ppobject = GetObject ();
60  return *ppobject;
61 }
62 
63 template <typename T>
64 T **
66 {
67  static T *pobject = 0;
68  if (pobject == 0)
69  {
70  pobject = new T ();
72  }
73  return &pobject;
74 }
75 
76 template <typename T>
77 void
79 {
80  T **ppobject = GetObject ();
81  delete (*ppobject);
82  *ppobject = 0;
83 }
84 
85 } // namespace ns3
86 
87 
88 #endif /* SIMULATION_SINGLETON_H */
This singleton class template ensures that the type for which we want a singleton has a lifetime boun...
static void DeleteObject(void)
static EventId ScheduleDestroy(MEM mem_ptr, OBJ obj)
Schedule an event to expire at Destroy time.
Definition: simulator.h:1076