]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/objectdataptr.tex
Use new menu item function
[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
26{\small \begin{verbatim}
27typedef T element\_type
28\end{verbatim}}
29
30\wxheading{Example}
31
32\begin{verbatim}
33
34// include file
35
36class MyCarRefData: public wxObjectRefData
37{
38public:
39 MyCarRefData() { m_price = 0; }
40
41 MyCarRefData( const MyCarRefData& data )
42 : wxObjectRefData()
43 {
44 m_price = data.m_price;
45 }
46
47 bool operator == (const MyCarRefData& data) const
48 {
49 return m_price == data.m_price;
50 }
51
52 void SetPrice( int price ) { m_price = price; }
53 int GetPrice() { return m_price; }
54
55protected:
56 int m_price;
57};
58
59class MyCar
60{
61public:
62 MyCar( int price );
369d17c0 63 MyCar( const MyCar& data );
ef36734a
RR
64
65 bool operator == ( const MyCar& car ) const;
66 bool operator != (const MyCar& car) const { return !(*this == car); }
67
68 void SetPrice( int price );
69 int GetPrice() const;
70
71 wxObjectRefPtr<MyCarRefData> m_data;
72
73protected:
74 void UnShare();
75};
76
77
78// implementation
79
80MyCar::MyCar( int price )
81{
82 m_data = new MyCarRefData;
83 m_data.get()->SetPrice( price );
84}
85
369d17c0
RR
86MyCar::MyCar( const MyCar& car )
87{
88 m_data.reset( car.m_data.get() );
89}
90
ef36734a
RR
91bool MyCar::operator == ( const MyCar& car ) const
92{
93 if (m_data.get() == car.m_data.get()) return true;
94
95 return (*m_data.get() == *car.m_data.get());
96}
97
98void MyCar::SetPrice( int price )
99{
100 UnShare();
101
102 m_data.get()->SetPrice( price );
103}
104
105int MyCar::GetPrice() const
106{
107 return m_data.get()->GetPrice();
108}
109
110void MyCar::UnShare()
111{
112 if (m_data.get()->GetCount() == 1)
113 return;
114
115 m_data.reset( new MyCarRefData( *m_data.get() ) );
116}
117
118\end{verbatim}
119
120
121\latexignore{\rtfignore{\wxheading{Members}}}
122
123\membersection{wxObjectDataPtr<T>::wxObjectDataPtr<T>}\label{wxobjectdataptrwxobjectdataptr}
124
125\func{wxEXPLICIT}{wxObjectDataPtr<T>}{\param{T* }{ptr = NULL}}
126
127Constructor. {\it ptr} is a pointer to the reference
128counted object to which this class points.
129
130\func{}{wxObjectDataPtr<T>}{\param{const wxObjectDataPtr<T>\& }{tocopy}}
131
132This copy constructor increases the count of the reference
133counted object to which {\it tocopy} points and then this
134class will point to, as well.
135
136\membersection{wxObjectDataPtr<T>::\destruct{wxObjectDataPtr<T>}}\label{wxobjectdataptrdtor}
137
138\func{}{\destruct{wxObjectDataPtr<T>}}{\void}
139
140Calls \helpref{DecRef}{wxobjectrefdatadecref} on the reference
141counted object to which this class points.
142
143\membersection{wxObjectDataPtr<T>::operator->}\label{wxobjectdataptroperatorpointer}
144
145\constfunc{T*}{operator->}{\void}
146
147Gets a pointer to the reference counted object to which
148this class points. Same as \helpref{get}{wxobjectdataptrget}.
149
150\membersection{wxObjectDataPtr<T>::operator=}\label{wxobjectdataptroperatorassign}
151
152\func{wxObjectDataPtr<T>\& operator}{operator=}{\param{const wxObjectDataPtr<T>\& }{tocopy}}
153
154\func{wxObjectDataPtr<T>\& operator}{operator=}{\param{T* }{ptr}}
155
156Assignment operators.
157
158\membersection{wxObjectDataPtr<T>::get}\label{wxobjectdataptrget}
159
160\constfunc{T*}{get}{\void}
161
162Gets a pointer to the reference counted object to which
163this class points.
164
165\membersection{wxObjectDataPtr<T>::reset}\label{wxobjectdataptrreset}
166
167\func{void}{reset}{\param{T* }{ptr}}
168
169Reset this class to {\it ptr} which points to a reference
170counted object.