00001 /* 00002 ----------------------------------------------------------------------------- 00003 This source file is part of OpenSpace3D 00004 For the latest info, see http://www.openspace3d.com 00005 00006 Copyright (c) 2010 I-maginer 00007 00008 This program is free software; you can redistribute it and/or modify it under 00009 the terms of the GNU Lesser General Public License as published by the Free Software 00010 Foundation; either version 2 of the License, or (at your option) any later 00011 version. 00012 00013 This program is distributed in the hope that it will be useful, but WITHOUT 00014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public License along with 00018 this program; if not, write to the Free Software Foundation, Inc., 59 Temple 00019 Place - Suite 330, Boston, MA 02111-1307, USA, or go to 00020 http://www.gnu.org/copyleft/lesser.txt 00021 00022 You may alternatively use this source under the terms of a specific version of 00023 the OpenSpace3D Unrestricted License provided you have obtained such a license from 00024 I-maginer. 00025 ----------------------------------------------------------------------------- 00026 */ 00027 00035 #ifndef __SINGLETON_H__ 00036 #define __SINGLETON_H__ 00037 00038 #include "../lib/common.h" 00039 00040 namespace NIDevice 00041 { 00042 namespace Core 00043 { 00048 template <typename DERIVATED_CLASS> class Singleton 00049 { 00050 public: 00051 protected: 00052 static DERIVATED_CLASS* instance; 00053 private: 00054 00055 public: 00059 Singleton(); 00060 00064 ~Singleton(); 00065 00069 static DERIVATED_CLASS& GetSingleton(); 00070 00074 static DERIVATED_CLASS* GetSingletonPtr(); 00075 protected: 00076 private: 00077 }; 00078 00079 template <class DERIVATED_CLASS> inline Singleton<DERIVATED_CLASS>::Singleton() 00080 { 00081 assert(!Singleton<DERIVATED_CLASS>::instance); 00082 #if defined(_MSC_VER) && _MSC_VER < 1200 00083 int offset = (int)(DERIVATED_CLASS*)1 - (int)(Singleton <DERIVATED_CLASS>*)(DERIVATED_CLASS*)1; 00084 Singleton<DERIVATED_CLASS>::instance = (DERIVATED_CLASS*)((int)this + offset); 00085 #else 00086 Singleton<DERIVATED_CLASS>::instance = static_cast<DERIVATED_CLASS*>(this); 00087 #endif 00088 }; 00089 00090 template <class DERIVATED_CLASS> inline Singleton<DERIVATED_CLASS>::~Singleton() 00091 { 00092 assert(Singleton<DERIVATED_CLASS>::instance); 00093 Singleton<DERIVATED_CLASS>::instance = 0; 00094 }; 00095 00096 template <class DERIVATED_CLASS> DERIVATED_CLASS& Singleton<DERIVATED_CLASS>::GetSingleton() 00097 { 00098 assert(Singleton<DERIVATED_CLASS>::instance); 00099 return (*Singleton<DERIVATED_CLASS>::instance); 00100 }; 00101 00102 template <class DERIVATED_CLASS> DERIVATED_CLASS* Singleton<DERIVATED_CLASS>::GetSingletonPtr() 00103 { 00104 return Singleton<DERIVATED_CLASS>::instance; 00105 }; 00106 } 00107 } 00108 00109 #endif
1.7.2