]>
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 | ||
26 | {\small \begin{verbatim} | |
27 | typedef T element\_type | |
28 | \end{verbatim}} | |
29 | ||
30 | \wxheading{Example} | |
31 | ||
32 | \begin{verbatim} | |
33 | ||
34 | // include file | |
35 | ||
36 | class MyCarRefData: public wxObjectRefData | |
37 | { | |
38 | public: | |
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 | ||
55 | protected: | |
56 | int m_price; | |
57 | }; | |
58 | ||
59 | class MyCar | |
60 | { | |
61 | public: | |
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 | ||
73 | protected: | |
74 | void UnShare(); | |
75 | }; | |
76 | ||
77 | ||
78 | // implementation | |
79 | ||
80 | MyCar::MyCar( int price ) | |
81 | { | |
82 | m_data = new MyCarRefData; | |
83 | m_data.get()->SetPrice( price ); | |
84 | } | |
85 | ||
369d17c0 RR |
86 | MyCar::MyCar( const MyCar& car ) |
87 | { | |
88 | m_data.reset( car.m_data.get() ); | |
89 | } | |
90 | ||
ef36734a RR |
91 | bool 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 | ||
98 | void MyCar::SetPrice( int price ) | |
99 | { | |
100 | UnShare(); | |
101 | ||
102 | m_data.get()->SetPrice( price ); | |
103 | } | |
104 | ||
105 | int MyCar::GetPrice() const | |
106 | { | |
107 | return m_data.get()->GetPrice(); | |
108 | } | |
109 | ||
110 | void 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 | ||
127 | Constructor. {\it ptr} is a pointer to the reference | |
128 | counted object to which this class points. | |
129 | ||
130 | \func{}{wxObjectDataPtr<T>}{\param{const wxObjectDataPtr<T>\& }{tocopy}} | |
131 | ||
132 | This copy constructor increases the count of the reference | |
133 | counted object to which {\it tocopy} points and then this | |
134 | class will point to, as well. | |
135 | ||
136 | \membersection{wxObjectDataPtr<T>::\destruct{wxObjectDataPtr<T>}}\label{wxobjectdataptrdtor} | |
137 | ||
138 | \func{}{\destruct{wxObjectDataPtr<T>}}{\void} | |
139 | ||
140 | Calls \helpref{DecRef}{wxobjectrefdatadecref} on the reference | |
141 | counted object to which this class points. | |
142 | ||
143 | \membersection{wxObjectDataPtr<T>::operator->}\label{wxobjectdataptroperatorpointer} | |
144 | ||
145 | \constfunc{T*}{operator->}{\void} | |
146 | ||
147 | Gets a pointer to the reference counted object to which | |
148 | this 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 | ||
156 | Assignment operators. | |
157 | ||
158 | \membersection{wxObjectDataPtr<T>::get}\label{wxobjectdataptrget} | |
159 | ||
160 | \constfunc{T*}{get}{\void} | |
161 | ||
162 | Gets a pointer to the reference counted object to which | |
163 | this class points. | |
164 | ||
165 | \membersection{wxObjectDataPtr<T>::reset}\label{wxobjectdataptrreset} | |
166 | ||
167 | \func{void}{reset}{\param{T* }{ptr}} | |
168 | ||
169 | Reset this class to {\it ptr} which points to a reference | |
170 | counted object. |