From: Shane F. Carr Date: Wed, 18 Mar 2020 00:29:59 +0000 (-0500) Subject: Revert changes to LocalArray (localpointer.h) X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3b659dc190f6e0abf163399ba1b8db8cdb1864c1;p=icu Revert changes to LocalArray (localpointer.h) --- diff --git a/icu4c/source/common/unicode/localpointer.h b/icu4c/source/common/unicode/localpointer.h index c69efbd5700..e011688b1a5 100644 --- a/icu4c/source/common/unicode/localpointer.h +++ b/icu4c/source/common/unicode/localpointer.h @@ -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(p), fLength(0) {} + explicit LocalArray(T *p=NULL) : LocalPointerBase(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(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(p) { - if (p != nullptr) { - fLength = length; - } else if (U_SUCCESS(errorCode)) { + LocalArray(T *p, UErrorCode &errorCode) : LocalPointerBase(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 &&src) U_NOEXCEPT : LocalArray(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 withLengthAndCheckErrorCode(T *p, int32_t length, UErrorCode& status) { - return LocalArray(p, length, status); + LocalArray(LocalArray &&src) U_NOEXCEPT : LocalPointerBase(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 &&p) - : LocalArray(p.release()) {} + : LocalPointerBase(p.release()) {} #endif /* U_HIDE_DRAFT_API */ /** @@ -466,9 +438,7 @@ public: LocalArray &operator=(LocalArray &&src) U_NOEXCEPT { delete[] LocalPointerBase::ptr; LocalPointerBase::ptr=src.ptr; - src.ptr=nullptr; - fLength=src.fLength; - src.fLength=0; + src.ptr=NULL; return *this; } @@ -483,7 +453,6 @@ public: */ LocalArray &operator=(std::unique_ptr &&p) U_NOEXCEPT { adoptInstead(p.release()); - fLength=0; return *this; } #endif /* U_HIDE_DRAFT_API */ @@ -497,9 +466,6 @@ public: T *temp=LocalPointerBase::ptr; LocalPointerBase::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::ptr; LocalPointerBase::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(LocalPointerBase::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; - } }; /** diff --git a/icu4c/source/i18n/measunit_extra.cpp b/icu4c/source/i18n/measunit_extra.cpp index 4417c886355..186d34777f3 100644 --- a/icu4c/source/i18n/measunit_extra.cpp +++ b/icu4c/source/i18n/measunit_extra.cpp @@ -757,15 +757,15 @@ MeasureUnit MeasureUnit::product(const MeasureUnit& other, UErrorCode& status) c return std::move(impl).build(status); } -LocalArray MeasureUnit::splitToSingleUnits(UErrorCode& status) const { +LocalArray 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::withLengthAndCheckErrorCode(arr, length, status); + return LocalArray(arr, status); } diff --git a/icu4c/source/i18n/unicode/measunit.h b/icu4c/source/i18n/unicode/measunit.h index e2e4426db0c..2a00d537d66 100644 --- a/icu4c/source/i18n/unicode/measunit.h +++ b/icu4c/source/i18n/unicode/measunit.h @@ -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 splitToSingleUnits(UErrorCode& status) const; -#endif // U_HIDE_DRAFT_API + LocalArray splitToSingleUnits(int32_t& outCount, UErrorCode& status) const; +#endif // U_HIDE_INTERNAL_API /** * getAvailable gets all of the available units. diff --git a/icu4c/source/test/intltest/measfmttest.cpp b/icu4c/source/test/intltest/measfmttest.cpp index 639bc6fe22f..664cbb45377 100644 --- a/icu4c/source/test/intltest/measfmttest.cpp +++ b/icu4c/source/test/intltest/measfmttest.cpp @@ -3560,10 +3560,11 @@ void MeasureFormatTest::verifyCompoundUnit( unit.getComplexity(status)); status.errIfFailureAndReset("%s: Complexity", identifier); - LocalArray subUnits = unit.splitToSingleUnits(status); - assertEquals(uid + ": Length", subIdentifierCount, subUnits.length()); + int32_t length; + LocalArray 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 subUnits = unit.splitToSingleUnits(status); - assertEquals(uid + ": Length", subIdentifierCount, subUnits.length()); + int32_t length; + LocalArray 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());