QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgssingleton.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgssingleton.h
3  --------------------------------------
4  Date : 24.11.2014
5  Copyright : (C) 2014 Matthias Kuhn
6  Email : matthias dot kuhn at gmx dot ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSSINGLETON_H
17 #define QGSSINGLETON_H
18 
19 template <typename T>
21 {
22  public:
23  static T* instance()
24  {
25  if ( sInstance == 0 )
26  {
27  sInstance = createInstance();
28  }
29  return sInstance;
30  }
31 
32  static void cleanup()
33  {
34  delete sInstance;
35  sInstance = 0;
36  }
37 
38  protected:
39  virtual ~QgsSingleton() {}
40 
41  explicit QgsSingleton()
42  {
43  }
44 
45  private:
46  static T* sInstance;
47  static T* createInstance()
48  {
49  return new T;
50  }
51 };
52 
53 template <typename T> T* QgsSingleton<T>::sInstance = 0;
54 
55 #endif // QGSSINGLETON_H