]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/scopedptr.h
Somehow, setting a tint color makes gauge work :/.
[wxWidgets.git] / interface / wx / scopedptr.h
CommitLineData
23324ae1 1/////////////////////////////////////////////////////////////////////////////
664e1314 2// Name: wx/scopedptr.h
e54c96f1 3// Purpose: interface of wxScopedPtr
23324ae1 4// Author: wxWidgets team
526954c5 5// Licence: wxWindows licence
23324ae1
FM
6/////////////////////////////////////////////////////////////////////////////
7
8/**
9 @class wxScopedPtr
7c913512
FM
10
11 This is a simple scoped smart pointer implementation that is similar to
b1b95a65
FM
12 the Boost smart pointers (see http://www.boost.org) but rewritten
13 to use macros instead.
7c913512 14
23324ae1 15 Since wxWidgets 2.9.0 there is also a templated version of this class
73473b3e 16 with the same name. See wxScopedPtr<T>.
7c913512 17
23324ae1
FM
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
b1b95a65 20 the @c std::auto_ptr<> in so far as it doesn't provide copy constructor
23324ae1 21 nor assignment operator. This limits what you can do with it but is much less
cbab1556 22 surprising than the "destructive copy" behaviour of the standard class.
7c913512 23
b1b95a65
FM
24 @b Example:
25
26 Below is an example of using a wxWidgets scoped smart pointer and pointer array.
27
28 @code
29 class MyClass{ ... };
30
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)
35
36 ...
37
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)
42
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());
47
48 // access the pointer
49 theObj->MyFunc();
50
51 // create an object with a new array of chars
52 wxCharArray theCharObj(new char[100]);
53
54 // access the array
55 theCharObj[0] = "!";
56 @endcode
57
3a74a290 58 @section scopedptr_newpointers Declaring new smart pointer types
b1b95a65
FM
59
60 To declare the smart pointer class @c CLASSNAME containing pointes to
61 a (possibly incomplete) type @c TYPE you should use
62 @code
63 wxDECLARE_SCOPED_PTR( TYPE, // type of the values
64 CLASSNAME ); // name of the class
65 @endcode
66 And later, when @c TYPE is fully defined, you must also use
67 @code
68 wxDEFINE_SCOPED_PTR( TYPE, CLASSNAME );
69 @endcode
70 to implement the scoped pointer class.
71
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
75 any legal name.
76
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:
81 @code
82 wxDEFINE_SCOPED_PTR_TYPE( TYPE );
83 @endcode
84 Once again, in this cass @c CLASSNAME will be @c TYPEPtr.
85
23324ae1 86 @library{wxbase}
b1b95a65 87 @category{smartpointers}
7c913512 88
e54c96f1 89 @see wxScopedArray
23324ae1 90*/
7c913512 91class wxScopedPtr
23324ae1
FM
92{
93public:
94 /**
b1b95a65
FM
95 Creates the smart pointer with the given pointer or none if @NULL.
96
97 On compilers that support it, this uses the explicit keyword.
23324ae1 98 */
b1b95a65 99 explicit wxScopedPtr(type* T = NULL);
23324ae1
FM
100
101 /**
102 Destructor frees the pointer help by this object if it is not @NULL.
103 */
104 ~wxScopedPtr();
105
106 /**
b1b95a65
FM
107 This operator gets the pointer stored in the smart pointer or returns
108 @NULL if there is none.
23324ae1 109 */
37494cb3 110 T* get() const;
23324ae1
FM
111
112 /**
113 This operator works like the standard C++ pointer operator to return the object
b1b95a65
FM
114 being pointed to by the pointer.
115
37494cb3 116 If the internal pointer is @NULL this method will cause an assert in debug mode.
23324ae1 117 */
37494cb3 118 T& operator *() const;
23324ae1
FM
119
120 /**
37494cb3
RR
121 Smart pointer member access. Returns pointer to its object.
122
123 If the internal pointer is @NULL this method will cause an assert in debug mode.
23324ae1 124 */
37494cb3 125 T* operator ->() const;
23324ae1
FM
126
127 /**
7c913512 128 Returns the currently hold pointer and resets the smart pointer object to
b1b95a65
FM
129 @NULL.
130
131 @remarks
132 After a call to this function the caller is responsible for deleting the
133 pointer.
23324ae1 134 */
4cc4bfaf 135 T* release();
23324ae1
FM
136
137 /**
4cc4bfaf 138 Deletes the currently held pointer and sets it to @a p or to @NULL if no
b1b95a65
FM
139 arguments are specified.
140
141 @note
142 This function does check to make sure that the pointer you are assigning
143 is not the same pointer that is already stored.
23324ae1 144 */
b1b95a65 145 reset(T* p = NULL);
23324ae1
FM
146
147 /**
b1b95a65 148 Swap the pointer inside the smart pointer with @a other. The pointer being
23324ae1
FM
149 swapped must be of the same type (hence the same class name).
150 */
b1b95a65 151 swap(wxScopedPtr& other);
23324ae1
FM
152};
153
23324ae1
FM
154/**
155 @class wxScopedTiedPtr
7c913512 156
b1b95a65
FM
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.
7c913512 163
23324ae1 164 @library{wxbase}
b1b95a65 165 @category{smartpointers}
23324ae1 166*/
4a31b0c3 167class wxScopedTiedPtr : public wxScopedPtr
23324ae1
FM
168{
169public:
170 /**
4cc4bfaf 171 Constructor creates a smart pointer initialized with @a ptr and stores
b1b95a65 172 @a ptr in the location specified by @a ppTie which must not be @NULL.
23324ae1 173 */
4cc4bfaf 174 wxScopedTiedPtr(T** ppTie, T* ptr);
23324ae1
FM
175
176 /**
6c107bd2
FM
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)
23324ae1 179 to the old value.
b1b95a65
FM
180
181 @warning
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!
23324ae1
FM
184 */
185 ~wxScopedTiedPtr();
186};
187
188
e54c96f1 189
23324ae1 190/**
6c107bd2 191 A scoped pointer template class.
058f225a 192
6c107bd2 193 It is the template version of the old-style @ref wxScopedPtr "scoped pointer macros".
7c913512 194
058f225a
VZ
195 Notice that objects of this class intentionally cannot be copied.
196
23324ae1 197 @library{wxbase}
b1b95a65 198 @category{smartpointers}
7c913512 199
73473b3e 200 @see wxSharedPtr<T>, wxWeakRef<T>
23324ae1 201*/
5cb947fd 202template<typename T>
7c913512 203class wxScopedPtr<T>
23324ae1
FM
204{
205public:
206 /**
058f225a
VZ
207 Constructor takes ownership of the pointer.
208
209 @param ptr
210 Pointer allocated with @c new or @NULL.
23324ae1 211 */
5cb947fd 212 wxScopedPtr(T* ptr = NULL);
23324ae1
FM
213
214 /**
058f225a 215 Destructor deletes the pointer.
23324ae1 216 */
5cb947fd 217 ~wxScopedPtr();
23324ae1
FM
218
219 /**
220 Returns pointer to object or @NULL.
221 */
328f5751 222 T* get() const;
23324ae1
FM
223
224 /**
7c913512 225 Conversion to a boolean expression (in a variant which is not
058f225a 226 convertible to anything but a boolean expression).
b1b95a65
FM
227
228 If this class contains a valid pointer it will return @true, if it contains
229 a @NULL pointer it will return @false.
23324ae1 230 */
328f5751 231 operator unspecified_bool_type() const;
23324ae1
FM
232
233 /**
b1b95a65
FM
234 Returns a reference to the object.
235
37494cb3 236 If the internal pointer is @NULL this method will cause an assert in debug mode.
23324ae1 237 */
37494cb3 238 T& operator*() const;
23324ae1
FM
239
240 /**
37494cb3
RR
241 Smart pointer member access. Returns pointer to object.
242
243 If the internal pointer is @NULL this method will cause an assert in debug mode.
23324ae1 244 */
b1b95a65 245 T* operator->() const;
23324ae1
FM
246
247 /**
248 Releases the current pointer and returns it.
b1b95a65
FM
249
250 @remarks
23324ae1
FM
251 Afterwards the caller is responsible for deleting
252 the data contained in the scoped pointer before.
253 */
254 T* release();
255
256 /**
b1b95a65
FM
257 Reset pointer to the value of @a ptr.
258 The previous pointer will be deleted.
23324ae1 259 */
4cc4bfaf 260 void reset(T* ptr = NULL);
23324ae1
FM
261
262 /**
263 Swaps pointers.
264 */
4cc4bfaf 265 void swap(wxScopedPtr<T>& ot);
23324ae1 266};
e54c96f1 267