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