]> granicus.if.org Git - icu/commitdiff
const implemented: const UnitPreferences *const
authorHugo van der Merwe <17109322+hugovdm@users.noreply.github.com>
Sat, 9 May 2020 00:56:11 +0000 (02:56 +0200)
committerHugo van der Merwe <17109322+hugovdm@users.noreply.github.com>
Sat, 9 May 2020 00:56:11 +0000 (02:56 +0200)
icu4c/source/common/cmemory.h
icu4c/source/i18n/unitsdata.cpp
icu4c/source/i18n/unitsdata.h
icu4c/source/test/intltest/unitsdatatest.cpp

index 2bd833c54a78f41dffb0846ce8d850ff3fdb487c..efc79efdf93de993ef30db117704f5bcd4ce594e 100644 (file)
@@ -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.
index b215c3498ca3c34e2f8b3282e6f357d7e743b7a7..a14dae637b65875387f20cc1c226fc1f1a15d7ff 100644 (file)
@@ -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<const UnitPreference **>(unitPrefs_.getAlias()) + m->prefsOffset;
+    outPreferences = unitPrefs_.getConstAlias() + m->prefsOffset;
     preferenceCount = m->prefsCount;
 }
 
index 173cc519425b7007cc9357febb9c60da7b195c15..6961b301621ec4d936e00016ffc623bc9be1ff1c 100644 (file)
@@ -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:
index f3e2eef0d845b4672d35f67fba6831f181c64f46..2e0fa90a7ab2e9982352b6fac951d87a8a40d014 100644 (file)
@@ -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,