ICU 59.1  59.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  }
216 #if U_HAVE_RVALUE_REFERENCES
217 
223  src.ptr=NULL;
224  }
225 #endif
226 
232  }
233 #if U_HAVE_RVALUE_REFERENCES
234 
242  return moveFrom(src);
243  }
244 #endif
245  // do not use #ifndef U_HIDE_DRAFT_API for moveFrom, needed by non-draft API
258  src.ptr=NULL;
259  return *this;
260  }
267  T *temp=LocalPointerBase<T>::ptr;
269  other.ptr=temp;
270  }
277  friend inline void swap(LocalPointer<T> &p1, LocalPointer<T> &p2) U_NOEXCEPT {
278  p1.swap(p2);
279  }
286  void adoptInstead(T *p) {
289  }
305  void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
306  if(U_SUCCESS(errorCode)) {
309  if(p==NULL) {
310  errorCode=U_MEMORY_ALLOCATION_ERROR;
311  }
312  } else {
313  delete p;
314  }
315  }
316 };
317 
336 template<typename T>
337 class LocalArray : public LocalPointerBase<T> {
338 public:
346  explicit LocalArray(T *p=NULL) : LocalPointerBase<T>(p) {}
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 
372  src.ptr=NULL;
373  }
374 #endif
375 
380  delete[] LocalPointerBase<T>::ptr;
381  }
382 #if U_HAVE_RVALUE_REFERENCES
383 
391  return moveFrom(src);
392  }
393 #endif
394  // do not use #ifndef U_HIDE_DRAFT_API for moveFrom, needed by non-draft API
405  delete[] LocalPointerBase<T>::ptr;
407  src.ptr=NULL;
408  return *this;
409  }
416  T *temp=LocalPointerBase<T>::ptr;
418  other.ptr=temp;
419  }
426  friend inline void swap(LocalArray<T> &p1, LocalArray<T> &p2) U_NOEXCEPT {
427  p1.swap(p2);
428  }
435  void adoptInstead(T *p) {
436  delete[] LocalPointerBase<T>::ptr;
438  }
454  void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
455  if(U_SUCCESS(errorCode)) {
456  delete[] LocalPointerBase<T>::ptr;
458  if(p==NULL) {
459  errorCode=U_MEMORY_ALLOCATION_ERROR;
460  }
461  } else {
462  delete[] p;
463  }
464  }
472  T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
473 };
474 
495 #if U_HAVE_RVALUE_REFERENCES
496 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
497  class LocalPointerClassName : public LocalPointerBase<Type> { \
498  public: \
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) { \
504  src.ptr=NULL; \
505  } \
506  ~LocalPointerClassName() { if (ptr != NULL) { closeFunction(ptr); } } \
507  LocalPointerClassName &operator=(LocalPointerClassName &&src) U_NOEXCEPT { \
508  return moveFrom(src); \
509  } \
510  LocalPointerClassName &moveFrom(LocalPointerClassName &src) U_NOEXCEPT { \
511  if (ptr != NULL) { closeFunction(ptr); } \
512  LocalPointerBase<Type>::ptr=src.ptr; \
513  src.ptr=NULL; \
514  return *this; \
515  } \
516  void swap(LocalPointerClassName &other) U_NOEXCEPT { \
517  Type *temp=LocalPointerBase<Type>::ptr; \
518  LocalPointerBase<Type>::ptr=other.ptr; \
519  other.ptr=temp; \
520  } \
521  friend inline void swap(LocalPointerClassName &p1, LocalPointerClassName &p2) U_NOEXCEPT { \
522  p1.swap(p2); \
523  } \
524  void adoptInstead(Type *p) { \
525  if (ptr != NULL) { closeFunction(ptr); } \
526  ptr=p; \
527  } \
528  }
529 #else
530 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
531  class LocalPointerClassName : public LocalPointerBase<Type> { \
532  public: \
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; \
540  src.ptr=NULL; \
541  return *this; \
542  } \
543  void swap(LocalPointerClassName &other) U_NOEXCEPT { \
544  Type *temp=LocalPointerBase<Type>::ptr; \
545  LocalPointerBase<Type>::ptr=other.ptr; \
546  other.ptr=temp; \
547  } \
548  friend inline void swap(LocalPointerClassName &p1, LocalPointerClassName &p2) U_NOEXCEPT { \
549  p1.swap(p2); \
550  } \
551  void adoptInstead(Type *p) { \
552  if (ptr != NULL) { closeFunction(ptr); } \
553  ptr=p; \
554  } \
555  }
556 #endif
557 
559 
560 #endif /* U_SHOW_CPLUSPLUS_API */
561 #endif /* __LOCALPOINTER_H__ */
LocalPointer< T > & operator=(LocalPointer< T > &&src) U_NOEXCEPT
Move assignment operator, leaves src with isNull().
Definition: localpointer.h:241
void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode)
Deletes the array it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:454
T & operator[](ptrdiff_t i) const
Array item access (writable).
Definition: localpointer.h:472
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:286
#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:346
"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:360
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
void swap(LocalPointer< T > &other) U_NOEXCEPT
Swap pointers.
Definition: localpointer.h:266
T * ptr
Actual pointer.
Definition: localpointer.h:153
Memory allocation error.
Definition: utypes.h:438
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
LocalPointer< T > & moveFrom(LocalPointer< T > &src) U_NOEXCEPT
Move assignment, leaves src with isNull().
Definition: localpointer.h:255
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:131
friend void swap(LocalPointer< T > &p1, LocalPointer< T > &p2) U_NOEXCEPT
Non-member LocalPointer swap function.
Definition: localpointer.h:277
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
LocalPointer(LocalPointer< T > &&src) U_NOEXCEPT
Move constructor, leaves src with isNull().
Definition: localpointer.h:222
UBool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:218
friend void swap(LocalArray< T > &p1, LocalArray< T > &p2) U_NOEXCEPT
Non-member LocalArray swap function.
Definition: localpointer.h:426
#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:337
LocalArray(LocalArray< T > &&src) U_NOEXCEPT
Move constructor, leaves src with isNull().
Definition: localpointer.h:371
~LocalPointer()
Destructor deletes the object it owns.
Definition: localpointer.h:230
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:539
LocalArray< T > & operator=(LocalArray< T > &&src) U_NOEXCEPT
Move assignment operator, leaves src with isNull().
Definition: localpointer.h:390
#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:435
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
void swap(LocalArray< T > &other) U_NOEXCEPT
Swap pointers.
Definition: localpointer.h:415
~LocalArray()
Destructor deletes the array it owns.
Definition: localpointer.h:379
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:305
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.
LocalArray< T > & moveFrom(LocalArray< T > &src) U_NOEXCEPT
Move assignment, leaves src with isNull().
Definition: localpointer.h:404
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:144
int8_t UBool
The ICU boolean type.
Definition: umachine.h:236