]> git.saurik.com Git - wxWidgets.git/blob - docs/latex/wx/objectdataptr.tex
added docs for wxObjectDataPtr<T> and examples for ref counting
[wxWidgets.git] / docs / latex / wx / objectdataptr.tex
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 );
63
64 bool operator == ( const MyCar& car ) const;
65 bool operator != (const MyCar& car) const { return !(*this == car); }
66
67 void SetPrice( int price );
68 int GetPrice() const;
69
70 wxObjectRefPtr<MyCarRefData> m_data;
71
72 protected:
73 void UnShare();
74 };
75
76
77 // implementation
78
79 MyCar::MyCar( int price )
80 {
81 m_data = new MyCarRefData;
82 m_data.get()->SetPrice( price );
83 }
84
85 bool MyCar::operator == ( const MyCar& car ) const
86 {
87 if (m_data.get() == car.m_data.get()) return true;
88
89 return (*m_data.get() == *car.m_data.get());
90 }
91
92 void MyCar::SetPrice( int price )
93 {
94 UnShare();
95
96 m_data.get()->SetPrice( price );
97 }
98
99 int MyCar::GetPrice() const
100 {
101 return m_data.get()->GetPrice();
102 }
103
104 void MyCar::UnShare()
105 {
106 if (m_data.get()->GetCount() == 1)
107 return;
108
109 m_data.reset( new MyCarRefData( *m_data.get() ) );
110 }
111
112 \end{verbatim}
113
114
115 \latexignore{\rtfignore{\wxheading{Members}}}
116
117 \membersection{wxObjectDataPtr<T>::wxObjectDataPtr<T>}\label{wxobjectdataptrwxobjectdataptr}
118
119 \func{wxEXPLICIT}{wxObjectDataPtr<T>}{\param{T* }{ptr = NULL}}
120
121 Constructor. {\it ptr} is a pointer to the reference
122 counted object to which this class points.
123
124 \func{}{wxObjectDataPtr<T>}{\param{const wxObjectDataPtr<T>\& }{tocopy}}
125
126 This copy constructor increases the count of the reference
127 counted object to which {\it tocopy} points and then this
128 class will point to, as well.
129
130 \membersection{wxObjectDataPtr<T>::\destruct{wxObjectDataPtr<T>}}\label{wxobjectdataptrdtor}
131
132 \func{}{\destruct{wxObjectDataPtr<T>}}{\void}
133
134 Calls \helpref{DecRef}{wxobjectrefdatadecref} on the reference
135 counted object to which this class points.
136
137 \membersection{wxObjectDataPtr<T>::operator->}\label{wxobjectdataptroperatorpointer}
138
139 \constfunc{T*}{operator->}{\void}
140
141 Gets a pointer to the reference counted object to which
142 this class points. Same as \helpref{get}{wxobjectdataptrget}.
143
144 \membersection{wxObjectDataPtr<T>::operator=}\label{wxobjectdataptroperatorassign}
145
146 \func{wxObjectDataPtr<T>\& operator}{operator=}{\param{const wxObjectDataPtr<T>\& }{tocopy}}
147
148 \func{wxObjectDataPtr<T>\& operator}{operator=}{\param{T* }{ptr}}
149
150 Assignment operators.
151
152 \membersection{wxObjectDataPtr<T>::get}\label{wxobjectdataptrget}
153
154 \constfunc{T*}{get}{\void}
155
156 Gets a pointer to the reference counted object to which
157 this class points.
158
159 \membersection{wxObjectDataPtr<T>::reset}\label{wxobjectdataptrreset}
160
161 \func{void}{reset}{\param{T* }{ptr}}
162
163 Reset this class to {\it ptr} which points to a reference
164 counted object.