]> granicus.if.org Git - icu/commitdiff
Revert changes to LocalArray (localpointer.h)
authorShane F. Carr <shane@unicode.org>
Wed, 18 Mar 2020 00:29:59 +0000 (19:29 -0500)
committerShane F. Carr <shane@unicode.org>
Wed, 18 Mar 2020 00:29:59 +0000 (19:29 -0500)
icu4c/source/common/unicode/localpointer.h
icu4c/source/i18n/measunit_extra.cpp
icu4c/source/i18n/unicode/measunit.h
icu4c/source/test/intltest/measfmttest.cpp

index c69efbd5700679a2ec56d6fdd907033ae0d2a076..e011688b1a54df61696c868dc85754c65ccb9f8b 100644 (file)
@@ -378,9 +378,9 @@ public:
      * @param p simple pointer to an array of T objects that is adopted
      * @stable ICU 4.4
      */
-    explicit LocalArray(T *p=nullptr) : LocalPointerBase<T>(p), fLength(0) {}
+    explicit LocalArray(T *p=NULL) : LocalPointerBase<T>(p) {}
     /**
-     * Constructor takes ownership and reports an error if nullptr.
+     * Constructor takes ownership and reports an error if NULL.
      *
      * This constructor is intended to be used with other-class constructors
      * that may report a failure UErrorCode,
@@ -389,50 +389,22 @@ public:
      *
      * @param p simple pointer to an array of T objects that is adopted
      * @param errorCode in/out UErrorCode, set to U_MEMORY_ALLOCATION_ERROR
-     *     if p==nullptr and no other failure code had been set
+     *     if p==NULL and no other failure code had been set
      * @stable ICU 56
      */
