weakref.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: weakref.h
  3. // Purpose: interface of wxWeakRefDynamic<T>, wxWeakRef<T>
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. wxWeakRefDynamic<T> is a template class for weak references that is used in
  9. the same way as wxWeakRef<T>. The only difference is that wxWeakRefDynamic
  10. defaults to using @c dynamic_cast for establishing the object reference
  11. (while wxWeakRef defaults to @c static_cast).
  12. So, wxWeakRef will detect a type mismatch during compile time and will
  13. have a little better run-time performance. The role of wxWeakRefDynamic
  14. is to handle objects which derived type one does not know.
  15. @note wxWeakRef<T> selects an implementation based on the static type of T.
  16. If T does not have wxTrackable statically, it defaults to a mixed-
  17. mode operation, where it uses @c dynamic_cast as the last measure
  18. (if available from the compiler and enabled when building wxWidgets).
  19. For general cases, wxWeakRef<T> is the better choice.
  20. For API documentation, see: wxWeakRef<T>.
  21. @tparam T
  22. The type to which the smart pointer points to.
  23. @nolibrary
  24. @category{smartpointers}
  25. */
  26. template<typename T>
  27. class wxWeakRefDynamic<T>
  28. {
  29. public:
  30. };
  31. /**
  32. wxWeakRef<T> is a template class for weak references to wxWidgets objects,
  33. such as wxEvtHandler, wxWindow and wxObject.
  34. A weak reference behaves much like an ordinary pointer, but when the object
  35. pointed is destroyed, the weak reference is automatically reset to a @NULL pointer.
  36. wxWeakRef<T> can be used whenever one must keep a pointer to an object
  37. that one does not directly own, and that may be destroyed before the object
  38. holding the reference.
  39. wxWeakRef<T> is a small object and the mechanism behind it is fast
  40. (@b O(1)). So the overall cost of using it is small.
  41. Example:
  42. @code
  43. wxWindow *wnd = new wxWindow( parent, wxID_ANY, "wxWindow" );
  44. wxWeakRef<wxWindow> wr = wnd;
  45. wxWindowRef wr2 = wnd; // Same as above, but using a typedef
  46. // Do things with window
  47. wnd->Show( true );
  48. // Weak ref is used like an ordinary pointer
  49. wr->Show( false );
  50. wnd->Destroy();
  51. // Now the weak ref has been reset, so we don't risk accessing
  52. // a dangling pointer:
  53. wxASSERT( wr==NULL );
  54. @endcode
  55. wxWeakRef<T> works for any objects that are derived from wxTrackable.
  56. By default, wxEvtHandler and wxWindow derive from wxTrackable.
  57. However, wxObject does not, so types like wxFont and wxColour are not
  58. trackable. The example below shows how to create a wxObject derived class
  59. that is trackable:
  60. @code
  61. class wxMyTrackableObject : public wxObject, public wxTrackable
  62. {
  63. // ... other members here
  64. };
  65. @endcode
  66. The following types of weak references are predefined:
  67. @code
  68. typedef wxWeakRef<wxEvtHandler> wxEvtHandlerRef;
  69. typedef wxWeakRef<wxWindow> wxWindowRef;
  70. @endcode
  71. @tparam T
  72. The type to which the smart pointer points to.
  73. @nolibrary
  74. @category{smartpointers}
  75. @see wxSharedPtr<T>, wxScopedPtr<T>
  76. */
  77. template<typename T>
  78. class wxWeakRef<T> : public wxTrackerNode
  79. {
  80. public:
  81. /// Type of the element stored by this reference.
  82. typedef T element_type;
  83. /**
  84. Constructor. The weak reference is initialized to @e pobj.
  85. */
  86. wxWeakRef(T* pobj = NULL);
  87. /**
  88. Copy constructor.
  89. */
  90. wxWeakRef(const wxWeakRef<T>& wr);
  91. /**
  92. Destructor.
  93. */
  94. virtual ~wxWeakRef();
  95. /**
  96. Called when the tracked object is destroyed. Be default sets
  97. internal pointer to @NULL.
  98. You need to call this method if you override it.
  99. */
  100. virtual void OnObjectDestroy();
  101. /**
  102. Release currently tracked object and rests object reference.
  103. */
  104. void Release();
  105. /**
  106. Returns pointer to the tracked object or @NULL.
  107. */
  108. T* get() const;
  109. /**
  110. Release currently tracked object and start tracking the same object as
  111. the wxWeakRef @e wr.
  112. */
  113. T* operator =(wxWeakRef<T>& wr);
  114. /**
  115. Implicit conversion to T*.
  116. Returns pointer to the tracked object or @NULL.
  117. */
  118. T* operator*() const;
  119. /**
  120. Returns a reference to the tracked object.
  121. If the internal pointer is @NULL this method will cause an assert in debug mode.
  122. */
  123. T& operator*() const;
  124. /**
  125. Smart pointer member access. Returns a pointer to the tracked object.
  126. If the internal pointer is @NULL this method will cause an assert in debug mode.
  127. */
  128. T* operator->();
  129. /**
  130. Releases the currently tracked object and starts tracking @e pobj.
  131. A weak reference may be reset by passing @e @NULL as @e pobj.
  132. */
  133. T* operator=(T* pobj);
  134. };