]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/scopedarray.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/scopedarray.h
3 // Purpose: interface of wxScopedArray
4 // Author: wxWidgets team
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
11 This is a simple scoped smart pointer array implementation that is similar to
12 the Boost smart pointers (see http://www.boost.org/) but rewritten to
17 Below is an example of using a wxWidgets scoped smart pointer and pointer array.
20 class MyClass { ... };
22 // declare a smart pointer to a MyClass called wxMyClassPtr
23 wxDECLARE_SCOPED_PTR(MyClass, wxMyClassPtr)
24 // declare a smart pointer to an array of chars
25 wxDECLARE_SCOPED_ARRAY(char, wxCharArray)
29 // define the first pointer class, must be complete
30 wxDEFINE_SCOPED_PTR(MyClass, wxMyClassPtr)
31 // define the second pointer class
32 wxDEFINE_SCOPED_ARRAY(char, wxCharArray)
34 // create an object with a new pointer to MyClass
35 wxMyClassPtr theObj(new MyClass());
36 // reset the pointer (deletes the previous one)
37 theObj.reset(new MyClass());
42 // create an object with a new array of chars
43 wxCharArray theCharObj(new char[100]);
49 <b>Declaring new smart pointer types:</b>
51 wxDECLAR_SCOPED_ARRAY( TYPE, // type of the values
52 CLASSNAME ); // name of the class
55 A smart pointer holds a pointer to an object (which must be complete when
56 wxDEFINE_SCOPED_ARRAY() is called).
58 The memory used by the object is deleted when the smart pointer goes out of
59 scope. The first argument of the macro is the pointer type, the second is the
60 name of the new smart pointer class being created. Below we will use wxScopedArray
61 to represent the scoped pointer array class, but the user may create the class with
65 @category{smartpointers}
73 Creates the smart pointer with the given pointer or none if @NULL. On
74 compilers that support it, this uses the explicit keyword.
76 wxScopedArray(type
* T
= NULL
);
79 This operator gets the pointer stored in the smart pointer or returns @NULL if
85 This operator acts like the standard [] indexing operator for C++ arrays. The
86 function does not do bounds checking.
88 const T
& operator [](long int i
);
91 Deletes the currently held pointer and sets it to 'p' or to @NULL if no
92 arguments are specified. This function does check to make sure that the
93 pointer you are assigning is not the same pointer that is already stored.
98 Swap the pointer inside the smart pointer with @a ot. The pointer being swapped
99 must be of the same type (hence the same class name).
101 swap(wxScopedArray
& ot
);
105 A scoped array template class.
107 This class is similar to boost scoped_array class:
108 http://www.boost.org/doc/libs/1_37_0/libs/smart_ptr/scoped_array.htm
110 Notice that objects of this class intentionally cannot be copied.
113 @category{smartpointers}
119 /// The type of the array elements.
120 typedef T element_type
;
123 Constructor takes ownership of the given array.
125 If @a array is @NULL, reset() must presumably be called later.
128 An array allocated using @c new[] or @NULL.
130 explicit wxScopedArray(T
* array
= NULL
);
132 /// Destructor destroy the array.
136 Conversion to a boolean expression (in a variant which is not
137 convertible to anything but a boolean expression).
139 If this class contains a valid array it will return @true, if it contains
140 a @NULL pointer it will return @false.
142 operator unspecified_bool_type() const;
145 Change the array pointer stored.
147 The previously stored array is deleted.
150 An array allocated using @c new[] or @NULL.
152 void reset(T
*array
= NULL
);
155 Return the n-th element of the array.
157 Must not be called if the array has no valid pointer.
159 T
& operator[](size_t n
) const;
162 Return the array pointer.
164 The returned pointer may be @NULL. It must not be deleted by the
165 caller, call @c reset(NULL) instead.
169 /// Swaps the contents of this array with another one.
170 void swap(wxScopedArray
&other
);