2 **********************************************************************
3 * Copyright (c) 2004-2015, 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"
23 * \brief C++ API: Formatter for measure objects.
27 * Constants for various widths.
28 * There are 4 widths: Wide, Short, Narrow, Numeric.
29 * For example, for English, when formatting "3 hours"
30 * Wide is "3 hours"; short is "3 hrs"; narrow is "3h";
31 * formatting "3 hours 17 minutes" as numeric give "3:17"
34 enum UMeasureFormatWidth
{
36 // Wide, short, and narrow must be first and in this order.
38 * Spell out measure units.
44 * Abbreviate measure units.
50 * Use symbols for measure units when possible.
53 UMEASFMT_WIDTH_NARROW
,
56 * Completely omit measure units when possible. For example, format
57 * '5 hours, 37 minutes' as '5:37'
60 UMEASFMT_WIDTH_NUMERIC
,
63 * Count of values in this enum.
66 UMEASFMT_WIDTH_COUNT
= 4
69 typedef enum UMeasureFormatWidth UMeasureFormatWidth
;
77 class MeasureFormatCacheData
;
78 class SharedNumberFormat
;
79 class SharedPluralRules
;
80 class QuantityFormatter
;
81 class SimplePatternFormatter
;
87 * A formatter for measure objects.
93 class U_I18N_API MeasureFormat
: public Format
{
95 using Format::parseObject
;
103 const Locale
&locale
, UMeasureFormatWidth width
, UErrorCode
&status
);
110 const Locale
&locale
,
111 UMeasureFormatWidth width
,
112 NumberFormat
*nfToAdopt
,
119 MeasureFormat(const MeasureFormat
&other
);
122 * Assignment operator.
125 MeasureFormat
&operator=(const MeasureFormat
&rhs
);
131 virtual ~MeasureFormat();
134 * Return true if given Format objects are semantically equal.
137 virtual UBool
operator==(const Format
&other
) const;
140 * Clones this object polymorphically.
143 virtual Format
*clone() const;
146 * Formats object to produce a string.
149 virtual UnicodeString
&format(
150 const Formattable
&obj
,
151 UnicodeString
&appendTo
,
153 UErrorCode
&status
) const;
156 * Parse a string to produce an object. This implementation sets
157 * status to U_UNSUPPORTED_ERROR.
161 virtual void parseObject(
162 const UnicodeString
&source
,
164 ParsePosition
&pos
) const;
167 * Formats measure objects to produce a string. An example of such a
168 * formatted string is 3 meters, 3.5 centimeters. Measure objects appear
169 * in the formatted string in the same order they appear in the "measures"
170 * array. The NumberFormat of this object is used only to format the amount
171 * of the very last measure. The other amounts are formatted with zero
172 * decimal places while rounding toward zero.
173 * @param measures array of measure objects.
174 * @param measureCount the number of measure objects.
175 * @param appendTo formatted string appended here.
176 * @param pos the field position.
177 * @param status the error.
178 * @return appendTo reference
182 UnicodeString
&formatMeasures(
183 const Measure
*measures
,
184 int32_t measureCount
,
185 UnicodeString
&appendTo
,
187 UErrorCode
&status
) const;
189 #ifndef U_HIDE_INTERNAL_API
191 * Apple-specific for now
194 UMeasureFormatWidth
getWidth(void) const;
195 #endif /* U_HIDE_INTERNAL_API */
198 #ifndef U_HIDE_DRAFT_API
200 * Formats a single measure per unit. An example of such a
201 * formatted string is 3.5 meters per second.
202 * @param measure The measure object. In above example, 3.5 meters.
203 * @param perUnit The per unit. In above example, it is
204 * *MeasureUnit::createSecond(status).
205 * @param appendTo formatted string appended here.
206 * @param pos the field position.
207 * @param status the error.
208 * @return appendTo reference
212 UnicodeString
&formatMeasurePerUnit(
213 const Measure
&measure
,
214 const MeasureUnit
&perUnit
,
215 UnicodeString
&appendTo
,
217 UErrorCode
&status
) const;
219 #endif /* U_HIDE_DRAFT_API */
222 * Return a formatter for CurrencyAmount objects in the given
224 * @param locale desired locale
225 * @param ec input-output error code
226 * @return a formatter object, or NULL upon error
229 static MeasureFormat
* U_EXPORT2
createCurrencyFormat(const Locale
& locale
,
233 * Return a formatter for CurrencyAmount objects in the default
235 * @param ec input-output error code
236 * @return a formatter object, or NULL upon error
239 static MeasureFormat
* U_EXPORT2
createCurrencyFormat(UErrorCode
& ec
);
242 * Return the class ID for this class. This is useful only for comparing to
243 * a return value from getDynamicClassID(). For example:
245 * . Base* polymorphic_pointer = createPolymorphicObject();
246 * . if (polymorphic_pointer->getDynamicClassID() ==
247 * . erived::getStaticClassID()) ...
249 * @return The class ID for all objects of this class.
252 static UClassID U_EXPORT2
getStaticClassID(void);
255 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
256 * method is to implement a simple version of RTTI, since not all C++
257 * compilers support genuine RTTI. Polymorphic operator==() and clone()
258 * methods call this method.
260 * @return The class ID for this object. All objects of a
261 * given class have the same class ID. Objects of
262 * other classes have different class IDs.
265 virtual UClassID
getDynamicClassID(void) const;
269 * Default constructor.
274 #ifndef U_HIDE_INTERNAL_API
278 * Initialize or change MeasureFormat class from subclass.
281 void initMeasureFormat(
282 const Locale
&locale
,
283 UMeasureFormatWidth width
,
284 NumberFormat
*nfToAdopt
,
288 * Allows subclass to change locale. Note that this method also changes
289 * the NumberFormat object. Returns TRUE if locale changed; FALSE if no
293 UBool
setMeasureFormatLocale(const Locale
&locale
, UErrorCode
&status
);
296 // Apple-only, temporarily public for Apple use
299 * Let subclass change NumberFormat.
302 void adoptNumberFormat(NumberFormat
*nfToAdopt
, UErrorCode
&status
);
309 const NumberFormat
&getNumberFormat() const;
315 const PluralRules
&getPluralRules() const;
321 Locale
getLocale(UErrorCode
&status
) const;
327 const char *getLocaleID(UErrorCode
&status
) const;
329 #endif /* U_HIDE_INTERNAL_API */
332 const MeasureFormatCacheData
*cache
;
333 const SharedNumberFormat
*numberFormat
;
334 const SharedPluralRules
*pluralRules
;
335 UMeasureFormatWidth width
;
337 // Declared outside of MeasureFormatSharedData because ListFormatter
338 // objects are relatively cheap to copy; therefore, they don't need to be
339 // shared across instances.
340 ListFormatter
*listFormatter
;
342 const QuantityFormatter
*getQuantityFormatter(
345 UErrorCode
&status
) const;
347 const SimplePatternFormatter
*getPerUnitFormatter(
349 int32_t widthIndex
) const;
351 const SimplePatternFormatter
*getPerFormatter(
353 UErrorCode
&status
) const;
355 int32_t withPerUnitAndAppend(
356 const UnicodeString
&formatted
,
357 const MeasureUnit
&perUnit
,
358 UnicodeString
&appendTo
,
359 UErrorCode
&status
) const;
361 UnicodeString
&formatMeasure(
362 const Measure
&measure
,
363 const NumberFormat
&nf
,
364 UnicodeString
&appendTo
,
366 UErrorCode
&status
) const;
368 UnicodeString
&formatMeasuresSlowTrack(
369 const Measure
*measures
,
370 int32_t measureCount
,
371 UnicodeString
& appendTo
,
373 UErrorCode
& status
) const;
375 UnicodeString
&formatNumeric(
376 const Formattable
*hms
, // always length 3: [0] is hour; [1] is
377 // minute; [2] is second.
378 int32_t bitMap
, // 1=hour set, 2=minute set, 4=second set
379 UnicodeString
&appendTo
,
380 UErrorCode
&status
) const;
382 UnicodeString
&formatNumeric(
384 const DateFormat
&dateFmt
,
385 UDateFormatField smallestField
,
386 const Formattable
&smallestAmount
,
387 UnicodeString
&appendTo
,
388 UErrorCode
&status
) const;
393 #endif // #if !UCONFIG_NO_FORMATTING
394 #endif // #ifndef MEASUREFORMAT_H