1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: AutoPtr class for array. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 /** |
|
20 * Holds reference to an auto_arrayptr. Prevents unsafe copying |
|
21 * |
|
22 * ?more_complete_description |
|
23 * |
|
24 * @lib ?library |
|
25 * @since S60 ?S60_version *** for example, S60 v3.0 |
|
26 */ |
|
27 |
|
28 template<typename Y> |
|
29 struct auto_arrayptr_ref |
|
30 { |
|
31 Y* iPtr; |
|
32 |
|
33 explicit |
|
34 auto_arrayptr_ref(Y* aPtr): iPtr(aPtr) { } |
|
35 }; |
|
36 |
|
37 /** |
|
38 * Auto pointer template implementation for C++ arrays |
|
39 * |
|
40 * ?more_complete_description |
|
41 * |
|
42 * @lib ?library |
|
43 * @since S60 ?S60_version *** for example, S60 v3.0 |
|
44 */ |
|
45 template<typename X> |
|
46 class auto_arrayptr |
|
47 { |
|
48 private: |
|
49 X* iPtr; |
|
50 |
|
51 public: |
|
52 |
|
53 typedef X element_type; |
|
54 |
|
55 /** |
|
56 * Constructor. |
|
57 * |
|
58 * @since S60 ?S60_version |
|
59 * @param ?arg1 ?description |
|
60 * @param ?arg2 ?description |
|
61 * @return ?description |
|
62 */ |
|
63 explicit |
|
64 auto_arrayptr(X* p = 0) throw() : iPtr(p) { } |
|
65 /** |
|
66 * Copy argument a to this object. Owned pointer ownership is transferred to this. |
|
67 * |
|
68 * @since S60 ?S60_version |
|
69 * @param ?arg1 ?description |
|
70 * @param ?arg2 ?description |
|
71 * @return ?description |
|
72 */ |
|
73 |
|
74 auto_arrayptr(auto_arrayptr& a) throw() : iPtr(a.release()) { } |
|
75 /** |
|
76 * Copy argument a to this object. Owned pointer ownership is transferred to this. |
|
77 * |
|
78 * @since S60 ?S60_version |
|
79 * @param ?arg1 ?description |
|
80 * @param ?arg2 ?description |
|
81 * @return ?description |
|
82 */ |
|
83 |
|
84 template<typename Y> |
|
85 auto_arrayptr(auto_arrayptr<Y>& a) throw() : iPtr(a.release()) { } |
|
86 |
|
87 /** |
|
88 * Assign argument a to this object. If this already owned pointer that pointer is deleted. |
|
89 * |
|
90 * @since S60 ?S60_version |
|
91 * @param ?arg1 ?description |
|
92 * @param ?arg2 ?description |
|
93 * @return ?description |
|
94 */ |
|
95 auto_arrayptr& operator=(auto_arrayptr& a) throw() |
|
96 { |
|
97 reset(a.release()); |
|
98 return *this; |
|
99 } |
|
100 /** |
|
101 * Assign argument a to this object. If this already owned pointer that pointer is deleted. |
|
102 * |
|
103 * @since S60 ?S60_version |
|
104 * @param ?arg1 ?description |
|
105 * @param ?arg2 ?description |
|
106 * @return ?description |
|
107 */ |
|
108 |
|
109 template<typename Y> |
|
110 auto_arrayptr& operator=(auto_arrayptr<Y>& a) throw() |
|
111 { |
|
112 reset(a.release()); |
|
113 return *this; |
|
114 } |
|
115 /** |
|
116 * Deletes the underlying pointer. |
|
117 * |
|
118 * @since S60 ?S60_version |
|
119 * @param ?arg1 ?description |
|
120 * @param ?arg2 ?description |
|
121 * @return ?description |
|
122 */ |
|
123 ~auto_arrayptr() { delete [] iPtr; } |
|
124 |
|
125 /** |
|
126 * Return owned pointer |
|
127 * |
|
128 * @since S60 ?S60_version |
|
129 * @param ?arg1 ?description |
|
130 * @param ?arg2 ?description |
|
131 * @return ?description |
|
132 */ |
|
133 |
|
134 X* get() const throw() { return iPtr; } |
|
135 /** |
|
136 * Releases ownership of the managed pointer and returns that pointer. |
|
137 * The *this object is left holding a null pointer. |
|
138 * |
|
139 * @since S60 ?S60_version |
|
140 * @param ?arg1 ?description |
|
141 * @param ?arg2 ?description |
|
142 * @return ?description |
|
143 */ |
|
144 |
|
145 X* release() throw() |
|
146 { |
|
147 X* tmp = iPtr; |
|
148 iPtr = 0; |
|
149 return tmp; |
|
150 } |
|
151 |
|
152 /** |
|
153 * Reset managed pointer. Deletes previously owned pointer. |
|
154 * The *this object is holding given pointer. |
|
155 * |
|
156 * @since S60 ?S60_version |
|
157 * @param ?arg1 ?description |
|
158 * @param ?arg2 ?description |
|
159 * @return ?description |
|
160 */ |
|
161 void reset(X* aPtr = 0) throw() |
|
162 { |
|
163 if (aPtr != iPtr) |
|
164 { |
|
165 delete [] iPtr; |
|
166 iPtr = aPtr; |
|
167 } |
|
168 } |
|
169 |
|
170 |
|
171 /** |
|
172 * Constructor for value object.Uses type conversion function. |
|
173 * |
|
174 * @since S60 ?S60_version |
|
175 * @param ?arg1 ?description |
|
176 * @param ?arg2 ?description |
|
177 * @return ?description |
|
178 */ |
|
179 auto_arrayptr(auto_arrayptr_ref<X> aRef) throw() |
|
180 : iPtr(aRef.iPtr) { } |
|
181 |
|
182 /** |
|
183 * Assign argument a (value object) to this object. If this already owned pointer that pointer is deleted. |
|
184 * Uses type conversion function. |
|
185 * |
|
186 * @since S60 ?S60_version |
|
187 * @param ?arg1 ?description |
|
188 * @param ?arg2 ?description |
|
189 * @return ?description |
|
190 */ |
|
191 auto_arrayptr& operator=(auto_arrayptr_ref<X> aRef) throw() |
|
192 { |
|
193 if (aRef.iPtr != this->get()) |
|
194 { |
|
195 delete [] iPtr; |
|
196 iPtr = aRef.iPtr; |
|
197 } |
|
198 return *this; |
|
199 } |
|
200 |
|
201 /** |
|
202 * Type conversion. Constructs an auto_arrayptr_ref from *this and returns it. |
|
203 * |
|
204 * @since S60 ?S60_version |
|
205 * @param ?arg1 ?description |
|
206 * @param ?arg2 ?description |
|
207 * @return ?description |
|
208 */ |
|
209 template<typename Y> |
|
210 operator auto_arrayptr_ref<Y>() throw() |
|
211 { return auto_arrayptr_ref<Y>(this->release()); } |
|
212 |
|
213 /** |
|
214 * Type conversion. Constructs a new auto_arrayptr using the underlying pointer held by *this. |
|
215 * Calls release() on *this, so *this no longer possesses the pointer. |
|
216 * Returns the new auto_arrayptr. |
|
217 * |
|
218 * @since S60 ?S60_version |
|
219 * @param ?arg1 ?description |
|
220 * @param ?arg2 ?description |
|
221 * @return ?description |
|
222 */ |
|
223 template<typename Y> |
|
224 operator auto_arrayptr<Y>() throw() |
|
225 { return auto_arrayptr<Y>(this->release()); } |
|
226 }; |
|