Project

General

Profile

Kinect Scol plugin
Singleton.h
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OpenSpace3D
4 For the latest info, see http://www.openspace3d.com
5 
6 Copyright (c) 2012 I-maginer
7 
8 This program is free software; you can redistribute it and/or modify it under
9 the terms of the GNU Lesser General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
11 version.
12 
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License along with
18 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20 http://www.gnu.org/copyleft/lesser.txt
21 
22 -----------------------------------------------------------------------------
23 */
24 
32 #ifndef __SINGLETON_H__
33 #define __SINGLETON_H__
34 
35 #include "../lib/common.h"
36 
41 template <typename DERIVATED_CLASS> class Singleton
42 {
43  public:
44  protected:
45  static DERIVATED_CLASS* instance;
46  private:
47 
48  public:
52  Singleton();
53 
57  ~Singleton();
58 
62  static DERIVATED_CLASS& GetSingleton();
63 
67  static DERIVATED_CLASS* GetSingletonPtr();
68  protected:
69  private:
70 };
71 
72 template <class DERIVATED_CLASS> inline Singleton<DERIVATED_CLASS>::Singleton()
73 {
74  assert(!Singleton<DERIVATED_CLASS>::instance);
75  #if defined(_MSC_VER) && _MSC_VER < 1200
76  int offset = (int)(DERIVATED_CLASS*)1 - (int)(Singleton <DERIVATED_CLASS>*)(DERIVATED_CLASS*)1;
77  Singleton<DERIVATED_CLASS>::instance = (DERIVATED_CLASS*)((int)this + offset);
78  #else
79  Singleton<DERIVATED_CLASS>::instance = static_cast<DERIVATED_CLASS*>(this);
80  #endif
81 };
82 
83 template <class DERIVATED_CLASS> inline Singleton<DERIVATED_CLASS>::~Singleton()
84 {
85  assert(Singleton<DERIVATED_CLASS>::instance);
86  Singleton<DERIVATED_CLASS>::instance = 0;
87 };
88 
89 template <class DERIVATED_CLASS> DERIVATED_CLASS& Singleton<DERIVATED_CLASS>::GetSingleton()
90 {
91  assert(Singleton<DERIVATED_CLASS>::instance);
92  return (*Singleton<DERIVATED_CLASS>::instance);
93 };
94 
95 template <class DERIVATED_CLASS> DERIVATED_CLASS* Singleton<DERIVATED_CLASS>::GetSingletonPtr()
96 {
97  return Singleton<DERIVATED_CLASS>::instance;
98 };
99 
100 #endif
~Singleton()
Definition: Singleton.h:83
Singleton()
Definition: Singleton.h:72
static DERIVATED_CLASS * GetSingletonPtr()
Definition: Singleton.h:95
static DERIVATED_CLASS & GetSingleton()
Definition: Singleton.h:89