]>
Commit | Line | Data |
---|---|---|
ef36734a RR |
1 | \section{\class{wxObjectDataPtr<T>}}\label{wxobjectdataptr} |
2 | ||
3 | This is helper template class to avoid memleaks because of missing calls | |
4 | to \helpref{wxObjectRefData::DecRef}{wxobjectrefdatadecref}. | |
5 | ||
6 | Despite the name this template can actually be used for any | |
7 | class implementing the reference counting interface and it | |
8 | does 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 | ||
18 | No base class | |
19 | ||
20 | \wxheading{Include files} | |
21 | ||
22 | <object.h> | |
23 | ||
24 | \wxheading{Data structures} | |
25 | ||
d60e8c6b RR |
26 | {\small% |
27 | \begin{verbatim} | |
28 | typedef T element_type | |
29 | \end{verbatim} | |
30 | }% | |
ef36734a RR |
31 | |
32 | \wxheading{Example} | |
33 | ||
34 | \begin{verbatim} | |
35 | ||
36 | // include file | |
37 | ||
38 | class MyCarRefData: public wxObjectRefData | |
39 | { | |
40 | public: | |
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 | ||
57 | protected: | |
58 | int m_price; | |
59 | }; | |
60 | ||
61 | class MyCar | |
62 | { | |
63 | public: | |
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 | ||
75 | protected: | |
76 | void UnShare(); | |
77 | }; | |
78 | ||
79 | ||
80 | // implementation | |
81 | ||
82 | MyCar::MyCar( int price ) | |
83 | { | |
84 | m_data = new MyCarRefData; | |
85 | m_data.get()->SetPrice( price ); | |
86 | } | |
87 | ||
369d17c0 RR |
88 | MyCar::MyCar( const MyCar& car ) |
89 | { | |
90 | m_data.reset( car.m_data.get() ); | |
91 | } | |
92 | ||
ef36734a RR |
93 | bool 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 | ||
100 | void MyCar::SetPrice( int price ) | |
101 | { | |
102 | UnShare(); | |
103 | ||
104 | m_data.get()->SetPrice( price ); | |
105 | } | |
106 | ||
107 | int MyCar::GetPrice() const | |
108 | { | |
109 | return m_data.get()->GetPrice(); | |
110 | } | |
111 | ||
112 | void 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 | ||
129 | Constructor. {\it ptr} is a pointer to the reference | |
130 | counted object to which this class points. | |
131 | ||
132 | \func{}{wxObjectDataPtr<T>}{\param{const wxObjectDataPtr<T>\& }{tocopy}} | |
133 | ||
134 | This copy constructor increases the count of the reference | |
135 | counted object to which {\it tocopy} points and then this | |
136 | class will point to, as well. | |
137 | ||
138 | \membersection{wxObjectDataPtr<T>::\destruct{wxObjectDataPtr<T>}}\label{wxobjectdataptrdtor} | |
139 | ||
140 | \func{}{\destruct{wxObjectDataPtr<T>}}{\void} | |
141 | ||
142 | Calls \helpref{DecRef}{wxobjectrefdatadecref} on the reference | |
143 | counted object to which this class points. | |
144 | ||
145 | \membersection{wxObjectDataPtr<T>::operator->}\label{wxobjectdataptroperatorpointer} | |
146 | ||
147 | \constfunc{T*}{operator->}{\void} | |
148 | ||
149 | Gets a pointer to the reference counted object to which | |
150 | this 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 | ||
158 | Assignment operators. | |
159 | ||
160 | \membersection{wxObjectDataPtr<T>::get}\label{wxobjectdataptrget} | |
161 | ||
162 | \constfunc{T*}{get}{\void} | |
163 | ||
164 | Gets a pointer to the reference counted object to which | |
165 | this class points. | |
166 | ||
167 | \membersection{wxObjectDataPtr<T>::reset}\label{wxobjectdataptrreset} | |
168 | ||
169 | \func{void}{reset}{\param{T* }{ptr}} | |
170 | ||
171 | Reset this class to {\it ptr} which points to a reference | |
172 | counted object. |