singleton.h
Go to the documentation of this file.
00001 
00006 #ifndef SINGLETON_HPP_
00007 #define SINGLETON_HPP_
00008 
00009 #include <boost/utility.hpp>
00010 #include <boost/thread/once.hpp>
00011 #include <boost/scoped_ptr.hpp>
00012 
00019 namespace Templates {
00020         template<class T>
00021         class Singleton : private boost::noncopyable
00022         {
00023         public:
00027                 static T& instance()
00028                 {
00029                 // Initialize object only once.
00030                         //std::cout<<std::endl<<"singleton instance called"<<std::endl<<std::endl;
00031                         boost::call_once(initSingleton, flag);
00032                         // Return reference to object.
00033                         return *t;
00034                 }
00035         protected:
00036                 Singleton() {}
00037                 //virtual ~Singleton() {}
00038                 ~Singleton() {}
00039         private:
00043                 static void initSingleton()
00044                 {
00045                 // Call constructor of the T object.
00046                         //std::cout<<std::endl<<"singleton init called"<<std::endl<<std::endl;
00047                         t.reset(new T());
00048                 }
00049                 static boost::scoped_ptr<T> t;
00050                 static boost::once_flag flag;
00051         };
00052 }
00056 template<class T> boost::scoped_ptr<T> Templates::Singleton<T>::t(0);
00057 
00061 template<class T> boost::once_flag Templates::Singleton<T>::flag = BOOST_ONCE_INIT;
00062 
00063 #endif /* SINGLETON_HPP_ */


proxy_common
Author(s): Ronny Hartanto
autogenerated on Mon Oct 6 2014 06:54:18