2 **********************************************************************
3 * Copyright (c) 2004-2014, 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"
15 #include "unicode/measure.h"
17 #if !UCONFIG_NO_FORMATTING
19 #include "unicode/format.h"
20 #include "unicode/udat.h"
24 * \brief C++ API: Formatter for measure objects.
27 #ifndef U_HIDE_DRAFT_API
29 * Constants for various widths.
30 * There are 3 widths: Wide, Short, Narrow.
31 * For example, for English, when formatting "3 hours"
32 * Wide is "3 hours"; short is "3 hrs"; narrow is "3h"
35 enum UMeasureFormatWidth
{
37 // Wide, short, and narrow must be first and in this order.
39 * Spell out measure units.
45 * Abbreviate measure units.
51 * Use symbols for measure units when possible.
54 UMEASFMT_WIDTH_NARROW
,
57 * Completely omit measure units when possible. For example, format
58 * '5 hours, 37 minutes' as '5:37'
61 UMEASFMT_WIDTH_NUMERIC
,
64 * Count of values in this enum.
70 typedef enum UMeasureFormatWidth UMeasureFormatWidth
;
71 #endif /* U_HIDE_DRAFT_API */
77 class MeasureFormatCacheData
;
78 class SharedNumberFormat
;
79 class SharedPluralRules
;
80 class QuantityFormatter
;
86 * A formatter for measure objects.
92 class U_I18N_API MeasureFormat
: public Format
{
94 using Format::parseObject
;
97 #ifndef U_HIDE_DRAFT_API
103 const Locale
&locale
, UMeasureFormatWidth width
, UErrorCode
&status
);
110 const Locale
&locale
,
111 UMeasureFormatWidth width
,
112 NumberFormat
*nfToAdopt
,
114 #endif /* U_HIDE_DRAFT_API */
120 MeasureFormat(const MeasureFormat
&other
);
123 * Assignment operator.
126 MeasureFormat
&operator=(const MeasureFormat
&rhs
);
132 virtual ~MeasureFormat();
135 * Return true if given Format objects are semantically equal.
138 virtual UBool
operator==(const Format
&other
) const;
141 * Clones this object polymorphically.
144 virtual Format
*clone() const;
147 * Formats object to produce a string.
150 virtual UnicodeString
&format(
151 const Formattable
&obj
,
152 UnicodeString
&appendTo
,
154 UErrorCode
&status
) const;
157 * Parse a string to produce an object. This implementation sets
158 * status to U_UNSUPPORTED_ERROR.
162 virtual void parseObject(
163 const UnicodeString
&source
,
165 ParsePosition
&pos
) const;
167 #ifndef U_HIDE_DRAFT_API
169 * Formats measure objects to produce a string. An example of such a
170 * formatted string is 3 meters, 3.5 centimeters. Measure objects appear
171 * in the formatted string in the same order they appear in the "measures"
172 * array. The NumberFormat of this object is used only to format the amount
173 * of the very last measure. The other amounts are formatted with zero
174 * decimal places while rounding toward zero.
175 * @param measures array of measure objects.
176 * @param measureCount the number of measure objects.
177 * @param appendTo formatted string appended here.
178 * @param pos the field position.
179 * @param status the error.
180 * @return appendTo reference
184 UnicodeString
&formatMeasures(
185 const Measure
*measures
,
186 int32_t measureCount
,
187 UnicodeString
&appendTo
,
189 UErrorCode
&status
) const;
190 #endif /* U_HIDE_DRAFT_API */
194 * Return a formatter for CurrencyAmount objects in the given
196 * @param locale desired locale
197 * @param ec input-output error code
198 * @return a formatter object, or NULL upon error
201 static MeasureFormat
* U_EXPORT2
createCurrencyFormat(const Locale
& locale
,
205 * Return a formatter for CurrencyAmount objects in the default
207 * @param ec input-output error code
208 * @return a formatter object, or NULL upon error
211 static MeasureFormat
* U_EXPORT2
createCurrencyFormat(UErrorCode
& ec
);
214 * Return the class ID for this class. This is useful only for comparing to
215 * a return value from getDynamicClassID(). For example:
217 * . Base* polymorphic_pointer = createPolymorphicObject();
218 * . if (polymorphic_pointer->getDynamicClassID() ==
219 * . erived::getStaticClassID()) ...
221 * @return The class ID for all objects of this class.
224 static UClassID U_EXPORT2
getStaticClassID(void);
227 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
228 * method is to implement a simple version of RTTI, since not all C++
229 * compilers support genuine RTTI. Polymorphic operator==() and clone()
230 * methods call this method.
232 * @return The class ID for this object. All objects of a
233 * given class have the same class ID. Objects of
234 * other classes have different class IDs.
237 virtual UClassID
getDynamicClassID(void) const;
241 * Default constructor.
246 #ifndef U_HIDE_INTERNAL_API
248 #ifndef U_HIDE_DRAFT_API
251 * Initialize or change MeasureFormat class from subclass.
254 void initMeasureFormat(
255 const Locale
&locale
,
256 UMeasureFormatWidth width
,
257 NumberFormat
*nfToAdopt
,
262 * Allows subclass to change locale. Note that this method also changes
263 * the NumberFormat object. Returns TRUE if locale changed; FALSE if no
267 UBool
setMeasureFormatLocale(const Locale
&locale
, UErrorCode
&status
);
270 // Apple-only, temporarily public for Apple use
273 * Let subclass change NumberFormat.
276 void adoptNumberFormat(NumberFormat
*nfToAdopt
, UErrorCode
&status
);
283 const NumberFormat
&getNumberFormat() const;
289 const PluralRules
&getPluralRules() const;
295 Locale
getLocale(UErrorCode
&status
) const;
301 const char *getLocaleID(UErrorCode
&status
) const;
303 #endif /* U_HIDE_INTERNAL_API */
306 const MeasureFormatCacheData
*cache
;
307 const SharedNumberFormat
*numberFormat
;
308 const SharedPluralRules
*pluralRules
;
309 #ifndef U_HIDE_DRAFT_API
310 UMeasureFormatWidth width
;
313 // Declared outside of MeasureFormatSharedData because ListFormatter
314 // objects are relatively cheap to copy; therefore, they don't need to be
315 // shared across instances.
316 ListFormatter
*listFormatter
;
318 const QuantityFormatter
*getQuantityFormatter(
321 UErrorCode
&status
) const;
323 UnicodeString
&formatMeasure(
324 const Measure
&measure
,
325 const NumberFormat
&nf
,
326 UnicodeString
&appendTo
,
328 UErrorCode
&status
) const;
330 UnicodeString
&formatMeasuresSlowTrack(
331 const Measure
*measures
,
332 int32_t measureCount
,
333 UnicodeString
& appendTo
,
335 UErrorCode
& status
) const;
337 UnicodeString
&formatNumeric(
338 const Formattable
*hms
, // always length 3: [0] is hour; [1] is
339 // minute; [2] is second.
340 int32_t bitMap
, // 1=hour set, 2=minute set, 4=second set
341 UnicodeString
&appendTo
,
342 UErrorCode
&status
) const;
344 UnicodeString
&formatNumeric(
346 const DateFormat
&dateFmt
,
347 UDateFormatField smallestField
,
348 const Formattable
&smallestAmount
,
349 UnicodeString
&appendTo
,
350 UErrorCode
&status
) const;
355 #endif // #if !UCONFIG_NO_FORMATTING
356 #endif // #ifndef MEASUREFORMAT_H