]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/trefcount.tex
add events API to wxHtmlWindow (patch #1504493 by Francesco Montorsi)
[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
40131743
RR
21\subsection{List of reference-counted wxWidgets classes}\label{refcountlist}
22
23The following classes in wxWidgets have efficient (i.e. fast) assignment operators
24and 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
40131743
RR
38\subsection{Make your own reference-counted class}\label{wxobjectoverview}
39
40Reference counting can be implemented easily using \helpref{wxObject}{wxobject}
41and \helpref{wxObjectRefData}{wxobjectrefdata} classes.
42
43First, derive a new class from \helpref{wxObjectRefData}{wxobjectrefdata} and
44put there the memory-consuming data.
45
46Then derive a new class from \helpref{wxObject}{wxobject} and implement there
47the public interface which will be seen by the user of your class.
48You'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
55in fact, all times you'll need to read the data from your wxObject-derived class,
56you'll need to call such function.
57
58Very important, all times you need to actually modify the data placed inside your
d479632a 59wxObject-derived class, you must first call the wxObject::UnShare
40131743
RR
60function to be sure that the modifications won't affect other instances which are
61eventually sharing your object's data.
62