A Discrete-Event Network Simulator
API
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 SINGLETON_H
20 #define SINGLETON_H
21 
28 namespace ns3
29 {
30 
59 template <typename T>
60 class Singleton
61 {
62  public:
63  // Delete copy constructor and assignment operator to avoid misuse
64  Singleton(const Singleton<T>&) = delete;
65  Singleton& operator=(const Singleton<T>&) = delete;
66 
75  static T* Get();
76 
77  protected:
80  {
81  }
82 
84  virtual ~Singleton()
85  {
86  }
87 };
88 
89 } // namespace ns3
90 
91 /********************************************************************
92  * Implementation of the templates declared above.
93  ********************************************************************/
94 
95 namespace ns3
96 {
97 
98 template <typename T>
99 T*
101 {
102  static T object;
103  return &object;
104 }
105 
106 } // namespace ns3
107 
108 #endif /* SINGLETON_H */
A template singleton.
Definition: singleton.h:61
Singleton(const Singleton< T > &)=delete
static T * Get()
Get a pointer to the singleton instance.
Definition: singleton.h:100
Singleton()
Constructor.
Definition: singleton.h:79
virtual ~Singleton()
Destructor.
Definition: singleton.h:84
Singleton & operator=(const Singleton< T > &)=delete
Every class exported by the ns3 library is enclosed in the ns3 namespace.