sharedptr.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: sharedptr.h
  3. // Purpose: interface of wxSharedPtr<T>
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. A smart pointer with non-intrusive reference counting.
  9. It is modelled after @c boost::shared_ptr<> and can be used with STL
  10. containers and wxVector<T> unlike @c std::auto_ptr<> and wxScopedPtr<T>.
  11. @library{wxbase}
  12. @category{smartpointers}
  13. @see wxScopedPtr<T>, wxWeakRef<T>, wxObjectDataPtr<T>
  14. */
  15. template<typename T>
  16. class wxSharedPtr<T>
  17. {
  18. public:
  19. /**
  20. Constructor.
  21. Creates shared pointer from the raw pointer @a ptr and takes ownership
  22. of it.
  23. */
  24. wxEXPLICIT wxSharedPtr(T* ptr = NULL);
  25. /**
  26. Constructor.
  27. Creates shared pointer from the raw pointer @a ptr and deleter @a d
  28. and takes ownership of it.
  29. @param ptr The raw pointer.
  30. @param d Deleter - a functor that is called instead of delete to
  31. free the @a ptr raw pointer when its reference count drops to
  32. zero.
  33. @since 3.0
  34. */
  35. template<typename Deleter>
  36. wxEXPLICIT wxSharedPtr(T* ptr, Deleter d);
  37. /**
  38. Copy constructor.
  39. */
  40. wxSharedPtr(const wxSharedPtr<T>& tocopy);
  41. /**
  42. Destructor.
  43. */
  44. ~wxSharedPtr();
  45. /**
  46. Returns pointer to its object or @NULL.
  47. */
  48. T* get() const;
  49. /**
  50. Conversion to a boolean expression (in a variant which is not
  51. convertible to anything but a boolean expression).
  52. If this class contains a valid pointer it will return @true, if it contains
  53. a @NULL pointer it will return @false.
  54. */
  55. operator unspecified_bool_type() const;
  56. /**
  57. Returns a reference to the object.
  58. If the internal pointer is @NULL this method will cause an assert in debug mode.
  59. */
  60. T operator*() const;
  61. /**
  62. Smart pointer member access. Returns pointer to its object.
  63. If the internal pointer is @NULL this method will cause an assert in debug mode.
  64. */
  65. T* operator->() const;
  66. /**
  67. Assignment operator.
  68. Releases any previously held pointer and creates a reference to @a ptr.
  69. */
  70. wxSharedPtr<T>& operator=(T* ptr);
  71. /**
  72. Assignment operator.
  73. Releases any previously held pointer and creates a reference to the
  74. same object as @a topcopy.
  75. */
  76. wxSharedPtr<T>& operator=(const wxSharedPtr<T>& tocopy);
  77. /**
  78. Reset pointer to @a ptr.
  79. If the reference count of the previously owned pointer was 1 it will be deleted.
  80. */
  81. void reset(T* ptr = NULL);
  82. /**
  83. Reset pointer to @a ptr.
  84. If the reference count of the previously owned pointer was 1 it will be deleted.
  85. @param ptr The new raw pointer.
  86. @param d Deleter - a functor that is called instead of delete to
  87. free the @a ptr raw pointer when its reference count drops to
  88. zero.
  89. @since 3.0
  90. */
  91. template<typename Deleter>
  92. void reset(T* ptr, Deleter d);
  93. /**
  94. Returns @true if this is the only pointer pointing to its object.
  95. */
  96. bool unique() const;
  97. /**
  98. Returns the number of pointers pointing to its object.
  99. */
  100. long use_count() const;
  101. };