00001 /* 00002 * Singleton.hpp 00003 * 00004 * Created on: Oct 17, 2011 00005 * Author: mriedel 00006 */ 00007 00008 #ifndef SINGLETON_HPP_ 00009 #define SINGLETON_HPP_ 00010 00011 #include <telekyb_defines/telekyb_defines.hpp> 00012 #include <ros/assert.h> 00013 00014 #include <boost/utility.hpp> 00015 00016 #include <telekyb_base/Base/BaseSingleton.hpp> 00017 00018 namespace TELEKYB_NAMESPACE 00019 { 00020 00021 template <class _T> 00022 class Singleton : public BaseSingleton, boost::noncopyable 00023 { 00024 private: 00025 // Singleton(const Singleton<_T> &); 00026 // Singleton& operator=(const Singleton<_T> &); 00027 static _T* m_pInstance; 00028 00029 protected: 00030 Singleton() { 00031 ROS_ASSERT( !m_pInstance ); // you can only create if it does not yet exist. 00032 m_pInstance = static_cast<_T*>(this); 00033 }; 00034 virtual ~Singleton() { 00035 ROS_ASSERT( m_pInstance ); // Delete should not get called if there is no instance. 00036 m_pInstance = NULL; 00037 }; 00038 00039 public: 00040 static _T& Instance() { 00041 if (!m_pInstance) { m_pInstance = new _T; } 00042 ROS_ASSERT( m_pInstance ); 00043 return ( *m_pInstance ); 00044 } 00045 00046 static _T* InstancePtr() { 00047 if (!m_pInstance) { m_pInstance = new _T; } 00048 ROS_ASSERT( m_pInstance ); 00049 return m_pInstance; 00050 } 00051 00052 }; 00053 00054 00055 template <class T> T* Singleton<T>::m_pInstance = NULL; 00056 00057 00058 } 00059 00060 00061 #endif /* SINGLETON_HPP_ */