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"
25 #include "unicode/uameasureformat.h"
29 * \brief C++ API: Compatibility APIs for measure formatting.
33 * Constants for various widths.
34 * There are 4 widths: Wide, Short, Narrow, Numeric.
35 * For example, for English, when formatting "3 hours"
36 * Wide is "3 hours"; short is "3 hrs"; narrow is "3h";
37 * formatting "3 hours 17 minutes" as numeric give "3:17"
40 enum UMeasureFormatWidth
{
42 // Wide, short, and narrow must be first and in this order.
44 * Spell out measure units.
50 * Abbreviate measure units.
56 * Use symbols for measure units when possible.
59 UMEASFMT_WIDTH_NARROW
,
62 * Completely omit measure units when possible. For example, format
63 * '5 hours, 37 minutes' as '5:37'
66 UMEASFMT_WIDTH_NUMERIC
,
68 #ifndef U_HIDE_DEPRECATED_API
70 * One more than the highest normal UMeasureFormatWidth value.
71 * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
73 UMEASFMT_WIDTH_COUNT
= 4
74 #endif // U_HIDE_DEPRECATED_API
75 #ifndef U_HIDE_INTERNAL_API
79 * Shorter, between SHORT and NARROW (SHORT without space in unit pattern)
82 UMEASFMT_WIDTH_SHORTER
= 8
83 #endif /* U_HIDE_INTERNAL_API */
86 typedef enum UMeasureFormatWidth UMeasureFormatWidth
;
94 class MeasureFormatCacheData
;
95 class SharedNumberFormat
;
96 class SharedPluralRules
;
97 class QuantityFormatter
;
98 class SimpleFormatter
;
101 class FieldPositionHandler
;
104 * <p><strong>IMPORTANT:</strong> New users are strongly encouraged to see if
105 * numberformatter.h fits their use case. Although not deprecated, this header
106 * is provided for backwards compatibility only.
112 class U_I18N_API MeasureFormat
: public Format
{
114 using Format::parseObject
;
115 using Format::format
;
120 * <strong>NOTE:</strong> New users are strongly encouraged to use
121 * {@link icu::number::NumberFormatter} instead of NumberFormat.
125 const Locale
&locale
, UMeasureFormatWidth width
, UErrorCode
&status
);
130 * <strong>NOTE:</strong> New users are strongly encouraged to use
131 * {@link icu::number::NumberFormatter} instead of NumberFormat.
135 const Locale
&locale
,
136 UMeasureFormatWidth width
,
137 NumberFormat
*nfToAdopt
,
144 MeasureFormat(const MeasureFormat
&other
);
147 * Assignment operator.
150 MeasureFormat
&operator=(const MeasureFormat
&rhs
);
156 virtual ~MeasureFormat();
159 * Return true if given Format objects are semantically equal.
162 virtual UBool
operator==(const Format
&other
) const;
165 * Clones this object polymorphically.
168 virtual MeasureFormat
*clone() const;
171 * Formats object to produce a string.
174 virtual UnicodeString
&format(
175 const Formattable
&obj
,
176 UnicodeString
&appendTo
,
178 UErrorCode
&status
) const;
180 #ifndef U_FORCE_HIDE_DRAFT_API
182 * Parse a string to produce an object. This implementation sets
183 * status to U_UNSUPPORTED_ERROR.
187 virtual void parseObject(
188 const UnicodeString
&source
,
190 ParsePosition
&pos
) const;
191 #endif // U_FORCE_HIDE_DRAFT_API
194 * Formats measure objects to produce a string. An example of such a
195 * formatted string is 3 meters, 3.5 centimeters. Measure objects appear
196 * in the formatted string in the same order they appear in the "measures"
197 * array. The NumberFormat of this object is used only to format the amount
198 * of the very last measure. The other amounts are formatted with zero
199 * decimal places while rounding toward zero.
200 * @param measures array of measure objects.
201 * @param measureCount the number of measure objects.
202 * @param appendTo formatted string appended here.
203 * @param pos the field position.
204 * @param status the error.
205 * @return appendTo reference
209 UnicodeString
&formatMeasures(
210 const Measure
*measures
,
211 int32_t measureCount
,
212 UnicodeString
&appendTo
,
214 UErrorCode
&status
) const;
216 #ifndef U_HIDE_INTERNAL_API
218 * Apple-specific for now.
219 * Like formatMeasures above, but with a
220 * FieldPositionIterator* instead of a FieldPosition&
222 * @param measures Array of measure objects.
223 * @param measureCount the number of measure objects.
224 * @param appendTo Formatted string appended here.
225 * @param posIter On return, can be used to iterate over positions
226 * of fields generated by this format call. Field
227 * values are defined in UAMeasureUnit.
228 * @param status The error.
229 * @return appendTo reference
233 UnicodeString
&formatMeasures(
234 const Measure
*measures
,
235 int32_t measureCount
,
236 UnicodeString
&appendTo
,
237 FieldPositionIterator
* posIter
,
238 UErrorCode
&status
) const;
241 * Apple-specific for now
244 UMeasureFormatWidth
getWidth(void) const;
246 #endif /* U_HIDE_INTERNAL_API */
250 * Formats a single measure per unit. An example of such a
251 * formatted string is 3.5 meters per second.
252 * @param measure The measure object. In above example, 3.5 meters.
253 * @param perUnit The per unit. In above example, it is
254 * `*%MeasureUnit::createSecond(status)`.
255 * @param appendTo formatted string appended here.
256 * @param pos the field position.
257 * @param status the error.
258 * @return appendTo reference
262 UnicodeString
&formatMeasurePerUnit(
263 const Measure
&measure
,
264 const MeasureUnit
&perUnit
,
265 UnicodeString
&appendTo
,
267 UErrorCode
&status
) const;
270 * Gets the display name of the specified {@link MeasureUnit} corresponding to the current
271 * locale and format width.
272 * @param unit The unit for which to get a display name.
273 * @param status the error.
274 * @return The display name in the locale and width specified in
275 * the MeasureFormat constructor, or null if there is no display name available
276 * for the specified unit.
280 UnicodeString
getUnitDisplayName(const MeasureUnit
& unit
, UErrorCode
&status
) const;
284 * Return a formatter for CurrencyAmount objects in the given
287 * <strong>NOTE:</strong> New users are strongly encouraged to use
288 * {@link icu::number::NumberFormatter} instead of NumberFormat.
289 * @param locale desired locale
290 * @param ec input-output error code
291 * @return a formatter object, or NULL upon error
294 static MeasureFormat
* U_EXPORT2
createCurrencyFormat(const Locale
& locale
,
298 * Return a formatter for CurrencyAmount objects in the default
301 * <strong>NOTE:</strong> New users are strongly encouraged to use
302 * {@link icu::number::NumberFormatter} instead of NumberFormat.
303 * @param ec input-output error code
304 * @return a formatter object, or NULL upon error
307 static MeasureFormat
* U_EXPORT2
createCurrencyFormat(UErrorCode
& ec
);
310 * Return the class ID for this class. This is useful only for comparing to
311 * a return value from getDynamicClassID(). For example:
313 * . Base* polymorphic_pointer = createPolymorphicObject();
314 * . if (polymorphic_pointer->getDynamicClassID() ==
315 * . erived::getStaticClassID()) ...
317 * @return The class ID for all objects of this class.
320 static UClassID U_EXPORT2
getStaticClassID(void);
323 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
324 * method is to implement a simple version of RTTI, since not all C++
325 * compilers support genuine RTTI. Polymorphic operator==() and clone()
326 * methods call this method.
328 * @return The class ID for this object. All objects of a
329 * given class have the same class ID. Objects of
330 * other classes have different class IDs.
333 virtual UClassID
getDynamicClassID(void) const;
337 * Default constructor.
342 #ifndef U_HIDE_INTERNAL_API
346 * Initialize or change MeasureFormat class from subclass.
349 void initMeasureFormat(
350 const Locale
&locale
,
351 UMeasureFormatWidth width
,
352 NumberFormat
*nfToAdopt
,
356 * Allows subclass to change locale. Note that this method also changes
357 * the NumberFormat object. Returns TRUE if locale changed; FALSE if no
361 UBool
setMeasureFormatLocale(const Locale
&locale
, UErrorCode
&status
);
364 // Apple-only, temporarily public for Apple use
367 * Let subclass change NumberFormat.
370 void adoptNumberFormat(NumberFormat
*nfToAdopt
, UErrorCode
&status
);
373 * Gets the display name for a unit.
374 * @param unit The unit whose display name to get.
375 * @param result Receives the name result, if any (if none,
377 * @return Reference to result
381 UnicodeString
&getUnitName(
382 const MeasureUnit
* unit
,
383 UnicodeString
&result
) const;
386 * Gets the display name for a set of units.
387 * @param units Array of units whose display name to get.
388 * @param unitCount The count of units
389 * @param listStyle The list style used for combining the unit names.
390 * @param result Receives the name result, if any (if none,
392 * @return Reference to result
396 UnicodeString
&getMultipleUnitNames(
397 const MeasureUnit
** units
,
399 UAMeasureNameListStyle listStyle
,
400 UnicodeString
&result
) const;
407 const NumberFormat
&getNumberFormatInternal() const;
411 * Always returns the short form currency formatter.
414 const NumberFormat
& getCurrencyFormatInternal() const;
420 const PluralRules
&getPluralRules() const;
426 Locale
getLocale(UErrorCode
&status
) const;
432 const char *getLocaleID(UErrorCode
&status
) const;
434 #endif /* U_HIDE_INTERNAL_API */
437 const MeasureFormatCacheData
*cache
;
438 const SharedNumberFormat
*numberFormat
;
439 const SharedPluralRules
*pluralRules
;
440 UMeasureFormatWidth fWidth
;
441 UBool stripPatternSpaces
;
443 // Declared outside of MeasureFormatSharedData because ListFormatter
444 // objects are relatively cheap to copy; therefore, they don't need to be
445 // shared across instances.
446 ListFormatter
*listFormatter
;
447 ListFormatter
*listFormatterStd
; // standard list style, option for display names; Apple specific
449 UnicodeString
&formatMeasure(
450 const Measure
&measure
,
451 const NumberFormat
&nf
,
452 UnicodeString
&appendTo
,
454 UErrorCode
&status
) const;
456 UnicodeString
&formatMeasuresSlowTrack(
457 const Measure
*measures
,
458 int32_t measureCount
,
459 UnicodeString
& appendTo
,
461 UErrorCode
& status
) const;
463 UnicodeString
&formatNumeric(
464 const Formattable
*hms
, // always length 3: [0] is hour; [1] is
465 // minute; [2] is second.
466 int32_t bitMap
, // 1=hour set, 2=minute set, 4=second set
467 UnicodeString
&appendTo
,
468 UErrorCode
&status
) const;
473 #endif // #if !UCONFIG_NO_FORMATTING
475 #endif /* U_SHOW_CPLUSPLUS_API */
477 #endif // #ifndef MEASUREFORMAT_H