singleton.h
Go to the documentation of this file.
1 
23 #ifndef SINGLETON_H_
24 #define SINGLETON_H_
25 
26 #include <iostream>
27 #include <boost/smart_ptr.hpp>
28 #include <boost/thread/mutex.hpp>
29 #include <boost/thread/thread.hpp>
30 #include <boost/function.hpp>
31 #include <boost/bind.hpp>
32 
33 namespace micros_swarm{
34 
35  template<class T>
36  class Singleton
37  {
38  public:
39  static void makeSingleton(boost::shared_ptr<T>& existed_ptr) //make an esisted ptr to be a singleton
40  {
41  if(existed_ptr.use_count() == 0) {
42  std::cout<<"WRONG: using an empty ptr to get singleton, exit."<<std::endl;
43  exit(-1);
44  }
45  else {
46  singleton_mutex_.lock();
47  singleton_object_ = existed_ptr;
48  singleton_mutex_.unlock();
49  }
50  }
51 
52  static boost::shared_ptr<T> getExistedSingleton() //get the existed singleton. it is in pairs with makeSingleton()
53  {
54  if(singleton_object_.use_count() == 0) {
55  std::cout<<"WRONG: try to get an not existed ptr, exit."<<std::endl;
56  exit(-1);
57  }
58  return singleton_object_;
59  }
60 
61  static void deleteSingleton() //delete the singleton
62  {
63  if(singleton_object_.use_count() != 0) {
64  singleton_mutex_.lock();
65  singleton_object_.reset();
66  singleton_mutex_.unlock();
67  }
68  }
69 
70  static boost::shared_ptr<T> getSingleton() //none parameter contruction
71  {
72  if(singleton_object_.use_count() == 0) {
73  singleton_mutex_.lock();
74  if(singleton_object_.use_count() == 0) {
76  }
77  singleton_mutex_.unlock();
78  }
79  return singleton_object_;
80  }
81 
82  template<class P1>
83  static boost::shared_ptr<T> getSingleton(P1 p1) //one parameter construction
84  {
85  if(singleton_object_.use_count() == 0) {
86  singleton_mutex_.lock();
87  if(singleton_object_.use_count() == 0) {
89  }
90  singleton_mutex_.unlock();
91  }
92  return singleton_object_;
93  }
94 
95  template<class P1, class P2>
96  static boost::shared_ptr<T> getSingleton(P1 p1, P2 p2) //two parameters construction
97  {
98  if(singleton_object_.use_count() == 0) {
99  singleton_mutex_.lock();
100  if(singleton_object_.use_count() == 0) {
101  singleton_object_ = boost::shared_ptr<T>(new T(p1, p2));
102  }
103  singleton_mutex_.unlock();
104  }
105  return singleton_object_;
106  }
107 
108  template<class P1, class P2, class P3>
109  static boost::shared_ptr<T> getSingleton(P1 p1, P2 p2, P3 p3) //three parameter construction
110  {
111  if(singleton_object_.use_count() == 0) {
112  singleton_mutex_.lock();
113  if(singleton_object_.use_count() == 0) {
114  singleton_object_ = boost::shared_ptr<T>(new T(p1, p2, p3));
115  }
116  singleton_mutex_.unlock();
117  }
118  return singleton_object_;
119  }
120 
121  static int use_count()
122  {
123  return singleton_object_.use_count();
124  }
125 
127  private:
129  private:
131  static boost::mutex singleton_mutex_; //TODO, is this ok for multi thread???
132  };
133 
134  template<class T>
136 
137  template<class T>
138  boost::mutex Singleton<T>::singleton_mutex_;
139 };
140 
141 #endif
142 
static boost::mutex singleton_mutex_
Definition: singleton.h:131
static boost::shared_ptr< T > singleton_object_
Definition: singleton.h:130
static boost::shared_ptr< T > getSingleton(P1 p1, P2 p2, P3 p3)
Definition: singleton.h:109
static int use_count()
Definition: singleton.h:121
static boost::shared_ptr< T > getSingleton()
Definition: singleton.h:70
static void deleteSingleton()
Definition: singleton.h:61
static boost::shared_ptr< T > getExistedSingleton()
Definition: singleton.h:52
static void makeSingleton(boost::shared_ptr< T > &existed_ptr)
Definition: singleton.h:39
static boost::shared_ptr< T > getSingleton(P1 p1)
Definition: singleton.h:83
static boost::shared_ptr< T > getSingleton(P1 p1, P2 p2)
Definition: singleton.h:96


micros_swarm
Author(s):
autogenerated on Mon Jun 10 2019 14:02:06