Singleton.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00020 #ifndef COIL_SINGLETON_H
00021 #define COIL_SINGLETON_H
00022 
00023 #include <coil/Mutex.h>
00024 #include <coil/Guard.h>
00025 
00026 namespace coil
00027 {
00105   template <class SingletonClass>
00106   class Singleton
00107   {
00108   public:
00109     typedef SingletonClass* SingletonClassPtr;
00110     typedef ::coil::Mutex Mutex;
00111 
00131     static SingletonClass& instance()
00132     {
00133 
00134       // DLC pattern
00135       if (!m_instance)
00136       {
00137         coil::Guard<coil::Mutex> guard(m_mutex);
00138         if (!m_instance)
00139           {
00140             m_instance = new SingletonClass();
00141           }
00142       }
00143       return *m_instance;
00144     }
00145 
00146   protected:
00162     Singleton(){};
00163 
00179     ~Singleton(){};
00180 
00181   private:
00182     Singleton(const Singleton& x);
00183     Singleton& operator=(const Singleton& x);
00184 
00185   protected:
00193     static coil::Mutex m_mutex;
00194 
00202     static SingletonClass* m_instance;
00203   };
00204 
00205   template <class SingletonClass>
00206   typename Singleton<SingletonClass>::SingletonClassPtr
00207   Singleton<SingletonClass>::m_instance;
00208   
00209   template <class SingletonClass>
00210   typename Singleton<SingletonClass>::Mutex
00211   Singleton<SingletonClass>::m_mutex;
00212 }; // namepsace coil
00213 
00214 #endif // COIL_SINGLETON_H


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Aug 27 2015 14:16:39