refcount.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: refcount.h
  3. // Purpose: topic overview
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @page overview_refcount Reference Counting
  9. @tableofcontents
  10. Many wxWidgets objects use a technique known as <em>reference counting</em>,
  11. also known as <em>copy on write</em> (COW). This means that when an object is
  12. assigned to another, no copying really takes place. Only the reference count on
  13. the shared object data is incremented and both objects share the same data (a
  14. very fast operation).
  15. But as soon as one of the two (or more) objects is modified, the data has to be
  16. copied because the changes to one of the objects shouldn't be seen in the
  17. others. As data copying only happens when the object is written to, this is
  18. known as COW.
  19. What is important to understand is that all this happens absolutely
  20. transparently to the class users and that whether an object is shared or not is
  21. not seen from the outside of the class - in any case, the result of any
  22. operation on it is the same.
  23. @section overview_refcount_equality Object Comparison
  24. The == and != operators of @ref overview_refcount_list "the reference counted classes"
  25. always do a <em>deep comparison</em>. This means that the equality operator
  26. will return @true if two objects are identical and not only if they share the
  27. same data.
  28. Note that wxWidgets follows the <em>STL philosophy</em>: when a comparison
  29. operator cannot be implemented efficiently (like for e.g. wxImage's ==
  30. operator which would need to compare the entire image's data, pixel-by-pixel),
  31. it's not implemented at all. That's why not all reference counted classes
  32. provide comparison operators.
  33. Also note that if you only need to do a @c shallow comparison between two
  34. wxObject derived classes, you should not use the == and != operators but
  35. rather the wxObject::IsSameAs() function.
  36. @section overview_refcount_destruct Object Destruction
  37. When a COW object destructor is called, it may not delete the data: if it's
  38. shared, the destructor will just decrement the shared data's reference count
  39. without destroying it. Only when the destructor of the last object owning the
  40. data is called, the data is really destroyed. Just like all other COW-things,
  41. this happens transparently to the class users so that you shouldn't care about
  42. it.
  43. @section overview_refcount_list List of Reference Counted Classes
  44. The following classes in wxWidgets have efficient (i.e. fast) assignment
  45. operators and copy constructors since they are reference-counted:
  46. @li wxAcceleratorTable
  47. @li wxAnimation
  48. @li wxBitmap
  49. @li wxBrush
  50. @li wxCursor
  51. @li wxFont
  52. @li wxGraphicsBrush
  53. @li wxGraphicsContext
  54. @li wxGraphicsFont
  55. @li wxGraphicsMatrix
  56. @li wxGraphicsPath
  57. @li wxGraphicsPen
  58. @li wxIcon
  59. @li wxImage
  60. @li wxMetafile
  61. @li wxPalette
  62. @li wxPen
  63. @li wxRegion
  64. @li wxString
  65. @li wxVariant
  66. @li wxVariantData
  67. Note that the list above reports the objects which are reference counted in all
  68. ports of wxWidgets; some ports may use this technique also for other classes.
  69. All the objects implement a function @b IsOk() to test if they are referencing
  70. valid data; when the objects are in uninitialized state, you can only use the
  71. @b IsOk() getter; trying to call any other getter, e.g. wxBrush::GetStyle() on
  72. the ::wxNullBrush object, will result in an assert failure in debug builds.
  73. @section overview_refcount_object Making Your Own Reference Counted Class
  74. Reference counting can be implemented easily using wxObject or using the
  75. intermediate wxRefCounter class directly. Alternatively, you can also use the
  76. wxObjectDataPtr<T> template.
  77. First, derive a new class from wxRefCounter (or wxObjectRefData when using a
  78. wxObject derived class) and put the memory-consuming data in it.
  79. Then derive a new class from wxObject and implement there the public interface
  80. which will be seen by the user of your class. You'll probably want to add a
  81. function to your class which does the cast from wxObjectRefData to your
  82. class-specific shared data. For example:
  83. @code
  84. MyClassRefData* GetData() const
  85. {
  86. return wx_static_cast(MyClassRefData*, m_refData);
  87. }
  88. @endcode
  89. In fact, any time you need to read the data from your wxObject-derived class,
  90. you will need to call this function.
  91. @note Any time you need to actually modify the data placed inside your wxObject
  92. derived class, you must first call the wxObject::UnShare() function to ensure
  93. that the modifications won't affect other instances which are eventually
  94. sharing your object's data.
  95. */