From 7997955f2dfde70ea46240e7e3028b63bdba6357 Mon Sep 17 00:00:00 2001 From: younies Date: Tue, 16 Jun 2020 18:28:30 +0200 Subject: [PATCH] ICU-21174 Add error-checking methods to MaybeStackVector & MemoryPool. - MaybeStackVector::emplaceBackAndCheckErrorCode() - MemoryPool::createAndCheckErrorCode() Started with cherry-picks of: 3b505977c0e82659896125698389b59eabe50b14 63b93bde5c4c9fe030d490d5d448087aa0d4e5fd --- icu4c/source/common/cmemory.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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; } -- 2.40.0