ICU 56.1  56.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
localpointer.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 2009-2015, 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:
193  explicit LocalPointer(T *p=NULL) : LocalPointerBase<T>(p) {}
194 #ifndef U_HIDE_DRAFT_API
195 
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 
220  src.ptr=NULL;
221  }
222 #endif
223 #endif /* U_HIDE_DRAFT_API */
224 
230  }
231 #ifndef U_HIDE_DRAFT_API
232 #if U_HAVE_RVALUE_REFERENCES
233 
240  LocalPointer<T> &operator=(LocalPointer<T> &&src) U_NOEXCEPT {
241  return moveFrom(src);
242  }
243 #endif
244 
256  src.ptr=NULL;
257  return *this;
258  }
265  T *temp=LocalPointerBase<T>::ptr;
267  other.ptr=temp;
268  }
275  friend inline void swap(LocalPointer<T> &p1, LocalPointer<T> &p2) U_NOEXCEPT {
276  p1.swap(p2);
277  }
278 #endif /* U_HIDE_DRAFT_API */
279 
285  void adoptInstead(T *p) {
288  }
289 #ifndef U_HIDE_DRAFT_API
290 
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 #endif /* U_HIDE_DRAFT_API */
317 };
318 
337 template<typename T>
338 class LocalArray : public LocalPointerBase<T> {
339 public:
345  explicit LocalArray(T *p=NULL) : LocalPointerBase<T>(p) {}
346 #ifndef U_HIDE_DRAFT_API
347 
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 
371  LocalArray(LocalArray<T> &&src) U_NOEXCEPT : LocalPointerBase<T>(src.ptr) {
372  src.ptr=NULL;
373  }
374 #endif
375 #endif /* U_HIDE_DRAFT_API */
376 
381  delete[] LocalPointerBase<T>::ptr;
382  }
383 #ifndef U_HIDE_DRAFT_API
384 #if U_HAVE_RVALUE_REFERENCES
385 
392  LocalArray<T> &operator=(LocalArray<T> &&src) U_NOEXCEPT {
393  return moveFrom(src);
394  }
395 #endif
396 
406  delete[] LocalPointerBase<T>::ptr;
408  src.ptr=NULL;
409  return *this;
410  }
417  T *temp=LocalPointerBase<T>::ptr;
419  other.ptr=temp;
420  }
427  friend inline void swap(LocalArray<T> &p1, LocalArray<T> &p2) U_NOEXCEPT {
428  p1.swap(p2);
429  }
430 #endif /* U_HIDE_DRAFT_API */
431 
437  void adoptInstead(T *p) {
438  delete[] LocalPointerBase<T>::ptr;
440  }
441 #ifndef U_HIDE_DRAFT_API
442 
457  void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
458  if(U_SUCCESS(errorCode)) {
459  delete[] LocalPointerBase<T>::ptr;
461  if(p==NULL) {
462  errorCode=U_MEMORY_ALLOCATION_ERROR;
463  }
464  } else {
465  delete[] p;
466  }
467  }
468 #endif /* U_HIDE_DRAFT_API */
469 
476  T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
477 };
478 
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
535 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
536  class LocalPointerClassName : public LocalPointerBase<Type> { \
537  public: \
538  explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
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  } \
554  void adoptInstead(Type *p) { \
555  closeFunction(ptr); \
556  ptr=p; \
557  } \
558  }
559 #endif
560 
562 
563 #endif /* U_SHOW_CPLUSPLUS_API */
564 #endif /* __LOCALPOINTER_H__ */