]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/trefcount.tex
don't need to check if we set the same cursor before setting it: SetCursor() already...
[wxWidgets.git] / docs / latex / wx / trefcount.tex
CommitLineData
40131743
RR
1\section{Reference counting}\label{trefcount}
2
55ccdb93 3\subsection{Why you shouldn't care about it}\label{refcount}
40131743
RR
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 21\subsection{List of reference-counted wxWidgets classes}\label{refcountlist}
55ccdb93
VZ
22\subsection{Object comparison}\label{refcountequality}
23
24The $==$ and $!=$ operators of \helpref{wxWidgets COW objects}{refcountlist}
25always do a {\tt deep} comparison.
26
27This means that the equality operator will return \true if two objects are
28identic and not only if they share the same data.
29
30Note that wxWidgets follows the {\it STL philosophy}: when a comparison operator cannot
31be implemented efficiently (like for e.g. wxImage's $==$ operator which would need to
32compare pixel-by-pixel the entire image's data), it's not implemented at all.
33
34That's why not all reference-counted wxWidgets classes provide comparison operators.
35
36Also note that if you only need to do a {\tt shallow} comparison between two
37\helpref{wxObject}{wxobject}-derived classes, you should not use the $==$ and $!=$ operators
38but rather the \helpref{wxObject::IsRefTo}{wxobjectisrefto} function.
39
40
41\subsection{Object destruction}\label{refcountdestruct}
42
43When a COW object destructor is called, it may not delete the data: if it's shared,
44the destructor will just decrement the shared data's reference count without destroying it.
45
46Only when the destructor of the last object owning the data is called, the data is really
47destroyed. As for all other COW-things, this happens transparently to the class users so
48that you shouldn't care about it.
49
40131743
RR
50
51The following classes in wxWidgets have efficient (i.e. fast) assignment operators
52and copy constructors since they are reference-counted:
53
54\helpref{wxAcceleratorTable}{wxacceleratortable}\\
55ccdb93
VZ
55\helpref{wxAnimation}{wxanimation}\\
56\helpref{wxBitmap}{wxbitmap}\\
40131743
RR
57\helpref{wxBrush}{wxbrush}\\
58\helpref{wxCursor}{wxcursor}\\
59\helpref{wxFont}{wxfont}\\
55ccdb93 60\helpref{wxIcon}{wxicon}\\
40131743
RR
61\helpref{wxImage}{wximage}\\
62\helpref{wxMetafile}{wxmetafile}\\
63\helpref{wxPalette}{wxpalette}\\
64\helpref{wxPen}{wxpen}\\
65\helpref{wxRegion}{wxregion}\\
40131743
RR
66\helpref{wxString}{wxstring}
67
55ccdb93
VZ
68Note that the list above reports the objects which are reference-counted in all ports of
69wxWidgets; some ports may use this tecnique also for other classes.
40131743
RR
70\subsection{Make your own reference-counted class}\label{wxobjectoverview}
71
72Reference counting can be implemented easily using \helpref{wxObject}{wxobject}
73and \helpref{wxObjectRefData}{wxobjectrefdata} classes.
74
75First, derive a new class from \helpref{wxObjectRefData}{wxobjectrefdata} and
76put there the memory-consuming data.
77
78Then derive a new class from \helpref{wxObject}{wxobject} and implement there
79the public interface which will be seen by the user of your class.
80You'll probably want to add a function to your class which does the cast from
81\helpref{wxObjectRefData}{wxobjectrefdata} to your class-specific shared data; e.g.:
82
83\begin{verbatim}
84 MyClassRefData *GetData() const { return wx_static_cast(MyClassRefData*, m_refData); }
85\end{verbatim}
86
87in fact, all times you'll need to read the data from your wxObject-derived class,
88you'll need to call such function.
89
90Very important, all times you need to actually modify the data placed inside your
d479632a 91wxObject-derived class, you must first call the wxObject::UnShare
40131743
RR
92function to be sure that the modifications won't affect other instances which are
93eventually sharing your object's data.
94