]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/scpdptr.tex
mention that Add/RemoveChild() are internal
[wxWidgets.git] / docs / latex / wx / scpdptr.tex
CommitLineData
762e1997 1\section{\class{wxScopedPtr}}\label{wxscopedptr}
5b222f1c
JS
2
3This is a simple scoped smart pointer implementation that is similar to
d83eeece 4the \urlref{Boost}{http://www.boost.org/} smart pointers but rewritten to
5b222f1c
JS
5use macros instead.
6
d83eeece
VZ
7A smart pointer holds a pointer to an object. The memory used by the object is
8deleted when the smart pointer goes out of scope. This class is different from
9the \texttt{std::auto\_ptr<>} in so far as it doesn't provide copy constructor
10nor assignment operator. This limits what you can do with it but is much less
11surprizing than the ``destructive copy'' behaviour of the standard class.
12
5b222f1c
JS
13\wxheading{Example}
14
15Below is an example of using a wxWindows scoped smart pointer and
16pointer array.
17
18\begin{verbatim}
19 class MyClass { /* ... */ };
20
21 // declare a smart pointer to a MyClass called wxMyClassPtr
22 wxDECLARE_SCOPED_PTR(MyClass, wxMyClassPtr)
23 // declare a smart pointer to an array of chars
24 wxDECLARE_SCOPED_ARRAY(char, wxCharArray)
25
26 ...
27
28 // define the first pointer class, must be complete
29 wxDEFINE_SCOPED_PTR(MyClass, wxMyClassPtr)
30 // define the second pointer class
31 wxDEFINE_SCOPED_ARRAY(char, wxCharArray)
32
33 // create an object with a new pointer to MyClass
34 wxMyClassPtr theObj(new MyClass());
35 // reset the pointer (deletes the previous one)
36 theObj.reset(new MyClass());
37
38 // access the pointer
39 theObj->MyFunc();
40
41 // create an object with a new array of chars
42 wxCharArray theCharObj(new char[100]);
43
44 // access the array
45 theCharObj[0] = "!";
46\end{verbatim}
47
48\wxheading{Declaring new smart pointer types}
49
d83eeece
VZ
50To declare the smart pointer class \texttt{CLASSNAME} containing pointes to a
51(possibly incomplete) type \texttt{TYPE} you should use
5b222f1c 52\begin{verbatim}
d83eeece 53 wxDECLARE_SCOPED_PTR( TYPE, // type of the values
5b222f1c
JS
54 CLASSNAME ); // name of the class
55\end{verbatim}
56
d83eeece
VZ
57And later, when \texttt{TYPE} is fully defined, you must also use
58\begin{verbatim}
59 wxDEFINE_SCOPED_PTR( TYPE, CLASSNAME );
60\end{verbatim}
61to implement the scoped pointer class.
62
63The first argument of these macro is the pointer type, the second is the name
64of the new smart pointer class being created. Below we will use wxScopedPtr to
5b222f1c
JS
65represent the scoped pointer class, but the user may create the class with any
66legal name.
67
d83eeece
VZ
68Alternatively, if you don't have to separate the point of declaration and
69definition of this class and if you accept the standard naming convention, that
70is that the scoped pointer for the class \texttt{Foo} is called
71\texttt{FooPtr}, you can use a single macro which replaces two macros above:
72\begin{verbatim}
73 wxDEFINE_SCOPED_PTR_TYPE( TYPE );
74\end{verbatim}
75Once again, in this cass \texttt{CLASSNAME} will be \texttt{TYPEPtr}.
76
5b222f1c
JS
77\wxheading{Include files}
78
79<wx/ptr\_scpd.h>
80
81\wxheading{See also}
82
83\helpref{wxScopedArray}{wxscopedarray}\rtfsp
84
85\latexignore{\rtfignore{\wxheading{Members}}}
86
87\membersection{wxScopedPtr::wxScopedPtr}
88
89\func{}{wxScopedPtr}{\param{type}{ * T = NULL}}
90
91Creates the smart pointer with the given pointer or none if NULL. On
92compilers that support it, this uses the explicit keyword.
93
5455e227
VZ
94\membersection{wxScopedPtr::release}
95
96\func{T *}{release}{\void}
97
98Returns the currently hold pointer and resets the smart pointer object to
99{\tt NULL}. After a call to this function the caller is responsible for
100deleting the pointer.
101
102
5b222f1c
JS
103\membersection{wxScopedPtr::reset}
104
105\func{\void}{reset}{\param{T}{ p * = NULL}}
106
5455e227 107Deletes the currently held pointer and sets it to {\it p} or to NULL if no
5b222f1c
JS
108arguments are specified. This function does check to make sure that the
109pointer you are assigning is not the same pointer that is already stored.
110
111\membersection{wxScopedPtr::operator *}
112
113\func{const T\&}{operator *}{\void}
114
115This operator works like the standard C++ pointer operator to return the object
116being pointed to by the pointer. If the pointer is NULL or invalid this will
117crash.
118
bf43ff9a 119\membersection{wxScopedPtr::operator -$>$} % TODO
5b222f1c 120
bf43ff9a 121\func{const T*}{operator -$>$}{\void} % TODO
5b222f1c
JS
122
123This operator works like the standard C++ pointer operator to return the pointer
124in the smart pointer or NULL if it is empty.
125
126\membersection{wxScopedPtr::get}
127
128\func{const T*}{get}{\void}
129
130This operator gets the pointer stored in the smart pointer or returns NULL if
131there is none.
132
133\membersection{wxScopedPtr::swap}
134
5455e227 135\func{\void}{swap}{\param{wxScopedPtr}{ \& other}}
5b222f1c 136
5455e227
VZ
137Swap the pointer inside the smart pointer with {\it other}. The pointer being
138swapped must be of the same type (hence the same class name).
5b222f1c 139