From: younies Date: Tue, 16 Jun 2020 16:28:30 +0000 (+0200) Subject: ICU-21174 Add error-checking methods to MaybeStackVector & MemoryPool. X-Git-Tag: cldr/2020-09-22~137 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7997955f2dfde70ea46240e7e3028b63bdba6357;p=icu ICU-21174 Add error-checking methods to MaybeStackVector & MemoryPool. - MaybeStackVector::emplaceBackAndCheckErrorCode() - MemoryPool::createAndCheckErrorCode() Started with cherry-picks of: 3b505977c0e82659896125698389b59eabe50b14 63b93bde5c4c9fe030d490d5d448087aa0d4e5fd --- diff --git a/icu4c/source/common/cmemory.h b/icu4c/source/common/cmemory.h index bdf6ba2e5ca..313180b29fb 100644 --- a/icu4c/source/common/cmemory.h +++ b/icu4c/source/common/cmemory.h @@ -731,6 +731,18 @@ public: return fPool[fCount++] = new T(std::forward(args)...); } + template + T* createAndCheckErrorCode(UErrorCode &status, Args &&... args) { + if (U_FAILURE(status)) { + return nullptr; + } + T *pointer = this->create(args...); + if (U_SUCCESS(status) && pointer == nullptr) { + status = U_MEMORY_ALLOCATION_ERROR; + } + return pointer; + } + /** * @return Number of elements that have been allocated. */ @@ -774,6 +786,11 @@ public: return this->create(args...); } + template + T *emplaceBackAndCheckErrorCode(UErrorCode &status, Args &&... args) { + return this->createAndCheckErrorCode(status, args...); + } + int32_t length() const { return this->fCount; }