]>
Commit | Line | Data |
---|---|---|
762e1997 | 1 | \section{\class{wxScopedPtr}}\label{wxscopedptr} |
5b222f1c JS |
2 | |
3 | This is a simple scoped smart pointer implementation that is similar to | |
4 | the \urlref{Boost}{http://www.boost.org} smart pointers but rewritten to | |
5 | use macros instead. | |
6 | ||
7 | \wxheading{Example} | |
8 | ||
9 | Below is an example of using a wxWindows scoped smart pointer and | |
10 | pointer array. | |
11 | ||
12 | \begin{verbatim} | |
13 | class MyClass { /* ... */ }; | |
14 | ||
15 | // declare a smart pointer to a MyClass called wxMyClassPtr | |
16 | wxDECLARE_SCOPED_PTR(MyClass, wxMyClassPtr) | |
17 | // declare a smart pointer to an array of chars | |
18 | wxDECLARE_SCOPED_ARRAY(char, wxCharArray) | |
19 | ||
20 | ... | |
21 | ||
22 | // define the first pointer class, must be complete | |
23 | wxDEFINE_SCOPED_PTR(MyClass, wxMyClassPtr) | |
24 | // define the second pointer class | |
25 | wxDEFINE_SCOPED_ARRAY(char, wxCharArray) | |
26 | ||
27 | // create an object with a new pointer to MyClass | |
28 | wxMyClassPtr theObj(new MyClass()); | |
29 | // reset the pointer (deletes the previous one) | |
30 | theObj.reset(new MyClass()); | |
31 | ||
32 | // access the pointer | |
33 | theObj->MyFunc(); | |
34 | ||
35 | // create an object with a new array of chars | |
36 | wxCharArray theCharObj(new char[100]); | |
37 | ||
38 | // access the array | |
39 | theCharObj[0] = "!"; | |
40 | \end{verbatim} | |
41 | ||
42 | \wxheading{Declaring new smart pointer types} | |
43 | ||
44 | \begin{verbatim} | |
45 | wxDECLAR_SCOPED_PTR( TYPE, // type of the values | |
46 | CLASSNAME ); // name of the class | |
47 | \end{verbatim} | |
48 | ||
49 | A smart pointer holds a pointer to an object (which must be complete | |
2b5f62a0 | 50 | when wxDEFINE\_SCOPED\_PTR() is called). The memory used by the object is |
5b222f1c JS |
51 | deleted when the smart pointer goes out of scope. The first argument |
52 | of the macro is the pointer type, the second is the name of the new | |
53 | smart pointer class being created. Below we will use wxScopedPtr to | |
54 | represent the scoped pointer class, but the user may create the class with any | |
55 | legal name. | |
56 | ||
57 | \wxheading{Include files} | |
58 | ||
59 | <wx/ptr\_scpd.h> | |
60 | ||
61 | \wxheading{See also} | |
62 | ||
63 | \helpref{wxScopedArray}{wxscopedarray}\rtfsp | |
64 | ||
65 | \latexignore{\rtfignore{\wxheading{Members}}} | |
66 | ||
67 | \membersection{wxScopedPtr::wxScopedPtr} | |
68 | ||
69 | \func{}{wxScopedPtr}{\param{type}{ * T = NULL}} | |
70 | ||
71 | Creates the smart pointer with the given pointer or none if NULL. On | |
72 | compilers that support it, this uses the explicit keyword. | |
73 | ||
5455e227 VZ |
74 | \membersection{wxScopedPtr::release} |
75 | ||
76 | \func{T *}{release}{\void} | |
77 | ||
78 | Returns the currently hold pointer and resets the smart pointer object to | |
79 | {\tt NULL}. After a call to this function the caller is responsible for | |
80 | deleting the pointer. | |
81 | ||
82 | ||
5b222f1c JS |
83 | \membersection{wxScopedPtr::reset} |
84 | ||
85 | \func{\void}{reset}{\param{T}{ p * = NULL}} | |
86 | ||
5455e227 | 87 | Deletes the currently held pointer and sets it to {\it p} or to NULL if no |
5b222f1c JS |
88 | arguments are specified. This function does check to make sure that the |
89 | pointer you are assigning is not the same pointer that is already stored. | |
90 | ||
91 | \membersection{wxScopedPtr::operator *} | |
92 | ||
93 | \func{const T\&}{operator *}{\void} | |
94 | ||
95 | This operator works like the standard C++ pointer operator to return the object | |
96 | being pointed to by the pointer. If the pointer is NULL or invalid this will | |
97 | crash. | |
98 | ||
bf43ff9a | 99 | \membersection{wxScopedPtr::operator -$>$} % TODO |
5b222f1c | 100 | |
bf43ff9a | 101 | \func{const T*}{operator -$>$}{\void} % TODO |
5b222f1c JS |
102 | |
103 | This operator works like the standard C++ pointer operator to return the pointer | |
104 | in the smart pointer or NULL if it is empty. | |
105 | ||
106 | \membersection{wxScopedPtr::get} | |
107 | ||
108 | \func{const T*}{get}{\void} | |
109 | ||
110 | This operator gets the pointer stored in the smart pointer or returns NULL if | |
111 | there is none. | |
112 | ||
113 | \membersection{wxScopedPtr::swap} | |
114 | ||
5455e227 | 115 | \func{\void}{swap}{\param{wxScopedPtr}{ \& other}} |
5b222f1c | 116 | |
5455e227 VZ |
117 | Swap the pointer inside the smart pointer with {\it other}. The pointer being |
118 | swapped must be of the same type (hence the same class name). | |
5b222f1c | 119 |