2 **********************************************************************
3 * Copyright (c) 2004-2016, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
7 * Created: April 20, 2004
9 **********************************************************************
11 #ifndef MEASUREFORMAT_H
12 #define MEASUREFORMAT_H
14 #include "unicode/utypes.h"
16 #if !UCONFIG_NO_FORMATTING
18 #include "unicode/format.h"
19 #include "unicode/udat.h"
21 #include "unicode/uameasureformat.h"
25 * \brief C++ API: Formatter for measure objects.
29 * Constants for various widths.
30 * There are 4 widths: Wide, Short, Narrow, Numeric.
31 * For example, for English, when formatting "3 hours"
32 * Wide is "3 hours"; short is "3 hrs"; narrow is "3h";
33 * formatting "3 hours 17 minutes" as numeric give "3:17"
36 enum UMeasureFormatWidth
{
38 // Wide, short, and narrow must be first and in this order.
40 * Spell out measure units.
46 * Abbreviate measure units.
52 * Use symbols for measure units when possible.
55 UMEASFMT_WIDTH_NARROW
,
58 * Completely omit measure units when possible. For example, format
59 * '5 hours, 37 minutes' as '5:37'
62 UMEASFMT_WIDTH_NUMERIC
,
65 * Count of values in this enum.
68 UMEASFMT_WIDTH_COUNT
= 4,
72 * Shorter, between SHORT and NARROW (SHORT without space in unit pattern)
75 UMEASFMT_WIDTH_SHORTER
= 8
79 typedef enum UMeasureFormatWidth UMeasureFormatWidth
;
87 class MeasureFormatCacheData
;
88 class SharedNumberFormat
;
89 class SharedPluralRules
;
90 class QuantityFormatter
;
91 class SimpleFormatter
;
94 class FieldPositionHandler
;
98 * A formatter for measure objects.
104 class U_I18N_API MeasureFormat
: public Format
{
106 using Format::parseObject
;
107 using Format::format
;
114 const Locale
&locale
, UMeasureFormatWidth width
, UErrorCode
&status
);
121 const Locale
&locale
,
122 UMeasureFormatWidth width
,
123 NumberFormat
*nfToAdopt
,
130 MeasureFormat(const MeasureFormat
&other
);
133 * Assignment operator.
136 MeasureFormat
&operator=(const MeasureFormat
&rhs
);
142 virtual ~MeasureFormat();
145 * Return true if given Format objects are semantically equal.
148 virtual UBool
operator==(const Format
&other
) const;
151 * Clones this object polymorphically.
154 virtual Format
*clone() const;
157 * Formats object to produce a string.
160 virtual UnicodeString
&format(
161 const Formattable
&obj
,
162 UnicodeString
&appendTo
,
164 UErrorCode
&status
) const;
167 * Parse a string to produce an object. This implementation sets
168 * status to U_UNSUPPORTED_ERROR.
172 virtual void parseObject(
173 const UnicodeString
&source
,
175 ParsePosition
&pos
) const;
178 * Formats measure objects to produce a string. An example of such a
179 * formatted string is 3 meters, 3.5 centimeters. Measure objects appear
180 * in the formatted string in the same order they appear in the "measures"
181 * array. The NumberFormat of this object is used only to format the amount
182 * of the very last measure. The other amounts are formatted with zero
183 * decimal places while rounding toward zero.
184 * @param measures array of measure objects.
185 * @param measureCount the number of measure objects.
186 * @param appendTo formatted string appended here.
187 * @param pos the field position.
188 * @param status the error.
189 * @return appendTo reference
193 UnicodeString
&formatMeasures(
194 const Measure
*measures
,
195 int32_t measureCount
,
196 UnicodeString
&appendTo
,
198 UErrorCode
&status
) const;
200 #ifndef U_HIDE_INTERNAL_API
202 * Apple-specific for now.
203 * Like formatMeasures above, but with a
204 * FieldPositionIterator* instead of a FieldPosition&
206 * @param measures Array of measure objects.
207 * @param measureCount the number of measure objects.
208 * @param appendTo Formatted string appended here.
209 * @param posIter On return, can be used to iterate over positions
210 * of fields generated by this format call. Field
211 * values are defined in UAMeasureUnit.
212 * @param status The error.
213 * @return appendTo reference
217 UnicodeString
&formatMeasures(
218 const Measure
*measures
,
219 int32_t measureCount
,
220 UnicodeString
&appendTo
,
221 FieldPositionIterator
* posIter
,
222 UErrorCode
&status
) const;
225 * Apple-specific for now
228 UMeasureFormatWidth
getWidth(void) const;
230 #endif /* U_HIDE_INTERNAL_API */
234 * Formats a single measure per unit. An example of such a
235 * formatted string is 3.5 meters per second.
236 * @param measure The measure object. In above example, 3.5 meters.
237 * @param perUnit The per unit. In above example, it is
238 * *MeasureUnit::createSecond(status).
239 * @param appendTo formatted string appended here.
240 * @param pos the field position.
241 * @param status the error.
242 * @return appendTo reference
246 UnicodeString
&formatMeasurePerUnit(
247 const Measure
&measure
,
248 const MeasureUnit
&perUnit
,
249 UnicodeString
&appendTo
,
251 UErrorCode
&status
) const;
255 * Return a formatter for CurrencyAmount objects in the given
257 * @param locale desired locale
258 * @param ec input-output error code
259 * @return a formatter object, or NULL upon error
262 static MeasureFormat
* U_EXPORT2
createCurrencyFormat(const Locale
& locale
,
266 * Return a formatter for CurrencyAmount objects in the default
268 * @param ec input-output error code
269 * @return a formatter object, or NULL upon error
272 static MeasureFormat
* U_EXPORT2
createCurrencyFormat(UErrorCode
& ec
);
275 * Return the class ID for this class. This is useful only for comparing to
276 * a return value from getDynamicClassID(). For example:
278 * . Base* polymorphic_pointer = createPolymorphicObject();
279 * . if (polymorphic_pointer->getDynamicClassID() ==
280 * . erived::getStaticClassID()) ...
282 * @return The class ID for all objects of this class.
285 static UClassID U_EXPORT2
getStaticClassID(void);
288 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
289 * method is to implement a simple version of RTTI, since not all C++
290 * compilers support genuine RTTI. Polymorphic operator==() and clone()
291 * methods call this method.
293 * @return The class ID for this object. All objects of a
294 * given class have the same class ID. Objects of
295 * other classes have different class IDs.
298 virtual UClassID
getDynamicClassID(void) const;
302 * Default constructor.
307 #ifndef U_HIDE_INTERNAL_API
311 * Initialize or change MeasureFormat class from subclass.
314 void initMeasureFormat(
315 const Locale
&locale
,
316 UMeasureFormatWidth width
,
317 NumberFormat
*nfToAdopt
,
321 * Allows subclass to change locale. Note that this method also changes
322 * the NumberFormat object. Returns TRUE if locale changed; FALSE if no
326 UBool
setMeasureFormatLocale(const Locale
&locale
, UErrorCode
&status
);
329 // Apple-only, temporarily public for Apple use
332 * Let subclass change NumberFormat.
335 void adoptNumberFormat(NumberFormat
*nfToAdopt
, UErrorCode
&status
);
338 * Gets the display name for a unit.
339 * @param unit The unit whose display name to get.
340 * @param result Receives the name result, if any (if none,
342 * @return Reference to result
346 UnicodeString
&getUnitName(
347 const MeasureUnit
* unit
,
348 UnicodeString
&result
) const;
351 * Gets the display name for a set of units.
352 * @param units Array of units whose display name to get.
353 * @param unitCount The count of units
354 * @param listStyle The list style used for combining the unit names.
355 * @param result Receives the name result, if any (if none,
357 * @return Reference to result
361 UnicodeString
&getMultipleUnitNames(
362 const MeasureUnit
** units
,
364 UAMeasureNameListStyle listStyle
,
365 UnicodeString
&result
) const;
372 const NumberFormat
&getNumberFormat() const;
378 const PluralRules
&getPluralRules() const;
384 Locale
getLocale(UErrorCode
&status
) const;
390 const char *getLocaleID(UErrorCode
&status
) const;
392 #endif /* U_HIDE_INTERNAL_API */
395 const MeasureFormatCacheData
*cache
;
396 const SharedNumberFormat
*numberFormat
;
397 const SharedPluralRules
*pluralRules
;
398 UMeasureFormatWidth width
;
399 UBool stripPatternSpaces
;
401 // Declared outside of MeasureFormatSharedData because ListFormatter
402 // objects are relatively cheap to copy; therefore, they don't need to be
403 // shared across instances.
404 ListFormatter
*listFormatter
;
405 ListFormatter
*listFormatterStd
; // standard list style, option for display names; Apple specific
407 const SimpleFormatter
*getFormatterOrNull(
408 const MeasureUnit
&unit
, UMeasureFormatWidth width
, int32_t index
) const;
410 const SimpleFormatter
*getFormatter(
411 const MeasureUnit
&unit
, UMeasureFormatWidth width
, int32_t index
,
412 UErrorCode
&errorCode
) const;
414 const SimpleFormatter
*getPluralFormatter(
415 const MeasureUnit
&unit
, UMeasureFormatWidth width
, int32_t index
,
416 UErrorCode
&errorCode
) const;
418 const SimpleFormatter
*getPerFormatter(
419 UMeasureFormatWidth width
,
420 UErrorCode
&status
) const;
422 int32_t withPerUnitAndAppend(
423 const UnicodeString
&formatted
,
424 const MeasureUnit
&perUnit
,
425 UnicodeString
&appendTo
,
426 UErrorCode
&status
) const;
428 UnicodeString
&formatMeasure(
429 const Measure
&measure
,
430 const NumberFormat
&nf
,
431 UnicodeString
&appendTo
,
433 UErrorCode
&status
) const;
435 UnicodeString
&formatMeasuresSlowTrack(
436 const Measure
*measures
,
437 int32_t measureCount
,
438 UnicodeString
& appendTo
,
440 UErrorCode
& status
) const;
442 UnicodeString
&formatNumeric(
443 const Formattable
*hms
, // always length 3: [0] is hour; [1] is
444 // minute; [2] is second.
445 int32_t bitMap
, // 1=hour set, 2=minute set, 4=second set
446 UnicodeString
&appendTo
,
447 FieldPositionHandler
& handler
,
448 UErrorCode
&status
) const;
450 UnicodeString
&formatNumeric(
452 const DateFormat
&dateFmt
,
453 UDateFormatField smallestField
,
454 const Formattable
&smallestAmount
,
455 UnicodeString
&appendTo
,
456 FieldPositionHandler
& handler
,
457 UErrorCode
&status
) const;
462 #endif // #if !UCONFIG_NO_FORMATTING
463 #endif // #ifndef MEASUREFORMAT_H