KaRL  3.0.0
ThreadSafeRefcounter.cpp
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 #ifndef _MADARA_THREADSAFE_REFCOUNTER_CPP_
3 #define _MADARA_THREADSAFE_REFCOUNTER_CPP_
4 
6 
8 template <typename T>
10  : ptr_ (0)
11 {
12 }
13 
15 template <typename T>
17  bool increase_count, bool manage)
18  : ptr_ (new Shim (ptr, manage))
19 {
20  if (increase_count)
21  increment ();
22 }
23 
25 template <typename T>
27  const ThreadSafeRefcounter &rhs)
28  : ptr_ (rhs.ptr_)
29 {
30  increment ();
31 }
32 
34 template <typename T>
36 {
37  decrement ();
38 }
39 
42 template <typename T>
43 void
45 {
46  decrement ();
47  ptr_ = new Shim (ptr);
48 }
49 
51 template <typename T>
52 void
54 {
55  if (this != &rhs)
56  {
57  decrement ();
58  ptr_ = rhs.ptr_;
59  increment ();
60  }
61 }
62 
64 template <typename T>
65 T *
67 {
68  T * result (0);
69 
70  if (ptr_)
71  {
72  result = ptr_->t_;
73  }
74 
75  return result;
76 }
77 
79 template <typename T>
80 const T *
82 {
83  const T * result (0);
84 
85  if (ptr_)
86  {
87  result = ptr_->t_;
88  }
89 
90  return result;
91 }
92 
94 template <typename T>
95 bool
97 {
98  return ptr_ && ptr_->refcount_ > 0;
99 }
100 
102 template <typename T>
103 T *
105 {
106  T * result (0);
107 
108  if (ptr_)
109  {
110  result = ptr_->t_;
111  }
112 
113  return result;
114 }
115 
117 template <typename T>
118 const T *
120 {
121  const T * result (0);
122 
123  if (ptr_)
124  {
125  result = ptr_->t_;
126  }
127 
128  return result;
129 }
130 
132 template <typename T>
133 T &
135 {
136  return *ptr_->t_;
137 }
138 
140 template <typename T>
141 const
142 T &
144 {
145  return *ptr_->t_;
146 }
147 
149 template <typename T>
150 T *
152 {
153  T * result (0);
154 
155  if (ptr_)
156  {
157  result = ptr_->t_;
158  }
159 
160  return result;
161 }
162 
164 template <typename T>
165 const T *
167 {
168  const T * result (0);
169 
170  if (ptr_)
171  {
172  result = ptr_->t_;
173  }
174 
175  return result;
176 }
177 
179 template <typename T>
180 void
182 {
183  if (ptr_ && ptr_->manage_)
184  {
185  MADARA_GUARD_TYPE guard (ptr_->mutex_);
186  ++ptr_->refcount_;
187  }
188 }
189 
191 template <typename T>
192 void
194 {
195  if (ptr_&& ptr_->manage_)
196  {
197  MADARA_GUARD_TYPE guard (ptr_->mutex_);
198  --ptr_->refcount_;
199  if (ptr_->refcount_ <= 0)
200  {
201  delete ptr_;
202  ptr_ = 0;
203  }
204  }
205 }
206 
207 template <typename T>
209  : t_ (t), refcount_ (1), manage_ (manage)
210 {
211 }
212 
213 template <typename T>
215 {
216  if (manage_)
217  delete t_;
218 }
219 
220 #endif /* _MADARA_THREADSAFE_REFCOUNTER_CPP_ */
This template class provides transparent reference counting of its template parameter T...
T * get_ptr(void)
get the underlying pointer
T * get(void)
get the underlying pointer
void operator=(T *ptr)
assignment operator for times when you don't want the reference increased for incoming ptr ...
T * operator->(void)
mimic pointer dereferencing
void increment(void)
implementation of the increment operation
virtual ~ThreadSafeRefcounter(void)
Dtor will delete pointer if refcount becomes 0.
A shim class that keeps track of the reference count and a pointer to the type T that's reference cou...
Shim(T *t, bool manage=true)
Constructor.
T & operator*(void)
dereference operator
bool is_valid(void) const
checks to see if the underlying pointer is valid
void decrement(void)
implementation of the decrement operation