1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (c) 2004-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
9 * Created: April 20, 2004
11 **********************************************************************
13 #ifndef MEASUREFORMAT_H
14 #define MEASUREFORMAT_H
16 #include "unicode/utypes.h"
18 #if U_SHOW_CPLUSPLUS_API
20 #if !UCONFIG_NO_FORMATTING
22 #include "unicode/format.h"
23 #include "unicode/udat.h"
27 * \brief C++ API: Compatibility APIs for measure formatting.
31 * Constants for various widths.
32 * There are 4 widths: Wide, Short, Narrow, Numeric.
33 * For example, for English, when formatting "3 hours"
34 * Wide is "3 hours"; short is "3 hrs"; narrow is "3h";
35 * formatting "3 hours 17 minutes" as numeric give "3:17"
38 enum UMeasureFormatWidth {
40 // Wide, short, and narrow must be first and in this order.
42 * Spell out measure units.
48 * Abbreviate measure units.
54 * Use symbols for measure units when possible.
57 UMEASFMT_WIDTH_NARROW,
60 * Completely omit measure units when possible. For example, format
61 * '5 hours, 37 minutes' as '5:37'
64 UMEASFMT_WIDTH_NUMERIC,
66 #ifndef U_HIDE_DEPRECATED_API
68 * One more than the highest normal UMeasureFormatWidth value.
69 * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
71 UMEASFMT_WIDTH_COUNT = 4
72 #endif // U_HIDE_DEPRECATED_API
75 typedef enum UMeasureFormatWidth UMeasureFormatWidth;
83 class MeasureFormatCacheData;
84 class SharedNumberFormat;
85 class SharedPluralRules;
86 class QuantityFormatter;
87 class SimpleFormatter;
92 * <p><strong>IMPORTANT:</strong> New users are strongly encouraged to see if
93 * numberformatter.h fits their use case. Although not deprecated, this header
94 * is provided for backwards compatibility only, and has much more limited
101 class U_I18N_API MeasureFormat : public Format {
103 using Format::parseObject;
104 using Format::format;
109 * <strong>NOTE:</strong> New users are strongly encouraged to use
110 * {@link icu::number::NumberFormatter} instead of NumberFormat.
114 const Locale &locale, UMeasureFormatWidth width, UErrorCode &status);
119 * <strong>NOTE:</strong> New users are strongly encouraged to use
120 * {@link icu::number::NumberFormatter} instead of NumberFormat.
124 const Locale &locale,
125 UMeasureFormatWidth width,
126 NumberFormat *nfToAdopt,
133 MeasureFormat(const MeasureFormat &other);
136 * Assignment operator.
139 MeasureFormat &operator=(const MeasureFormat &rhs);
145 virtual ~MeasureFormat();
148 * Return true if given Format objects are semantically equal.
151 virtual bool operator==(const Format &other) const override;
154 * Clones this object polymorphically.
157 virtual MeasureFormat *clone() const override;
160 * Formats object to produce a string.
163 virtual UnicodeString &format(
164 const Formattable &obj,
165 UnicodeString &appendTo,
167 UErrorCode &status) const override;
169 #ifndef U_FORCE_HIDE_DRAFT_API
171 * Parse a string to produce an object. This implementation sets
172 * status to U_UNSUPPORTED_ERROR.
176 virtual void parseObject(
177 const UnicodeString &source,
179 ParsePosition &pos) const override;
180 #endif // U_FORCE_HIDE_DRAFT_API
183 * Formats measure objects to produce a string. An example of such a
184 * formatted string is 3 meters, 3.5 centimeters. Measure objects appear
185 * in the formatted string in the same order they appear in the "measures"
186 * array. The NumberFormat of this object is used only to format the amount
187 * of the very last measure. The other amounts are formatted with zero
188 * decimal places while rounding toward zero.
189 * @param measures array of measure objects.
190 * @param measureCount the number of measure objects.
191 * @param appendTo formatted string appended here.
192 * @param pos the field position.
193 * @param status the error.
194 * @return appendTo reference
198 UnicodeString &formatMeasures(
199 const Measure *measures,
200 int32_t measureCount,
201 UnicodeString &appendTo,
203 UErrorCode &status) const;
206 * Formats a single measure per unit. An example of such a
207 * formatted string is 3.5 meters per second.
208 * @param measure The measure object. In above example, 3.5 meters.
209 * @param perUnit The per unit. In above example, it is
210 * `*%MeasureUnit::createSecond(status)`.
211 * @param appendTo formatted string appended here.
212 * @param pos the field position.
213 * @param status the error.
214 * @return appendTo reference
218 UnicodeString &formatMeasurePerUnit(
219 const Measure &measure,
220 const MeasureUnit &perUnit,
221 UnicodeString &appendTo,
223 UErrorCode &status) const;
226 * Gets the display name of the specified {@link MeasureUnit} corresponding to the current
227 * locale and format width.
228 * @param unit The unit for which to get a display name.
229 * @param status the error.
230 * @return The display name in the locale and width specified in
231 * the MeasureFormat constructor, or null if there is no display name available
232 * for the specified unit.
236 UnicodeString getUnitDisplayName(const MeasureUnit& unit, UErrorCode &status) const;
240 * Return a formatter for CurrencyAmount objects in the given
243 * <strong>NOTE:</strong> New users are strongly encouraged to use
244 * {@link icu::number::NumberFormatter} instead of NumberFormat.
245 * @param locale desired locale
246 * @param ec input-output error code
247 * @return a formatter object, or NULL upon error
250 static MeasureFormat* U_EXPORT2 createCurrencyFormat(const Locale& locale,
254 * Return a formatter for CurrencyAmount objects in the default
257 * <strong>NOTE:</strong> New users are strongly encouraged to use
258 * {@link icu::number::NumberFormatter} instead of NumberFormat.
259 * @param ec input-output error code
260 * @return a formatter object, or NULL upon error
263 static MeasureFormat* U_EXPORT2 createCurrencyFormat(UErrorCode& ec);
266 * Return the class ID for this class. This is useful only for comparing to
267 * a return value from getDynamicClassID(). For example:
269 * . Base* polymorphic_pointer = createPolymorphicObject();
270 * . if (polymorphic_pointer->getDynamicClassID() ==
271 * . erived::getStaticClassID()) ...
273 * @return The class ID for all objects of this class.
276 static UClassID U_EXPORT2 getStaticClassID(void);
279 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
280 * method is to implement a simple version of RTTI, since not all C++
281 * compilers support genuine RTTI. Polymorphic operator==() and clone()
282 * methods call this method.
284 * @return The class ID for this object. All objects of a
285 * given class have the same class ID. Objects of
286 * other classes have different class IDs.
289 virtual UClassID getDynamicClassID(void) const override;
293 * Default constructor.
298 #ifndef U_HIDE_INTERNAL_API
302 * Initialize or change MeasureFormat class from subclass.
305 void initMeasureFormat(
306 const Locale &locale,
307 UMeasureFormatWidth width,
308 NumberFormat *nfToAdopt,
312 * Allows subclass to change locale. Note that this method also changes
313 * the NumberFormat object. Returns true if locale changed; false if no
317 UBool setMeasureFormatLocale(const Locale &locale, UErrorCode &status);
321 * Let subclass change NumberFormat.
324 void adoptNumberFormat(NumberFormat *nfToAdopt, UErrorCode &status);
330 const NumberFormat &getNumberFormatInternal() const;
334 * Always returns the short form currency formatter.
337 const NumberFormat& getCurrencyFormatInternal() const;
343 const PluralRules &getPluralRules() const;
349 Locale getLocale(UErrorCode &status) const;
355 const char *getLocaleID(UErrorCode &status) const;
357 #endif /* U_HIDE_INTERNAL_API */
360 const MeasureFormatCacheData *cache;
361 const SharedNumberFormat *numberFormat;
362 const SharedPluralRules *pluralRules;
363 UMeasureFormatWidth fWidth;
365 // Declared outside of MeasureFormatSharedData because ListFormatter
366 // objects are relatively cheap to copy; therefore, they don't need to be
367 // shared across instances.
368 ListFormatter *listFormatter;
370 UnicodeString &formatMeasure(
371 const Measure &measure,
372 const NumberFormat &nf,
373 UnicodeString &appendTo,
375 UErrorCode &status) const;
377 UnicodeString &formatMeasuresSlowTrack(
378 const Measure *measures,
379 int32_t measureCount,
380 UnicodeString& appendTo,
382 UErrorCode& status) const;
384 UnicodeString &formatNumeric(
385 const Formattable *hms, // always length 3: [0] is hour; [1] is
386 // minute; [2] is second.
387 int32_t bitMap, // 1=hour set, 2=minute set, 4=second set
388 UnicodeString &appendTo,
389 UErrorCode &status) const;
394 #endif // #if !UCONFIG_NO_FORMATTING
396 #endif /* U_SHOW_CPLUSPLUS_API */
398 #endif // #ifndef MEASUREFORMAT_H