From: Hugo van der Merwe <17109322+hugovdm@users.noreply.github.com> Date: Sat, 9 May 2020 00:56:11 +0000 (+0200) Subject: const implemented: const UnitPreferences *const X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3b1d33a54356d7ced2273f436420f18bea655bbb;p=icu const implemented: const UnitPreferences *const --- diff --git a/icu4c/source/common/cmemory.h b/icu4c/source/common/cmemory.h index 2bd833c54a7..efc79efdf93 100644 --- a/icu4c/source/common/cmemory.h +++ b/icu4c/source/common/cmemory.h @@ -323,6 +323,11 @@ public: * @return the array pointer */ T *getAlias() const { return ptr; } + /** + * Access without ownership change. + * @return the array pointer + */ + const T *getConstAlias() const { return ptr; } /** * Returns the array limit. Simple convenience method. * @return getAlias()+getCapacity() @@ -779,6 +784,10 @@ public: return this->fPool.getAlias(); } + const T *const *getConstAlias() const { + return this->fPool.getConstAlias(); + } + /** * Array item access (read-only). * No index bounds check. diff --git a/icu4c/source/i18n/unitsdata.cpp b/icu4c/source/i18n/unitsdata.cpp index b215c3498ca..a14dae637b6 100644 --- a/icu4c/source/i18n/unitsdata.cpp +++ b/icu4c/source/i18n/unitsdata.cpp @@ -386,13 +386,13 @@ U_I18N_API UnitPreferences::UnitPreferences(UErrorCode &status) { // of some kind. void U_I18N_API UnitPreferences::getPreferencesFor(const char *category, const char *usage, const char *region, - const UnitPreference **&outPreferences, + const UnitPreference *const *&outPreferences, int32_t &preferenceCount, UErrorCode &status) const { int32_t idx = getPreferenceMetadataIndex(&metadata_, category, usage, region, status); if (U_FAILURE(status)) { return; } U_ASSERT(idx >= 0); // Failures should have been taken care of by `status`. const UnitPreferenceMetadata *m = metadata_[idx]; - outPreferences = const_cast(unitPrefs_.getAlias()) + m->prefsOffset; + outPreferences = unitPrefs_.getConstAlias() + m->prefsOffset; preferenceCount = m->prefsCount; } diff --git a/icu4c/source/i18n/unitsdata.h b/icu4c/source/i18n/unitsdata.h index 173cc519425..6961b301621 100644 --- a/icu4c/source/i18n/unitsdata.h +++ b/icu4c/source/i18n/unitsdata.h @@ -160,7 +160,7 @@ class U_I18N_API UnitPreferences { * TODO: maybe replace `UnitPreference **&outPrefrences` with a slice class? */ void getPreferencesFor(const char *category, const char *usage, const char *region, - const UnitPreference **&outPreferences, int32_t &preferenceCount, + const UnitPreference *const *&outPreferences, int32_t &preferenceCount, UErrorCode &status) const; protected: diff --git a/icu4c/source/test/intltest/unitsdatatest.cpp b/icu4c/source/test/intltest/unitsdatatest.cpp index f3e2eef0d84..2e0fa90a7ab 100644 --- a/icu4c/source/test/intltest/unitsdatatest.cpp +++ b/icu4c/source/test/intltest/unitsdatatest.cpp @@ -91,7 +91,7 @@ void UnitsDataTest::testGetPreferences() { for (const auto &t : testCases) { logln(t.name); - const UnitPreference **prefs; + const UnitPreference *const *prefs; int32_t prefsCount; preferences.getPreferencesFor(t.category, t.usage, t.region, prefs, prefsCount, status); if (status.errIfFailureAndReset("getPreferencesFor(\"%s\", \"%s\", \"%s\", ...", t.category,