Schnek
singleton.hpp
1 /*
2  * singleton.hpp
3  *
4  * Created on: 9 May 2012
5  * Author: Holger Schmitz
6  * Email: holger@notjustphysics.com
7  *
8  * Copyright 2012 Holger Schmitz
9  *
10  * This file is part of Schnek.
11  *
12  * Schnek is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * Schnek is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Schnek. If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26 
27 #ifndef SCHNEK_SINGLETON_HPP_
28 #define SCHNEK_SINGLETON_HPP_
29 
30 namespace schnek
31 {
32 
41 template<class TYPE>
43 {
44  public:
48  static TYPE* create()
49  {
50  return new TYPE();
51  }
52 };
53 
54 
76 template<
77  class TYPE,
78  template<class> class CreationPolicy = CreateUsingNew
79 >
80 class Singleton
81 {
82  public:
83 
89  static TYPE& instance()
90  {
91  if (!pInstance)
92  {
93  pInstance = CreationPolicy<TYPE>::create();
94  }
95  return *pInstance;
96  }
97 
98  protected:
99  Singleton() {}
102  private:
104  Singleton(const Singleton&);
106  Singleton& operator=(const Singleton&);
107 
108 
112  static TYPE* pInstance;
113 };
114 
115 template<
116  class TYPE,
117  template<class> class CreationPolicy
118 >
120 
121 
122 }
123 
124 #endif // SCHNEK_SINGLETON_HPP_
~Singleton()
Definition: singleton.hpp:101
Definition: singleton.hpp:80
static TYPE & instance()
Definition: singleton.hpp:89
Definition: singleton.hpp:42
Definition: algo.hpp:30
static TYPE * create()
Definition: singleton.hpp:48