]> granicus.if.org Git - icu/commitdiff
ICU-21174 Add error-checking methods to MaybeStackVector & MemoryPool.
authoryounies <younies@chromium.org>
Tue, 16 Jun 2020 16:28:30 +0000 (18:28 +0200)
committerHugo van der Merwe <17109322+hugovdm@users.noreply.github.com>
Tue, 11 Aug 2020 17:52:09 +0000 (19:52 +0200)
- MaybeStackVector::emplaceBackAndCheckErrorCode()
- MemoryPool::createAndCheckErrorCode()

Started with cherry-picks of:
3b505977c0e82659896125698389b59eabe50b14
63b93bde5c4c9fe030d490d5d448087aa0d4e5fd

icu4c/source/common/cmemory.h

index bdf6ba2e5ca1e2e50276c7d92df5fba650949ce8..313180b29fbd84f989b9e36bc971451094267408 100644 (file)
@@ -731,6 +731,18 @@ public:
         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 @@ public:
         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;
     }