]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: ptr_scpd.h | |
e54c96f1 | 3 | // Purpose: interface of wxScopedPtr |
23324ae1 FM |
4 | // Author: wxWidgets team |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /** | |
10 | @class wxScopedPtr | |
11 | @wxheader{ptr_scpd.h} | |
7c913512 FM |
12 | |
13 | This is a simple scoped smart pointer implementation that is similar to | |
23324ae1 FM |
14 | the Boost smart pointers but rewritten to |
15 | use macros instead. | |
7c913512 | 16 | |
23324ae1 | 17 | Since wxWidgets 2.9.0 there is also a templated version of this class |
e54c96f1 | 18 | with the same name. See wxScopedPtrT(). |
7c913512 | 19 | |
23324ae1 FM |
20 | A smart pointer holds a pointer to an object. The memory used by the object is |
21 | deleted when the smart pointer goes out of scope. This class is different from | |
22 | the @c std::auto_ptr in so far as it doesn't provide copy constructor | |
23 | nor assignment operator. This limits what you can do with it but is much less | |
cdbcf4c2 | 24 | surprizing than the "destructive copy" behaviour of the standard class. |
7c913512 | 25 | |
23324ae1 FM |
26 | @library{wxbase} |
27 | @category{FIXME} | |
7c913512 | 28 | |
e54c96f1 | 29 | @see wxScopedArray |
23324ae1 | 30 | */ |
7c913512 | 31 | class wxScopedPtr |
23324ae1 FM |
32 | { |
33 | public: | |
34 | /** | |
35 | Creates the smart pointer with the given pointer or none if @NULL. On | |
36 | compilers that support it, this uses the explicit keyword. | |
37 | */ | |
4cc4bfaf | 38 | explicit wxScopedPtr(type T = NULL); |
23324ae1 FM |
39 | |
40 | /** | |
41 | Destructor frees the pointer help by this object if it is not @NULL. | |
42 | */ | |
43 | ~wxScopedPtr(); | |
44 | ||
45 | /** | |
46 | This operator gets the pointer stored in the smart pointer or returns @NULL if | |
47 | there is none. | |
48 | */ | |
49 | const T* get(); | |
50 | ||
51 | /** | |
52 | This operator works like the standard C++ pointer operator to return the object | |
53 | being pointed to by the pointer. If the pointer is @NULL or invalid this will | |
54 | crash. | |
55 | */ | |
56 | const T operator *(); | |
57 | ||
58 | /** | |
59 | This operator works like the standard C++ pointer operator to return the pointer | |
60 | in the smart pointer or @NULL if it is empty. | |
61 | */ | |
62 | const T* operator -(); | |
63 | ||
64 | /** | |
7c913512 | 65 | Returns the currently hold pointer and resets the smart pointer object to |
23324ae1 FM |
66 | @NULL. After a call to this function the caller is responsible for |
67 | deleting the pointer. | |
68 | */ | |
4cc4bfaf | 69 | T* release(); |
23324ae1 FM |
70 | |
71 | /** | |
4cc4bfaf | 72 | Deletes the currently held pointer and sets it to @a p or to @NULL if no |
23324ae1 FM |
73 | arguments are specified. This function does check to make sure that the |
74 | pointer you are assigning is not the same pointer that is already stored. | |
75 | */ | |
4cc4bfaf | 76 | reset(T p = NULL); |
23324ae1 FM |
77 | |
78 | /** | |
79 | Swap the pointer inside the smart pointer with @e other. The pointer being | |
80 | swapped must be of the same type (hence the same class name). | |
81 | */ | |
7c913512 | 82 | swap(wxScopedPtr amp; other); |
23324ae1 FM |
83 | }; |
84 | ||
85 | ||
e54c96f1 | 86 | |
23324ae1 FM |
87 | /** |
88 | @class wxScopedArray | |
89 | @wxheader{ptr_scpd.h} | |
7c913512 FM |
90 | |
91 | This is a simple scoped smart pointer array implementation that is similar to | |
23324ae1 FM |
92 | the Boost smart pointers but rewritten to |
93 | use macros instead. | |
7c913512 | 94 | |
23324ae1 FM |
95 | @library{wxbase} |
96 | @category{FIXME} | |
7c913512 | 97 | |
e54c96f1 | 98 | @see wxScopedPtr |
23324ae1 | 99 | */ |
7c913512 | 100 | class wxScopedArray |
23324ae1 FM |
101 | { |
102 | public: | |
103 | /** | |
104 | Creates the smart pointer with the given pointer or none if @NULL. On | |
105 | compilers that support it, this uses the explicit keyword. | |
106 | */ | |
4cc4bfaf | 107 | wxScopedArray(type T = NULL); |
23324ae1 FM |
108 | |
109 | /** | |
110 | This operator gets the pointer stored in the smart pointer or returns @NULL if | |
111 | there is none. | |
112 | */ | |
113 | const T* get(); | |
114 | ||
115 | /** | |
116 | This operator acts like the standard [] indexing operator for C++ arrays. The | |
117 | function does not do bounds checking. | |
118 | */ | |
119 | const T operator [](long int i); | |
120 | ||
121 | /** | |
7c913512 | 122 | Deletes the currently held pointer and sets it to 'p' or to @NULL if no |
23324ae1 FM |
123 | arguments are specified. This function does check to make sure that the |
124 | pointer you are assigning is not the same pointer that is already stored. | |
125 | */ | |
4cc4bfaf | 126 | reset(T p = NULL); |
23324ae1 FM |
127 | |
128 | /** | |
129 | Swap the pointer inside the smart pointer with 'ot'. The pointer being swapped | |
130 | must be of the same type (hence the same class name). | |
131 | */ | |
7c913512 | 132 | swap(wxScopedPtr amp; ot); |
23324ae1 FM |
133 | }; |
134 | ||
135 | ||
e54c96f1 | 136 | |
23324ae1 FM |
137 | /** |
138 | @class wxScopedTiedPtr | |
139 | @wxheader{ptr_scpd.h} | |
7c913512 | 140 | |
23324ae1 | 141 | This is a variation on the topic of wxScopedPtr. This |
cdbcf4c2 | 142 | class is also a smart pointer but in addition it "ties" the pointer value to |
23324ae1 FM |
143 | another variable. In other words, during the life time of this class the value |
144 | of that variable is set to be the same as the value of the pointer itself and | |
145 | it is reset to its old value when the object is destroyed. This class is | |
146 | especially useful when converting the existing code (which may already store | |
147 | the pointers value in some variable) to the smart pointers. | |
7c913512 | 148 | |
23324ae1 FM |
149 | @library{wxbase} |
150 | @category{FIXME} | |
151 | */ | |
7c913512 | 152 | class wxScopedTiedPtr |
23324ae1 FM |
153 | { |
154 | public: | |
155 | /** | |
4cc4bfaf FM |
156 | Constructor creates a smart pointer initialized with @a ptr and stores |
157 | @a ptr in the location specified by @a ppTie which must not be | |
23324ae1 FM |
158 | @NULL. |
159 | */ | |
4cc4bfaf | 160 | wxScopedTiedPtr(T** ppTie, T* ptr); |
23324ae1 FM |
161 | |
162 | /** | |
163 | Destructor frees the pointer help by this object and restores the value stored | |
164 | at the tied location (as specified in the @ref ctor() constructor) | |
165 | to the old value. | |
23324ae1 | 166 | Warning: this location may now contain an uninitialized value if it hadn't been |
7c913512 | 167 | initialized previously, in particular don't count on it magically being |
23324ae1 FM |
168 | @NULL! |
169 | */ | |
170 | ~wxScopedTiedPtr(); | |
171 | }; | |
172 | ||
173 | ||
e54c96f1 | 174 | |
23324ae1 | 175 | /** |
23324ae1 | 176 | @wxheader{ptr_scpd.h} |
7c913512 | 177 | |
23324ae1 FM |
178 | A scoped pointer template class. It is the template version of |
179 | the old-style @ref overview_wxscopedptr "scoped pointer macros". | |
7c913512 | 180 | |
23324ae1 FM |
181 | @library{wxbase} |
182 | @category{FIXME} | |
7c913512 | 183 | |
e54c96f1 | 184 | @see wxSharedPtr, wxWeakRef |
23324ae1 | 185 | */ |
7c913512 | 186 | class wxScopedPtr<T> |
23324ae1 FM |
187 | { |
188 | public: | |
189 | /** | |
190 | Constructor. | |
191 | */ | |
4cc4bfaf | 192 | wxScopedPtrT(T* ptr = NULL); |
23324ae1 FM |
193 | |
194 | /** | |
195 | Destructor. | |
196 | */ | |
197 | ~wxScopedPtrT(); | |
198 | ||
199 | /** | |
200 | Returns pointer to object or @NULL. | |
201 | */ | |
328f5751 | 202 | T* get() const; |
23324ae1 FM |
203 | |
204 | /** | |
7c913512 | 205 | Conversion to a boolean expression (in a variant which is not |
23324ae1 FM |
206 | convertable to anything but a boolean expression). If this class |
207 | contains a valid pointer it will return @e @true, if it contains | |
208 | a @NULL pointer it will return @e @false. | |
209 | */ | |
328f5751 | 210 | operator unspecified_bool_type() const; |
23324ae1 FM |
211 | |
212 | /** | |
213 | Returns a reference to the object. If the internal pointer is @NULL | |
214 | this method will cause an assert in debug mode. | |
215 | */ | |
328f5751 | 216 | T operator*() const; |
23324ae1 FM |
217 | |
218 | /** | |
7c913512 | 219 | Returns pointer to object. If the pointer is @NULL this method will |
23324ae1 FM |
220 | cause an assert in debug mode. |
221 | */ | |
328f5751 | 222 | T* operator-() const; |
23324ae1 FM |
223 | |
224 | /** | |
225 | Releases the current pointer and returns it. | |
226 | Afterwards the caller is responsible for deleting | |
227 | the data contained in the scoped pointer before. | |
228 | */ | |
229 | T* release(); | |
230 | ||
231 | /** | |
232 | Reset pointer to the value of @e ptr. The | |
233 | previous pointer will be deleted. | |
234 | */ | |
4cc4bfaf | 235 | void reset(T* ptr = NULL); |
23324ae1 FM |
236 | |
237 | /** | |
238 | Swaps pointers. | |
239 | */ | |
4cc4bfaf | 240 | void swap(wxScopedPtr<T>& ot); |
23324ae1 | 241 | }; |
e54c96f1 | 242 |