D:/WorkDir/SugoiProjects/SugoiTools/include/SugoiTools/singleton.h

00001 //==========================================================
00002 // Singleton.h
00003 //
00004 // Définition de la classe CSingleton
00005 //
00006 //==========================================================
00007 
00008 #ifndef SINGLETON_H
00009 #define SINGLETON_H
00010 
00011 //==========================================================
00012 // En-têtes
00013 //==========================================================
00014 #include "SugoiTools\config.h"
00015 #include <cstdlib>
00016 #include "SugoiTools\debug_new.h"
00017 
00018 namespace SGE{
00019     //==========================================================
00020     // Template servant à construire les classes singleton
00021     //==========================================================
00022     template <class T>
00023     class CSingleton
00024     {
00025     public :
00026 
00027         //----------------------------------------------------------------
00028         // Renvoie l'instance unique de la classe
00029         //----------------------------------------------------------------
00030         static T& Instance()
00031         {
00032             if (!Inst)
00033                 Inst = new T;
00034 
00035             return *Inst;
00036         }
00037 
00038         //----------------------------------------------------------------
00039         // Détruit l'instance unique de la classe
00040         //----------------------------------------------------------------
00041         static void Destroy()
00042         {
00043             delete Inst;
00044             Inst = NULL;
00045         }
00046 
00047     protected :
00048 
00049         //----------------------------------------------------------------
00050         // Constructeur par défaut
00051         //----------------------------------------------------------------
00052         CSingleton() {}
00053 
00054         //----------------------------------------------------------------
00055         // Destructeur
00056         //----------------------------------------------------------------
00057         ~CSingleton() {}
00058 
00059     private :
00060 
00061         //----------------------------------------------------------------
00062         // Données membres
00063         //----------------------------------------------------------------
00064         static T* Inst; // Instance de la classe
00065 
00066         //----------------------------------------------------------------
00067         // Copie interdite
00068         //----------------------------------------------------------------
00069         CSingleton(CSingleton&);
00070         void operator =(CSingleton&);
00071     };
00072 
00073     //----------------------------------------------------------------
00074     // Instanciation du singleton
00075     //----------------------------------------------------------------
00076     template <class T> T* CSingleton<T>::Inst = NULL;
00077 
00078     #include <Debug/DebugNewOff.h>
00079 };//namespace SGE
00080 #endif // SINGLETON_H

Generated on Mon Mar 19 23:15:11 2007 for SugoiTools by  doxygen 1.4.6-NO