]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/scopedptr.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/scopedptr.h
3 // Purpose: interface of wxScopedPtr
4 // Author: wxWidgets team
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
11 This is a simple scoped smart pointer implementation that is similar to
12 the Boost smart pointers (see http://www.boost.org) but rewritten
13 to use macros instead.
15 Since wxWidgets 2.9.0 there is also a templated version of this class
16 with the same name. See wxScopedPtr<T>.
18 A smart pointer holds a pointer to an object. The memory used by the object is
19 deleted when the smart pointer goes out of scope. This class is different from
20 the @c std::auto_ptr<> in so far as it doesn't provide copy constructor
21 nor assignment operator. This limits what you can do with it but is much less
22 surprising than the "destructive copy" behaviour of the standard class.
26 Below is an example of using a wxWidgets scoped smart pointer and pointer array.
31 // declare a smart pointer to a MyClass called wxMyClassPtr
32 wxDECLARE_SCOPED_PTR(MyClass, wxMyClassPtr)
33 // declare a smart pointer to an array of chars
34 wxDECLARE_SCOPED_ARRAY(char, wxCharArray)
38 // define the first pointer class, must be complete
39 wxDEFINE_SCOPED_PTR(MyClass, wxMyClassPtr)
40 // define the second pointer class
41 wxDEFINE_SCOPED_ARRAY(char, wxCharArray)
43 // create an object with a new pointer to MyClass
44 wxMyClassPtr theObj(new MyClass());
45 // reset the pointer (deletes the previous one)
46 theObj.reset(new MyClass());
51 // create an object with a new array of chars
52 wxCharArray theCharObj(new char[100]);
58 @section scopedptr_newpointers Declaring new smart pointer types
60 To declare the smart pointer class @c CLASSNAME containing pointes to
61 a (possibly incomplete) type @c TYPE you should use
63 wxDECLARE_SCOPED_PTR( TYPE, // type of the values
64 CLASSNAME ); // name of the class
66 And later, when @c TYPE is fully defined, you must also use
68 wxDEFINE_SCOPED_PTR( TYPE, CLASSNAME );
70 to implement the scoped pointer class.
72 The first argument of these macro is the pointer type, the second is the name
73 of the new smart pointer class being created. Below we will use wxScopedPtr
74 to represent the scoped pointer class, but the user may create the class with
77 Alternatively, if you don't have to separate the point of declaration and
78 definition of this class and if you accept the standard naming convention,
79 that is that the scoped pointer for the class @c Foo is called @c FooPtr,
80 you can use a single macro which replaces two macros above:
82 wxDEFINE_SCOPED_PTR_TYPE( TYPE );
84 Once again, in this cass @c CLASSNAME will be @c TYPEPtr.
87 @category{smartpointers}
95 Creates the smart pointer with the given pointer or none if @NULL.
97 On compilers that support it, this uses the explicit keyword.
99 explicit wxScopedPtr(type
* T
= NULL
);
102 Destructor frees the pointer help by this object if it is not @NULL.
107 This operator gets the pointer stored in the smart pointer or returns
108 @NULL if there is none.
113 This operator works like the standard C++ pointer operator to return the object
114 being pointed to by the pointer.
116 If the internal pointer is @NULL this method will cause an assert in debug mode.
118 T
& operator *() const;
121 Smart pointer member access. Returns pointer to its object.
123 If the internal pointer is @NULL this method will cause an assert in debug mode.
125 T
* operator ->() const;
128 Returns the currently hold pointer and resets the smart pointer object to
132 After a call to this function the caller is responsible for deleting the
138 Deletes the currently held pointer and sets it to @a p or to @NULL if no
139 arguments are specified.
142 This function does check to make sure that the pointer you are assigning
143 is not the same pointer that is already stored.
148 Swap the pointer inside the smart pointer with @a other. The pointer being
149 swapped must be of the same type (hence the same class name).
151 swap(wxScopedPtr
& other
);
155 @class wxScopedTiedPtr
157 This is a variation on the topic of wxScopedPtr. This class is also a smart pointer
158 but in addition it "ties" the pointer value to another variable. In other words,
159 during the life time of this class the value of that variable is set to be the same
160 as the value of the pointer itself and it is reset to its old value when the object
161 is destroyed. This class is especially useful when converting the existing code
162 (which may already store the pointers value in some variable) to the smart pointers.
165 @category{smartpointers}
167 class wxScopedTiedPtr
: public wxScopedPtr
171 Constructor creates a smart pointer initialized with @a ptr and stores
172 @a ptr in the location specified by @a ppTie which must not be @NULL.
174 wxScopedTiedPtr(T
** ppTie
, T
* ptr
);
177 Destructor frees the pointer help by this object and restores the value
178 stored at the tied location (as specified in the @ref wxScopedTiedPtr() constructor)
182 This location may now contain an uninitialized value if it hadn't been
183 initialized previously, in particular don't count on it magically being @NULL!
191 A scoped pointer template class.
193 It is the template version of the old-style @ref wxScopedPtr "scoped pointer macros".
195 Notice that objects of this class intentionally cannot be copied.
198 @category{smartpointers}
200 @see wxSharedPtr<T>, wxWeakRef<T>
207 Constructor takes ownership of the pointer.
210 Pointer allocated with @c new or @NULL.
212 wxScopedPtr(T
* ptr
= NULL
);
215 Destructor deletes the pointer.
220 Returns pointer to object or @NULL.
225 Conversion to a boolean expression (in a variant which is not
226 convertible to anything but a boolean expression).
228 If this class contains a valid pointer it will return @true, if it contains
229 a @NULL pointer it will return @false.
231 operator unspecified_bool_type() const;
234 Returns a reference to the object.
236 If the internal pointer is @NULL this method will cause an assert in debug mode.
238 T
& operator*() const;
241 Smart pointer member access. Returns pointer to object.
243 If the internal pointer is @NULL this method will cause an assert in debug mode.
245 T
* operator->() const;
248 Releases the current pointer and returns it.
251 Afterwards the caller is responsible for deleting
252 the data contained in the scoped pointer before.
257 Reset pointer to the value of @a ptr.
258 The previous pointer will be deleted.
260 void reset(T
* ptr
= NULL
);
265 void swap(wxScopedPtr
<T
>& ot
);