PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. #ifndef TEST_SUITE_REFCOUNT_H__
  2. #define TEST_SUITE_REFCOUNT_H__
  3. struct RCObjBase {
  4. /*!
  5. Return the numbers of active references.
  6. \return The internal \c refCount value.
  7. */
  8. int ref_count() const
  9. {
  10. return refCount;
  11. }
  12. /*!
  13. Add one reference.
  14. \return The reference counter value.
  15. */
  16. int addref() const
  17. {
  18. return add_ref();
  19. }
  20. /*!
  21. Delete one reference. If the refCount is zero, the
  22. object is deleted.
  23. \return The reference counter value, which can be zero after
  24. deletion.
  25. */
  26. int delref() const
  27. {
  28. if (ref_count() == 0 || del_ref() == 0 )
  29. {
  30. delete this;
  31. return 0;
  32. }
  33. return ref_count();
  34. }
  35. protected:
  36. RCObjBase();
  37. RCObjBase(const RCObjBase& );
  38. virtual ~RCObjBase() = 0;
  39. private:
  40. RCObjBase& operator=(const RCObjBase& );
  41. friend struct RCObj;
  42. int add_ref() const
  43. {
  44. return ++refCount;
  45. }
  46. int del_ref() const
  47. {
  48. return --refCount;
  49. }
  50. mutable int refCount;
  51. };
  52. struct RCObj : virtual RCObjBase {
  53. protected:
  54. RCObj()
  55. {
  56. }
  57. };
  58. /*! Reference an RCObj object
  59. \return The input pointer \a r
  60. */
  61. template <class T>
  62. inline
  63. T* addref(T* r)
  64. {
  65. return (r && r->addref() ) ? r : 0;
  66. }
  67. /*! Unreference an RCObj object.
  68. \return The input pointer \a r or nil if the object was deleted.
  69. */
  70. template <class T>
  71. inline
  72. T* delref(T* r)
  73. {
  74. return ( r && r->delref() ) ? r : 0;
  75. }
  76. RCObjBase::RCObjBase()
  77. : refCount(0)
  78. {
  79. }
  80. RCObjBase::~RCObjBase()
  81. {
  82. }
  83. RCObjBase::RCObjBase(const RCObjBase&)
  84. : refCount(0)
  85. {
  86. }
  87. RCObjBase& RCObjBase::operator=(const RCObjBase&)
  88. {
  89. return *this;
  90. }
  91. template <class T>
  92. struct RCPtr {
  93. typedef T* pointer_type;
  94. typedef T& refernce_type;
  95. typedef T value_type;
  96. RCPtr();
  97. RCPtr(T* realPtr);
  98. RCPtr(const RCPtr& rhs);
  99. ~RCPtr();
  100. RCPtr& operator=(const RCPtr& rhs);
  101. RCPtr& operator=(T* realPtr);
  102. T* operator->() { return pointee; }
  103. T& operator*() { return *pointee; }
  104. const T* operator->() const { return pointee; }
  105. const T& operator*() const { return *pointee; }
  106. operator T*() { return pointee; }
  107. operator T&() { return *pointee; }
  108. operator const T*() const { return pointee; }
  109. operator const T&() const { return *pointee; }
  110. T* get() { return pointee; }
  111. T* get() const { return pointee; }
  112. private:
  113. T* pointee;
  114. };
  115. template <class T>
  116. inline
  117. RCPtr<T>::RCPtr()
  118. : pointee(0)
  119. {
  120. }
  121. template <class T>
  122. inline
  123. RCPtr<T>::RCPtr(T* realPtr)
  124. : pointee(realPtr)
  125. {
  126. addref(pointee);
  127. }
  128. template <class T>
  129. inline
  130. RCPtr<T>::RCPtr(const RCPtr& rhs)
  131. : pointee(rhs.pointee)
  132. {
  133. addref(pointee);
  134. }
  135. template <class T>
  136. inline
  137. RCPtr<T>::~RCPtr()
  138. {
  139. delref(pointee);
  140. }
  141. template <class T>
  142. inline
  143. RCPtr<T>& RCPtr<T>::operator=(const RCPtr& rhs)
  144. {
  145. if (pointee != rhs.pointee) {
  146. delref(pointee);
  147. pointee = rhs.pointee;
  148. addref(pointee);
  149. }
  150. return *this;
  151. }
  152. #endif //TEST_SUITE_REFCOUNT_H__