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: 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
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
;
103 * <p><strong>IMPORTANT:</strong> New users are strongly encouraged to see if
104 * numberformatter.h fits their use case. Although not deprecated, this header
105 * is provided for backwards compatibility only.
111 class U_I18N_API MeasureFormat
: public Format
{
113 using Format::parseObject
;
114 using Format::format
;
119 * <strong>NOTE:</strong> New users are strongly encouraged to use
120 * {@link icu::number::NumberFormatter} instead of NumberFormat.
124 const Locale
&locale
, UMeasureFormatWidth width
, UErrorCode
&status
);
129 * <strong>NOTE:</strong> New users are strongly encouraged to use
130 * {@link icu::number::NumberFormatter} instead of NumberFormat.
134 const Locale
&locale
,
135 UMeasureFormatWidth width
,
136 NumberFormat
*nfToAdopt
,
143 MeasureFormat(const MeasureFormat
&other
);
146 * Assignment operator.
149 MeasureFormat
&operator=(const MeasureFormat
&rhs
);
155 virtual ~MeasureFormat();
158 * Return true if given Format objects are semantically equal.
161 virtual UBool
operator==(const Format
&other
) const;
164 * Clones this object polymorphically.
167 virtual Format
*clone() const;
170 * Formats object to produce a string.
173 virtual UnicodeString
&format(
174 const Formattable
&obj
,
175 UnicodeString
&appendTo
,
177 UErrorCode
&status
) const;
180 * Parse a string to produce an object. This implementation sets
181 * status to U_UNSUPPORTED_ERROR.
185 virtual void parseObject(
186 const UnicodeString
&source
,
188 ParsePosition
&pos
) const;
191 * Formats measure objects to produce a string. An example of such a
192 * formatted string is 3 meters, 3.5 centimeters. Measure objects appear
193 * in the formatted string in the same order they appear in the "measures"
194 * array. The NumberFormat of this object is used only to format the amount
195 * of the very last measure. The other amounts are formatted with zero
196 * decimal places while rounding toward zero.
197 * @param measures array of measure objects.
198 * @param measureCount the number of measure objects.
199 * @param appendTo formatted string appended here.
200 * @param pos the field position.
201 * @param status the error.
202 * @return appendTo reference
206 UnicodeString
&formatMeasures(
207 const Measure
*measures
,
208 int32_t measureCount
,
209 UnicodeString
&appendTo
,
211 UErrorCode
&status
) const;
213 #ifndef U_HIDE_INTERNAL_API
215 * Apple-specific for now.
216 * Like formatMeasures above, but with a
217 * FieldPositionIterator* instead of a FieldPosition&
219 * @param measures Array of measure objects.
220 * @param measureCount the number of measure objects.
221 * @param appendTo Formatted string appended here.
222 * @param posIter On return, can be used to iterate over positions
223 * of fields generated by this format call. Field
224 * values are defined in UAMeasureUnit.
225 * @param status The error.
226 * @return appendTo reference
230 UnicodeString
&formatMeasures(
231 const Measure
*measures
,
232 int32_t measureCount
,
233 UnicodeString
&appendTo
,
234 FieldPositionIterator
* posIter
,
235 UErrorCode
&status
) const;
238 * Apple-specific for now
241 UMeasureFormatWidth
getWidth(void) const;
243 #endif /* U_HIDE_INTERNAL_API */
247 * Formats a single measure per unit. An example of such a
248 * formatted string is 3.5 meters per second.
249 * @param measure The measure object. In above example, 3.5 meters.
250 * @param perUnit The per unit. In above example, it is
251 * `*%MeasureUnit::createSecond(status)`.
252 * @param appendTo formatted string appended here.
253 * @param pos the field position.
254 * @param status the error.
255 * @return appendTo reference
259 UnicodeString
&formatMeasurePerUnit(
260 const Measure
&measure
,
261 const MeasureUnit
&perUnit
,
262 UnicodeString
&appendTo
,
264 UErrorCode
&status
) const;
267 * Gets the display name of the specified {@link MeasureUnit} corresponding to the current
268 * locale and format width.
269 * @param unit The unit for which to get a display name.
270 * @param status the error.
271 * @return The display name in the locale and width specified in
272 * the MeasureFormat constructor, or null if there is no display name available
273 * for the specified unit.
277 UnicodeString
getUnitDisplayName(const MeasureUnit
& unit
, UErrorCode
&status
) const;
281 * Return a formatter for CurrencyAmount objects in the given
284 * <strong>NOTE:</strong> New users are strongly encouraged to use
285 * {@link icu::number::NumberFormatter} instead of NumberFormat.
286 * @param locale desired locale
287 * @param ec input-output error code
288 * @return a formatter object, or NULL upon error
291 static MeasureFormat
* U_EXPORT2
createCurrencyFormat(const Locale
& locale
,
295 * Return a formatter for CurrencyAmount objects in the default
298 * <strong>NOTE:</strong> New users are strongly encouraged to use
299 * {@link icu::number::NumberFormatter} instead of NumberFormat.
300 * @param ec input-output error code
301 * @return a formatter object, or NULL upon error
304 static MeasureFormat
* U_EXPORT2
createCurrencyFormat(UErrorCode
& ec
);
307 * Return the class ID for this class. This is useful only for comparing to
308 * a return value from getDynamicClassID(). For example:
310 * . Base* polymorphic_pointer = createPolymorphicObject();
311 * . if (polymorphic_pointer->getDynamicClassID() ==
312 * . erived::getStaticClassID()) ...
314 * @return The class ID for all objects of this class.
317 static UClassID U_EXPORT2
getStaticClassID(void);
320 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
321 * method is to implement a simple version of RTTI, since not all C++
322 * compilers support genuine RTTI. Polymorphic operator==() and clone()
323 * methods call this method.
325 * @return The class ID for this object. All objects of a
326 * given class have the same class ID. Objects of
327 * other classes have different class IDs.
330 virtual UClassID
getDynamicClassID(void) const;
334 * Default constructor.
339 #ifndef U_HIDE_INTERNAL_API
343 * Initialize or change MeasureFormat class from subclass.
346 void initMeasureFormat(
347 const Locale
&locale
,
348 UMeasureFormatWidth width
,
349 NumberFormat
*nfToAdopt
,
353 * Allows subclass to change locale. Note that this method also changes
354 * the NumberFormat object. Returns TRUE if locale changed; FALSE if no
358 UBool
setMeasureFormatLocale(const Locale
&locale
, UErrorCode
&status
);
361 // Apple-only, temporarily public for Apple use
364 * Let subclass change NumberFormat.
367 void adoptNumberFormat(NumberFormat
*nfToAdopt
, UErrorCode
&status
);
370 * Gets the display name for a unit.
371 * @param unit The unit whose display name to get.
372 * @param result Receives the name result, if any (if none,
374 * @return Reference to result
378 UnicodeString
&getUnitName(
379 const MeasureUnit
* unit
,
380 UnicodeString
&result
) const;
383 * Gets the display name for a set of units.
384 * @param units Array of units whose display name to get.
385 * @param unitCount The count of units
386 * @param listStyle The list style used for combining the unit names.
387 * @param result Receives the name result, if any (if none,
389 * @return Reference to result
393 UnicodeString
&getMultipleUnitNames(
394 const MeasureUnit
** units
,
396 UAMeasureNameListStyle listStyle
,
397 UnicodeString
&result
) const;
404 const NumberFormat
&getNumberFormatInternal() const;
408 * Always returns the short form currency formatter.
411 const NumberFormat
& getCurrencyFormatInternal() const;
417 const PluralRules
&getPluralRules() const;
423 Locale
getLocale(UErrorCode
&status
) const;
429 const char *getLocaleID(UErrorCode
&status
) const;
431 #endif /* U_HIDE_INTERNAL_API */
434 const MeasureFormatCacheData
*cache
;
435 const SharedNumberFormat
*numberFormat
;
436 const SharedPluralRules
*pluralRules
;
437 UMeasureFormatWidth fWidth
;
438 UBool stripPatternSpaces
;
440 // Declared outside of MeasureFormatSharedData because ListFormatter
441 // objects are relatively cheap to copy; therefore, they don't need to be
442 // shared across instances.
443 ListFormatter
*listFormatter
;
444 ListFormatter
*listFormatterStd
; // standard list style, option for display names; Apple specific
446 UnicodeString
&formatMeasure(
447 const Measure
&measure
,
448 const NumberFormat
&nf
,
449 UnicodeString
&appendTo
,
451 UErrorCode
&status
) const;
453 UnicodeString
&formatMeasuresSlowTrack(
454 const Measure
*measures
,
455 int32_t measureCount
,
456 UnicodeString
& appendTo
,
458 UErrorCode
& status
) const;
460 UnicodeString
&formatNumeric(
461 const Formattable
*hms
, // always length 3: [0] is hour; [1] is
462 // minute; [2] is second.
463 int32_t bitMap
, // 1=hour set, 2=minute set, 4=second set
464 UnicodeString
&appendTo
,
465 FieldPositionHandler
& handler
,
466 UErrorCode
&status
) const;
468 UnicodeString
&formatNumeric(
470 const DateFormat
&dateFmt
,
471 UDateFormatField smallestField
,
472 const Formattable
&smallestAmount
,
473 UnicodeString
&appendTo
,
474 FieldPositionHandler
& handler
,
475 UErrorCode
&status
) const;
479 #endif // U_SHOW_CPLUSPLUS_API
481 #endif // #if !UCONFIG_NO_FORMATTING
482 #endif // #ifndef MEASUREFORMAT_H