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