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 !UCONFIG_NO_FORMATTING
20 #include "unicode/format.h"
21 #include "unicode/udat.h"
23 #include "unicode/uameasureformat.h"
27 * \brief C++ API: Formatter for measure objects.
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
73 #ifndef U_HIDE_INTERNAL_API
77 * Shorter, between SHORT and NARROW (SHORT without space in unit pattern)
80 UMEASFMT_WIDTH_SHORTER
= 8
81 #endif /* U_HIDE_INTERNAL_API */
84 typedef enum UMeasureFormatWidth UMeasureFormatWidth
;
86 #if U_SHOW_CPLUSPLUS_API
93 class MeasureFormatCacheData
;
94 class SharedNumberFormat
;
95 class SharedPluralRules
;
96 class QuantityFormatter
;
97 class SimpleFormatter
;
100 class FieldPositionHandler
;
104 * A formatter for measure objects.
110 class U_I18N_API MeasureFormat
: public Format
{
112 using Format::parseObject
;
113 using Format::format
;
120 const Locale
&locale
, UMeasureFormatWidth width
, UErrorCode
&status
);
127 const Locale
&locale
,
128 UMeasureFormatWidth width
,
129 NumberFormat
*nfToAdopt
,
136 MeasureFormat(const MeasureFormat
&other
);
139 * Assignment operator.
142 MeasureFormat
&operator=(const MeasureFormat
&rhs
);
148 virtual ~MeasureFormat();
151 * Return true if given Format objects are semantically equal.
154 virtual UBool
operator==(const Format
&other
) const;
157 * Clones this object polymorphically.
160 virtual Format
*clone() const;
163 * Formats object to produce a string.
166 virtual UnicodeString
&format(
167 const Formattable
&obj
,
168 UnicodeString
&appendTo
,
170 UErrorCode
&status
) const;
173 * Parse a string to produce an object. This implementation sets
174 * status to U_UNSUPPORTED_ERROR.
178 virtual void parseObject(
179 const UnicodeString
&source
,
181 ParsePosition
&pos
) const;
184 * Formats measure objects to produce a string. An example of such a
185 * formatted string is 3 meters, 3.5 centimeters. Measure objects appear
186 * in the formatted string in the same order they appear in the "measures"
187 * array. The NumberFormat of this object is used only to format the amount
188 * of the very last measure. The other amounts are formatted with zero
189 * decimal places while rounding toward zero.
190 * @param measures array of measure objects.
191 * @param measureCount the number of measure objects.
192 * @param appendTo formatted string appended here.
193 * @param pos the field position.
194 * @param status the error.
195 * @return appendTo reference
199 UnicodeString
&formatMeasures(
200 const Measure
*measures
,
201 int32_t measureCount
,
202 UnicodeString
&appendTo
,
204 UErrorCode
&status
) const;
206 #ifndef U_HIDE_INTERNAL_API
208 * Apple-specific for now.
209 * Like formatMeasures above, but with a
210 * FieldPositionIterator* instead of a FieldPosition&
212 * @param measures Array of measure objects.
213 * @param measureCount the number of measure objects.
214 * @param appendTo Formatted string appended here.
215 * @param posIter On return, can be used to iterate over positions
216 * of fields generated by this format call. Field
217 * values are defined in UAMeasureUnit.
218 * @param status The error.
219 * @return appendTo reference
223 UnicodeString
&formatMeasures(
224 const Measure
*measures
,
225 int32_t measureCount
,
226 UnicodeString
&appendTo
,
227 FieldPositionIterator
* posIter
,
228 UErrorCode
&status
) const;
231 * Apple-specific for now
234 UMeasureFormatWidth
getWidth(void) const;
236 #endif /* U_HIDE_INTERNAL_API */
240 * Formats a single measure per unit. An example of such a
241 * formatted string is 3.5 meters per second.
242 * @param measure The measure object. In above example, 3.5 meters.
243 * @param perUnit The per unit. In above example, it is
244 * *MeasureUnit::createSecond(status).
245 * @param appendTo formatted string appended here.
246 * @param pos the field position.
247 * @param status the error.
248 * @return appendTo reference
252 UnicodeString
&formatMeasurePerUnit(
253 const Measure
&measure
,
254 const MeasureUnit
&perUnit
,
255 UnicodeString
&appendTo
,
257 UErrorCode
&status
) const;
259 #ifndef U_HIDE_DRAFT_API
261 * Gets the display name of the specified {@link MeasureUnit} corresponding to the current
262 * locale and format width.
263 * @param unit The unit for which to get a display name.
264 * @param status the error.
265 * @return The display name in the locale and width specified in
266 * {@link MeasureFormat#getInstance}, or null if there is no display name available
267 * for the specified unit.
271 UnicodeString
getUnitDisplayName(const MeasureUnit
& unit
, UErrorCode
&status
) const;
272 #endif /* U_HIDE_DRAFT_API */
276 * Return a formatter for CurrencyAmount objects in the given
278 * @param locale desired locale
279 * @param ec input-output error code
280 * @return a formatter object, or NULL upon error
283 static MeasureFormat
* U_EXPORT2
createCurrencyFormat(const Locale
& locale
,
287 * Return a formatter for CurrencyAmount objects in the default
289 * @param ec input-output error code
290 * @return a formatter object, or NULL upon error
293 static MeasureFormat
* U_EXPORT2
createCurrencyFormat(UErrorCode
& ec
);
296 * Return the class ID for this class. This is useful only for comparing to
297 * a return value from getDynamicClassID(). For example:
299 * . Base* polymorphic_pointer = createPolymorphicObject();
300 * . if (polymorphic_pointer->getDynamicClassID() ==
301 * . erived::getStaticClassID()) ...
303 * @return The class ID for all objects of this class.
306 static UClassID U_EXPORT2
getStaticClassID(void);
309 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
310 * method is to implement a simple version of RTTI, since not all C++
311 * compilers support genuine RTTI. Polymorphic operator==() and clone()
312 * methods call this method.
314 * @return The class ID for this object. All objects of a
315 * given class have the same class ID. Objects of
316 * other classes have different class IDs.
319 virtual UClassID
getDynamicClassID(void) const;
323 * Default constructor.
328 #ifndef U_HIDE_INTERNAL_API
332 * Initialize or change MeasureFormat class from subclass.
335 void initMeasureFormat(
336 const Locale
&locale
,
337 UMeasureFormatWidth width
,
338 NumberFormat
*nfToAdopt
,
342 * Allows subclass to change locale. Note that this method also changes
343 * the NumberFormat object. Returns TRUE if locale changed; FALSE if no
347 UBool
setMeasureFormatLocale(const Locale
&locale
, UErrorCode
&status
);
350 // Apple-only, temporarily public for Apple use
353 * Let subclass change NumberFormat.
356 void adoptNumberFormat(NumberFormat
*nfToAdopt
, UErrorCode
&status
);
359 * Gets the display name for a unit.
360 * @param unit The unit whose display name to get.
361 * @param result Receives the name result, if any (if none,
363 * @return Reference to result
367 UnicodeString
&getUnitName(
368 const MeasureUnit
* unit
,
369 UnicodeString
&result
) const;
372 * Gets the display name for a set of units.
373 * @param units Array of units whose display name to get.
374 * @param unitCount The count of units
375 * @param listStyle The list style used for combining the unit names.
376 * @param result Receives the name result, if any (if none,
378 * @return Reference to result
382 UnicodeString
&getMultipleUnitNames(
383 const MeasureUnit
** units
,
385 UAMeasureNameListStyle listStyle
,
386 UnicodeString
&result
) const;
393 const NumberFormat
&getNumberFormat() const;
399 const PluralRules
&getPluralRules() const;
405 Locale
getLocale(UErrorCode
&status
) const;
411 const char *getLocaleID(UErrorCode
&status
) const;
413 #endif /* U_HIDE_INTERNAL_API */
416 const MeasureFormatCacheData
*cache
;
417 const SharedNumberFormat
*numberFormat
;
418 const SharedPluralRules
*pluralRules
;
419 UMeasureFormatWidth width
;
420 UBool stripPatternSpaces
;
422 // Declared outside of MeasureFormatSharedData because ListFormatter
423 // objects are relatively cheap to copy; therefore, they don't need to be
424 // shared across instances.
425 ListFormatter
*listFormatter
;
426 ListFormatter
*listFormatterStd
; // standard list style, option for display names; Apple specific
428 const SimpleFormatter
*getFormatterOrNull(
429 const MeasureUnit
&unit
, UMeasureFormatWidth width
, int32_t index
) const;
431 const SimpleFormatter
*getFormatter(
432 const MeasureUnit
&unit
, UMeasureFormatWidth width
, int32_t index
,
433 UErrorCode
&errorCode
) const;
435 const SimpleFormatter
*getPluralFormatter(
436 const MeasureUnit
&unit
, UMeasureFormatWidth width
, int32_t index
,
437 UErrorCode
&errorCode
) const;
439 const SimpleFormatter
*getPerFormatter(
440 UMeasureFormatWidth width
,
441 UErrorCode
&status
) const;
443 int32_t withPerUnitAndAppend(
444 const UnicodeString
&formatted
,
445 const MeasureUnit
&perUnit
,
446 UnicodeString
&appendTo
,
447 UErrorCode
&status
) const;
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 FieldPositionHandler
& handler
,
469 UErrorCode
&status
) const;
471 UnicodeString
&formatNumeric(
473 const DateFormat
&dateFmt
,
474 UDateFormatField smallestField
,
475 const Formattable
&smallestAmount
,
476 UnicodeString
&appendTo
,
477 FieldPositionHandler
& handler
,
478 UErrorCode
&status
) const;
482 #endif // U_SHOW_CPLUSPLUS_API
484 #endif // #if !UCONFIG_NO_FORMATTING
485 #endif // #ifndef MEASUREFORMAT_H