MADARA  3.4.1
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 {
11 }
12 
14 template<typename T>
15 madara::utility::Refcounter<T>::Refcounter(T* ptr, bool increase_count)
16  : ptr_(new Shim(ptr))
17 {
18  if (increase_count)
19  increment();
20 }
21 
23 template<typename T>
25  : ptr_(rhs.ptr_)
26 {
27  increment();
28 }
29 
31 template<typename T>
33 {
34  decrement();
35 }
36 
39 template<typename T>
41 {
42  decrement();
43  ptr_ = new Shim(ptr);
44 }
45 
47 template<typename T>
49 {
50  if (this != &rhs)
51  {
52  decrement();
53  ptr_ = rhs.ptr_;
54  increment();
55  }
56 }
57 
59 template<typename T>
61 {
62  return ptr_->t_;
63 }
64 
66 template<typename T>
68 {
69  return ptr_->t_;
70 }
71 
73 template<typename T>
75 {
76  return ptr_->t_;
77 }
78 
80 template<typename T>
82 {
83  return ptr_->t_;
84 }
85 
87 template<typename T>
89 {
90  return *ptr_->t_;
91 }
92 
94 template<typename T>
96 {
97  return *ptr_->t_;
98 }
99 
101 template<typename T>
103 {
104  return ptr_->t_;
105 }
106 
108 template<typename T>
110 {
111  return ptr_->t_;
112 }
113 
115 template<typename T>
117 {
118  if (ptr_)
119  ++ptr_->refcount_;
120 }
121 
123 template<typename T>
125 {
126  if (ptr_)
127  {
128  --ptr_->refcount_;
129  if (ptr_->refcount_ <= 0)
130  {
131  delete ptr_;
132  ptr_ = 0;
133  }
134  }
135 }
136 
137 template<typename T>
139 {
140 }
141 
142 template<typename T>
144 {
145  delete t_;
146 }
147 
148 #endif /* _REFCOUNTER_CPP_ */
This template class provides transparent reference counting of its template parameter T.
Definition: Refcounter.h:22
T * get(void)
get the underlying pointer
Definition: Refcounter.cpp:74
T & operator*(void)
dereference operator
Definition: Refcounter.cpp:88
T * get_ptr(void)
get the underlying pointer
Definition: Refcounter.cpp:60
void decrement(void)
implementation of the decrement operation
Definition: Refcounter.cpp:124
void operator=(T *ptr)
assignment operator for times when you don't want the reference increased for incoming ptr
Definition: Refcounter.cpp:40
void increment(void)
implementation of the increment operation
Definition: Refcounter.cpp:116
Shim * ptr_
Pointer to the Shim.
Definition: Refcounter.h:92
T * operator->(void)
mimic pointer dereferencing
Definition: Refcounter.cpp:102
Refcounter(void)
default Ctor
Definition: Refcounter.cpp:9
A shim class that keeps track of the reference count and a pointer to the type T that's reference cou...
Definition: Refcounter.h:77