NGL  6.5
The NCCA Graphics Library
Singleton.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 Vincent Bonnet
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 as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
16  */
17 #ifndef SINGLETON_H_
18 #define SINGLETON_H_
19 // must include types.h first for Real and GLEW if required
20 #include "Types.h"
21 #include <cstdlib>
22 //----------------------------------------------------------------------------------------------------------------------
25 //----------------------------------------------------------------------------------------------------------------------
26 namespace ngl
27 {
28 //----------------------------------------------------------------------------------------------------------------------
35 //----------------------------------------------------------------------------------------------------------------------
36 
37 template <class T>
38 
39 
41 {
42 public:
43 
44  //----------------------------------------------------------------------------------------------------------------------
47  //----------------------------------------------------------------------------------------------------------------------
48 
49  static T* instance();
50  Singleton(const Singleton&) = delete;
51  Singleton& operator=(const Singleton&) = delete;
52 protected:
53 
54  //----------------------------------------------------------------------------------------------------------------------
56  //----------------------------------------------------------------------------------------------------------------------
57  Singleton();
58 
59  //----------------------------------------------------------------------------------------------------------------------
61  //----------------------------------------------------------------------------------------------------------------------
62  virtual ~Singleton();
63  //----------------------------------------------------------------------------------------------------------------------
65  //----------------------------------------------------------------------------------------------------------------------
66  static T* s_instance;
67  static void killSingleton() { if(s_instance !=nullptr) delete s_instance;}
68 };
69 
70 
71 template<class T> T* Singleton<T>::s_instance = 0;
72 //----------------------------------------------------------------------------------------------------------------------
74 //----------------------------------------------------------------------------------------------------------------------
75 template<class T> Singleton<T>::Singleton()
76 { ; }
77 
78 //----------------------------------------------------------------------------------------------------------------------
80 //----------------------------------------------------------------------------------------------------------------------
81 
82 template<class T> Singleton<T>::~Singleton()
83 {
84 }
85 //----------------------------------------------------------------------------------------------------------------------
87 //----------------------------------------------------------------------------------------------------------------------
88 template<class T> T* Singleton<T>::instance()
89 {
90  if (s_instance == 0)
91  {
92  s_instance = new T();
93  std::atexit(killSingleton);
94  }
95 
96  return static_cast<T*>(s_instance);
97 
98 }
99 
100 
101 } // end ngl namespace
102 
103 #endif // __SINGLETON_H__
104 //----------------------------------------------------------------------------------------------------------------------
105 
main definition of types and namespace
#define NGL_DLLEXPORT
Definition: Types.h:65
implementation files for RibExport class
Definition: AABB.cpp:22
virtual ~Singleton()
Destructor.
Definition: Singleton.h:82
Singleton()
Constructor.
Definition: Singleton.h:75
static T * instance()
Get the instance.
Definition: Singleton.h:88
static void killSingleton()
Definition: Singleton.h:67
static T * s_instance
unique instance of the singleton
Definition: Singleton.h:66
Singleton template added to NGL framework 20/04/10 by jmacey.
Definition: Singleton.h:40