ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

singleton.h

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #ifndef COMMON_SINGLETON_H
00024 #define COMMON_SINGLETON_H
00025 
00026 #include "common/noncopyable.h"
00027 
00028 namespace Common {
00029 
00033 template<class T>
00034 class Singleton : NonCopyable {
00035 private:
00036     Singleton<T>(const Singleton<T> &);
00037     Singleton<T> &operator=(const Singleton<T> &);
00038 
00046     //template<class T>
00047 #if defined(__WINS__)
00048 //FIXME verify if __WINS__ needs this still
00049 public:
00050 #endif
00051     static T *makeInstance() {
00052         return new T();
00053     }
00054 
00055     static void destroyInstance() {
00056         delete _singleton;
00057         _singleton = 0;
00058     }
00059 
00060 
00061 public:
00062     static bool hasInstance() {
00063         return _singleton != 0;
00064     }
00065 
00066     static T& instance() {
00067         // TODO: We aren't thread safe. For now we ignore it since the
00068         // only thing using this singleton template is the config manager,
00069         // and that is first instantiated long before any threads.
00070         // TODO: We don't leak, but the destruction order is nevertheless
00071         // semi-random. If we use multiple singletons, the destruction
00072         // order might become an issue. There are various approaches
00073         // to solve that problem, but for now this is sufficient
00074         if (!_singleton)
00075             _singleton = T::makeInstance();
00076         return *_singleton;
00077     }
00078 
00079     static void destroy() {
00080         T::destroyInstance();
00081     }
00082 protected:
00083     Singleton<T>()      { }
00084 #ifdef __SYMBIAN32__
00085     virtual ~Singleton()    { }
00086 #else
00087     virtual ~Singleton<T>() { }
00088 #endif
00089 
00090     typedef T   SingletonBaseType;
00091 
00092     static T *_singleton;
00093 };
00094 
00101 #define DECLARE_SINGLETON(T) \
00102     template<> T *Singleton<T>::_singleton = 0
00103 
00104 } // End of namespace Common
00105 
00106 #endif


Generated on Sat Feb 9 2019 05:00:41 for ResidualVM by doxygen 1.7.1
curved edge   curved edge