author | Gustavo J. A. M. Carneiro <gjc@inescporto.pt> |
Fri, 11 Jul 2008 18:39:19 +0100 | |
changeset 3416 | 926ccb845111 |
parent 1696 | 0de65f4c8c43 |
permissions | -rw-r--r-- |
483
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
2 |
/* |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
3 |
* Copyright (c) 2007 INRIA |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
4 |
* |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or modify |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License version 2 as |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation; |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
8 |
* |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
13 |
* |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
15 |
* along with this program; if not, write to the Free Software |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
17 |
* |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
18 |
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
19 |
*/ |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
20 |
#ifndef SIMULATION_SINGLETON_H |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
21 |
#define SIMULATION_SINGLETON_H |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
22 |
|
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
23 |
namespace ns3 { |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
24 |
|
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
25 |
/** |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
26 |
* This singleton class template ensures that the type |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
27 |
* for which we want a singleton has a lifetime bounded |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
28 |
* by the simulation lifetime. That it, the underlying |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
29 |
* type will be automatically deleted upon a users' call |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
30 |
* to Simulator::Destroy. |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
31 |
*/ |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
32 |
template <typename T> |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
33 |
class SimulationSingleton |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
34 |
{ |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
35 |
public: |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
36 |
/** |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
37 |
* \returns the instance underlying this singleton. |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
38 |
* |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
39 |
* This instance will be automatically deleted when the |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
40 |
* user calls ns3::Simulator::Destroy. |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
41 |
*/ |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
42 |
static T *Get (void); |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
43 |
private: |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
44 |
static T **GetObject (void); |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
45 |
static void DeleteObject (void); |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
46 |
}; |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
47 |
|
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
48 |
}//namespace ns3 |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
49 |
|
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
50 |
|
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
51 |
#include "simulator.h" |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
52 |
|
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
53 |
namespace ns3 { |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
54 |
|
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
55 |
template <typename T> |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
56 |
T * |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
57 |
SimulationSingleton<T>::Get (void) |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
58 |
{ |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
59 |
T ** ppobject = GetObject (); |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
60 |
return *ppobject; |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
61 |
} |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
62 |
|
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
63 |
template <typename T> |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
64 |
T ** |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
65 |
SimulationSingleton<T>::GetObject (void) |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
66 |
{ |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
67 |
static T *pobject = 0; |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
68 |
if (pobject == 0) |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
69 |
{ |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
70 |
pobject = new T (); |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
71 |
Simulator::ScheduleDestroy (&SimulationSingleton<T>::DeleteObject); |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
72 |
} |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
73 |
return &pobject; |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
74 |
} |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
75 |
|
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
76 |
template <typename T> |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
77 |
void |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
78 |
SimulationSingleton<T>::DeleteObject (void) |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
79 |
{ |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
80 |
T **ppobject = GetObject (); |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
81 |
delete (*ppobject); |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
82 |
*ppobject = 0; |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
83 |
} |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
84 |
|
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
85 |
} // namespace ns3 |
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
86 |
|
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
87 |
|
24c99e8ebaa2
a singleton template class to simplify the implementation of a few classes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff
changeset
|
88 |
#endif /* SIMULATION_SINGLETON_H */ |