autoPtr.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _AUTOPTR_H_
  23. #define _AUTOPTR_H_
  24. #ifndef _TYPETRAITS_H_
  25. # include "platform/typetraits.h"
  26. #endif
  27. template<class T>
  28. struct AutoPtrRef
  29. {
  30. T* _ptr;
  31. AutoPtrRef(T *ptr)
  32. : _ptr(ptr)
  33. {}
  34. };
  35. /// A simple smart pointer.
  36. /// An extended version of std::auto_ptr which supports a deletion policy.
  37. /// The delete policy indicates how the ptr is to be deleted. DeleteSingle,
  38. /// the default, is used to delete individual objects. DeleteArray can
  39. /// can be used to delete arrays.
  40. /// <code>
  41. /// AutoPtr<Object> ptr(new Object);
  42. /// AutoPtr<Object,DeleteSingle> ptr(new Object);
  43. /// AutoPtr<Object,DeleteArray> ptr(new Object[10]);
  44. /// </code>
  45. /// AutoPtrs do not perform reference counting and assume total ownership
  46. /// of any object assigned to them. Assigning an AutoPtr to another transfers
  47. /// that ownership and resets the source AutoPtr to 0.
  48. template<class T, class P = DeleteSingle>
  49. class AutoPtr
  50. {
  51. public:
  52. typedef T ValueType;
  53. explicit AutoPtr(T *ptr = 0): _ptr(ptr) {}
  54. ~AutoPtr()
  55. {
  56. P::destroy(_ptr);
  57. }
  58. // Copy constructors
  59. AutoPtr(AutoPtr &rhs): _ptr(rhs.release()) {}
  60. template<class U>
  61. AutoPtr(AutoPtr<U,P> &rhs): _ptr(rhs.release()) { }
  62. /// Transfer ownership, any object currently be referenced is deleted and
  63. /// rhs is set to 0.
  64. AutoPtr& operator= (AutoPtr &rhs)
  65. {
  66. reset(rhs.release());
  67. return *this;
  68. }
  69. template<class U>
  70. AutoPtr& operator= (AutoPtr<U,P> &rhs)
  71. {
  72. reset(rhs.release());
  73. return *this;
  74. }
  75. // Access
  76. T* ptr() const { return _ptr; }
  77. T& operator*() const { return *_ptr; }
  78. T* operator->() const { return _ptr; }
  79. T& operator[](size_t index) { return (_ptr)[index]; }
  80. /// Release ownership of the object without deleting it.
  81. T* release()
  82. {
  83. T* tmp(_ptr);
  84. _ptr = 0;
  85. return tmp;
  86. }
  87. /// Equivalent to *this = (T*)ptr, except that operator=(T*) isn't provided for.
  88. void reset(T* ptr = 0)
  89. {
  90. if (_ptr != ptr)
  91. {
  92. P::destroy(_ptr);
  93. _ptr = ptr;
  94. }
  95. }
  96. // Conversion to/from ref type
  97. AutoPtr(AutoPtrRef<T> ref): _ptr(ref._ptr) {}
  98. AutoPtr& operator= (AutoPtrRef<T> ref)
  99. {
  100. reset(ref._ptr);
  101. return *this;
  102. }
  103. bool isNull() const { return _ptr == NULL; }
  104. bool isValid() const { return !isNull(); }
  105. template<class U>
  106. operator AutoPtrRef<U>() { return AutoPtrRef<U>(release()); }
  107. template<class U>
  108. operator AutoPtr<U,P>() { return AutoPtr<U,P>(release()); }
  109. private:
  110. T *_ptr;
  111. };
  112. #endif