ICU 57.1  57.1
localpointer.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 2009-2016, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 *******************************************************************************
8 * file name: localpointer.h
9 * encoding: US-ASCII
10 * tab size: 8 (not used)
11 * indentation:4
12 *
13 * created on: 2009nov13
14 * created by: Markus W. Scherer
15 */
16 
17 #ifndef __LOCALPOINTER_H__
18 #define __LOCALPOINTER_H__
19 
39 #include "unicode/utypes.h"
40 
41 #if U_SHOW_CPLUSPLUS_API
42 
44 
63 template<typename T>
65 public:
71  explicit LocalPointerBase(T *p=NULL) : ptr(p) {}
77  ~LocalPointerBase() { /* delete ptr; */ }
83  UBool isNull() const { return ptr==NULL; }
89  UBool isValid() const { return ptr!=NULL; }
97  bool operator==(const T *other) const { return ptr==other; }
105  bool operator!=(const T *other) const { return ptr!=other; }
111  T *getAlias() const { return ptr; }
117  T &operator*() const { return *ptr; }
123  T *operator->() const { return ptr; }
130  T *orphan() {
131  T *p=ptr;
132  ptr=NULL;
133  return p;
134  }
142  void adoptInstead(T *p) {
143  // delete ptr;
144  ptr=p;
145  }
146 protected:
151  T *ptr;
152 private:
153  // No comparison operators with other LocalPointerBases.
154  bool operator==(const LocalPointerBase<T> &other);
155  bool operator!=(const LocalPointerBase<T> &other);
156  // No ownership sharing: No copy constructor, no assignment operator.
158  void operator=(const LocalPointerBase<T> &other);
159  // No heap allocation. Use only on the stack.
160  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
163  static void * U_EXPORT2 operator new(size_t, void *ptr);
164 #endif
165 };
166 
185 template<typename T>
186 class LocalPointer : public LocalPointerBase<T> {
187 public:
195  explicit LocalPointer(T *p=NULL) : LocalPointerBase<T>(p) {}
209  LocalPointer(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
210  if(p==NULL && U_SUCCESS(errorCode)) {
211  errorCode=U_MEMORY_ALLOCATION_ERROR;
212  }
213  }
214 #ifndef U_HIDE_DRAFT_API
215 #if U_HAVE_RVALUE_REFERENCES
216 
222  src.ptr=NULL;
223  }
224 #endif
225 #endif /* U_HIDE_DRAFT_API */
226 
232  }
233 #ifndef U_HIDE_DRAFT_API
234 #if U_HAVE_RVALUE_REFERENCES
235 
242  LocalPointer<T> &operator=(LocalPointer<T> &&src) U_NOEXCEPT {
243  return moveFrom(src);
244  }
245 #endif
246 
258  src.ptr=NULL;
259  return *this;
260  }
267  T *temp=LocalPointerBase<T>::ptr;
269  other.ptr=temp;
270  }
271 #endif /* U_HIDE_DRAFT_API */
272 
278  friend inline void swap(LocalPointer<T> &p1, LocalPointer<T> &p2) U_NOEXCEPT {
279  p1.swap(p2);
280  }
287  void adoptInstead(T *p) {
290  }
306  void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
307  if(U_SUCCESS(errorCode)) {
310  if(p==NULL) {
311  errorCode=U_MEMORY_ALLOCATION_ERROR;
312  }
313  } else {
314  delete p;
315  }
316  }
317 };
318 
337 template<typename T>
338 class LocalArray : public LocalPointerBase<T> {
339 public:
347  explicit LocalArray(T *p=NULL) : LocalPointerBase<T>(p) {}
348 #ifndef U_HIDE_DRAFT_API
349 
362  LocalArray(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
363  if(p==NULL && U_SUCCESS(errorCode)) {
364  errorCode=U_MEMORY_ALLOCATION_ERROR;
365  }
366  }
367 #if U_HAVE_RVALUE_REFERENCES
368 
374  src.ptr=NULL;
375  }
376 #endif
377 #endif /* U_HIDE_DRAFT_API */
378 
383  delete[] LocalPointerBase<T>::ptr;
384  }
385 #ifndef U_HIDE_DRAFT_API
386 #if U_HAVE_RVALUE_REFERENCES
387 
394  LocalArray<T> &operator=(LocalArray<T> &&src) U_NOEXCEPT {
395  return moveFrom(src);
396  }
397 #endif
398 
408  delete[] LocalPointerBase<T>::ptr;
410  src.ptr=NULL;
411  return *this;
412  }
419  T *temp=LocalPointerBase<T>::ptr;
421  other.ptr=temp;
422  }
423 #endif /* U_HIDE_DRAFT_API */
424 
430  friend inline void swap(LocalArray<T> &p1, LocalArray<T> &p2) U_NOEXCEPT {
431  p1.swap(p2);
432  }
439  void adoptInstead(T *p) {
440  delete[] LocalPointerBase<T>::ptr;
442  }
443 #ifndef U_HIDE_DRAFT_API
444 
459  void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
460  if(U_SUCCESS(errorCode)) {
461  delete[] LocalPointerBase<T>::ptr;
463  if(p==NULL) {
464  errorCode=U_MEMORY_ALLOCATION_ERROR;
465  }
466  } else {
467  delete[] p;
468  }
469  }
470 #endif /* U_HIDE_DRAFT_API */
471 
478  T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
479 };
480 
504 #if U_HAVE_RVALUE_REFERENCES
505 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
506  class LocalPointerClassName : public LocalPointerBase<Type> { \
507  public: \
508  using LocalPointerBase<Type>::operator*; \
509  using LocalPointerBase<Type>::operator->; \
510  explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
511  LocalPointerClassName(LocalPointerClassName &&src) U_NOEXCEPT \
512  : LocalPointerBase<Type>(src.ptr) { \
513  src.ptr=NULL; \
514  } \
515  ~LocalPointerClassName() { closeFunction(ptr); } \
516  LocalPointerClassName &operator=(LocalPointerClassName &&src) U_NOEXCEPT { \
517  return moveFrom(src); \
518  } \
519  LocalPointerClassName &moveFrom(LocalPointerClassName &src) U_NOEXCEPT { \
520  closeFunction(ptr); \
521  LocalPointerBase<Type>::ptr=src.ptr; \
522  src.ptr=NULL; \
523  return *this; \
524  } \
525  void swap(LocalPointerClassName &other) U_NOEXCEPT { \
526  Type *temp=LocalPointerBase<Type>::ptr; \
527  LocalPointerBase<Type>::ptr=other.ptr; \
528  other.ptr=temp; \
529  } \
530  friend inline void swap(LocalPointerClassName &p1, LocalPointerClassName &p2) U_NOEXCEPT { \
531  p1.swap(p2); \
532  } \
533  void adoptInstead(Type *p) { \
534  closeFunction(ptr); \
535  ptr=p; \
536  } \
537  }
538 #else
539 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
540  class LocalPointerClassName : public LocalPointerBase<Type> { \
541  public: \
542  using LocalPointerBase<Type>::operator*; \
543  using LocalPointerBase<Type>::operator->; \
544  explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
545  ~LocalPointerClassName() { closeFunction(ptr); } \
546  LocalPointerClassName &moveFrom(LocalPointerClassName &src) U_NOEXCEPT { \
547  closeFunction(ptr); \
548  LocalPointerBase<Type>::ptr=src.ptr; \
549  src.ptr=NULL; \
550  return *this; \
551  } \
552  void swap(LocalPointerClassName &other) U_NOEXCEPT { \
553  Type *temp=LocalPointerBase<Type>::ptr; \
554  LocalPointerBase<Type>::ptr=other.ptr; \
555  other.ptr=temp; \
556  } \
557  friend inline void swap(LocalPointerClassName &p1, LocalPointerClassName &p2) U_NOEXCEPT { \
558  p1.swap(p2); \
559  } \
560  void adoptInstead(Type *p) { \
561  closeFunction(ptr); \
562  ptr=p; \
563  } \
564  }
565 #endif
566 
568 
569 #endif /* U_SHOW_CPLUSPLUS_API */
570 #endif /* __LOCALPOINTER_H__ */
void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode)
Deletes the array it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:459
T & operator[](ptrdiff_t i) const
Array item access (writable).
Definition: localpointer.h:478
T * getAlias() const
Access without ownership change.
Definition: localpointer.h:111
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:287
#define U_SUCCESS(x)
Does the error code indicate success?
Definition: utypes.h:709
LocalPointer(T *p, UErrorCode &errorCode)
Constructor takes ownership and reports an error if NULL.
Definition: localpointer.h:209
LocalArray(T *p=NULL)
Constructor takes ownership.
Definition: localpointer.h:347
"Smart pointer" class, deletes objects via the standard C++ delete operator.
Definition: localpointer.h:186
LocalArray(T *p, UErrorCode &errorCode)
Constructor takes ownership and reports an error if NULL.
Definition: localpointer.h:362
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:151
Memory allocation error.
Definition: utypes.h:513
bool operator!=(const T *other) const
Comparison with a simple pointer, so that existing code with !=NULL need not be changed.
Definition: localpointer.h:105
LocalPointer(T *p=NULL)
Constructor takes ownership.
Definition: localpointer.h:195
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:129
friend void swap(LocalPointer< T > &p1, LocalPointer< T > &p2) U_NOEXCEPT
Non-member LocalPointer swap function.
Definition: localpointer.h:278
T * orphan()
Gives up ownership; the internal pointer becomes NULL.
Definition: localpointer.h:130
LocalPointerBase(T *p=NULL)
Constructor takes ownership.
Definition: localpointer.h:71
"Smart pointer" base class; do not use directly: use LocalPointer etc.
Definition: localpointer.h:64
UBool isNull() const
NULL check.
Definition: localpointer.h:83
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:430
#define NULL
Define NULL if necessary, to 0 for C++ and to ((void *)0) for C.
Definition: utypes.h:186
"Smart pointer" class, deletes objects via the C++ array delete[] operator.
Definition: localpointer.h:338
~LocalPointer()
Destructor deletes the object it owns.
Definition: localpointer.h:230
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:529
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:130
void adoptInstead(T *p)
Deletes the array it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:439
UBool isValid() const
NULL check.
Definition: localpointer.h:89
bool operator==(const T *other) const
Comparison with a simple pointer, so that existing code with ==NULL need not be changed.
Definition: localpointer.h:97
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:476
void swap(LocalArray< T > &other) U_NOEXCEPT
Swap pointers.
Definition: localpointer.h:418
~LocalArray()
Destructor deletes the array it owns.
Definition: localpointer.h:382
T * operator->() const
Access without ownership change.
Definition: localpointer.h:123
void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:306
T & operator*() const
Access without ownership change.
Definition: localpointer.h:117
~LocalPointerBase()
Destructor deletes the object it owns.
Definition: localpointer.h:77
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:407
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:142
int8_t UBool
The ICU boolean type.
Definition: umachine.h:234