SmartObjectPtr.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // "$Id: ComPtr.h 23677 2011-04-18 07:37:15Z wang_haifeng $"
  3. //
  4. // Copyright (c)1992-2011, ZheJiang Dahua Technology Stock CO.LTD.
  5. // All Rights Reserved.
  6. //
  7. // Description: 组件智能指针类
  8. // Revisions: Year-Month-Day SVN-Author Modification
  9. //
  10. #ifndef __INFRA_OBJECT_SMART_PTR_H__
  11. #define __INFRA_OBJECT_SMART_PTR_H__
  12. #include <stddef.h>
  13. #include "Infra/Assert.h"
  14. #include "Defs.h"
  15. #include "Detail/smartptr_detail.h"
  16. namespace Dahua {
  17. namespace Memory {
  18. /// 管理带引用计数的智能指针类模板
  19. template<class T>
  20. class TSmartObjectPtr
  21. {
  22. #if defined(_MSC_VER) && (_MSC_VER < 1300)
  23. public:
  24. #else
  25. template<class Y> friend class TSmartObjectPtr;
  26. #endif
  27. T* m_ptr;
  28. typedef TSmartObjectPtr<T> this_type;
  29. class null_type;
  30. public:
  31. typedef T element_type;
  32. typedef T value_type;
  33. typedef T* pointer;
  34. /// 空构造函数; 实现从 NULL 到 TSmartObjectPtr 的隐式转换
  35. /// \param [in] ptr 当且仅当ptr为NULL时, 该构造函数才会被调用
  36. TSmartObjectPtr(null_type* ptr = NULL)
  37. : m_ptr(NULL)
  38. {
  39. }
  40. /// 构造函数; 非空指针构造函数
  41. template<class Y> explicit TSmartObjectPtr(Y* ptr)
  42. : m_ptr(ptr)
  43. {
  44. }
  45. template<class Y> TSmartObjectPtr(TSmartObjectPtr<Y> const& rhs)
  46. : m_ptr(rhs.m_ptr)
  47. {
  48. addObjectRef(m_ptr);
  49. }
  50. TSmartObjectPtr(TSmartObjectPtr const& rhs)
  51. : m_ptr(rhs.m_ptr)
  52. {
  53. addObjectRef(m_ptr);
  54. }
  55. template<class Y> TSmartObjectPtr(TSmartObjectPtr<Y> const& rhs, Detail::static_cast_tag)
  56. : m_ptr(static_cast<T*>(rhs.m_ptr))
  57. {
  58. addObjectRef(m_ptr);
  59. }
  60. template<class Y> TSmartObjectPtr(TSmartObjectPtr<Y> const& rhs, Detail::const_cast_tag)
  61. : m_ptr(const_cast<T*>(rhs.m_ptr))
  62. {
  63. addObjectRef(m_ptr);
  64. }
  65. template<class Y> TSmartObjectPtr(TSmartObjectPtr<Y> const& rhs, Detail::dynamic_cast_tag)
  66. : m_ptr(dynamic_cast<T*>(rhs.m_ptr))
  67. {
  68. addObjectRef(m_ptr);
  69. }
  70. ~TSmartObjectPtr()
  71. {
  72. releaseObject(m_ptr);
  73. }
  74. void swap(TSmartObjectPtr& rhs)
  75. {
  76. if (&rhs != this)
  77. {
  78. element_type* p = rhs.m_ptr;
  79. rhs.m_ptr = m_ptr;
  80. m_ptr = p;
  81. }
  82. }
  83. void reset()
  84. {
  85. this_type().swap(*this);
  86. }
  87. template<class Y> void reset(Y* ptr)
  88. {
  89. this_type(ptr).swap(*this);
  90. }
  91. template<class Y> void reset(TSmartObjectPtr<Y> const& rhs)
  92. {
  93. this_type(rhs).swap(*this);
  94. }
  95. TSmartObjectPtr& operator=(TSmartObjectPtr const& rhs)
  96. {
  97. if (&rhs != this) this_type(rhs).swap(*this);
  98. return *this;
  99. }
  100. element_type* operator->() const
  101. {
  102. DAHUA_ASSERT(m_ptr != NULL);
  103. return m_ptr;
  104. }
  105. element_type& operator*() const
  106. {
  107. DAHUA_ASSERT(m_ptr != NULL);
  108. return *m_ptr;
  109. }
  110. element_type* get() const
  111. {
  112. return m_ptr;
  113. }
  114. // implicit conversion to "bool"
  115. typedef T* this_type::*unspecified_bool_type;
  116. operator unspecified_bool_type() const // never throws
  117. {
  118. return m_ptr == NULL ? 0 : &this_type::m_ptr;
  119. }
  120. bool operator!() const // never throws
  121. {
  122. return m_ptr == NULL;
  123. }
  124. }; // TSmartObjectPtr
  125. } // namespace Memory
  126. // 比较
  127. template<class T, class U> inline bool operator==(Memory::TSmartObjectPtr<T> const& a, Memory::TSmartObjectPtr<U> const& b)
  128. {
  129. return a.get() == b.get();
  130. }
  131. template<class T, class U> inline bool operator!=(Memory::TSmartObjectPtr<T> const& a, Memory::TSmartObjectPtr<U> const& b)
  132. {
  133. return a.get() != b.get();
  134. }
  135. template<class T, class U> inline bool operator<(Memory::TSmartObjectPtr<T> const& a, Memory::TSmartObjectPtr<U> const& b)
  136. {
  137. return a.get() < b.get();
  138. }
  139. template<class T, class U> inline bool operator<=(Memory::TSmartObjectPtr<T> const& a, Memory::TSmartObjectPtr<U> const& b)
  140. {
  141. return a.get() <= b.get();
  142. }
  143. template<class T, class U> inline bool operator>(Memory::TSmartObjectPtr<T> const& a, Memory::TSmartObjectPtr<U> const& b)
  144. {
  145. return a.get() > b.get();
  146. }
  147. template<class T, class U> inline bool operator>=(Memory::TSmartObjectPtr<T> const& a, Memory::TSmartObjectPtr<U> const& b)
  148. {
  149. return a.get() >= b.get();
  150. }
  151. // 交换
  152. template<class T> inline void swap(Memory::TSmartObjectPtr<T>& a, Memory::TSmartObjectPtr<T>& b)
  153. {
  154. a.swap(b);
  155. }
  156. // 转型
  157. template<class T, class U> Memory::TSmartObjectPtr<T> inline static_pointer_cast(Memory::TSmartObjectPtr<U> const& r)
  158. {
  159. return Memory::TSmartObjectPtr<T>(r, Memory::Detail::static_cast_tag());
  160. }
  161. template<class T, class U> Memory::TSmartObjectPtr<T> inline const_pointer_cast(Memory::TSmartObjectPtr<U> const& r)
  162. {
  163. return Memory::TSmartObjectPtr<T>(r, Memory::Detail::const_cast_tag());
  164. }
  165. template<class T, class U> Memory::TSmartObjectPtr<T> inline dynamic_pointer_cast(Memory::TSmartObjectPtr<U> const& r)
  166. {
  167. return Memory::TSmartObjectPtr<T>(r, Memory::Detail::dynamic_cast_tag());
  168. }
  169. // get_pointer(p) is a generic way to say p.get()
  170. template<class T> inline T * get_pointer(Memory::TSmartObjectPtr<T> const & p)
  171. {
  172. return p.get();
  173. }
  174. } // namespace Dahua
  175. #endif // __INFRA_OBJECT_SMART_PTR_H__