Ptr.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // This code is in the public domain -- Ignacio Castaño <castano@gmail.com>
  2. #ifndef NV_CORE_PTR_H
  3. #define NV_CORE_PTR_H
  4. #include "nvcore.h"
  5. #include "Debug.h"
  6. #include "RefCounted.h"
  7. namespace nv
  8. {
  9. class WeakProxy;
  10. /** Simple auto pointer template class.
  11. *
  12. * This is very similar to the standard auto_ptr class, but with some
  13. * additional limitations to make its use less error prone:
  14. * - Copy constructor and assignment operator are disabled.
  15. * - reset method is removed.
  16. *
  17. * The semantics of the standard auto_ptr are not clear and change depending
  18. * on the std implementation. For a discussion of the problems of auto_ptr read:
  19. * http://www.awprofessional.com/content/images/020163371X/autoptrupdate\auto_ptr_update.html
  20. */
  21. template <class T>
  22. class AutoPtr
  23. {
  24. NV_FORBID_COPY(AutoPtr);
  25. NV_FORBID_HEAPALLOC();
  26. public:
  27. /// Ctor.
  28. AutoPtr(T * p = NULL) : m_ptr(p) { }
  29. template <class Q>
  30. AutoPtr(Q * p) : m_ptr(static_cast<T *>(p)) { }
  31. /// Dtor. Deletes owned pointer.
  32. ~AutoPtr() {
  33. delete m_ptr;
  34. m_ptr = NULL;
  35. }
  36. /// Delete owned pointer and assign new one.
  37. void operator=( T * p ) {
  38. if (p != m_ptr)
  39. {
  40. delete m_ptr;
  41. m_ptr = p;
  42. }
  43. }
  44. template <class Q>
  45. void operator=( Q * p ) {
  46. if (p != m_ptr)
  47. {
  48. delete m_ptr;
  49. m_ptr = static_cast<T *>(p);
  50. }
  51. }
  52. /// Member access.
  53. T * operator -> () const {
  54. nvDebugCheck(m_ptr != NULL);
  55. return m_ptr;
  56. }
  57. /// Get reference.
  58. T & operator*() const {
  59. nvDebugCheck(m_ptr != NULL);
  60. return *m_ptr;
  61. }
  62. /// Get pointer.
  63. T * ptr() const { return m_ptr; }
  64. /// Relinquish ownership of the underlying pointer and returns that pointer.
  65. T * release() {
  66. T * tmp = m_ptr;
  67. m_ptr = NULL;
  68. return tmp;
  69. }
  70. /// Const pointer equal comparation.
  71. friend bool operator == (const AutoPtr<T> & ap, const T * const p) {
  72. return (ap.ptr() == p);
  73. }
  74. /// Const pointer nequal comparation.
  75. friend bool operator != (const AutoPtr<T> & ap, const T * const p) {
  76. return (ap.ptr() != p);
  77. }
  78. /// Const pointer equal comparation.
  79. friend bool operator == (const T * const p, const AutoPtr<T> & ap) {
  80. return (ap.ptr() == p);
  81. }
  82. /// Const pointer nequal comparation.
  83. friend bool operator != (const T * const p, const AutoPtr<T> & ap) {
  84. return (ap.ptr() != p);
  85. }
  86. private:
  87. T * m_ptr;
  88. };
  89. /// Smart pointer template class.
  90. template <class BaseClass>
  91. class SmartPtr {
  92. public:
  93. // BaseClass must implement addRef() and release().
  94. typedef SmartPtr<BaseClass> ThisType;
  95. /// Default ctor.
  96. SmartPtr() : m_ptr(NULL)
  97. {
  98. }
  99. /// Other type assignment.
  100. template <class OtherBase>
  101. SmartPtr( const SmartPtr<OtherBase> & tc )
  102. {
  103. m_ptr = static_cast<BaseClass *>( tc.ptr() );
  104. if (m_ptr) {
  105. m_ptr->addRef();
  106. }
  107. }
  108. /// Copy ctor.
  109. SmartPtr( const ThisType & bc )
  110. {
  111. m_ptr = bc.ptr();
  112. if (m_ptr) {
  113. m_ptr->addRef();
  114. }
  115. }
  116. /// Copy cast ctor. SmartPtr(NULL) is valid.
  117. explicit SmartPtr( BaseClass * bc )
  118. {
  119. m_ptr = bc;
  120. if (m_ptr) {
  121. m_ptr->addRef();
  122. }
  123. }
  124. /// Dtor.
  125. ~SmartPtr()
  126. {
  127. set(NULL);
  128. }
  129. /// -> operator.
  130. BaseClass * operator -> () const
  131. {
  132. nvCheck( m_ptr != NULL );
  133. return m_ptr;
  134. }
  135. /// * operator.
  136. BaseClass & operator*() const
  137. {
  138. nvCheck( m_ptr != NULL );
  139. return *m_ptr;
  140. }
  141. /// Get pointer.
  142. BaseClass * ptr() const
  143. {
  144. return m_ptr;
  145. }
  146. /// Other type assignment.
  147. template <class OtherBase>
  148. void operator = ( const SmartPtr<OtherBase> & tc )
  149. {
  150. set( static_cast<BaseClass *>(tc.ptr()) );
  151. }
  152. /// This type assignment.
  153. void operator = ( const ThisType & bc )
  154. {
  155. set( bc.ptr() );
  156. }
  157. /// Pointer assignment.
  158. void operator = ( BaseClass * bc )
  159. {
  160. set( bc );
  161. }
  162. /// Other type equal comparation.
  163. template <class OtherBase>
  164. bool operator == ( const SmartPtr<OtherBase> & other ) const
  165. {
  166. return m_ptr == other.ptr();
  167. }
  168. /// This type equal comparation.
  169. bool operator == ( const ThisType & bc ) const
  170. {
  171. return m_ptr == bc.ptr();
  172. }
  173. /// Const pointer equal comparation.
  174. bool operator == ( const BaseClass * const bc ) const
  175. {
  176. return m_ptr == bc;
  177. }
  178. /// Other type not equal comparation.
  179. template <class OtherBase>
  180. bool operator != ( const SmartPtr<OtherBase> & other ) const
  181. {
  182. return m_ptr != other.ptr();
  183. }
  184. /// Other type not equal comparation.
  185. bool operator != ( const ThisType & bc ) const
  186. {
  187. return m_ptr != bc.ptr();
  188. }
  189. /// Const pointer not equal comparation.
  190. bool operator != (const BaseClass * const bc) const
  191. {
  192. return m_ptr != bc;
  193. }
  194. /// This type lower than comparation.
  195. bool operator < (const ThisType & p) const
  196. {
  197. return m_ptr < p.ptr();
  198. }
  199. bool isValid() const {
  200. return isValidPtr(m_ptr);
  201. }
  202. private:
  203. // Set this pointer.
  204. void set( BaseClass * p )
  205. {
  206. if (p) p->addRef();
  207. if (m_ptr) m_ptr->release();
  208. m_ptr = p;
  209. }
  210. private:
  211. BaseClass * m_ptr;
  212. };
  213. /// Smart pointer template class.
  214. template <class T>
  215. class WeakPtr {
  216. public:
  217. WeakPtr() {}
  218. WeakPtr(T * p) { operator=(p); }
  219. WeakPtr(const SmartPtr<T> & p) { operator=(p.ptr()); }
  220. // Default constructor and assignment from weak_ptr<T> are OK.
  221. void operator=(T * p)
  222. {
  223. if (p) {
  224. m_proxy = p->getWeakProxy();
  225. nvDebugCheck(m_proxy != NULL);
  226. nvDebugCheck(m_proxy->ptr() == p);
  227. }
  228. else {
  229. m_proxy = NULL;
  230. }
  231. }
  232. void operator=(const SmartPtr<T> & ptr) { operator=(ptr.ptr()); }
  233. bool operator==(const SmartPtr<T> & p) const { return ptr() == p.ptr(); }
  234. bool operator!=(const SmartPtr<T> & p) const { return ptr() != p.ptr(); }
  235. bool operator==(const WeakPtr<T> & p) const { return ptr() == p.ptr(); }
  236. bool operator!=(const WeakPtr<T> & p) const { return ptr() != p.ptr(); }
  237. bool operator==(T * p) const { return ptr() == p; }
  238. bool operator!=(T * p) const { return ptr() != p; }
  239. T * operator->() const
  240. {
  241. T * p = ptr();
  242. nvDebugCheck(p != NULL);
  243. return p;
  244. }
  245. T * ptr() const
  246. {
  247. if (m_proxy != NULL) {
  248. return static_cast<T *>(m_proxy->ptr());
  249. }
  250. return NULL;
  251. }
  252. private:
  253. mutable SmartPtr<WeakProxy> m_proxy;
  254. };
  255. } // nv namespace
  256. #endif // NV_CORE_PTR_H