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