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 bdf6ba2..313180b 100644 --- a/icu4c/source/common/cmemory.h +++ b/icu4c/source/common/cmemory.h
@@ -731,6 +731,18 @@ class MemoryPool : public UMemory { return fPool[fCount++] = new T(std::forward<Args>(args)...); } + template <typename... Args> + 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 @@ class MaybeStackVector : protected MemoryPool<T, stackCapacity> { return this->create(args...); } + template <typename... Args> + T *emplaceBackAndCheckErrorCode(UErrorCode &status, Args &&... args) { + return this->createAndCheckErrorCode(status, args...); + } + int32_t length() const { return this->fCount; }