HSmartPtr.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*****************************************************************************
  2. * HSmartPtr.h
  3. *****************************************************************************
  4. *
  5. * Project: HALCON/C++
  6. * Description: Template class for smart pointers used by tools and tuples
  7. *
  8. * (c) 2010-2020 by MVTec Software GmbH
  9. * www.mvtec.com
  10. *
  11. *****************************************************************************
  12. *
  13. *
  14. *****************************************************************************/
  15. #ifndef HCPP_SMART_PTR_H
  16. #define HCPP_SMART_PTR_H
  17. class LIntExport HSmartPtrRef
  18. {
  19. public:
  20. HSmartPtrRef();
  21. unsigned int GetCount() const {return mCount;}
  22. void addref() const;
  23. bool deref() const;
  24. protected:
  25. mutable unsigned int mCount;
  26. };
  27. namespace HalconCpp
  28. {
  29. template <class T> class HSmartPtr
  30. {
  31. public:
  32. HSmartPtr();
  33. HSmartPtr(T* ptr);
  34. HSmartPtr(HSmartPtr const & ptr);
  35. virtual ~HSmartPtr();
  36. virtual HSmartPtr& operator=(HSmartPtr const & ptr);
  37. virtual HSmartPtr& operator=(T* ptr);
  38. T* operator->() const;
  39. T& operator*() const;
  40. T* Ref() const;
  41. operator T&() const;
  42. bool operator!() const;
  43. operator bool() const;
  44. bool operator==(const HSmartPtr& ptr) const;
  45. bool operator==(const T* ptr) const;
  46. bool operator!=(const HSmartPtr& ptr) const;
  47. bool operator!=(const T* ptr) const;
  48. void Reset();
  49. protected:
  50. void deref();
  51. void addref();
  52. void reset(T* ptr=0);
  53. T* mPtr;
  54. };
  55. template <class T> HSmartPtr<T>::HSmartPtr() : mPtr(NULL) {}
  56. template <class T> HSmartPtr<T>::HSmartPtr(T* ptr) : mPtr(ptr)
  57. {
  58. addref();
  59. }
  60. template <class T> HSmartPtr<T>::HSmartPtr(HSmartPtr<T> const & ptr) : mPtr(ptr.mPtr)
  61. {
  62. addref();
  63. }
  64. template <class T> HSmartPtr<T>::~HSmartPtr()
  65. {
  66. deref();
  67. }
  68. template <class T> HSmartPtr<T>& HSmartPtr<T>::operator=(HSmartPtr<T> const & ptr)
  69. {
  70. if (mPtr != ptr.mPtr)
  71. reset(ptr.mPtr);
  72. return *this;
  73. }
  74. template <class T> HSmartPtr<T>& HSmartPtr<T>::operator=(T* ptr)
  75. {
  76. if (mPtr != ptr)
  77. reset(ptr);
  78. return *this;
  79. }
  80. template <class T> T* HSmartPtr<T>::operator->() const
  81. {
  82. return mPtr;
  83. }
  84. template <class T> T& HSmartPtr<T>::operator*() const
  85. {
  86. return *mPtr;
  87. }
  88. template <class T> T* HSmartPtr<T>::Ref() const
  89. {
  90. return mPtr;
  91. }
  92. template <class T> HSmartPtr<T>::operator T&() const
  93. {
  94. return *mPtr;
  95. }
  96. template <class T> bool HSmartPtr<T>::operator!() const
  97. {
  98. return mPtr == NULL;
  99. }
  100. template <class T> HSmartPtr<T>::operator bool() const
  101. {
  102. return mPtr != NULL;
  103. }
  104. template <class T> bool HSmartPtr<T>::operator==(const HSmartPtr<T>& ptr) const
  105. {
  106. return mPtr == ptr.mPtr;
  107. }
  108. template <class T> bool HSmartPtr<T>::operator==(const T* ptr) const
  109. {
  110. return mPtr == ptr;
  111. }
  112. template <class T> bool HSmartPtr<T>::operator!=(const HSmartPtr<T>& ptr) const
  113. {
  114. return !operator==(ptr);
  115. }
  116. template <class T> bool HSmartPtr<T>::operator!=(const T* ptr) const
  117. {
  118. return !operator==(ptr);
  119. }
  120. template <class T> void HSmartPtr<T>::Reset()
  121. {
  122. deref();
  123. }
  124. template <class T> void HSmartPtr<T>::deref()
  125. {
  126. if (mPtr)
  127. {
  128. /* It is neccessary to invalidate the smart pointer before releasing the
  129. * refered object for avoiding that within the update handlers of the
  130. * deleted object the smart pointer is used again.
  131. * This can happen, e.g., if this smart pointer is member of an array.
  132. * ------------------------------------------------------------------- */
  133. T* ptr = mPtr;
  134. mPtr = 0;
  135. if (ptr->deref())
  136. delete ptr;
  137. }
  138. }
  139. template <class T> void HSmartPtr<T>::addref()
  140. {
  141. if (mPtr)
  142. mPtr->addref();
  143. }
  144. template <class T> void HSmartPtr<T>::reset(T* ptr/*=0*/)
  145. {
  146. deref();
  147. mPtr = ptr;
  148. addref();
  149. }
  150. }
  151. #endif