OLD | NEW |
1 /* | 1 /* |
2 ******************************************************************************* | 2 ******************************************************************************* |
3 * | 3 * |
4 * Copyright (C) 2009-2012, International Business Machines | 4 * Copyright (C) 2009-2015, International Business Machines |
5 * Corporation and others. All Rights Reserved. | 5 * Corporation and others. All Rights Reserved. |
6 * | 6 * |
7 ******************************************************************************* | 7 ******************************************************************************* |
8 * file name: localpointer.h | 8 * file name: localpointer.h |
9 * encoding: US-ASCII | 9 * encoding: US-ASCII |
10 * tab size: 8 (not used) | 10 * tab size: 8 (not used) |
11 * indentation:4 | 11 * indentation:4 |
12 * | 12 * |
13 * created on: 2009nov13 | 13 * created on: 2009nov13 |
14 * created by: Markus W. Scherer | 14 * created by: Markus W. Scherer |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 ptr=p; | 144 ptr=p; |
145 } | 145 } |
146 protected: | 146 protected: |
147 /** | 147 /** |
148 * Actual pointer. | 148 * Actual pointer. |
149 * @internal | 149 * @internal |
150 */ | 150 */ |
151 T *ptr; | 151 T *ptr; |
152 private: | 152 private: |
153 // No comparison operators with other LocalPointerBases. | 153 // No comparison operators with other LocalPointerBases. |
154 bool operator==(const LocalPointerBase &other); | 154 bool operator==(const LocalPointerBase<T> &other); |
155 bool operator!=(const LocalPointerBase &other); | 155 bool operator!=(const LocalPointerBase<T> &other); |
156 // No ownership transfer: No copy constructor, no assignment operator. | 156 // No ownership sharing: No copy constructor, no assignment operator. |
157 LocalPointerBase(const LocalPointerBase &other); | 157 LocalPointerBase(const LocalPointerBase<T> &other); |
158 void operator=(const LocalPointerBase &other); | 158 void operator=(const LocalPointerBase<T> &other); |
159 // No heap allocation. Use only on the stack. | 159 // No heap allocation. Use only on the stack. |
160 static void * U_EXPORT2 operator new(size_t size); | 160 static void * U_EXPORT2 operator new(size_t size); |
161 static void * U_EXPORT2 operator new[](size_t size); | 161 static void * U_EXPORT2 operator new[](size_t size); |
162 #if U_HAVE_PLACEMENT_NEW | 162 #if U_HAVE_PLACEMENT_NEW |
163 static void * U_EXPORT2 operator new(size_t, void *ptr); | 163 static void * U_EXPORT2 operator new(size_t, void *ptr); |
164 #endif | 164 #endif |
165 }; | 165 }; |
166 | 166 |
167 /** | 167 /** |
168 * "Smart pointer" class, deletes objects via the standard C++ delete operator. | 168 * "Smart pointer" class, deletes objects via the standard C++ delete operator. |
(...skipping 15 matching lines...) Expand all Loading... |
184 */ | 184 */ |
185 template<typename T> | 185 template<typename T> |
186 class LocalPointer : public LocalPointerBase<T> { | 186 class LocalPointer : public LocalPointerBase<T> { |
187 public: | 187 public: |
188 /** | 188 /** |
189 * Constructor takes ownership. | 189 * Constructor takes ownership. |
190 * @param p simple pointer to an object that is adopted | 190 * @param p simple pointer to an object that is adopted |
191 * @stable ICU 4.4 | 191 * @stable ICU 4.4 |
192 */ | 192 */ |
193 explicit LocalPointer(T *p=NULL) : LocalPointerBase<T>(p) {} | 193 explicit LocalPointer(T *p=NULL) : LocalPointerBase<T>(p) {} |
| 194 #ifndef U_HIDE_DRAFT_API |
| 195 /** |
| 196 * Constructor takes ownership and reports an error if NULL. |
| 197 * |
| 198 * This constructor is intended to be used with other-class constructors |
| 199 * that may report a failure UErrorCode, |
| 200 * so that callers need to check only for U_FAILURE(errorCode) |
| 201 * and not also separately for isNull(). |
| 202 * |
| 203 * @param p simple pointer to an object that is adopted |
| 204 * @param errorCode in/out UErrorCode, set to U_MEMORY_ALLOCATION_ERROR |
| 205 * if p==NULL and no other failure code had been set |
| 206 * @draft ICU 55 |
| 207 */ |
| 208 LocalPointer(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) { |
| 209 if(p==NULL && U_SUCCESS(errorCode)) { |
| 210 errorCode=U_MEMORY_ALLOCATION_ERROR; |
| 211 } |
| 212 } |
| 213 #if U_HAVE_RVALUE_REFERENCES |
| 214 /** |
| 215 * Move constructor, leaves src with isNull(). |
| 216 * @param src source smart pointer |
| 217 * @draft ICU 56 |
| 218 */ |
| 219 LocalPointer(LocalPointer<T> &&src) U_NOEXCEPT : LocalPointerBase<T>(src.ptr
) { |
| 220 src.ptr=NULL; |
| 221 } |
| 222 #endif |
| 223 #endif /* U_HIDE_DRAFT_API */ |
194 /** | 224 /** |
195 * Destructor deletes the object it owns. | 225 * Destructor deletes the object it owns. |
196 * @stable ICU 4.4 | 226 * @stable ICU 4.4 |
197 */ | 227 */ |
198 ~LocalPointer() { | 228 ~LocalPointer() { |
199 delete LocalPointerBase<T>::ptr; | 229 delete LocalPointerBase<T>::ptr; |
200 } | 230 } |
| 231 #ifndef U_HIDE_DRAFT_API |
| 232 #if U_HAVE_RVALUE_REFERENCES |
| 233 /** |
| 234 * Move assignment operator, leaves src with isNull(). |
| 235 * The behavior is undefined if *this and src are the same object. |
| 236 * @param src source smart pointer |
| 237 * @return *this |
| 238 * @draft ICU 56 |
| 239 */ |
| 240 LocalPointer<T> &operator=(LocalPointer<T> &&src) U_NOEXCEPT { |
| 241 return moveFrom(src); |
| 242 } |
| 243 #endif |
| 244 /** |
| 245 * Move assignment, leaves src with isNull(). |
| 246 * The behavior is undefined if *this and src are the same object. |
| 247 * |
| 248 * Can be called explicitly, does not need C++11 support. |
| 249 * @param src source smart pointer |
| 250 * @return *this |
| 251 * @draft ICU 56 |
| 252 */ |
| 253 LocalPointer<T> &moveFrom(LocalPointer<T> &src) U_NOEXCEPT { |
| 254 delete LocalPointerBase<T>::ptr; |
| 255 LocalPointerBase<T>::ptr=src.ptr; |
| 256 src.ptr=NULL; |
| 257 return *this; |
| 258 } |
| 259 /** |
| 260 * Swap pointers. |
| 261 * @param other other smart pointer |
| 262 * @draft ICU 56 |
| 263 */ |
| 264 void swap(LocalPointer<T> &other) U_NOEXCEPT { |
| 265 T *temp=LocalPointerBase<T>::ptr; |
| 266 LocalPointerBase<T>::ptr=other.ptr; |
| 267 other.ptr=temp; |
| 268 } |
| 269 /** |
| 270 * Non-member LocalPointer swap function. |
| 271 * @param p1 will get p2's pointer |
| 272 * @param p2 will get p1's pointer |
| 273 * @draft ICU 56 |
| 274 */ |
| 275 friend inline void swap(LocalPointer<T> &p1, LocalPointer<T> &p2) U_NOEXCEPT
{ |
| 276 p1.swap(p2); |
| 277 } |
| 278 #endif /* U_HIDE_DRAFT_API */ |
201 /** | 279 /** |
202 * Deletes the object it owns, | 280 * Deletes the object it owns, |
203 * and adopts (takes ownership of) the one passed in. | 281 * and adopts (takes ownership of) the one passed in. |
204 * @param p simple pointer to an object that is adopted | 282 * @param p simple pointer to an object that is adopted |
205 * @stable ICU 4.4 | 283 * @stable ICU 4.4 |
206 */ | 284 */ |
207 void adoptInstead(T *p) { | 285 void adoptInstead(T *p) { |
208 delete LocalPointerBase<T>::ptr; | 286 delete LocalPointerBase<T>::ptr; |
209 LocalPointerBase<T>::ptr=p; | 287 LocalPointerBase<T>::ptr=p; |
210 } | 288 } |
| 289 #ifndef U_HIDE_DRAFT_API |
| 290 /** |
| 291 * Deletes the object it owns, |
| 292 * and adopts (takes ownership of) the one passed in. |
| 293 * |
| 294 * If U_FAILURE(errorCode), then the current object is retained and the new
one deleted. |
| 295 * |
| 296 * If U_SUCCESS(errorCode) but the input pointer is NULL, |
| 297 * then U_MEMORY_ALLOCATION_ERROR is set, |
| 298 * the current object is deleted, and NULL is set. |
| 299 * |
| 300 * @param p simple pointer to an object that is adopted |
| 301 * @param errorCode in/out UErrorCode, set to U_MEMORY_ALLOCATION_ERROR |
| 302 * if p==NULL and no other failure code had been set |
| 303 * @draft ICU 55 |
| 304 */ |
| 305 void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) { |
| 306 if(U_SUCCESS(errorCode)) { |
| 307 delete LocalPointerBase<T>::ptr; |
| 308 LocalPointerBase<T>::ptr=p; |
| 309 if(p==NULL) { |
| 310 errorCode=U_MEMORY_ALLOCATION_ERROR; |
| 311 } |
| 312 } else { |
| 313 delete p; |
| 314 } |
| 315 } |
| 316 #endif /* U_HIDE_DRAFT_API */ |
211 }; | 317 }; |
212 | 318 |
213 /** | 319 /** |
214 * "Smart pointer" class, deletes objects via the C++ array delete[] operator. | 320 * "Smart pointer" class, deletes objects via the C++ array delete[] operator. |
215 * For most methods see the LocalPointerBase base class. | 321 * For most methods see the LocalPointerBase base class. |
216 * Adds operator[] for array item access. | 322 * Adds operator[] for array item access. |
217 * | 323 * |
218 * Usage example: | 324 * Usage example: |
219 * \code | 325 * \code |
220 * LocalArray<UnicodeString> a(new UnicodeString[2]); | 326 * LocalArray<UnicodeString> a(new UnicodeString[2]); |
221 * a[0].append((UChar)0x61); | 327 * a[0].append((UChar)0x61); |
222 * if(some condition) { return; } // no need to explicitly delete the array | 328 * if(some condition) { return; } // no need to explicitly delete the array |
223 * a.adoptInstead(new UnicodeString[4]); | 329 * a.adoptInstead(new UnicodeString[4]); |
224 * a[3].append((UChar)0x62).append((UChar)0x63).reverse(); | 330 * a[3].append((UChar)0x62).append((UChar)0x63).reverse(); |
225 * // no need to explicitly delete the array | 331 * // no need to explicitly delete the array |
226 * \endcode | 332 * \endcode |
227 * | 333 * |
228 * @see LocalPointerBase | 334 * @see LocalPointerBase |
229 * @stable ICU 4.4 | 335 * @stable ICU 4.4 |
230 */ | 336 */ |
231 template<typename T> | 337 template<typename T> |
232 class LocalArray : public LocalPointerBase<T> { | 338 class LocalArray : public LocalPointerBase<T> { |
233 public: | 339 public: |
234 /** | 340 /** |
235 * Constructor takes ownership. | 341 * Constructor takes ownership. |
236 * @param p simple pointer to an array of T objects that is adopted | 342 * @param p simple pointer to an array of T objects that is adopted |
237 * @stable ICU 4.4 | 343 * @stable ICU 4.4 |
238 */ | 344 */ |
239 explicit LocalArray(T *p=NULL) : LocalPointerBase<T>(p) {} | 345 explicit LocalArray(T *p=NULL) : LocalPointerBase<T>(p) {} |
| 346 #ifndef U_HIDE_DRAFT_API |
| 347 /** |
| 348 * Constructor takes ownership and reports an error if NULL. |
| 349 * |
| 350 * This constructor is intended to be used with other-class constructors |
| 351 * that may report a failure UErrorCode, |
| 352 * so that callers need to check only for U_FAILURE(errorCode) |
| 353 * and not also separately for isNull(). |
| 354 * |
| 355 * @param p simple pointer to an array of T objects that is adopted |
| 356 * @param errorCode in/out UErrorCode, set to U_MEMORY_ALLOCATION_ERROR |
| 357 * if p==NULL and no other failure code had been set |
| 358 * @draft ICU 56 |
| 359 */ |
| 360 LocalArray(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) { |
| 361 if(p==NULL && U_SUCCESS(errorCode)) { |
| 362 errorCode=U_MEMORY_ALLOCATION_ERROR; |
| 363 } |
| 364 } |
| 365 #if U_HAVE_RVALUE_REFERENCES |
| 366 /** |
| 367 * Move constructor, leaves src with isNull(). |
| 368 * @param src source smart pointer |
| 369 * @draft ICU 56 |
| 370 */ |
| 371 LocalArray(LocalArray<T> &&src) U_NOEXCEPT : LocalPointerBase<T>(src.ptr) { |
| 372 src.ptr=NULL; |
| 373 } |
| 374 #endif |
| 375 #endif /* U_HIDE_DRAFT_API */ |
240 /** | 376 /** |
241 * Destructor deletes the array it owns. | 377 * Destructor deletes the array it owns. |
242 * @stable ICU 4.4 | 378 * @stable ICU 4.4 |
243 */ | 379 */ |
244 ~LocalArray() { | 380 ~LocalArray() { |
245 delete[] LocalPointerBase<T>::ptr; | 381 delete[] LocalPointerBase<T>::ptr; |
246 } | 382 } |
| 383 #ifndef U_HIDE_DRAFT_API |
| 384 #if U_HAVE_RVALUE_REFERENCES |
| 385 /** |
| 386 * Move assignment operator, leaves src with isNull(). |
| 387 * The behavior is undefined if *this and src are the same object. |
| 388 * @param src source smart pointer |
| 389 * @return *this |
| 390 * @draft ICU 56 |
| 391 */ |
| 392 LocalArray<T> &operator=(LocalArray<T> &&src) U_NOEXCEPT { |
| 393 return moveFrom(src); |
| 394 } |
| 395 #endif |
| 396 /** |
| 397 * Move assignment, leaves src with isNull(). |
| 398 * The behavior is undefined if *this and src are the same object. |
| 399 * |
| 400 * Can be called explicitly, does not need C++11 support. |
| 401 * @param src source smart pointer |
| 402 * @return *this |
| 403 * @draft ICU 56 |
| 404 */ |
| 405 LocalArray<T> &moveFrom(LocalArray<T> &src) U_NOEXCEPT { |
| 406 delete[] LocalPointerBase<T>::ptr; |
| 407 LocalPointerBase<T>::ptr=src.ptr; |
| 408 src.ptr=NULL; |
| 409 return *this; |
| 410 } |
| 411 /** |
| 412 * Swap pointers. |
| 413 * @param other other smart pointer |
| 414 * @draft ICU 56 |
| 415 */ |
| 416 void swap(LocalArray<T> &other) U_NOEXCEPT { |
| 417 T *temp=LocalPointerBase<T>::ptr; |
| 418 LocalPointerBase<T>::ptr=other.ptr; |
| 419 other.ptr=temp; |
| 420 } |
| 421 /** |
| 422 * Non-member LocalArray swap function. |
| 423 * @param p1 will get p2's pointer |
| 424 * @param p2 will get p1's pointer |
| 425 * @draft ICU 56 |
| 426 */ |
| 427 friend inline void swap(LocalArray<T> &p1, LocalArray<T> &p2) U_NOEXCEPT { |
| 428 p1.swap(p2); |
| 429 } |
| 430 #endif /* U_HIDE_DRAFT_API */ |
247 /** | 431 /** |
248 * Deletes the array it owns, | 432 * Deletes the array it owns, |
249 * and adopts (takes ownership of) the one passed in. | 433 * and adopts (takes ownership of) the one passed in. |
250 * @param p simple pointer to an array of T objects that is adopted | 434 * @param p simple pointer to an array of T objects that is adopted |
251 * @stable ICU 4.4 | 435 * @stable ICU 4.4 |
252 */ | 436 */ |
253 void adoptInstead(T *p) { | 437 void adoptInstead(T *p) { |
254 delete[] LocalPointerBase<T>::ptr; | 438 delete[] LocalPointerBase<T>::ptr; |
255 LocalPointerBase<T>::ptr=p; | 439 LocalPointerBase<T>::ptr=p; |
256 } | 440 } |
| 441 #ifndef U_HIDE_DRAFT_API |
| 442 /** |
| 443 * Deletes the array it owns, |
| 444 * and adopts (takes ownership of) the one passed in. |
| 445 * |
| 446 * If U_FAILURE(errorCode), then the current array is retained and the new o
ne deleted. |
| 447 * |
| 448 * If U_SUCCESS(errorCode) but the input pointer is NULL, |
| 449 * then U_MEMORY_ALLOCATION_ERROR is set, |
| 450 * the current array is deleted, and NULL is set. |
| 451 * |
| 452 * @param p simple pointer to an array of T objects that is adopted |
| 453 * @param errorCode in/out UErrorCode, set to U_MEMORY_ALLOCATION_ERROR |
| 454 * if p==NULL and no other failure code had been set |
| 455 * @draft ICU 56 |
| 456 */ |
| 457 void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) { |
| 458 if(U_SUCCESS(errorCode)) { |
| 459 delete[] LocalPointerBase<T>::ptr; |
| 460 LocalPointerBase<T>::ptr=p; |
| 461 if(p==NULL) { |
| 462 errorCode=U_MEMORY_ALLOCATION_ERROR; |
| 463 } |
| 464 } else { |
| 465 delete[] p; |
| 466 } |
| 467 } |
| 468 #endif /* U_HIDE_DRAFT_API */ |
257 /** | 469 /** |
258 * Array item access (writable). | 470 * Array item access (writable). |
259 * No index bounds check. | 471 * No index bounds check. |
260 * @param i array index | 472 * @param i array index |
261 * @return reference to the array item | 473 * @return reference to the array item |
262 * @stable ICU 4.4 | 474 * @stable ICU 4.4 |
263 */ | 475 */ |
264 T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; } | 476 T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; } |
265 }; | 477 }; |
266 | 478 |
(...skipping 13 matching lines...) Expand all Loading... |
280 * utf8OutLength=ucasemap_utf8ToLower(csm.getAlias(), | 492 * utf8OutLength=ucasemap_utf8ToLower(csm.getAlias(), |
281 * utf8Out, (int32_t)sizeof(utf8Out), | 493 * utf8Out, (int32_t)sizeof(utf8Out), |
282 * utf8In, utf8InLength, &errorCode); | 494 * utf8In, utf8InLength, &errorCode); |
283 * if(U_FAILURE(errorCode)) { return; } // no need to explicitly delete the UCa
seMap | 495 * if(U_FAILURE(errorCode)) { return; } // no need to explicitly delete the UCa
seMap |
284 * \endcode | 496 * \endcode |
285 * | 497 * |
286 * @see LocalPointerBase | 498 * @see LocalPointerBase |
287 * @see LocalPointer | 499 * @see LocalPointer |
288 * @stable ICU 4.4 | 500 * @stable ICU 4.4 |
289 */ | 501 */ |
| 502 #if U_HAVE_RVALUE_REFERENCES |
| 503 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction)
\ |
| 504 class LocalPointerClassName : public LocalPointerBase<Type> { \ |
| 505 public: \ |
| 506 explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p)
{} \ |
| 507 LocalPointerClassName(LocalPointerClassName &&src) U_NOEXCEPT \ |
| 508 : LocalPointerBase<Type>(src.ptr) { \ |
| 509 src.ptr=NULL; \ |
| 510 } \ |
| 511 ~LocalPointerClassName() { closeFunction(ptr); } \ |
| 512 LocalPointerClassName &operator=(LocalPointerClassName &&src) U_NOEXCEPT
{ \ |
| 513 return moveFrom(src); \ |
| 514 } \ |
| 515 LocalPointerClassName &moveFrom(LocalPointerClassName &src) U_NOEXCEPT {
\ |
| 516 closeFunction(ptr); \ |
| 517 LocalPointerBase<Type>::ptr=src.ptr; \ |
| 518 src.ptr=NULL; \ |
| 519 return *this; \ |
| 520 } \ |
| 521 void swap(LocalPointerClassName &other) U_NOEXCEPT { \ |
| 522 Type *temp=LocalPointerBase<Type>::ptr; \ |
| 523 LocalPointerBase<Type>::ptr=other.ptr; \ |
| 524 other.ptr=temp; \ |
| 525 } \ |
| 526 friend inline void swap(LocalPointerClassName &p1, LocalPointerClassName
&p2) U_NOEXCEPT { \ |
| 527 p1.swap(p2); \ |
| 528 } \ |
| 529 void adoptInstead(Type *p) { \ |
| 530 closeFunction(ptr); \ |
| 531 ptr=p; \ |
| 532 } \ |
| 533 } |
| 534 #else |
290 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction)
\ | 535 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction)
\ |
291 class LocalPointerClassName : public LocalPointerBase<Type> { \ | 536 class LocalPointerClassName : public LocalPointerBase<Type> { \ |
292 public: \ | 537 public: \ |
293 explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p)
{} \ | 538 explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p)
{} \ |
294 ~LocalPointerClassName() { closeFunction(ptr); } \ | 539 ~LocalPointerClassName() { closeFunction(ptr); } \ |
| 540 LocalPointerClassName &moveFrom(LocalPointerClassName &src) U_NOEXCEPT {
\ |
| 541 closeFunction(ptr); \ |
| 542 LocalPointerBase<Type>::ptr=src.ptr; \ |
| 543 src.ptr=NULL; \ |
| 544 return *this; \ |
| 545 } \ |
| 546 void swap(LocalPointerClassName &other) U_NOEXCEPT { \ |
| 547 Type *temp=LocalPointerBase<Type>::ptr; \ |
| 548 LocalPointerBase<Type>::ptr=other.ptr; \ |
| 549 other.ptr=temp; \ |
| 550 } \ |
| 551 friend inline void swap(LocalPointerClassName &p1, LocalPointerClassName
&p2) U_NOEXCEPT { \ |
| 552 p1.swap(p2); \ |
| 553 } \ |
295 void adoptInstead(Type *p) { \ | 554 void adoptInstead(Type *p) { \ |
296 closeFunction(ptr); \ | 555 closeFunction(ptr); \ |
297 ptr=p; \ | 556 ptr=p; \ |
298 } \ | 557 } \ |
299 } | 558 } |
| 559 #endif |
300 | 560 |
301 U_NAMESPACE_END | 561 U_NAMESPACE_END |
302 | 562 |
303 #endif /* U_SHOW_CPLUSPLUS_API */ | 563 #endif /* U_SHOW_CPLUSPLUS_API */ |
304 #endif /* __LOCALPOINTER_H__ */ | 564 #endif /* __LOCALPOINTER_H__ */ |
OLD | NEW |