ICU 62.1 62.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
65template<typename T>
67public:
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 }
148protected:
153 T *ptr;
154private:
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
187template<typename T>
189public:
190 using LocalPointerBase<T>::operator*;
191 using LocalPointerBase<T>::operator->;
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)) {
214 }
215 }
239 return moveFrom(src);
240 }
241 // do not use #ifndef U_HIDE_DRAFT_API for moveFrom, needed by non-draft API
274 p1.swap(p2);
275 }
302 if(U_SUCCESS(errorCode)) {
305 if(p==NULL) {
307 }
308 } else {
309 delete p;
310 }
311 }
312};
313
332template<typename T>
333class LocalArray : public LocalPointerBase<T> {
334public:
335 using LocalPointerBase<T>::operator*;
336 using LocalPointerBase<T>::operator->;
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)) {
359 }
360 }
375 }
384 return moveFrom(src);
385 }
386 // do not use #ifndef U_HIDE_DRAFT_API for moveFrom, needed by non-draft API
419 p1.swap(p2);
420 }
447 if(U_SUCCESS(errorCode)) {
450 if(p==NULL) {
452 }
453 } else {
454 delete[] p;
455 }
456 }
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__ */
"Smart pointer" class, deletes objects via the C++ array delete[] operator.
LocalArray(LocalArray< T > &&src)
Move constructor, leaves src with isNull().
T & operator[](ptrdiff_t i) const
Array item access (writable).
void swap(LocalArray< T > &other)
Swap pointers.
~LocalArray()
Destructor deletes the array it owns.
LocalArray(T *p=NULL)
Constructor takes ownership.
void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode)
Deletes the array it owns, and adopts (takes ownership of) the one passed in.
LocalArray< T > & moveFrom(LocalArray< T > &src)
Move assignment, leaves src with isNull().
void adoptInstead(T *p)
Deletes the array it owns, and adopts (takes ownership of) the one passed in.
LocalArray(T *p, UErrorCode &errorCode)
Constructor takes ownership and reports an error if NULL.
friend void swap(LocalArray< T > &p1, LocalArray< T > &p2)
Non-member LocalArray swap function.
LocalArray< T > & operator=(LocalArray< T > &&src)
Move assignment operator, leaves src with isNull().
"Smart pointer" base class; do not use directly: use LocalPointer etc.
bool operator!=(const T *other) const
Comparison with a simple pointer, so that existing code with !=NULL need not be changed.
UBool isValid() const
NULL check.
T * orphan()
Gives up ownership; the internal pointer becomes NULL.
LocalPointerBase(T *p=NULL)
Constructor takes ownership.
UBool isNull() const
NULL check.
T * ptr
Actual pointer.
T & operator*() const
Access without ownership change.
T * operator->() const
Access without ownership change.
bool operator==(const T *other) const
Comparison with a simple pointer, so that existing code with ==NULL need not be changed.
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
T * getAlias() const
Access without ownership change.
~LocalPointerBase()
Destructor deletes the object it owns.
"Smart pointer" class, deletes objects via the standard C++ delete operator.
LocalPointer(LocalPointer< T > &&src)
Move constructor, leaves src with isNull().
void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
void swap(LocalPointer< T > &other)
Swap pointers.
LocalPointer(T *p, UErrorCode &errorCode)
Constructor takes ownership and reports an error if NULL.
LocalPointer< T > & moveFrom(LocalPointer< T > &src)
Move assignment, leaves src with isNull().
LocalPointer< T > & operator=(LocalPointer< T > &&src)
Move assignment operator, leaves src with isNull().
~LocalPointer()
Destructor deletes the object it owns.
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
LocalPointer(T *p=NULL)
Constructor takes ownership.
friend void swap(LocalPointer< T > &p1, LocalPointer< T > &p2)
Non-member LocalPointer swap function.
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
UBool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
int8_t UBool
The ICU boolean type.
Definition umachine.h:236
Basic definitions for ICU, for both C and C++ APIs.
#define NULL
Define NULL if necessary, to nullptr for C++ and to ((void *)0) for C.
Definition utypes.h:188
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers,...
Definition utypes.h:396
@ U_MEMORY_ALLOCATION_ERROR
Memory allocation error.
Definition utypes.h:438
#define U_SUCCESS(x)
Does the error code indicate success?
Definition utypes.h:689
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition uversion.h:138
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition uversion.h:137