PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/test-suite/refcount.h

#
C++ Header | 198 lines | 132 code | 43 blank | 23 comment | 8 complexity | 00a438ce81417955c9262eb9dabb7559 MD5 | raw file
  1#ifndef TEST_SUITE_REFCOUNT_H__
  2#define TEST_SUITE_REFCOUNT_H__
  3
  4struct RCObjBase  {
  5  /*!
  6    Return  the numbers of active references.
  7
  8    \return The internal \c refCount value.
  9  */
 10  int ref_count() const
 11  {
 12    return refCount;
 13  }
 14
 15  /*!
 16    Add one reference.
 17
 18    \return The reference counter value.
 19  */
 20  int addref() const
 21  {
 22    return  add_ref();
 23  }
 24    
 25  /*!
 26    Delete one reference. If the refCount is zero, the
 27    object is deleted. 
 28
 29    \return The reference counter value, which can be zero after
 30    deletion.
 31  */
 32  int delref() const
 33  {
 34    if (ref_count() == 0 || del_ref() == 0 )
 35      {
 36	delete this;
 37	return 0;
 38      } 
 39    return ref_count();
 40  }
 41    
 42protected:
 43  RCObjBase();
 44  RCObjBase(const RCObjBase& );
 45  virtual ~RCObjBase() = 0;
 46
 47private:
 48
 49  RCObjBase& operator=(const RCObjBase& );
 50
 51  friend struct RCObj;
 52
 53  int add_ref() const
 54  {
 55    return ++refCount;
 56  }
 57    
 58  int del_ref() const
 59  {
 60    return --refCount;
 61  }
 62    
 63  mutable int refCount;
 64};
 65
 66struct RCObj : virtual RCObjBase {
 67protected:
 68  RCObj()
 69  {
 70  }
 71};
 72
 73/*! Reference an RCObj object
 74  \return The input pointer \a r
 75*/
 76template <class T>
 77inline
 78T* addref(T* r)
 79{ 
 80  return (r && r->addref() ) ? r : 0;
 81}
 82  
 83/*! Unreference an RCObj object.
 84  \return The input pointer \a r or nil if the object was deleted.
 85*/
 86template <class T>
 87inline
 88T* delref(T* r)
 89{ 
 90  return ( r && r->delref() ) ? r : 0;
 91}
 92
 93
 94
 95RCObjBase::RCObjBase()
 96  : refCount(0)
 97{
 98}
 99
100
101RCObjBase::~RCObjBase()
102{
103}
104
105RCObjBase::RCObjBase(const RCObjBase&)
106  : refCount(0)
107{
108}
109
110
111RCObjBase& RCObjBase::operator=(const RCObjBase&)
112{
113  return *this;
114}
115
116
117template <class T>                     
118struct RCPtr  {
119  typedef T* pointer_type;
120  typedef T& refernce_type;
121  typedef T  value_type;  
122  
123  RCPtr();
124  RCPtr(T* realPtr);
125  RCPtr(const RCPtr& rhs);
126
127  ~RCPtr();
128
129  RCPtr& operator=(const RCPtr& rhs);
130
131  RCPtr& operator=(T* realPtr);
132  
133  T* operator->() { return pointee; }
134  T& operator*() { return *pointee; }
135
136  const T* operator->() const { return pointee; }
137  const T& operator*() const { return *pointee; }
138
139  operator T*() { return pointee; }
140  operator T&() { return *pointee; }  
141
142  operator const T*() const { return pointee; }
143  operator const T&() const { return *pointee; }  
144
145  T* get() { return pointee; }
146  T* get() const { return pointee; }
147
148    
149private:
150  T* pointee;
151};
152
153
154template <class T>
155inline
156RCPtr<T>::RCPtr()
157  : pointee(0)
158{
159}
160
161template <class T>
162inline
163RCPtr<T>::RCPtr(T* realPtr)
164  : pointee(realPtr)
165{
166  addref(pointee);
167}
168
169template <class T>
170inline
171RCPtr<T>::RCPtr(const RCPtr& rhs)
172  : pointee(rhs.pointee)
173{
174  addref(pointee);
175}
176
177template <class T>
178inline
179RCPtr<T>::~RCPtr()
180{
181  delref(pointee);
182}
183
184template <class T>
185inline
186RCPtr<T>& RCPtr<T>::operator=(const RCPtr& rhs)
187{
188  if (pointee != rhs.pointee) {
189    delref(pointee);
190    pointee = rhs.pointee;
191    addref(pointee);
192  }
193  return *this;
194}
195
196
197
198#endif //TEST_SUITE_REFCOUNT_H__