]>
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
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
12 This is a simple scoped smart pointer array implementation that is similar to
13 the Boost smart pointers (see http://www.boost.org/) but rewritten to
18 Below is an example of using a wxWidgets scoped smart pointer and pointer array.
21 class MyClass { ... };
23 // declare a smart pointer to a MyClass called wxMyClassPtr
24 wxDECLARE_SCOPED_PTR(MyClass, wxMyClassPtr)
25 // declare a smart pointer to an array of chars
26 wxDECLARE_SCOPED_ARRAY(char, wxCharArray)
30 // define the first pointer class, must be complete
31 wxDEFINE_SCOPED_PTR(MyClass, wxMyClassPtr)
32 // define the second pointer class
33 wxDEFINE_SCOPED_ARRAY(char, wxCharArray)
35 // create an object with a new pointer to MyClass
36 wxMyClassPtr theObj(new MyClass());
37 // reset the pointer (deletes the previous one)
38 theObj.reset(new MyClass());
43 // create an object with a new array of chars
44 wxCharArray theCharObj(new char[100]);
50 <b>Declaring new smart pointer types:</b>
52 wxDECLAR_SCOPED_ARRAY( TYPE, // type of the values
53 CLASSNAME ); // name of the class
56 A smart pointer holds a pointer to an object (which must be complete when
57 wxDEFINE_SCOPED_ARRAY() is called).
59 The memory used by the object is deleted when the smart pointer goes out of
60 scope. The first argument of the macro is the pointer type, the second is the
61 name of the new smart pointer class being created. Below we will use wxScopedArray
62 to represent the scoped pointer array class, but the user may create the class with
66 @category{smartpointers}
74 Creates the smart pointer with the given pointer or none if @NULL. On
75 compilers that support it, this uses the explicit keyword.
77 wxScopedArray(type
* T
= NULL
);
80 This operator gets the pointer stored in the smart pointer or returns @NULL if
86 This operator acts like the standard [] indexing operator for C++ arrays. The
87 function does not do bounds checking.
89 const T
& operator [](long int i
);
92 Deletes the currently held pointer and sets it to 'p' or to @NULL if no
93 arguments are specified. This function does check to make sure that the
94 pointer you are assigning is not the same pointer that is already stored.
99 Swap the pointer inside the smart pointer with @a ot. The pointer being swapped
100 must be of the same type (hence the same class name).
102 swap(wxScopedArray
& ot
);
106 A scoped array template class.
108 This class is similar to boost scoped_array class:
109 http://www.boost.org/doc/libs/1_37_0/libs/smart_ptr/scoped_array.htm
111 Notice that objects of this class intentionally cannot be copied.
114 @category{smartpointers}
120 /// The type of the array elements.
121 typedef T element_type
;
124 Constructor takes ownership of the given array.
126 If @a array is @NULL, reset() must presumably be called later.
129 An array allocated using @c new[] or @NULL.
131 explicit wxScopedArray(T
* array
= NULL
);
133 /// Destructor destroy the array.
137 Conversion to a boolean expression (in a variant which is not
138 convertible to anything but a boolean expression).
140 If this class contains a valid array it will return @true, if it contains
141 a @NULL pointer it will return @false.
143 operator unspecified_bool_type() const;
146 Change the array pointer stored.
148 The previously stored array is deleted.
151 An array allocated using @c new[] or @NULL.
153 void reset(T
*array
= NULL
);
156 Return the n-th element of the array.
158 Must not be called if the array has no valid pointer.
160 T
& operator[](size_t n
) const;
163 Return the array pointer.
165 The returned pointer may be @NULL. It must not be deleted by the
166 caller, call @c reset(NULL) instead.
170 /// Swaps the contents of this array with another one.
171 void swap(wxScopedArray
&other
);