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