]>
git.saurik.com Git - wxWidgets.git/blob - interface/ptr_scpd.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxScopedPtr
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
13 This is a simple scoped smart pointer implementation that is similar to
14 the Boost smart pointers (see http://www.boost.org) but rewritten
15 to use macros instead.
17 Since wxWidgets 2.9.0 there is also a templated version of this class
18 with the same name. See wxScopedPtr<T>.
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
24 surprizing than the "destructive copy" behaviour of the standard class.
28 Below is an example of using a wxWidgets scoped smart pointer and pointer array.
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)
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)
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());
53 // create an object with a new array of chars
54 wxCharArray theCharObj(new char[100]);
60 @section wxscopedptr_newpointers Declaring new smart pointer types
62 To declare the smart pointer class @c CLASSNAME containing pointes to
63 a (possibly incomplete) type @c TYPE you should use
65 wxDECLARE_SCOPED_PTR( TYPE, // type of the values
66 CLASSNAME ); // name of the class
68 And later, when @c TYPE is fully defined, you must also use
70 wxDEFINE_SCOPED_PTR( TYPE, CLASSNAME );
72 to implement the scoped pointer class.
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
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:
84 wxDEFINE_SCOPED_PTR_TYPE( TYPE );
86 Once again, in this cass @c CLASSNAME will be @c TYPEPtr.
89 @category{smartpointers}
97 Creates the smart pointer with the given pointer or none if @NULL.
99 On compilers that support it, this uses the explicit keyword.
101 explicit wxScopedPtr(type
* T
= NULL
);
104 Destructor frees the pointer help by this object if it is not @NULL.
109 This operator gets the pointer stored in the smart pointer or returns
110 @NULL if there is none.
115 This operator works like the standard C++ pointer operator to return the object
116 being pointed to by the pointer.
119 If the pointer is @NULL or invalid this will crash.
121 const T
& operator *();
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.
127 const T
* operator ->();
130 Returns the currently hold pointer and resets the smart pointer object to
134 After a call to this function the caller is responsible for deleting the
140 Deletes the currently held pointer and sets it to @a p or to @NULL if no
141 arguments are specified.
144 This function does check to make sure that the pointer you are assigning
145 is not the same pointer that is already stored.
150 Swap the pointer inside the smart pointer with @a other. The pointer being
151 swapped must be of the same type (hence the same class name).
153 swap(wxScopedPtr
& other
);
160 @wxheader{ptr_scpd.h}
162 This is a simple scoped smart pointer array implementation that is similar to
163 the Boost smart pointers (see http://www.boost.org/) but rewritten to
168 Below is an example of using a wxWidgets scoped smart pointer and pointer array.
171 class MyClass { ... };
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)
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)
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());
190 // access the pointer
193 // create an object with a new array of chars
194 wxCharArray theCharObj(new char[100]);
200 <b>Declaring new smart pointer types:</b>
202 wxDECLAR_SCOPED_ARRAY( TYPE, // type of the values
203 CLASSNAME ); // name of the class
206 A smart pointer holds a pointer to an object (which must be complete when
207 wxDEFINE_SCOPED_ARRAY() is called).
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
216 @category{smartpointers}
224 Creates the smart pointer with the given pointer or none if @NULL. On
225 compilers that support it, this uses the explicit keyword.
227 wxScopedArray(type
* T
= NULL
);
230 This operator gets the pointer stored in the smart pointer or returns @NULL if
236 This operator acts like the standard [] indexing operator for C++ arrays. The
237 function does not do bounds checking.
239 const T
& operator [](long int i
);
242 Deletes the currently held pointer and sets it to 'p' or to @NULL if no
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.
249 Swap the pointer inside the smart pointer with @a ot. The pointer being swapped
250 must be of the same type (hence the same class name).
252 swap(wxScopedPtr
& ot
);
258 @class wxScopedTiedPtr
259 @wxheader{ptr_scpd.h}
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.
269 @category{smartpointers}
271 class wxScopedTiedPtr
: public wxScopedPtr
275 Constructor creates a smart pointer initialized with @a ptr and stores
276 @a ptr in the location specified by @a ppTie which must not be @NULL.
278 wxScopedTiedPtr(T
** ppTie
, T
* ptr
);
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)
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!
295 @wxheader{ptr_scpd.h}
297 A scoped pointer template class. It is the template version of
298 the old-style @ref classwx_scoped_ptr "scoped pointer macros".
301 @category{smartpointers}
303 @see wxSharedPtr<T>, wxWeakRef<T>
312 wxScopedPtr(T
* ptr
= NULL
);
320 Returns pointer to object or @NULL.
325 Conversion to a boolean expression (in a variant which is not
326 convertable to anything but a boolean expression).
328 If this class contains a valid pointer it will return @true, if it contains
329 a @NULL pointer it will return @false.
331 operator unspecified_bool_type() const;
334 Returns a reference to the object.
337 If the internal pointer is @NULL this method will cause an assert
343 Returns pointer to object. If the pointer is @NULL this method will
344 cause an assert in debug mode.
346 T
* operator->() const;
349 Releases the current pointer and returns it.
352 Afterwards the caller is responsible for deleting
353 the data contained in the scoped pointer before.
358 Reset pointer to the value of @a ptr.
359 The previous pointer will be deleted.
361 void reset(T
* ptr
= NULL
);
366 void swap(wxScopedPtr
<T
>& ot
);