]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/objectdataptr.tex
avoid undefined behavior from token paste resulting in more than one token
[wxWidgets.git] / docs / latex / wx / objectdataptr.tex
CommitLineData
ef36734a
RR
1\section{\class{wxObjectDataPtr<T>}}\label{wxobjectdataptr}
2
a60b0ddc
RR
3This is helper template class primarily written to avoid memory
4leaks because of missing calls to \helpref{wxObjectRefData::DecRef}{wxobjectrefdatadecref}.
ef36734a 5
a60b0ddc
RR
6Despite the name this template can actually be used as a
7smart pointer for any class implementing the reference
8counting interface and it does not use or depend on wxObject.
9
10The difference to \helpref{wxSharedPtr}{wxsharedptr} is that
11wxObjectDataPtr relies on the reference counting to be in
12the class pointed to where as wxSharedPtr implements the
13reference counting itself.
ef36734a
RR
14
15\wxheading{See also}
16
17\helpref{wxObject}{wxobject},
18\helpref{wxObjectRefData}{wxobjectrefdata},
19\helpref{Reference counting}{trefcount}
20
a60b0ddc
RR
21\helpref{wxSharedPtr}{wxsharedptr},
22\helpref{wxScopedPtr}{wxscopedptrtemplate},
23\helpref{wxWeakRef}{wxweakref}
24
25
ef36734a
RR
26\wxheading{Derived from}
27
28No base class
29
30\wxheading{Include files}
31
32<object.h>
33
34\wxheading{Data structures}
35
d60e8c6b
RR
36{\small%
37\begin{verbatim}
38typedef T element_type
39\end{verbatim}
40}%
ef36734a
RR
41
42\wxheading{Example}
43
44\begin{verbatim}
45
ef36734a
RR
46class MyCarRefData: public wxObjectRefData
47{
48public:
49 MyCarRefData() { m_price = 0; }
50
51 MyCarRefData( const MyCarRefData& data )
52 : wxObjectRefData()
53 {
54 m_price = data.m_price;
55 }
56
ef36734a
RR
57 void SetPrice( int price ) { m_price = price; }
58 int GetPrice() { return m_price; }
59
60protected:
61 int m_price;
62};
63
64class MyCar
65{
66public:
a60b0ddc
RR
67 MyCar( int price ) : m_data( new MyCarRefData )
68 {
69 m_data->SetPrice( price );
70 }
71
72 MyCar& operator =( const MyCar& tocopy )
73 {
74 m_data = tocopy.m_data;
75 return *this;
76 }
ef36734a 77
a60b0ddc
RR
78 bool operator == ( const MyCar& other ) const
79 {
80 if (m_data.get() == other.m_data.get()) return true;
81 return (m_data->GetPrice() == other.m_data->GetPrice());
82 }
ef36734a 83
a60b0ddc
RR
84 void SetPrice( int price )
85 {
86 UnShare();
87 m_data->SetPrice( price );
88 }
89
90 int GetPrice() const
91 {
92 return m_data->GetPrice();
93 }
ef36734a 94
a60b0ddc 95 wxObjectDataPtr<MyCarRefData> m_data;
ef36734a
RR
96
97protected:
a60b0ddc
RR
98 void UnShare()
99 {
100 if (m_data->GetRefCount() == 1)
101 return;
ef36734a 102
a60b0ddc
RR
103 m_data.reset( new MyCarRefData( *m_data ) );
104 }
105};
ef36734a
RR
106
107\end{verbatim}
108
109
110\latexignore{\rtfignore{\wxheading{Members}}}
111
112\membersection{wxObjectDataPtr<T>::wxObjectDataPtr<T>}\label{wxobjectdataptrwxobjectdataptr}
113
114\func{wxEXPLICIT}{wxObjectDataPtr<T>}{\param{T* }{ptr = NULL}}
115
116Constructor. {\it ptr} is a pointer to the reference
117counted object to which this class points.
118
119\func{}{wxObjectDataPtr<T>}{\param{const wxObjectDataPtr<T>\& }{tocopy}}
120
121This copy constructor increases the count of the reference
122counted object to which {\it tocopy} points and then this
123class will point to, as well.
124
125\membersection{wxObjectDataPtr<T>::\destruct{wxObjectDataPtr<T>}}\label{wxobjectdataptrdtor}
126
127\func{}{\destruct{wxObjectDataPtr<T>}}{\void}
128
129Calls \helpref{DecRef}{wxobjectrefdatadecref} on the reference
130counted object to which this class points.
131
a60b0ddc
RR
132\membersection{wxObjectDataPtr<T>::operator unspecified\_bool\_type}\label{wxobjectdataptroperatorbool}
133
134\constfunc{}{operator unspecified\_bool\_type}{\void}
135
136Conversion to a boolean expression (in a variant which is not
137convertable to anything but a boolean expression). If this class
138contains a valid pointer it will return {\it true}, if it contains
139a NULL pointer it will return {\it false}.
140
141\membersection{wxObjectDataPtr<T>::operator*}\label{wxobjectdataptroperatorreft}
142
143\constfunc{T \&}{operator*}{\void}
144
145Returns a reference to the object. If the internal pointer is NULL
146this method will cause an assert in debug mode.
147
ef36734a
RR
148\membersection{wxObjectDataPtr<T>::operator->}\label{wxobjectdataptroperatorpointer}
149
150\constfunc{T*}{operator->}{\void}
151
e39d30c0
RR
152Returns a pointer to the reference counted object to which
153this class points. If this the internal pointer is NULL,
154this method will assert in debug mode.
ef36734a
RR
155
156\membersection{wxObjectDataPtr<T>::operator=}\label{wxobjectdataptroperatorassign}
157
158\func{wxObjectDataPtr<T>\& operator}{operator=}{\param{const wxObjectDataPtr<T>\& }{tocopy}}
159
160\func{wxObjectDataPtr<T>\& operator}{operator=}{\param{T* }{ptr}}
161
162Assignment operators.
163
164\membersection{wxObjectDataPtr<T>::get}\label{wxobjectdataptrget}
165
166\constfunc{T*}{get}{\void}
167
168Gets a pointer to the reference counted object to which
169this class points.
170
171\membersection{wxObjectDataPtr<T>::reset}\label{wxobjectdataptrreset}
172
173\func{void}{reset}{\param{T* }{ptr}}
174
175Reset this class to {\it ptr} which points to a reference
176counted object.