]> git.saurik.com Git - wxWidgets.git/blame - interface/ptr_scpd.h
GetSocketManager() has no GUI-specific version under Darwin finally
[wxWidgets.git] / interface / ptr_scpd.h
CommitLineData
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
b1b95a65
FM
14 the Boost smart pointers (see http://www.boost.org) but rewritten
15 to use macros instead.
7c913512 16
23324ae1 17 Since wxWidgets 2.9.0 there is also a templated version of this class
73473b3e 18 with the same name. See wxScopedPtr<T>.
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
b1b95a65 22 the @c std::auto_ptr<> in so far as it doesn't provide copy constructor
23324ae1 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
b1b95a65
FM
26 @b Example:
27
28 Below is an example of using a wxWidgets scoped smart pointer and pointer array.
29
30 @code
31 class MyClass{ ... };
32
33 // declare a smart pointer to a MyClass called wxMyClassPtr
34 wxDECLARE_SCOPED_PTR(MyClass, wxMyClassPtr)
35 // declare a smart pointer to an array of chars
36 wxDECLARE_SCOPED_ARRAY(char, wxCharArray)
37
38 ...
39
40 // define the first pointer class, must be complete
41 wxDEFINE_SCOPED_PTR(MyClass, wxMyClassPtr)
42 // define the second pointer class
43 wxDEFINE_SCOPED_ARRAY(char, wxCharArray)
44
45 // create an object with a new pointer to MyClass
46 wxMyClassPtr theObj(new MyClass());
47 // reset the pointer (deletes the previous one)
48 theObj.reset(new MyClass());
49
50 // access the pointer
51 theObj->MyFunc();
52
53 // create an object with a new array of chars
54 wxCharArray theCharObj(new char[100]);
55
56 // access the array
57 theCharObj[0] = "!";
58 @endcode
59
60 @section wxscopedptr_newpointers Declaring new smart pointer types
61
62 To declare the smart pointer class @c CLASSNAME containing pointes to
63 a (possibly incomplete) type @c TYPE you should use
64 @code
65 wxDECLARE_SCOPED_PTR( TYPE, // type of the values
66 CLASSNAME ); // name of the class
67 @endcode
68 And later, when @c TYPE is fully defined, you must also use
69 @code
70 wxDEFINE_SCOPED_PTR( TYPE, CLASSNAME );
71 @endcode
72 to implement the scoped pointer class.
73
74 The first argument of these macro is the pointer type, the second is the name
75 of the new smart pointer class being created. Below we will use wxScopedPtr
76 to represent the scoped pointer class, but the user may create the class with
77 any legal name.
78
79 Alternatively, if you don't have to separate the point of declaration and
80 definition of this class and if you accept the standard naming convention,
81 that is that the scoped pointer for the class @c Foo is called @c FooPtr,
82 you can use a single macro which replaces two macros above:
83 @code
84 wxDEFINE_SCOPED_PTR_TYPE( TYPE );
85 @endcode
86 Once again, in this cass @c CLASSNAME will be @c TYPEPtr.
87
23324ae1 88 @library{wxbase}
b1b95a65 89 @category{smartpointers}
7c913512 90
e54c96f1 91 @see wxScopedArray
23324ae1 92*/
7c913512 93class wxScopedPtr
23324ae1
FM
94{
95public:
96 /**
b1b95a65
FM
97 Creates the smart pointer with the given pointer or none if @NULL.
98
99 On compilers that support it, this uses the explicit keyword.
23324ae1 100 */
b1b95a65 101 explicit wxScopedPtr(type* T = NULL);
23324ae1
FM
102
103 /**
104 Destructor frees the pointer help by this object if it is not @NULL.
105 */
106 ~wxScopedPtr();
107
108 /**
b1b95a65
FM
109 This operator gets the pointer stored in the smart pointer or returns
110 @NULL if there is none.
23324ae1
FM
111 */
112 const T* get();
113
114 /**
115 This operator works like the standard C++ pointer operator to return the object
b1b95a65
FM
116 being pointed to by the pointer.
117
118 @note
119 If the pointer is @NULL or invalid this will crash.
23324ae1 120 */
b1b95a65 121 const T& operator *();
23324ae1
FM
122
123 /**
124 This operator works like the standard C++ pointer operator to return the pointer
125 in the smart pointer or @NULL if it is empty.
126 */
b1b95a65 127 const T* operator ->();
23324ae1
FM
128
129 /**
7c913512 130 Returns the currently hold pointer and resets the smart pointer object to
b1b95a65
FM
131 @NULL.
132
133 @remarks
134 After a call to this function the caller is responsible for deleting the
135 pointer.
23324ae1 136 */
4cc4bfaf 137 T* release();
23324ae1
FM
138
139 /**
4cc4bfaf 140 Deletes the currently held pointer and sets it to @a p or to @NULL if no
b1b95a65
FM
141 arguments are specified.
142
143 @note
144 This function does check to make sure that the pointer you are assigning
145 is not the same pointer that is already stored.
23324ae1 146 */
b1b95a65 147 reset(T* p = NULL);
23324ae1
FM
148
149 /**
b1b95a65 150 Swap the pointer inside the smart pointer with @a other. The pointer being
23324ae1
FM
151 swapped must be of the same type (hence the same class name).
152 */
b1b95a65 153 swap(wxScopedPtr& other);
23324ae1
FM
154};
155
156
e54c96f1 157
23324ae1
FM
158/**
159 @class wxScopedArray
160 @wxheader{ptr_scpd.h}
7c913512
FM
161
162 This is a simple scoped smart pointer array implementation that is similar to
b1b95a65 163 the Boost smart pointers (see http://www.boost.org/) but rewritten to
23324ae1 164 use macros instead.
7c913512 165
b1b95a65
FM
166 @b Example:
167
168 Below is an example of using a wxWidgets scoped smart pointer and pointer array.
169
170 @code
171 class MyClass { ... };
172
173 // declare a smart pointer to a MyClass called wxMyClassPtr
174 wxDECLARE_SCOPED_PTR(MyClass, wxMyClassPtr)
175 // declare a smart pointer to an array of chars
176 wxDECLARE_SCOPED_ARRAY(char, wxCharArray)
177
178 ...
179
180 // define the first pointer class, must be complete
181 wxDEFINE_SCOPED_PTR(MyClass, wxMyClassPtr)
182 // define the second pointer class
183 wxDEFINE_SCOPED_ARRAY(char, wxCharArray)
184
185 // create an object with a new pointer to MyClass
186 wxMyClassPtr theObj(new MyClass());
187 // reset the pointer (deletes the previous one)
188 theObj.reset(new MyClass());
189
190 // access the pointer
191 theObj->MyFunc();
192
193 // create an object with a new array of chars
194 wxCharArray theCharObj(new char[100]);
195
196 // access the array
197 theCharObj[0] = "!";
198 @endcode
199
200 <b>Declaring new smart pointer types:</b>
201 @code
202 wxDECLAR_SCOPED_ARRAY( TYPE, // type of the values
203 CLASSNAME ); // name of the class
204 @endcode
205
206 A smart pointer holds a pointer to an object (which must be complete when
207 wxDEFINE_SCOPED_ARRAY() is called).
208
209 The memory used by the object is deleted when the smart pointer goes out of
210 scope. The first argument of the macro is the pointer type, the second is the
211 name of the new smart pointer class being created. Below we will use wxScopedArray
212 to represent the scoped pointer array class, but the user may create the class with
213 any legal name.
214
23324ae1 215 @library{wxbase}
b1b95a65 216 @category{smartpointers}
7c913512 217
e54c96f1 218 @see wxScopedPtr
23324ae1 219*/
7c913512 220class wxScopedArray
23324ae1
FM
221{
222public:
223 /**
224 Creates the smart pointer with the given pointer or none if @NULL. On
225 compilers that support it, this uses the explicit keyword.
226 */
b1b95a65 227 wxScopedArray(type* T = NULL);
23324ae1
FM
228
229 /**
230 This operator gets the pointer stored in the smart pointer or returns @NULL if
231 there is none.
232 */
233 const T* get();
234
235 /**
236 This operator acts like the standard [] indexing operator for C++ arrays. The
237 function does not do bounds checking.
238 */
b1b95a65 239 const T& operator [](long int i);
23324ae1
FM
240
241 /**
7c913512 242 Deletes the currently held pointer and sets it to 'p' or to @NULL if no
23324ae1
FM
243 arguments are specified. This function does check to make sure that the
244 pointer you are assigning is not the same pointer that is already stored.
245 */
b1b95a65 246 reset(T* p = NULL);
23324ae1
FM
247
248 /**
b1b95a65 249 Swap the pointer inside the smart pointer with @a ot. The pointer being swapped
23324ae1
FM
250 must be of the same type (hence the same class name).
251 */
b1b95a65 252 swap(wxScopedPtr& ot);
23324ae1
FM
253};
254
255
e54c96f1 256
23324ae1
FM
257/**
258 @class wxScopedTiedPtr
259 @wxheader{ptr_scpd.h}
7c913512 260
b1b95a65
FM
261 This is a variation on the topic of wxScopedPtr. This class is also a smart pointer
262 but in addition it "ties" the pointer value to another variable. In other words,
263 during the life time of this class the value of that variable is set to be the same
264 as the value of the pointer itself and it is reset to its old value when the object
265 is destroyed. This class is especially useful when converting the existing code
266 (which may already store the pointers value in some variable) to the smart pointers.
7c913512 267
23324ae1 268 @library{wxbase}
b1b95a65 269 @category{smartpointers}
23324ae1 270*/
4a31b0c3 271class wxScopedTiedPtr : public wxScopedPtr
23324ae1
FM
272{
273public:
274 /**
4cc4bfaf 275 Constructor creates a smart pointer initialized with @a ptr and stores
b1b95a65 276 @a ptr in the location specified by @a ppTie which must not be @NULL.
23324ae1 277 */
4cc4bfaf 278 wxScopedTiedPtr(T** ppTie, T* ptr);
23324ae1
FM
279
280 /**
281 Destructor frees the pointer help by this object and restores the value stored
282 at the tied location (as specified in the @ref ctor() constructor)
283 to the old value.
b1b95a65
FM
284
285 @warning
286 This location may now contain an uninitialized value if it hadn't been
287 initialized previously, in particular don't count on it magically being @NULL!
23324ae1
FM
288 */
289 ~wxScopedTiedPtr();
290};
291
292
e54c96f1 293
23324ae1 294/**
23324ae1 295 @wxheader{ptr_scpd.h}
7c913512 296
23324ae1 297 A scoped pointer template class. It is the template version of
b1b95a65 298 the old-style @ref classwx_scoped_ptr "scoped pointer macros".
7c913512 299
23324ae1 300 @library{wxbase}
b1b95a65 301 @category{smartpointers}
7c913512 302
73473b3e 303 @see wxSharedPtr<T>, wxWeakRef<T>
23324ae1 304*/
5cb947fd 305template<typename T>
7c913512 306class wxScopedPtr<T>
23324ae1
FM
307{
308public:
309 /**
310 Constructor.
311 */
5cb947fd 312 wxScopedPtr(T* ptr = NULL);
23324ae1
FM
313
314 /**
315 Destructor.
316 */
5cb947fd 317 ~wxScopedPtr();
23324ae1
FM
318
319 /**
320 Returns pointer to object or @NULL.
321 */
328f5751 322 T* get() const;
23324ae1
FM
323
324 /**
7c913512 325 Conversion to a boolean expression (in a variant which is not
b1b95a65
FM
326 convertable to anything but a boolean expression).
327
328 If this class contains a valid pointer it will return @true, if it contains
329 a @NULL pointer it will return @false.
23324ae1 330 */
328f5751 331 operator unspecified_bool_type() const;
23324ae1
FM
332
333 /**
b1b95a65
FM
334 Returns a reference to the object.
335
336 @note
337 If the internal pointer is @NULL this method will cause an assert
338 in debug mode.
23324ae1 339 */
328f5751 340 T operator*() const;
23324ae1
FM
341
342 /**
7c913512 343 Returns pointer to object. If the pointer is @NULL this method will
23324ae1
FM
344 cause an assert in debug mode.
345 */
b1b95a65 346 T* operator->() const;
23324ae1
FM
347
348 /**
349 Releases the current pointer and returns it.
b1b95a65
FM
350
351 @remarks
23324ae1
FM
352 Afterwards the caller is responsible for deleting
353 the data contained in the scoped pointer before.
354 */
355 T* release();
356
357 /**
b1b95a65
FM
358 Reset pointer to the value of @a ptr.
359 The previous pointer will be deleted.
23324ae1 360 */
4cc4bfaf 361 void reset(T* ptr = NULL);
23324ae1
FM
362
363 /**
364 Swaps pointers.
365 */
4cc4bfaf 366 void swap(wxScopedPtr<T>& ot);
23324ae1 367};
e54c96f1 368