]>
Commit | Line | Data |
---|---|---|
ef36734a RR |
1 | \section{\class{wxObjectDataPtr<T>}}\label{wxobjectdataptr} |
2 | ||
a60b0ddc RR |
3 | This is helper template class primarily written to avoid memory |
4 | leaks because of missing calls to \helpref{wxObjectRefData::DecRef}{wxobjectrefdatadecref}. | |
ef36734a | 5 | |
a60b0ddc RR |
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. | |
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 | ||
28 | No base class | |
29 | ||
30 | \wxheading{Include files} | |
31 | ||
32 | <object.h> | |
33 | ||
34 | \wxheading{Data structures} | |
35 | ||
d60e8c6b RR |
36 | {\small% |
37 | \begin{verbatim} | |
38 | typedef T element_type | |
39 | \end{verbatim} | |
40 | }% | |
ef36734a RR |
41 | |
42 | \wxheading{Example} | |
43 | ||
44 | \begin{verbatim} | |
45 | ||
ef36734a RR |
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 | ||
ef36734a RR |
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: | |
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 | |
97 | protected: | |
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 | ||
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 | ||
a60b0ddc RR |
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 | ||
ef36734a RR |
148 | \membersection{wxObjectDataPtr<T>::operator->}\label{wxobjectdataptroperatorpointer} |
149 | ||
150 | \constfunc{T*}{operator->}{\void} | |
151 | ||
e39d30c0 RR |
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. | |
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 | ||
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. |