-    LocalArray(T *p, UErrorCode &errorCode) : LocalArray<T>(p) {
-        if(p==nullptr && U_SUCCESS(errorCode)) {
-            errorCode=U_MEMORY_ALLOCATION_ERROR;
-        }
-    }
-
-private:
-    /** Constructor for withLengthAndCheckErrorCode() */
-    LocalArray(T *p, int32_t length, UErrorCode &errorCode) : LocalArray<T>(p) {
-        if (p != nullptr) {
-            fLength = length;
-        } else if (U_SUCCESS(errorCode)) {
+    LocalArray(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
+        if(p==NULL && U_SUCCESS(errorCode)) {
             errorCode=U_MEMORY_ALLOCATION_ERROR;
         }
     }
-
-public:
     /**
      * Move constructor, leaves src with isNull().
      * @param src source smart pointer
      * @stable ICU 56
      */
-    LocalArray(LocalArray<T> &&src) U_NOEXCEPT : LocalArray<T>(src.ptr) {
-        src.ptr=nullptr;
-    }
-
-#ifndef U_HIDE_DRAFT_API
-    /**
-     * Construct a LocalArray with a specified length.
-     *
-     * @param p simple pointer to an array of T objects that is adopted
-     * @param length number of valid objects in the array, accesible via length()
-     * @param errorCode in/out UErrorCode, set to U_MEMORY_ALLOCATION_ERROR
-     *     if p==nullptr and no other failure code had been set
-     *
-     * @draft ICU 67
-     */
-    static LocalArray<T> withLengthAndCheckErrorCode(T *p, int32_t length, UErrorCode& status) {
-        return LocalArray(p, length, status);
+    LocalArray(LocalArray<T> &&src) U_NOEXCEPT : LocalPointerBase<T>(src.ptr) {
+        src.ptr=NULL;
     }
-#endif // U_HIDE_DRAFT_API
 
 #ifndef U_HIDE_DRAFT_API
     /**
@@ -446,7 +418,7 @@ public:
      * @draft ICU 64
      */
     explicit LocalArray(std::unique_ptr<T[]> &&p)
-        : LocalArray<T>(p.release()) {}
+        : LocalPointerBase<T>(p.release()) {}
 #endif  /* U_HIDE_DRAFT_API */
 
     /**
@@ -466,9 +438,7 @@ public:
     LocalArray<T> &operator=(LocalArray<T> &&src) U_NOEXCEPT {
         delete[] LocalPointerBase<T>::ptr;
         LocalPointerBase<T>::ptr=src.ptr;
-        src.ptr=nullptr;
-        fLength=src.fLength;
-        src.fLength=0;
+        src.ptr=NULL;
         return *this;
     }
 
@@ -483,7 +453,6 @@ public:
      */
     LocalArray<T> &operator=(std::unique_ptr<T[]> &&p) U_NOEXCEPT {
         adoptInstead(p.release());
-        fLength=0;
         return *this;
     }
 #endif  /* U_HIDE_DRAFT_API */
@@ -497,9 +466,6 @@ public:
         T *temp=LocalPointerBase<T>::ptr;
         LocalPointerBase<T>::ptr=other.ptr;
         other.ptr=temp;
-        int32_t tempLength=fLength;
-        fLength=other.fLength;
-        other.fLength=tempLength;
     }
     /**
      * Non-member LocalArray swap function.
@@ -519,7 +485,6 @@ public:
     void adoptInstead(T *p) {
         delete[] LocalPointerBase<T>::ptr;
         LocalPointerBase<T>::ptr=p;
-        fLength=0;
     }
     /**
      * Deletes the array it owns,
@@ -546,7 +511,6 @@ public:
         } else {
             delete[] p;
         }
-        fLength=0;
     }
     /**
      * Array item access (writable).
@@ -573,24 +537,6 @@ public:
         return std::unique_ptr<T[]>(LocalPointerBase<T>::orphan());
     }
 #endif  /* U_HIDE_DRAFT_API */
-
-#ifndef U_HIDE_DRAFT_API
-    /**
-     * The length of the array contained in the LocalArray. The size must be
-     * provided when the LocalArray is constructed.
-     *
-     * @return The length of the array, or 0 if unknown.
-     * @draft ICU 67
-     */
-    int32_t length() const { return fLength; }
-#endif // U_HIDE_DRAFT_API
-
-private:
-    int32_t fLength = 0;
-
-    LocalArray(T *p, int32_t length) : LocalArray(p) {
-        fLength = length;
-    }
 };
 
 /**
index 4417c886355d9de870cafaa859f98e6c8ec5349c..186d34777f327d258eb297202ecd938d8dd77641 100644 (file)
@@ -757,15 +757,15 @@ MeasureUnit MeasureUnit::product(const MeasureUnit& other, UErrorCode& status) c
     return std::move(impl).build(status);
 }
 
-LocalArray<MeasureUnit> MeasureUnit::splitToSingleUnits(UErrorCode& status) const {
+LocalArray<MeasureUnit> MeasureUnit::splitToSingleUnits(int32_t& outCount, UErrorCode& status) const {
     MeasureUnitImpl temp;
     const MeasureUnitImpl& impl = MeasureUnitImpl::forMeasureUnit(*this, temp, status);
-    const int32_t length = impl.units.length();
-    MeasureUnit* arr = new MeasureUnit[length];
-    for (int32_t i = 0; i < length; i++) {
+    outCount = impl.units.length();
+    MeasureUnit* arr = new MeasureUnit[outCount];
+    for (int32_t i = 0; i < outCount; i++) {
         arr[i] = impl.units[i]->build(status);
     }
-    return LocalArray<MeasureUnit>::withLengthAndCheckErrorCode(arr, length, status);
+    return LocalArray<MeasureUnit>(arr, status);
 }
 
 
index e2e4426db0cb4c436af2d5886649348f9506aea6..2a00d537d668ad5e641e7ae1b8da17115507e08f 100644 (file)
@@ -361,6 +361,7 @@ class U_I18N_API MeasureUnit: public UObject {
      * @param prefix The SI prefix, from UMeasureSIPrefix.
      * @param status Set if this is not a SINGLE unit or if another error occurs.
      * @return A new SINGLE unit.
+     * @draft ICU 67
      */
     MeasureUnit withSIPrefix(UMeasureSIPrefix prefix, UErrorCode& status) const;
 
@@ -373,6 +374,7 @@ class U_I18N_API MeasureUnit: public UObject {
      *
      * @param status Set if this is not a SINGLE unit or if another error occurs.
      * @return The SI prefix of this SINGLE unit, from UMeasureSIPrefix.
+     * @draft ICU 67
      */
     UMeasureSIPrefix getSIPrefix(UErrorCode& status) const;
 
@@ -386,6 +388,7 @@ class U_I18N_API MeasureUnit: public UObject {
      * @param dimensionality The dimensionality (power).
      * @param status Set if this is not a SINGLE unit or if another error occurs.
      * @return A new SINGLE unit.
+     * @draft ICU 67
      */
     MeasureUnit withDimensionality(int32_t dimensionality, UErrorCode& status) const;
 
@@ -398,6 +401,7 @@ class U_I18N_API MeasureUnit: public UObject {
      *
      * @param status Set if this is not a SINGLE unit or if another error occurs.
      * @return The dimensionality (power) of this simple unit.
+     * @draft ICU 67
      */
     int32_t getDimensionality(UErrorCode& status) const;
 
@@ -411,6 +415,7 @@ class U_I18N_API MeasureUnit: public UObject {
      *
      * @param status Set if this is a SEQUENCE unit or if another error occurs.
      * @return The reciprocal of the target unit.
+     * @draft ICU 67
      */
     MeasureUnit reciprocal(UErrorCode& status) const;
 
@@ -429,9 +434,12 @@ class U_I18N_API MeasureUnit: public UObject {
      * @param other The MeasureUnit to multiply with the target.
      * @param status Set if this or other is a SEQUENCE unit or if another error occurs.
      * @return The product of the target unit with the provided unit.
+     * @draft ICU 67
      */
     MeasureUnit product(const MeasureUnit& other, UErrorCode& status) const;
+#endif // U_HIDE_DRAFT_API
 
+#ifndef U_HIDE_INTERNAL_API
     /**
      * Gets the list of SINGLE units contained within a SEQUENCE of COMPOUND unit.
      *
@@ -443,11 +451,15 @@ class U_I18N_API MeasureUnit: public UObject {
      *
      * If this is a SINGLE unit, an array of length 1 will be returned.
      *
+     * TODO(ICU-21021): Finalize this API and propose it as draft.
+     *
+     * @param outCount The number of elements in the return array.
      * @param status Set if an error occurs.
      * @return An array of single units, owned by the caller.
+     * @internal ICU 67 Technical Preview
      */
-    LocalArray<MeasureUnit> splitToSingleUnits(UErrorCode& status) const;
-#endif // U_HIDE_DRAFT_API
+    LocalArray<MeasureUnit> splitToSingleUnits(int32_t& outCount, UErrorCode& status) const;
+#endif // U_HIDE_INTERNAL_API
 
     /**
      * getAvailable gets all of the available units.
index 639bc6fe22fcb4e5dbf130addb7fd15a93cd5909..664cbb453770991952fbb78b03ef0e6e30eb7865 100644 (file)
@@ -3560,10 +3560,11 @@ void MeasureFormatTest::verifyCompoundUnit(
         unit.getComplexity(status));
     status.errIfFailureAndReset("%s: Complexity", identifier);
 
-    LocalArray<MeasureUnit> subUnits = unit.splitToSingleUnits(status);
-    assertEquals(uid + ": Length", subIdentifierCount, subUnits.length());
+    int32_t length;
+    LocalArray<MeasureUnit> subUnits = unit.splitToSingleUnits(length, status);
+    assertEquals(uid + ": Length", subIdentifierCount, length);
     for (int32_t i = 0;; i++) {
-        if (i >= subIdentifierCount || i >= subUnits.length()) break;
+        if (i >= subIdentifierCount || i >= length) break;
         assertEquals(uid + ": Sub-unit #" + Int64ToUnicodeString(i),
             subIdentifiers[i],
             subUnits[i].getIdentifier());
@@ -3592,10 +3593,11 @@ void MeasureFormatTest::verifySequenceUnit(
         unit.getComplexity(status));
     status.errIfFailureAndReset("%s: Complexity", identifier);
 
-    LocalArray<MeasureUnit> subUnits = unit.splitToSingleUnits(status);
-    assertEquals(uid + ": Length", subIdentifierCount, subUnits.length());
+    int32_t length;
+    LocalArray<MeasureUnit> subUnits = unit.splitToSingleUnits(length, status);
+    assertEquals(uid + ": Length", subIdentifierCount, length);
     for (int32_t i = 0;; i++) {
-        if (i >= subIdentifierCount || i >= subUnits.length()) break;
+        if (i >= subIdentifierCount || i >= length) break;
         assertEquals(uid + ": Sub-unit #" + Int64ToUnicodeString(i),
             subIdentifiers[i],
             subUnits[i].getIdentifier());