singleton.hpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * singleton.hpp: Generic Singleton pattern implementation
00003  ****************************************************************************
00004  * Copyright (C) 2009 VideoLAN
00005  *
00006  * Authors: Hugo Beauzee-Luyssen <beauze.h # gmail - com>
00007  *
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software Foundation, Inc.,
00020  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
00021  *****************************************************************************/
00022 
00023 #ifndef _SINGLETON_HPP_
00024 #define _SINGLETON_HPP_
00025 
00026 #include <stdlib.h>
00027 #include "qt4.hpp"
00028 
00029 template <typename T>
00030 class       Singleton
00031 {
00032 public:
00033     static T*      getInstance( intf_thread_t *p_intf = NULL )
00034     {
00035         if ( m_instance == NULL )
00036             m_instance = new T( p_intf );
00037         return m_instance;
00038     }
00039 
00040     static void    killInstance()
00041     {
00042         if ( m_instance != NULL )
00043         {
00044             delete m_instance;
00045             m_instance = NULL;
00046         }
00047     }
00048 protected:
00049     Singleton(){}
00050     virtual ~Singleton(){}
00051     /* Not implemented since these methods should *NEVER* been called.
00052     If they do, it probably won't compile :) */
00053     Singleton(const Singleton<T>&);
00054     Singleton<T>&   operator=(const Singleton<T>&);
00055 
00056 private:
00057     static T*      m_instance;
00058 };
00059 
00060 template <typename T>
00061 T*  Singleton<T>::m_instance = NULL;
00062 
00063 #endif // _SINGLETON_HPP_

Generated on Tue May 25 08:04:58 2010 for VLC by  doxygen 1.5.6