ICU 60.1  60.1
localpointer.h
Go to the documentation of this file.
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *
6 * Copyright (C) 2009-2016, International Business Machines
7 * Corporation and others. All Rights Reserved.
8 *
9 *******************************************************************************
10 * file name: localpointer.h
11 * encoding: UTF-8
12 * tab size: 8 (not used)
13 * indentation:4
14 *
15 * created on: 2009nov13
16 * created by: Markus W. Scherer
17 */
18 
19 #ifndef __LOCALPOINTER_H__
20 #define __LOCALPOINTER_H__
21 
41 #include "unicode/utypes.h"
42 
43 #if U_SHOW_CPLUSPLUS_API
44 
46 
65 template<typename T>
67 public:
73  explicit LocalPointerBase(T *p=NULL) : ptr(p) {}
79  ~LocalPointerBase() { /* delete ptr; */ }
85  UBool isNull() const { return ptr==NULL; }
91  UBool isValid() const { return ptr!=NULL; }
99  bool operator==(const T *other) const { return ptr==other; }
107  bool operator!=(const T *other) const { return ptr!=other; }
113  T *getAlias() const { return ptr; }
119  T &operator*() const { return *ptr; }
125  T *operator->() const { return ptr; }
132  T *orphan() {
133  T *p=ptr;
134  ptr=NULL;
135  return p;
136  }
144  void adoptInstead(T *p) {
145  // delete ptr;
146  ptr=p;
147  }
148 protected:
153  T *ptr;
154 private:
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.
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);
166 #endif
167 };
168 
187 template<typename T>
188 class LocalPointer : public LocalPointerBase<T> {
189 public:
197  explicit LocalPointer(T *p=NULL) : LocalPointerBase<T>(p) {}
211  LocalPointer(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
212  if(p==NULL && U_SUCCESS(errorCode)) {
213  errorCode=U_MEMORY_ALLOCATION_ERROR;
214  }
215  }
221  LocalPointer(LocalPointer<T> &&src) U_NOEXCEPT : LocalPointerBase<T>(src.ptr) {
222  src.ptr=NULL;
223  }
230  }
239  return moveFrom(src);
240  }
241  // do not use #ifndef U_HIDE_DRAFT_API for moveFrom, needed by non-draft API
254  src.ptr=NULL;
255  return *this;
256  }
262  void swap(LocalPointer<T> &other) U_NOEXCEPT {
263  T *temp=LocalPointerBase<T>::ptr;
265  other.ptr=temp;
266  }
273  friend inline void swap(LocalPointer<T> &p1, LocalPointer<T> &p2) U_NOEXCEPT {
274  p1.swap(p2);
275  }
282  void adoptInstead(T *p) {
285  }
301  void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
302  if(U_SUCCESS(errorCode)) {
305  if(p==NULL) {
306  errorCode=U_MEMORY_ALLOCATION_ERROR;
307  }
308  } else {
309  delete p;
310  }
311  }
312 };
313 
332 template<typename T>
333 class LocalArray : public LocalPointerBase<T> {
334 public:
342  explicit LocalArray(T *p=NULL) : LocalPointerBase<T>(p) {}
356  LocalArray(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
357  if(p==NULL && U_SUCCESS(errorCode)) {
358  errorCode=U_MEMORY_ALLOCATION_ERROR;
359  }
360  }
366  LocalArray(LocalArray<T> &&src) U_NOEXCEPT : LocalPointerBase<T>(src.ptr) {
367  src.ptr=NULL;
368  }
374  delete[] LocalPointerBase<T>::ptr;
375  }
383  LocalArray<T> &operator=(LocalArray<T> &&src) U_NOEXCEPT {
384  return moveFrom(src);
385  }
386  // do not use #ifndef U_HIDE_DRAFT_API for moveFrom, needed by non-draft API
397  delete[] LocalPointerBase<T>::ptr;
399  src.ptr=NULL;
400  return *this;
401  }
407  void swap(LocalArray<T> &other) U_NOEXCEPT {
408  T *temp=LocalPointerBase<T>::ptr;
410  other.ptr=temp;
411  }
418  friend inline void swap(LocalArray<T> &p1, LocalArray<T> &p2) U_NOEXCEPT {
419  p1.swap(p2);
420  }
427  void adoptInstead(T *p) {
428  delete[] LocalPointerBase<T>::ptr;
430  }
446  void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
447  if(U_SUCCESS(errorCode)) {
448  delete[] LocalPointerBase<T>::ptr;
450  if(p==NULL) {
451  errorCode=U_MEMORY_ALLOCATION_ERROR;
452  }
453  } else {
454  delete[] p;
455  }
456  }
464  T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
465 };
466 
487 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
488  class LocalPointerClassName : public LocalPointerBase<Type> { \
489  public: \
490  using LocalPointerBase<Type>::operator*; \
491  using LocalPointerBase<Type>::operator->; \
492  explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
493  LocalPointerClassName(LocalPointerClassName &&src) U_NOEXCEPT \
494  : LocalPointerBase<Type>(src.ptr) { \
495  src.ptr=NULL; \
496  } \
497  ~LocalPointerClassName() { if (ptr != NULL) { closeFunction(ptr); } } \
498  LocalPointerClassName &operator=(LocalPointerClassName &&src) U_NOEXCEPT { \
499  return moveFrom(src); \
500  } \
501  LocalPointerClassName &moveFrom(LocalPointerClassName &src) U_NOEXCEPT { \
502  if (ptr != NULL) { closeFunction(ptr); } \
503  LocalPointerBase<Type>::ptr=src.ptr; \
504  src.ptr=NULL; \
505  return *this; \
506  } \
507  void swap(LocalPointerClassName &other) U_NOEXCEPT { \
508  Type *temp=LocalPointerBase<Type>::ptr; \
509  LocalPointerBase<Type>::ptr=other.ptr; \
510  other.ptr=temp; \
511  } \
512  friend inline void swap(LocalPointerClassName &p1, LocalPointerClassName &p2) U_NOEXCEPT { \
513  p1.swap(p2); \
514  } \
515  void adoptInstead(Type *p) { \
516  if (ptr != NULL) { closeFunction(ptr); } \
517  ptr=p; \
518  } \
519  }
520 
522 
523 #endif /* U_SHOW_CPLUSPLUS_API */
524 #endif /* __LOCALPOINTER_H__ */
friend void swap(LocalArray< T > &p1, LocalArray< T > &p2)
Non-member LocalArray swap function.
Definition: localpointer.h:418
LocalPointer< T > & operator=(LocalPointer< T > &&src)
Move assignment operator, leaves src with isNull().
Definition: localpointer.h:238
void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode)
Deletes the array it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:446
T & operator[](ptrdiff_t i) const
Array item access (writable).
Definition: localpointer.h:464
T * getAlias() const
Access without ownership change.
Definition: localpointer.h:113
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:282
void swap(LocalArray< T > &other)
Swap pointers.
Definition: localpointer.h:407
#define U_SUCCESS(x)
Does the error code indicate success?
Definition: utypes.h:683
LocalPointer(T *p, UErrorCode &errorCode)
Constructor takes ownership and reports an error if NULL.
Definition: localpointer.h:211
LocalArray(T *p=NULL)
Constructor takes ownership.
Definition: localpointer.h:342
"Smart pointer" class, deletes objects via the standard C++ delete operator.
Definition: localpointer.h:188
LocalArray(T *p, UErrorCode &errorCode)
Constructor takes ownership and reports an error if NULL.
Definition: localpointer.h:356
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
LocalArray< T > & moveFrom(LocalArray< T > &src)
Move assignment, leaves src with isNull().
Definition: localpointer.h:396
void swap(LocalPointer< T > &other)
Swap pointers.
Definition: localpointer.h:262
LocalArray(LocalArray< T > &&src)
Move constructor, leaves src with isNull().
Definition: localpointer.h:366
T * ptr
Actual pointer.
Definition: localpointer.h:153
Memory allocation error.
Definition: utypes.h:438
LocalPointer< T > & moveFrom(LocalPointer< T > &src)
Move assignment, leaves src with isNull().
Definition: localpointer.h:251
bool operator!=(const T *other) const
Comparison with a simple pointer, so that existing code with !=NULL need not be changed.
Definition: localpointer.h:107
LocalPointer(T *p=NULL)
Constructor takes ownership.
Definition: localpointer.h:197
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:131
T * orphan()
Gives up ownership; the internal pointer becomes NULL.
Definition: localpointer.h:132
LocalPointerBase(T *p=NULL)
Constructor takes ownership.
Definition: localpointer.h:73
"Smart pointer" base class; do not use directly: use LocalPointer etc.
Definition: localpointer.h:66
UBool isNull() const
NULL check.
Definition: localpointer.h:85
UBool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:218
#define NULL
Define NULL if necessary, to nullptr for C++ and to ((void *)0) for C.
Definition: utypes.h:188
"Smart pointer" class, deletes objects via the C++ array delete[] operator.
Definition: localpointer.h:333
~LocalPointer()
Destructor deletes the object it owns.
Definition: localpointer.h:228
LocalArray< T > & operator=(LocalArray< T > &&src)
Move assignment operator, leaves src with isNull().
Definition: localpointer.h:383
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:132
void adoptInstead(T *p)
Deletes the array it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:427
UBool isValid() const
NULL check.
Definition: localpointer.h:91
bool operator==(const T *other) const
Comparison with a simple pointer, so that existing code with ==NULL need not be changed.
Definition: localpointer.h:99
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:396
~LocalArray()
Destructor deletes the array it owns.
Definition: localpointer.h:373
T * operator->() const
Access without ownership change.
Definition: localpointer.h:125
void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:301
T & operator*() const
Access without ownership change.
Definition: localpointer.h:119
~LocalPointerBase()
Destructor deletes the object it owns.
Definition: localpointer.h:79
Basic definitions for ICU, for both C and C++ APIs.
friend void swap(LocalPointer< T > &p1, LocalPointer< T > &p2)
Non-member LocalPointer swap function.
Definition: localpointer.h:273
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:144
LocalPointer(LocalPointer< T > &&src)
Move constructor, leaves src with isNull().
Definition: localpointer.h:221
int8_t UBool
The ICU boolean type.
Definition: umachine.h:236