X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/729e4ab9bc6618bc3d8a898e575df7f4019e29ca..b801cf366c7671a99bdcef84d1e9c0ec64b36723:/icuSources/common/unicode/localpointer.h diff --git a/icuSources/common/unicode/localpointer.h b/icuSources/common/unicode/localpointer.h index b76a1f85..e3aabf65 100644 --- a/icuSources/common/unicode/localpointer.h +++ b/icuSources/common/unicode/localpointer.h @@ -1,7 +1,7 @@ /* ******************************************************************************* * -* Copyright (C) 2009-2010, International Business Machines +* Copyright (C) 2009-2014, International Business Machines * Corporation and others. All Rights Reserved. * ******************************************************************************* @@ -144,6 +144,10 @@ public: ptr=p; } protected: + /** + * Actual pointer. + * @internal + */ T *ptr; private: // No comparison operators with other LocalPointerBases. @@ -187,6 +191,26 @@ public: * @stable ICU 4.4 */ explicit LocalPointer(T *p=NULL) : LocalPointerBase(p) {} +#ifndef U_HIDE_DRAFT_API + /** + * Constructor takes ownership and reports an error if NULL. + * + * This constructor is intended to be used with other-class constructors + * that may report a failure UErrorCode, + * so that callers need to check only for U_FAILURE(errorCode) + * and not also separately for isNull(). + * + * @param p simple pointer to an object that is adopted + * @param errorCode in/out UErrorCode, set to U_MEMORY_ALLOCATION_ERROR + * if p==NULL and no other failure code had been set + * @draft ICU 55 + */ + LocalPointer(T *p, UErrorCode &errorCode) : LocalPointerBase(p) { + if(p==NULL && U_SUCCESS(errorCode)) { + errorCode=U_MEMORY_ALLOCATION_ERROR; + } + } +#endif /* U_HIDE_DRAFT_API */ /** * Destructor deletes the object it owns. * @stable ICU 4.4 @@ -204,6 +228,34 @@ public: delete LocalPointerBase::ptr; LocalPointerBase::ptr=p; } +#ifndef U_HIDE_DRAFT_API + /** + * Deletes the object it owns, + * and adopts (takes ownership of) the one passed in. + * + * If U_FAILURE(errorCode), then the current object is retained and the new one deleted. + * + * If U_SUCCESS(errorCode) but the input pointer is NULL, + * then U_MEMORY_ALLOCATION_ERROR is set, + * the current object is deleted, and NULL is set. + * + * @param p simple pointer to an object that is adopted + * @param errorCode in/out UErrorCode, set to U_MEMORY_ALLOCATION_ERROR + * if p==NULL and no other failure code had been set + * @draft ICU 55 + */ + void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) { + if(U_SUCCESS(errorCode)) { + delete LocalPointerBase::ptr; + LocalPointerBase::ptr=p; + if(p==NULL) { + errorCode=U_MEMORY_ALLOCATION_ERROR; + } + } else { + delete p; + } + } +#endif /* U_HIDE_DRAFT_API */ }; /**