MADARA  3.2.3
ScopedArray.inl
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 #ifndef _SCOPED_ARRAY_CPP_
3 #define _SCOPED_ARRAY_CPP_
4 
6 
8 template <typename T>
10  : ptr_ (0)
11 {
12 }
13 
15 template <typename T>
16 madara::utility::ScopedArray<T>::ScopedArray (T *ptr, bool increase_count)
17  : ptr_ (ptr ? new Shim (ptr) : 0)
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 
46  if (ptr)
47  ptr_ = new Shim (ptr);
48  else
49  ptr_ = 0;
50 }
51 
53 template <typename T>
54 void
56 {
57  if (this != &rhs)
58  {
59  decrement ();
60  ptr_ = rhs.ptr_;
61  increment ();
62  }
63 }
64 
66 template <typename T>
67 T *
69 {
70  return ptr_->t_;
71 }
72 
74 template <typename T>
75 const T *
77 {
78  return ptr_->t_;
79 }
80 
82 template <typename T>
83 T *
85 {
86  return ptr_->t_;
87 }
88 
90 template <typename T>
91 const T *
93 {
94  return ptr_->t_;
95 }
96 
97 
99 template <typename T>
100 void
102 {
103  if (ptr_)
104  ++ptr_->refcount_;
105 }
106 
108 template <typename T>
109 void
111 {
112  if (ptr_)
113  {
114  --ptr_->refcount_;
115  if (ptr_->refcount_ <= 0)
116  {
117  delete ptr_;
118  ptr_ = 0;
119  }
120  }
121 }
122 
123 template <typename T>
125  : t_ (t), refcount_ (1)
126 {
127 }
128 
129 template <typename T>
131 {
132  delete [] t_;
133 }
134 
135 #endif /* _REFCOUNTER_CPP_ */
virtual ~ScopedArray(void)
Dtor will delete pointer if refcount becomes 0.
Definition: ScopedArray.inl:33
T * t_
Pointer to the object that&#39;s being reference counted.
Definition: ScopedArray.h:74
Shim * ptr_
Pointer to the Shim.
Definition: ScopedArray.h:81
void increment(void)
implementation of the increment operation
T * get(void)
get the underlying pointer
Definition: ScopedArray.inl:84
ScopedArray(void)
default Ctor
Definition: ScopedArray.inl:9
void decrement(void)
implementation of the decrement operation
int refcount_
Current value of the reference count.
Definition: ScopedArray.h:77
T * get_ptr(void)
get the underlying pointer
Definition: ScopedArray.inl:68
void operator=(T *ptr)
assignment operator for times when you don&#39;t want the reference increased for incoming ptr ...
Definition: ScopedArray.inl:42
A shim class that keeps track of the reference count and a pointer to the type T that&#39;s reference cou...
Definition: ScopedArray.h:65
This template class provides transparent reference counting of its template parameter T...
Definition: ScopedArray.h:22