FIFE 2008.0
|
00001 /*************************************************************************** 00002 * Copyright (C) 2005-2008 by the FIFE team * 00003 * http://www.fifengine.de * 00004 * This file is part of FIFE. * 00005 * * 00006 * FIFE is free software; you can redistribute it and/or * 00007 * modify it under the terms of the GNU Lesser General Public * 00008 * License as published by the Free Software Foundation; either * 00009 * version 2.1 of the License, or (at your option) any later version. * 00010 * * 00011 * This library is distributed in the hope that it will be useful, * 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00014 * Lesser General Public License for more details. * 00015 * * 00016 * You should have received a copy of the GNU Lesser General Public * 00017 * License along with this library; if not, write to the * 00018 * Free Software Foundation, Inc., * 00019 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * 00020 ***************************************************************************/ 00021 00022 #ifndef FIFE_SINGLETON_H 00023 #define FIFE_SINGLETON_H 00024 00025 #define SINGLEFRIEND(classname) friend class FIFE::StaticSingleton<classname>; classname(); virtual ~classname(); 00026 00027 // Standard C++ library includes 00028 #include <cassert> 00029 00030 // 3rd party library includes 00031 00032 // FIFE includes 00033 // These includes are split up in two parts, separated by one empty line 00034 // First block: files included from the FIFE root src directory 00035 // Second block: files included from the same folder 00036 #include "fifeclass.h" 00037 00038 namespace FIFE { 00039 00044 template <typename T> class StaticSingleton: public FifeClass { 00045 public: 00046 00047 static T* instance() { 00048 static T inst; 00049 return &inst; 00050 } 00051 00052 protected: 00053 00054 StaticSingleton(): FifeClass() { 00055 } 00056 00057 virtual ~StaticSingleton() { 00058 } 00059 00060 private: 00061 StaticSingleton(const StaticSingleton<T>&): FifeClass() {} 00062 StaticSingleton<T>& operator=(const StaticSingleton<T>&) { 00063 return this; 00064 } 00065 00066 }; 00067 00082 template <typename T> class DynamicSingleton: public FifeClass { 00083 public: 00084 static T* instance() { 00085 assert(m_instance); 00086 return m_instance; 00087 } 00088 00089 DynamicSingleton(): FifeClass() { 00090 assert(!m_instance); 00091 m_instance = static_cast<T*>(this); 00092 } 00093 00094 virtual ~DynamicSingleton() { 00095 m_instance = 0; 00096 } 00097 00098 private: 00099 static T* m_instance; 00100 00101 DynamicSingleton(const DynamicSingleton<T>&): FifeClass() {}; 00102 DynamicSingleton<T&> operator=(const DynamicSingleton<T>&) {}; 00103 00104 }; 00105 00106 template <typename T> T* DynamicSingleton<T>::m_instance = 0; 00107 00108 }//FIFE 00109 00110 #endif