1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 2009-2016, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
10 * file name: localpointer.h
12 * tab size: 8 (not used)
15 * created on: 2009nov13
16 * created by: Markus W. Scherer
19 #ifndef __LOCALPOINTER_H__
20 #define __LOCALPOINTER_H__
24 * \brief C++ API: "Smart pointers" for use with and in ICU4C C++ code.
26 * These classes are inspired by
28 * - boost::scoped_ptr & boost::scoped_array
29 * - Taligent Safe Pointers (TOnlyPointerTo)
31 * but none of those provide for all of the goals for ICU smart pointers:
32 * - Smart pointer owns the object and releases it when it goes out of scope.
33 * - No transfer of ownership via copy/assignment to reduce misuse. Simpler & more robust.
34 * - ICU-compatible: No exceptions.
35 * - Need to be able to orphan/release the pointer and its ownership.
36 * - Need variants for normal C++ object pointers, C++ arrays, and ICU C service objects.
38 * For details see http://site.icu-project.org/design/cpp/scoped_ptr
41 #include "unicode/utypes.h"
43 #if U_SHOW_CPLUSPLUS_API
48 * "Smart pointer" base class; do not use directly: use LocalPointer etc.
50 * Base class for smart pointer classes that do not throw exceptions.
52 * Do not use this base class directly, since it does not delete its pointer.
53 * A subclass must implement methods that delete the pointer:
54 * Destructor and adoptInstead().
56 * There is no operator T *() provided because the programmer must decide
57 * whether to use getAlias() (without transfer of ownership) or orphan()
58 * (with transfer of ownership and NULLing of the pointer).
62 * @see U_DEFINE_LOCAL_OPEN_POINTER
66 class LocalPointerBase {
69 * Constructor takes ownership.
70 * @param p simple pointer to an object that is adopted
73 explicit LocalPointerBase(T *p=NULL) : ptr(p) {}
75 * Destructor deletes the object it owns.
76 * Subclass must override: Base class does nothing.
79 ~LocalPointerBase() { /* delete ptr; */ }
82 * @return TRUE if ==NULL
85 UBool isNull() const { return ptr==NULL; }
88 * @return TRUE if !=NULL
91 UBool isValid() const { return ptr!=NULL; }
93 * Comparison with a simple pointer, so that existing code
94 * with ==NULL need not be changed.
95 * @param other simple pointer for comparison
96 * @return true if this pointer value equals other
99 bool operator==(const T *other) const { return ptr==other; }
101 * Comparison with a simple pointer, so that existing code
102 * with !=NULL need not be changed.
103 * @param other simple pointer for comparison
104 * @return true if this pointer value differs from other
107 bool operator!=(const T *other) const { return ptr!=other; }
109 * Access without ownership change.
110 * @return the pointer value
113 T *getAlias() const { return ptr; }
115 * Access without ownership change.
116 * @return the pointer value as a reference
119 T &operator*() const { return *ptr; }
121 * Access without ownership change.
122 * @return the pointer value
125 T *operator->() const { return ptr; }
127 * Gives up ownership; the internal pointer becomes NULL.
128 * @return the pointer value;
129 * caller becomes responsible for deleting the object
138 * Deletes the object it owns,
139 * and adopts (takes ownership of) the one passed in.
140 * Subclass must override: Base class does not delete the object.
141 * @param p simple pointer to an object that is adopted
144 void adoptInstead(T *p) {
155 // No comparison operators with other LocalPointerBases.
156 bool operator==(const LocalPointerBase<T> &other);
157 bool operator!=(const LocalPointerBase<T> &other);
158 // No ownership sharing: No copy constructor, no assignment operator.
159 LocalPointerBase(const LocalPointerBase<T> &other);
160 void operator=(const LocalPointerBase<T> &other);
161 // No heap allocation. Use only on the stack.
162 static void * U_EXPORT2 operator new(size_t size);
163 static void * U_EXPORT2 operator new[](size_t size);
164 #if U_HAVE_PLACEMENT_NEW
165 static void * U_EXPORT2 operator new(size_t, void *ptr);
170 * "Smart pointer" class, deletes objects via the standard C++ delete operator.
171 * For most methods see the LocalPointerBase base class.
175 * LocalPointer<UnicodeString> s(new UnicodeString((UChar32)0x50005));
176 * int32_t length=s->length(); // 2
177 * UChar lead=s->charAt(0); // 0xd900
178 * if(some condition) { return; } // no need to explicitly delete the pointer
179 * s.adoptInstead(new UnicodeString((UChar)0xfffc));
180 * length=s->length(); // 1
181 * // no need to explicitly delete the pointer
184 * @see LocalPointerBase
188 class LocalPointer : public LocalPointerBase<T> {
190 using LocalPointerBase<T>::operator*;
191 using LocalPointerBase<T>::operator->;
193 * Constructor takes ownership.
194 * @param p simple pointer to an object that is adopted
197 explicit LocalPointer(T *p=NULL) : LocalPointerBase<T>(p) {}
199 * Constructor takes ownership and reports an error if NULL.
201 * This constructor is intended to be used with other-class constructors
202 * that may report a failure UErrorCode,
203 * so that callers need to check only for U_FAILURE(errorCode)
204 * and not also separately for isNull().
206 * @param p simple pointer to an object that is adopted
207 * @param errorCode in/out UErrorCode, set to U_MEMORY_ALLOCATION_ERROR
208 * if p==NULL and no other failure code had been set
211 LocalPointer(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
212 if(p==NULL && U_SUCCESS(errorCode)) {
213 errorCode=U_MEMORY_ALLOCATION_ERROR;
216 #if U_HAVE_RVALUE_REFERENCES
218 * Move constructor, leaves src with isNull().
219 * @param src source smart pointer
222 LocalPointer(LocalPointer<T> &&src) U_NOEXCEPT : LocalPointerBase<T>(src.ptr) {
227 * Destructor deletes the object it owns.
231 delete LocalPointerBase<T>::ptr;
233 #if U_HAVE_RVALUE_REFERENCES
235 * Move assignment operator, leaves src with isNull().
236 * The behavior is undefined if *this and src are the same object.
237 * @param src source smart pointer
241 LocalPointer<T> &operator=(LocalPointer<T> &&src) U_NOEXCEPT {
242 return moveFrom(src);
245 // do not use #ifndef U_HIDE_DRAFT_API for moveFrom, needed by non-draft API
247 * Move assignment, leaves src with isNull().
248 * The behavior is undefined if *this and src are the same object.
250 * Can be called explicitly, does not need C++11 support.
251 * @param src source smart pointer
255 LocalPointer<T> &moveFrom(LocalPointer<T> &src) U_NOEXCEPT {
256 delete LocalPointerBase<T>::ptr;
257 LocalPointerBase<T>::ptr=src.ptr;
263 * @param other other smart pointer
266 void swap(LocalPointer<T> &other) U_NOEXCEPT {
267 T *temp=LocalPointerBase<T>::ptr;
268 LocalPointerBase<T>::ptr=other.ptr;
272 * Non-member LocalPointer swap function.
273 * @param p1 will get p2's pointer
274 * @param p2 will get p1's pointer
277 friend inline void swap(LocalPointer<T> &p1, LocalPointer<T> &p2) U_NOEXCEPT {
281 * Deletes the object it owns,
282 * and adopts (takes ownership of) the one passed in.
283 * @param p simple pointer to an object that is adopted
286 void adoptInstead(T *p) {
287 delete LocalPointerBase<T>::ptr;
288 LocalPointerBase<T>::ptr=p;
291 * Deletes the object it owns,
292 * and adopts (takes ownership of) the one passed in.
294 * If U_FAILURE(errorCode), then the current object is retained and the new one deleted.
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.
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
305 void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
306 if(U_SUCCESS(errorCode)) {
307 delete LocalPointerBase<T>::ptr;
308 LocalPointerBase<T>::ptr=p;
310 errorCode=U_MEMORY_ALLOCATION_ERROR;
319 * "Smart pointer" class, deletes objects via the C++ array delete[] operator.
320 * For most methods see the LocalPointerBase base class.
321 * Adds operator[] for array item access.
325 * LocalArray<UnicodeString> a(new UnicodeString[2]);
326 * a[0].append((UChar)0x61);
327 * if(some condition) { return; } // no need to explicitly delete the array
328 * a.adoptInstead(new UnicodeString[4]);
329 * a[3].append((UChar)0x62).append((UChar)0x63).reverse();
330 * // no need to explicitly delete the array
333 * @see LocalPointerBase
337 class LocalArray : public LocalPointerBase<T> {
339 using LocalPointerBase<T>::operator*;
340 using LocalPointerBase<T>::operator->;
342 * Constructor takes ownership.
343 * @param p simple pointer to an array of T objects that is adopted
346 explicit LocalArray(T *p=NULL) : LocalPointerBase<T>(p) {}
348 * Constructor takes ownership and reports an error if NULL.
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().
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
360 LocalArray(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
361 if(p==NULL && U_SUCCESS(errorCode)) {
362 errorCode=U_MEMORY_ALLOCATION_ERROR;
365 #if U_HAVE_RVALUE_REFERENCES
367 * Move constructor, leaves src with isNull().
368 * @param src source smart pointer
371 LocalArray(LocalArray<T> &&src) U_NOEXCEPT : LocalPointerBase<T>(src.ptr) {
376 * Destructor deletes the array it owns.
380 delete[] LocalPointerBase<T>::ptr;
382 #if U_HAVE_RVALUE_REFERENCES
384 * Move assignment operator, leaves src with isNull().
385 * The behavior is undefined if *this and src are the same object.
386 * @param src source smart pointer
390 LocalArray<T> &operator=(LocalArray<T> &&src) U_NOEXCEPT {
391 return moveFrom(src);
394 // do not use #ifndef U_HIDE_DRAFT_API for moveFrom, needed by non-draft API
396 * Move assignment, leaves src with isNull().
397 * The behavior is undefined if *this and src are the same object.
399 * Can be called explicitly, does not need C++11 support.
400 * @param src source smart pointer
404 LocalArray<T> &moveFrom(LocalArray<T> &src) U_NOEXCEPT {
405 delete[] LocalPointerBase<T>::ptr;
406 LocalPointerBase<T>::ptr=src.ptr;
412 * @param other other smart pointer
415 void swap(LocalArray<T> &other) U_NOEXCEPT {
416 T *temp=LocalPointerBase<T>::ptr;
417 LocalPointerBase<T>::ptr=other.ptr;
421 * Non-member LocalArray swap function.
422 * @param p1 will get p2's pointer
423 * @param p2 will get p1's pointer
426 friend inline void swap(LocalArray<T> &p1, LocalArray<T> &p2) U_NOEXCEPT {
430 * Deletes the array it owns,
431 * and adopts (takes ownership of) the one passed in.
432 * @param p simple pointer to an array of T objects that is adopted
435 void adoptInstead(T *p) {
436 delete[] LocalPointerBase<T>::ptr;
437 LocalPointerBase<T>::ptr=p;
440 * Deletes the array it owns,
441 * and adopts (takes ownership of) the one passed in.
443 * If U_FAILURE(errorCode), then the current array is retained and the new one deleted.
445 * If U_SUCCESS(errorCode) but the input pointer is NULL,
446 * then U_MEMORY_ALLOCATION_ERROR is set,
447 * the current array is deleted, and NULL is set.
449 * @param p simple pointer to an array of T objects that is adopted
450 * @param errorCode in/out UErrorCode, set to U_MEMORY_ALLOCATION_ERROR
451 * if p==NULL and no other failure code had been set
454 void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
455 if(U_SUCCESS(errorCode)) {
456 delete[] LocalPointerBase<T>::ptr;
457 LocalPointerBase<T>::ptr=p;
459 errorCode=U_MEMORY_ALLOCATION_ERROR;
466 * Array item access (writable).
467 * No index bounds check.
468 * @param i array index
469 * @return reference to the array item
472 T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
476 * \def U_DEFINE_LOCAL_OPEN_POINTER
477 * "Smart pointer" definition macro, deletes objects via the closeFunction.
478 * Defines a subclass of LocalPointerBase which works just
479 * like LocalPointer<Type> except that this subclass will use the closeFunction
480 * rather than the C++ delete operator.
484 * LocalUCaseMapPointer csm(ucasemap_open(localeID, options, &errorCode));
485 * utf8OutLength=ucasemap_utf8ToLower(csm.getAlias(),
486 * utf8Out, (int32_t)sizeof(utf8Out),
487 * utf8In, utf8InLength, &errorCode);
488 * if(U_FAILURE(errorCode)) { return; } // no need to explicitly delete the UCaseMap
491 * @see LocalPointerBase
495 #if U_HAVE_RVALUE_REFERENCES
496 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
497 class LocalPointerClassName : public LocalPointerBase<Type> { \
499 using LocalPointerBase<Type>::operator*; \
500 using LocalPointerBase<Type>::operator->; \
501 explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
502 LocalPointerClassName(LocalPointerClassName &&src) U_NOEXCEPT \
503 : LocalPointerBase<Type>(src.ptr) { \
506 ~LocalPointerClassName() { if (ptr != NULL) { closeFunction(ptr); } } \
507 LocalPointerClassName &operator=(LocalPointerClassName &&src) U_NOEXCEPT { \
508 return moveFrom(src); \
510 LocalPointerClassName &moveFrom(LocalPointerClassName &src) U_NOEXCEPT { \
511 if (ptr != NULL) { closeFunction(ptr); } \
512 LocalPointerBase<Type>::ptr=src.ptr; \
516 void swap(LocalPointerClassName &other) U_NOEXCEPT { \
517 Type *temp=LocalPointerBase<Type>::ptr; \
518 LocalPointerBase<Type>::ptr=other.ptr; \
521 friend inline void swap(LocalPointerClassName &p1, LocalPointerClassName &p2) U_NOEXCEPT { \
524 void adoptInstead(Type *p) { \
525 if (ptr != NULL) { closeFunction(ptr); } \
530 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
531 class LocalPointerClassName : public LocalPointerBase<Type> { \
533 using LocalPointerBase<Type>::operator*; \
534 using LocalPointerBase<Type>::operator->; \
535 explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
536 ~LocalPointerClassName() { closeFunction(ptr); } \
537 LocalPointerClassName &moveFrom(LocalPointerClassName &src) U_NOEXCEPT { \
538 if (ptr != NULL) { closeFunction(ptr); } \
539 LocalPointerBase<Type>::ptr=src.ptr; \
543 void swap(LocalPointerClassName &other) U_NOEXCEPT { \
544 Type *temp=LocalPointerBase<Type>::ptr; \
545 LocalPointerBase<Type>::ptr=other.ptr; \
548 friend inline void swap(LocalPointerClassName &p1, LocalPointerClassName &p2) U_NOEXCEPT { \
551 void adoptInstead(Type *p) { \
552 if (ptr != NULL) { closeFunction(ptr); } \
560 #endif /* U_SHOW_CPLUSPLUS_API */
561 #endif /* __LOCALPOINTER_H__ */