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