TonicTones
Src/Api/Singleton.h
00001 //      Singleton.h
00002 //      
00003 //      Copyright 2010 Jérémy Laumon <jeremy.laumon@gmail.com>
00004 //      
00005 //      This program is free software; you can redistribute it and/or modify
00006 //      it under the terms of the GNU General Public License as published by
00007 //      the Free Software Foundation; either version 2 of the License, or
00008 //      (at your option) any later version.
00009 //      
00010 //      This program is distributed in the hope that it will be useful,
00011 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //      GNU General Public License for more details.
00014 //      
00015 //      You should have received a copy of the GNU General Public License
00016 //      along with this program; if not, write to the Free Software
00017 //      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00018 //      MA 02110-1301, USA.
00019 
00020 #ifndef SINGLETON_H
00021 #define SINGLETON_H
00022 
00030 template <typename T>
00031 class Singleton
00032 {
00033 protected:
00034   Singleton() {}
00035   ~Singleton() {}
00036 
00037 public:
00038 
00042     static T *instance()
00043     {
00044         if (NULL == inst)
00045         {
00046             inst = new T;
00047         }
00048         return (static_cast<T*> (inst));
00049     }
00050 
00054     static void destroy()
00055     {
00056         if (NULL != inst)
00057         {
00058             delete inst;
00059             inst = NULL;
00060         }
00061     }
00062 
00063 private:
00064     static T *inst;
00065     
00066     Singleton(const Singleton&);
00067     Singleton& operator =(const Singleton&);
00068 };
00069 
00070 template <typename T>
00071 T *Singleton<T>::inst = NULL;
00072 
00073 #endif
 All Classes Functions Variables