SingletonT.hh
00001 /* 00002 * Gazebo - Outdoor Multi-Robot Simulator 00003 * Copyright (C) 2003 00004 * Nate Koenig & Andrew Howard 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 * 00020 */ 00021 /* Desc: Singleton base class 00022 * Author: Nate Koenig 00023 * Date: 2 Sept 2007 00024 * SVN: $Id: SingletonT.hh 156 2007-11-26 20:26:57Z natepak $ 00025 */ 00026 00027 #ifndef SINGLETONT_HH 00028 #define SINGLETONT_HH 00029 00030 template < class DOOMED > 00031 class DestroyerT; 00032 00034 template <class T> 00035 class SingletonT 00036 { 00038 public: static T *Instance() 00039 { 00040 if (!myself) 00041 { 00042 myself = new T(); 00043 destroyer.SetDoomed(myself); 00044 } 00045 00046 return myself; 00047 } 00048 00050 protected: SingletonT() {} 00051 00053 protected: virtual ~SingletonT() {} 00054 00055 private: static DestroyerT< T > destroyer; 00056 private: static T *myself; 00057 }; 00058 00059 template <class T> 00060 DestroyerT<T> SingletonT< T >::destroyer; 00061 00062 template <class T> 00063 T *SingletonT<T>::myself = 0; 00064 00065 00066 00068 template < class DOOMED > 00069 class DestroyerT 00070 { 00071 00073 public: DestroyerT(DOOMED* = 0); 00074 00076 public: ~DestroyerT(); 00077 00079 public: void SetDoomed(DOOMED*); 00080 00083 private: DestroyerT(const DestroyerT<DOOMED>&); 00084 00086 private: DestroyerT<DOOMED>& operator=(const DestroyerT<DOOMED>&); 00087 00088 private: DOOMED* doomed; 00089 }; 00090 00091 template <class DOOMED> 00092 DestroyerT<DOOMED>::DestroyerT( DOOMED* d ) 00093 { 00094 doomed = d; 00095 } 00096 00097 template <class DOOMED> 00098 DestroyerT<DOOMED>::~DestroyerT() { 00099 delete doomed; 00100 } 00101 00102 template <class DOOMED> 00103 void DestroyerT<DOOMED>::SetDoomed( DOOMED* d ) 00104 { 00105 doomed = d; 00106 } 00107 00108 #endif