]> git.saurik.com Git - wxWidgets.git/blob - docs/latex/wx/trefcount.tex
added NZST and NZDT time zones (request 617912)
[wxWidgets.git] / docs / latex / wx / trefcount.tex
1 \section{Reference counting}\label{trefcount}
2
3 \subsection{Reference counting and why you shouldn't care about it}\label{refcount}
4
5 Many wxWidgets objects use a technique known as \it{reference counting}, also known
6 as {\it copy on write} (COW).
7 This means that when an object is assigned to another, no copying really takes place:
8 only the reference count on the shared object data is incremented and both objects
9 share the same data (a very fast operation).
10
11 But as soon as one of the two (or more) objects is modified, the data has to be
12 copied because the changes to one of the objects shouldn't be seen in the
13 others. As data copying only happens when the object is written to, this is
14 known as COW.
15
16 What is important to understand is that all this happens absolutely
17 transparently to the class users and that whether an object is shared or not
18 is not seen from the outside of the class - in any case, the result of any
19 operation on it is the same.
20
21 \subsection{List of reference-counted wxWidgets classes}\label{refcountlist}
22
23 The following classes in wxWidgets have efficient (i.e. fast) assignment operators
24 and copy constructors since they are reference-counted:
25
26 \helpref{wxAcceleratorTable}{wxacceleratortable}\\
27 \helpref{wxBrush}{wxbrush}\\
28 \helpref{wxCursor}{wxcursor}\\
29 \helpref{wxFont}{wxfont}\\
30 \helpref{wxImage}{wximage}\\
31 \helpref{wxMetafile}{wxmetafile}\\
32 \helpref{wxPalette}{wxpalette}\\
33 \helpref{wxPen}{wxpen}\\
34 \helpref{wxRegion}{wxregion}\\
35 \helpref{wxRegionIterator}{wxregioniterator}\\
36 \helpref{wxString}{wxstring}
37
38 \subsection{Make your own reference-counted class}\label{wxobjectoverview}
39
40 Reference counting can be implemented easily using \helpref{wxObject}{wxobject}
41 and \helpref{wxObjectRefData}{wxobjectrefdata} classes.
42
43 First, derive a new class from \helpref{wxObjectRefData}{wxobjectrefdata} and
44 put there the memory-consuming data.
45
46 Then derive a new class from \helpref{wxObject}{wxobject} and implement there
47 the public interface which will be seen by the user of your class.
48 You'll probably want to add a function to your class which does the cast from
49 \helpref{wxObjectRefData}{wxobjectrefdata} to your class-specific shared data; e.g.:
50
51 \begin{verbatim}
52 MyClassRefData *GetData() const { return wx_static_cast(MyClassRefData*, m_refData); }
53 \end{verbatim}
54
55 in fact, all times you'll need to read the data from your wxObject-derived class,
56 you'll need to call such function.
57
58 Very important, all times you need to actually modify the data placed inside your
59 wxObject-derived class, you must first call the wxObject::UnShare
60 function to be sure that the modifications won't affect other instances which are
61 eventually sharing your object's data.
62