]> granicus.if.org Git - icu/commitdiff
add emplaceBackAndConfirm to not confirm the emplace back everytime
authorYounies Mahmoud <younies.mahmoud@gmail.com>
Fri, 12 Jun 2020 22:10:58 +0000 (00:10 +0200)
committerYounies Mahmoud <younies.mahmoud@gmail.com>
Fri, 12 Jun 2020 22:10:58 +0000 (00:10 +0200)
icu4c/source/common/cmemory.h
icu4c/source/i18n/complexunitsconverter.cpp
icu4c/source/i18n/unitsrouter.cpp

index efc79efdf93de993ef30db117704f5bcd4ce594e..e54d397405da22880ee4685b979d8a18eacc50b2 100644 (file)
@@ -776,6 +776,12 @@ public:
         return this->create(args...);
     }
 
+    template <typename... Args>
+    void emplaceBackAndConfirm(UErrorCode &status, Args &&... args) {
+        T *pointer = this->create(args...);
+        if (pointer == nullptr) { status = U_MEMORY_ALLOCATION_ERROR; }
+    }
+
     int32_t length() const {
         return this->fCount;
     }
index c67fad84690328815f073e95b6aec1ed8cf0759a..b501a6bd49b500960e50e2ce1f924367c4cd7ee9 100644 (file)
@@ -20,8 +20,8 @@ ComplexUnitsConverter::ComplexUnitsConverter(const MeasureUnit inputUnit, const
                                              const ConversionRates &ratesInfo, UErrorCode &status) {
 
     if (outputUnits.getComplexity(status) != UMeasureUnitComplexity::UMEASURE_UNIT_MIXED) {
-        unitConverters_.emplaceBack(inputUnit, outputUnits, ratesInfo, status);
-        units_.emplaceBack(outputUnits);
+        unitConverters_.emplaceBackAndConfirm(status, inputUnit, outputUnits, ratesInfo, status);
+        units_.emplaceBackAndConfirm(status, outputUnits);
         return;
     }
 
@@ -44,7 +44,7 @@ ComplexUnitsConverter::ComplexUnitsConverter(const MeasureUnit inputUnit, const
     MaybeStackVector<MeasureUnit> singleUnitsInOrder;
     for (int i = 0; i < length; ++i) {
         // TODO(younies): ensure units being in order in phase 2. Now, the units in order by default.
-        singleUnitsInOrder.emplaceBack(singleUnits[i]);
+        singleUnitsInOrder.emplaceBackAndConfirm(status, singleUnits[i]);
     }
 
     if (singleUnitsInOrder.length() == 0) {
@@ -54,10 +54,11 @@ ComplexUnitsConverter::ComplexUnitsConverter(const MeasureUnit inputUnit, const
 
     for (int i = 0, n = singleUnitsInOrder.length(); i < n; i++) {
         if (i == 0) { // first element
-            unitConverters_.emplaceBack(inputUnit, *singleUnitsInOrder[i], ratesInfo, status);
+            unitConverters_.emplaceBackAndConfirm(status, inputUnit, *singleUnitsInOrder[i], ratesInfo,
+                                                status);
         } else {
-            unitConverters_.emplaceBack(*singleUnitsInOrder[i - 1], *singleUnitsInOrder[i], ratesInfo,
-                                        status);
+            unitConverters_.emplaceBackAndConfirm(status, *singleUnitsInOrder[i - 1],
+                                                *singleUnitsInOrder[i], ratesInfo, status);
         }
 
         if (U_FAILURE(status)) { return; }
@@ -85,7 +86,8 @@ MaybeStackVector<Measure> ComplexUnitsConverter::convert(double quantity, UError
             Formattable formattableNewQuantity(newQuantity);
 
             // NOTE: Measure would own its MeasureUnit.
-            result.emplaceBack(formattableNewQuantity, new MeasureUnit(*units_[i]), status);
+            result.emplaceBackAndConfirm(status, formattableNewQuantity, new MeasureUnit(*units_[i]),
+                                       status);
 
             // Keep the residual of the quantity.
             //   For example: `3.6 feet`, keep only `0.6 feet`
@@ -94,7 +96,7 @@ MaybeStackVector<Measure> ComplexUnitsConverter::convert(double quantity, UError
             Formattable formattableQuantity(quantity);
 
             // NOTE: Measure would own its MeasureUnit.
-            result.emplaceBack(formattableQuantity, new MeasureUnit(*units_[i]), status);
+            result.emplaceBackAndConfirm(status, formattableQuantity, new MeasureUnit(*units_[i]), status);
         }
     }
 
index 5c337341bfd1c947ce7bfcf3f6a49cc0f01826e4..1cfa2b971d490f71368332d96c6cd775d230ba67 100644 (file)
@@ -40,8 +40,8 @@ UnitsRouter::UnitsRouter(MeasureUnit inputUnit, StringPiece region, StringPiece
         MeasureUnit complexTargetUnit = MeasureUnit::forIdentifier(preference.unit.data(), status);
         if (U_FAILURE(status)) { return; }
 
-        converterPreferences_.emplaceBack(inputUnit, complexTargetUnit, preference.geq, conversionRates,
-                                          status);
+        converterPreferences_.emplaceBackAndConfirm(status, inputUnit, complexTargetUnit, preference.geq,
+                                                    conversionRates, status);
         if (U_FAILURE(status)) { return; }
     }
 }