1 \section{\class{wxObjectDataPtr<T>
}}\label{wxobjectdataptr
}
3 This is helper template class primarily written to avoid memory
4 leaks because of missing calls to
\helpref{wxObjectRefData::DecRef
}{wxobjectrefdatadecref
}.
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()
}.
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.
18 \helpref{wxObject
}{wxobject
},
19 \helpref{wxObjectRefData
}{wxobjectrefdata
},
20 \helpref{Reference counting
}{trefcount
}
22 \helpref{wxSharedPtr
}{wxsharedptr
},
23 \helpref{wxScopedPtr
}{wxscopedptrtemplate
},
24 \helpref{wxWeakRef
}{wxweakref
}
27 \wxheading{Derived from
}
31 \wxheading{Include files
}
35 \wxheading{Data structures
}
39 typedef T element_type
47 class MyCarRefData: public wxObjectRefData
50 MyCarRefData()
{ m_price =
0;
}
52 MyCarRefData( const MyCarRefData& data )
55 m_price = data.m_price;
58 void SetPrice( int price )
{ m_price = price;
}
59 int GetPrice()
{ return m_price;
}
68 MyCar( int price ) : m_data( new MyCarRefData )
70 m_data->SetPrice( price );
73 MyCar& operator =( const MyCar& tocopy )
75 m_data = tocopy.m_data;
79 bool operator == ( const MyCar& other ) const
81 if (m_data.get() == other.m_data.get()) return true;
82 return (m_data->GetPrice() == other.m_data->GetPrice());
85 void SetPrice( int price )
88 m_data->SetPrice( price );
93 return m_data->GetPrice();
96 wxObjectDataPtr<MyCarRefData> m_data;
101 if (m_data->GetRefCount() ==
1)
104 m_data.reset( new MyCarRefData( *m_data ) );
111 \latexignore{\rtfignore{\wxheading{Members
}}}
113 \membersection{wxObjectDataPtr<T>::wxObjectDataPtr<T>
}\label{wxobjectdataptrwxobjectdataptr
}
115 \func{wxEXPLICIT
}{wxObjectDataPtr<T>
}{\param{T*
}{ptr = NULL
}}
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
122 \func{}{wxObjectDataPtr<T>
}{\param{const wxObjectDataPtr<T>\&
}{tocopy
}}
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.
128 \membersection{wxObjectDataPtr<T>::
\destruct{wxObjectDataPtr<T>
}}\label{wxobjectdataptrdtor
}
130 \func{}{\destruct{wxObjectDataPtr<T>
}}{\void}
132 Decreases the reference count of the object to which this
135 \membersection{wxObjectDataPtr<T>::operator unspecified
\_bool\_type}\label{wxobjectdataptroperatorbool
}
137 \constfunc{}{operator unspecified
\_bool\_type}{\void}
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
}.
144 \membersection{wxObjectDataPtr<T>::operator*
}\label{wxobjectdataptroperatorreft
}
146 \constfunc{T \&
}{operator*
}{\void}
148 Returns a reference to the object. If the internal pointer is NULL
149 this method will cause an assert in debug mode.
151 \membersection{wxObjectDataPtr<T>::operator->
}\label{wxobjectdataptroperatorpointer
}
153 \constfunc{T*
}{operator->
}{\void}
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.
159 \membersection{wxObjectDataPtr<T>::operator=
}\label{wxobjectdataptroperatorassign
}
161 \func{wxObjectDataPtr<T>\& operator
}{operator=
}{\param{const wxObjectDataPtr<T>\&
}{tocopy
}}
163 \func{wxObjectDataPtr<T>\& operator
}{operator=
}{\param{T*
}{ptr
}}
165 Assignment operators.
167 \membersection{wxObjectDataPtr<T>::get
}\label{wxobjectdataptrget
}
169 \constfunc{T*
}{get
}{\void}
171 Gets a pointer to the reference counted object to which
174 \membersection{wxObjectDataPtr<T>::reset
}\label{wxobjectdataptrreset
}
176 \func{void
}{reset
}{\param{T*
}{ptr
}}
178 Reset this class to
{\it ptr
} which points to a reference
179 counted object and calls
{\bf T::DecRef()
} on the previously