From c8d0e349052217cc43da97877a120228c5fa3b8f Mon Sep 17 00:00:00 2001 From: Shane Carr Date: Fri, 9 Feb 2018 22:52:42 +0000 Subject: [PATCH] ICU-13587 Changing NumberFormatter adoption methods to take non-const pointers, and updating documentation strings according to feedback. X-SVN-Rev: 40886 --- icu4c/source/i18n/number_fluent.cpp | 8 ++++-- icu4c/source/i18n/unicode/numberformatter.h | 32 ++++++++++++--------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/icu4c/source/i18n/number_fluent.cpp b/icu4c/source/i18n/number_fluent.cpp index 3be3401ef3a..27113106c50 100644 --- a/icu4c/source/i18n/number_fluent.cpp +++ b/icu4c/source/i18n/number_fluent.cpp @@ -33,12 +33,13 @@ Derived NumberFormatterSettings::unit(const icu::MeasureUnit &unit) con } template -Derived NumberFormatterSettings::adoptUnit(const icu::MeasureUnit *unit) const { +Derived NumberFormatterSettings::adoptUnit(icu::MeasureUnit *unit) const { Derived copy(*this); // Just copy the unit into the MacroProps by value, and delete it since we have ownership. // NOTE: Slicing occurs here. However, CurrencyUnit can be restored from MeasureUnit. // TimeUnit may be affected, but TimeUnit is not as relevant to number formatting. if (unit != nullptr) { + // TODO: On nullptr, reset to default value? copy.fMacros.unit = *unit; delete unit; } @@ -54,10 +55,11 @@ Derived NumberFormatterSettings::perUnit(const icu::MeasureUnit &perUni } template -Derived NumberFormatterSettings::adoptPerUnit(const icu::MeasureUnit *perUnit) const { +Derived NumberFormatterSettings::adoptPerUnit(icu::MeasureUnit *perUnit) const { Derived copy(*this); // See comments above about slicing and ownership. if (perUnit != nullptr) { + // TODO: On nullptr, reset to default value? copy.fMacros.perUnit = *perUnit; delete perUnit; } @@ -96,7 +98,7 @@ Derived NumberFormatterSettings::symbols(const DecimalFormatSymbols &sy } template -Derived NumberFormatterSettings::adoptSymbols(const NumberingSystem *ns) const { +Derived NumberFormatterSettings::adoptSymbols(NumberingSystem *ns) const { Derived copy(*this); copy.fMacros.symbols.setTo(ns); return copy; diff --git a/icu4c/source/i18n/unicode/numberformatter.h b/icu4c/source/i18n/unicode/numberformatter.h index ac852f27e8e..d2c70a99055 100644 --- a/icu4c/source/i18n/unicode/numberformatter.h +++ b/icu4c/source/i18n/unicode/numberformatter.h @@ -1515,7 +1515,8 @@ class U_I18N_API NumberFormatterSettings { * All units will be properly localized with locale data, and all units are compatible with notation styles, * rounding strategies, and other number formatter settings. * - * Pass this method any instance of {@link MeasureUnit}. For units of measure: + * Pass this method any instance of {@link MeasureUnit}. For units of measure (which often involve the + * factory methods that return a pointer): * *
      * NumberFormatter::with().adoptUnit(MeasureUnit::createMeter(status))
@@ -1550,7 +1551,11 @@ class U_I18N_API NumberFormatterSettings {
 
     /**
      * Like unit(), but takes ownership of a pointer.  Convenient for use with the MeasureFormat factory
-     * methods, which return pointers that need ownership.
+     * methods, which return pointers that need ownership.  Example:
+     *
+     * 
+     * NumberFormatter::with().adoptUnit(MeasureUnit::createMeter(status))
+     * 
* * @param unit * The unit to render. @@ -1559,19 +1564,14 @@ class U_I18N_API NumberFormatterSettings { * @see MeasureUnit * @draft ICU 60 */ - Derived adoptUnit(const icu::MeasureUnit *unit) const; + Derived adoptUnit(icu::MeasureUnit *unit) const; /** * Sets a unit to be used in the denominator. For example, to format "3 m/s", pass METER to the unit and SECOND to * the perUnit. * - * Pass this method any instance of {@link MeasureUnit}. For example: - * - *
-     * NumberFormatter::with()
-     *      .adoptUnit(MeasureUnit::createMeter(status))
-     *      .adoptPerUnit(MeasureUnit::createSecond(status))
-     * 
+ * Pass this method any instance of {@link MeasureUnit}. Since MeasureUnit factory methods return pointers, the + * {@link #adoptPerUnit} version of this method is often more useful. * * The default is not to display any unit in the denominator. * @@ -1587,7 +1587,13 @@ class U_I18N_API NumberFormatterSettings { /** * Like perUnit(), but takes ownership of a pointer. Convenient for use with the MeasureFormat factory - * methods, which return pointers that need ownership. + * methods, which return pointers that need ownership. Example: + * + *
+     * NumberFormatter::with()
+     *      .adoptUnit(MeasureUnit::createMeter(status))
+     *      .adoptPerUnit(MeasureUnit::createSecond(status))
+     * 
* * @param perUnit * The unit to render in the denominator. @@ -1596,7 +1602,7 @@ class U_I18N_API NumberFormatterSettings { * @see MeasureUnit * @draft ICU 61 */ - Derived adoptPerUnit(const icu::MeasureUnit *perUnit) const; + Derived adoptPerUnit(icu::MeasureUnit *perUnit) const; /** * Specifies the rounding strategy to use when formatting numbers. @@ -1761,7 +1767,7 @@ class U_I18N_API NumberFormatterSettings { * @see NumberingSystem * @draft ICU 60 */ - Derived adoptSymbols(const NumberingSystem *symbols) const; + Derived adoptSymbols(NumberingSystem *symbols) const; /** * Sets the width of the unit (measure unit or currency). Most common values: -- 2.40.0