Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

# KeosSingleton.h

Go to the documentation of this file.

    00001 /*
    00002  * This source file is part of KEOS (Free 3D Engine)
    00003  * For the latest info, see http://www.keosengine.org/
    00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
    00005  *
    00006  * This program is free software; you can redistribute it and/or modify it under
    00007  * the terms of the GNU Lesser General Public License as published by the Free Software
    00008  * Foundation; either version 2 of the License, or (at your option) any later
    00009  * version.
    00010  *
    00011  * This program is distributed in the hope that it will be useful, but WITHOUT
    00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    00014  *
    00015  * You should have received a copy of the GNU Lesser General Public License along with
    00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
    00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
    00018  * http://www.gnu.org/copyleft/lesser.txt.
    00019  *
    00020  */
    00021
    00022 #ifndef KEOS_SINGLETON_H
    00023 #define KEOS_SINGLETON_H
    00024
    00025 #include "KeosPrerequisites.h"
    00026
    00027 namespace Keos
    00028 {
    00029     //-----------------------------------------------------------------------
    00033         template <class T>
    00034     class CSingleton
    00035     {
    00036     public :
    00037
    00041         static void Create()
    00042         {
    00043             if (!ms_pInst)
    00044                 ms_pInst = new T;
    00045         }
    00046
    00050         static T& Instance()
    00051         {
    00052             if (!ms_pInst)
    00053                 ms_pInst = new T;
    00054
    00055             return *ms_pInst;
    00056         }
    00057
    00061         static void Destroy()
    00062         {
    00063             delete ms_pInst;
    00064             ms_pInst = NULL;
    00065         }
    00066
    00070                 static void _SetInstance(T* pInst)
    00071                 {
    00072                         ms_pInst = pInst;
    00073                 }
    00074
    00075     protected :
    00076
    00080         CSingleton() {}
    00081
    00085         ~CSingleton() {}
    00086
    00087     private :
    00088
    00090         static T* ms_pInst;
    00091
    00095         CSingleton(CSingleton&);
    00096
    00100         void operator =(CSingleton&);
    00101     };
    00102
    00103     // Instance initialisation
    00104     template <class T> T* CSingleton<T>::ms_pInst = NULL;
    00105
    00106 } // namespace Keos
    00107
    00108 #endif // KEOS_SINGLETON_H