singleton.h
Go to the documentation of this file.
00001 
00023 #ifndef SINGLETON_H_
00024 #define SINGLETON_H_
00025 
00026 #include <iostream>
00027 #include <boost/smart_ptr.hpp>
00028 #include <boost/thread/mutex.hpp>
00029 #include <boost/thread/thread.hpp>
00030 #include <boost/function.hpp>
00031 #include <boost/bind.hpp>
00032 
00033 namespace micros_swarm{
00034     
00035     template<class T>
00036     class Singleton
00037     {
00038         public:
00039             static void makeSingleton(boost::shared_ptr<T>& existed_ptr)  //make an esisted ptr to be a singleton
00040             {
00041                 if(existed_ptr.use_count() == 0) {
00042                     std::cout<<"WRONG: using an empty ptr to get singleton, exit."<<std::endl;
00043                     exit(-1);
00044                 }
00045                 else {
00046                     singleton_mutex_.lock();
00047                     singleton_object_ = existed_ptr;
00048                     singleton_mutex_.unlock();
00049                 }
00050             }
00051 
00052             static boost::shared_ptr<T> getExistedSingleton()  //get the existed singleton. it is in pairs with makeSingleton()
00053             {
00054                 if(singleton_object_.use_count() == 0) {
00055                     std::cout<<"WRONG: try to get an not existed ptr, exit."<<std::endl;
00056                     exit(-1);
00057                 }
00058                 return singleton_object_;
00059             }
00060 
00061             static void deleteSingleton()  //delete the singleton
00062             {
00063                 if(singleton_object_.use_count() != 0) {
00064                     singleton_mutex_.lock();
00065                     singleton_object_.reset();
00066                     singleton_mutex_.unlock();
00067                 }
00068             }
00069 
00070             static boost::shared_ptr<T> getSingleton()  //none parameter contruction
00071             {
00072                 if(singleton_object_.use_count() == 0) {
00073                     singleton_mutex_.lock();
00074                     if(singleton_object_.use_count() == 0) {
00075                         singleton_object_ = boost::shared_ptr<T>(new T());
00076                     }
00077                     singleton_mutex_.unlock();
00078                 }
00079                 return singleton_object_;
00080             }
00081             
00082             template<class P1>
00083             static boost::shared_ptr<T> getSingleton(P1 p1)  //one parameter construction
00084             {
00085                 if(singleton_object_.use_count() == 0) {
00086                     singleton_mutex_.lock();
00087                     if(singleton_object_.use_count() == 0) {
00088                         singleton_object_ = boost::shared_ptr<T>(new T(p1));
00089                     }
00090                     singleton_mutex_.unlock();
00091                 }
00092                 return singleton_object_;
00093             }
00094             
00095             template<class P1, class P2>
00096             static boost::shared_ptr<T> getSingleton(P1 p1, P2 p2)  //two parameters construction
00097             {
00098                 if(singleton_object_.use_count() == 0) {
00099                     singleton_mutex_.lock();
00100                     if(singleton_object_.use_count() == 0) {
00101                         singleton_object_ = boost::shared_ptr<T>(new T(p1, p2));
00102                     }
00103                     singleton_mutex_.unlock();
00104                 }
00105                 return singleton_object_;
00106             }
00107             
00108             template<class P1, class P2, class P3>
00109             static boost::shared_ptr<T> getSingleton(P1 p1, P2 p2, P3 p3)  //three parameter construction
00110             {
00111                 if(singleton_object_.use_count() == 0) {
00112                     singleton_mutex_.lock();
00113                     if(singleton_object_.use_count() == 0) {
00114                         singleton_object_ = boost::shared_ptr<T>(new T(p1, p2, p3));
00115                     }
00116                     singleton_mutex_.unlock();
00117                 }
00118                 return singleton_object_;
00119             }
00120             
00121             static int use_count()
00122             {
00123                 return singleton_object_.use_count();
00124             }
00125             
00126             ~Singleton(){}
00127         private:
00128             Singleton(){}
00129         private:
00130             static boost::shared_ptr<T> singleton_object_;
00131             static boost::mutex singleton_mutex_;  //TODO, is this ok for multi thread???
00132     };
00133     
00134     template<class T>
00135     boost::shared_ptr<T> Singleton<T>::singleton_object_;
00136     
00137     template<class T>
00138     boost::mutex Singleton<T>::singleton_mutex_;
00139 };
00140     
00141 #endif
00142 


micros_swarm
Author(s):
autogenerated on Thu Jun 6 2019 18:52:14