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