aboutsummaryrefslogtreecommitdiff
path: root/Examples/test-suite/refcount.h
blob: 7d07e7242b91f89cc22ee990eda1d243e2d4345e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#ifndef TEST_SUITE_REFCOUNT_H__
#define TEST_SUITE_REFCOUNT_H__

struct RCObjBase  {
  /*!
    Return  the numbers of active references.

    \return The internal \c refCount value.
  */
  int ref_count() const
  {
    return refCount;
  }

  /*!
    Add one reference.

    \return The reference counter value.
  */
  int addref() const
  {
    return  add_ref();
  }
    
  /*!
    Delete one reference. If the refCount is zero, the
    object is deleted. 

    \return The reference counter value, which can be zero after
    deletion.
  */
  int delref() const
  {
    if (ref_count() == 0 || del_ref() == 0 )
      {
	delete this;
	return 0;
      } 
    return ref_count();
  }
    
protected:
  RCObjBase();
  RCObjBase(const RCObjBase& );
  virtual ~RCObjBase() = 0;

private:

  RCObjBase& operator=(const RCObjBase& );

  friend struct RCObj;

  int add_ref() const
  {
    return ++refCount;
  }
    
  int del_ref() const
  {
    return --refCount;
  }
    
  mutable int refCount;
};

struct RCObj : virtual RCObjBase {
protected:
  RCObj()
  {
  }
};

/*! Reference an RCObj object
  \return The input pointer \a r
*/
template <class T>
inline
T* addref(T* r)
{ 
  return (r && r->addref() ) ? r : 0;
}
  
/*! Unreference an RCObj object.
  \return The input pointer \a r or nil if the object was deleted.
*/
template <class T>
inline
T* delref(T* r)
{ 
  return ( r && r->delref() ) ? r : 0;
}



RCObjBase::RCObjBase()
  : refCount(0)
{
}


RCObjBase::~RCObjBase()
{
}

RCObjBase::RCObjBase(const RCObjBase&)
  : refCount(0)
{
}


RCObjBase& RCObjBase::operator=(const RCObjBase&)
{
  return *this;
}


template <class T>                     
struct RCPtr  {
  typedef T* pointer_type;
  typedef T& refernce_type;
  typedef T  value_type;  
  
  RCPtr();
  RCPtr(T* realPtr);
  RCPtr(const RCPtr& rhs);

  ~RCPtr();

  RCPtr& operator=(const RCPtr& rhs);

  RCPtr& operator=(T* realPtr);
  
  T* operator->() { return pointee; }
  T& operator*() { return *pointee; }

  const T* operator->() const { return pointee; }
  const T& operator*() const { return *pointee; }

  operator T*() { return pointee; }
  operator T&() { return *pointee; }  

  operator const T*() const { return pointee; }
  operator const T&() const { return *pointee; }  

  T* get() { return pointee; }
  T* get() const { return pointee; }

    
private:
  T* pointee;
};


template <class T>
inline
RCPtr<T>::RCPtr()
  : pointee(0)
{
}

template <class T>
inline
RCPtr<T>::RCPtr(T* realPtr)
  : pointee(realPtr)
{
  addref(pointee);
}

template <class T>
inline
RCPtr<T>::RCPtr(const RCPtr& rhs)
  : pointee(rhs.pointee)
{
  addref(pointee);
}

template <class T>
inline
RCPtr<T>::~RCPtr()
{
  delref(pointee);
}

template <class T>
inline
RCPtr<T>& RCPtr<T>::operator=(const RCPtr& rhs)
{
  if (pointee != rhs.pointee) {
    delref(pointee);
    pointee = rhs.pointee;
    addref(pointee);
  }
  return *this;
}



#endif //TEST_SUITE_REFCOUNT_H__