MADARA  3.2.3
Refcounter.cpp
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 #ifndef _REFCOUNTER_CPP_
3 #define _REFCOUNTER_CPP_
4 
6 
8 template <typename T>
10  : ptr_ (0)
11 {
12 }
13 
15 template <typename T>
16 madara::utility::Refcounter<T>::Refcounter (T *ptr, bool increase_count)
17  : ptr_ (new Shim (ptr))
18 {
19  if (increase_count)
20  increment ();
21 }
22 
24 template <typename T>
26  : ptr_ (rhs.ptr_)
27 {
28  increment ();
29 }
30 
32 template <typename T>
34 {
35  decrement ();
36 }
37 
40 template <typename T>
41 void
43 {
44  decrement ();
45  ptr_ = new Shim (ptr);
46 }
47 
49 template <typename T>
50 void
52 {
53  if (this != &rhs)
54  {
55  decrement ();
56  ptr_ = rhs.ptr_;
57  increment ();
58  }
59 }
60 
62 template <typename T>
63 T *
65 {
66  return ptr_->t_;
67 }
68 
70 template <typename T>
71 const T *
73 {
74  return ptr_->t_;
75 }
76 
78 template <typename T>
79 T *
81 {
82  return ptr_->t_;
83 }
84 
86 template <typename T>
87 const T *
89 {
90  return ptr_->t_;
91 }
92 
93 
95 template <typename T>
96 T &
98 {
99  return *ptr_->t_;
100 }
101 
103 template <typename T>
104 const
105 T &
107 {
108  return *ptr_->t_;
109 }
110 
112 template <typename T>
113 T *
115 {
116  return ptr_->t_;
117 }
118 
120 template <typename T>
121 const T *
123 {
124  return ptr_->t_;
125 }
126 
128 template <typename T>
129 void
131 {
132  if (ptr_)
133  ++ptr_->refcount_;
134 }
135 
137 template <typename T>
138 void
140 {
141  if (ptr_)
142  {
143  --ptr_->refcount_;
144  if (ptr_->refcount_ <= 0)
145  {
146  delete ptr_;
147  ptr_ = 0;
148  }
149  }
150 }
151 
152 template <typename T>
154  : t_ (t), refcount_ (1)
155 {
156 }
157 
158 template <typename T>
160 {
161  delete t_;
162 }
163 
164 #endif /* _REFCOUNTER_CPP_ */
T & operator*(void)
dereference operator
Definition: Refcounter.cpp:97
T * operator->(void)
mimic pointer dereferencing
Definition: Refcounter.cpp:114
A shim class that keeps track of the reference count and a pointer to the type T that&#39;s reference cou...
Definition: Refcounter.h:76
Shim * ptr_
Pointer to the Shim.
Definition: Refcounter.h:92
T * t_
Pointer to the object that&#39;s being reference counted.
Definition: Refcounter.h:85
T * get_ptr(void)
get the underlying pointer
Definition: Refcounter.cpp:64
virtual ~Refcounter(void)
Dtor will delete pointer if refcount becomes 0.
Definition: Refcounter.cpp:33
T * get(void)
get the underlying pointer
Definition: Refcounter.cpp:80
void decrement(void)
implementation of the decrement operation
Definition: Refcounter.cpp:139
void increment(void)
implementation of the increment operation
Definition: Refcounter.cpp:130
This template class provides transparent reference counting of its template parameter T...
Definition: IteratorImpl.h:17
Refcounter(void)
default Ctor
Definition: Refcounter.cpp:9
int refcount_
Current value of the reference count.
Definition: Refcounter.h:88
void operator=(T *ptr)
assignment operator for times when you don&#39;t want the reference increased for incoming ptr ...
Definition: Refcounter.cpp:42