]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
374ca955 A |
3 | /* |
4 | ********************************************************************** | |
2ca993e8 | 5 | * Copyright (c) 2004-2016, International Business Machines |
374ca955 A |
6 | * Corporation and others. All Rights Reserved. |
7 | ********************************************************************** | |
8 | * Author: Alan Liu | |
9 | * Created: April 20, 2004 | |
10 | * Since: ICU 3.0 | |
11 | ********************************************************************** | |
12 | */ | |
13 | #ifndef MEASUREFORMAT_H | |
14 | #define MEASUREFORMAT_H | |
15 | ||
16 | #include "unicode/utypes.h" | |
17 | ||
18 | #if !UCONFIG_NO_FORMATTING | |
19 | ||
20 | #include "unicode/format.h" | |
57a6839d | 21 | #include "unicode/udat.h" |
2ca993e8 A |
22 | // Apple specific: |
23 | #include "unicode/uameasureformat.h" | |
374ca955 | 24 | |
73c04bcf A |
25 | /** |
26 | * \file | |
0f5d89e8 | 27 | * \brief C++ API: Compatibility APIs for measure formatting. |
73c04bcf A |
28 | */ |
29 | ||
57a6839d A |
30 | /** |
31 | * Constants for various widths. | |
b331163b | 32 | * There are 4 widths: Wide, Short, Narrow, Numeric. |
57a6839d | 33 | * For example, for English, when formatting "3 hours" |
b331163b A |
34 | * Wide is "3 hours"; short is "3 hrs"; narrow is "3h"; |
35 | * formatting "3 hours 17 minutes" as numeric give "3:17" | |
36 | * @stable ICU 53 | |
57a6839d A |
37 | */ |
38 | enum UMeasureFormatWidth { | |
39 | ||
40 | // Wide, short, and narrow must be first and in this order. | |
41 | /** | |
42 | * Spell out measure units. | |
b331163b | 43 | * @stable ICU 53 |
57a6839d A |
44 | */ |
45 | UMEASFMT_WIDTH_WIDE, | |
46 | ||
47 | /** | |
48 | * Abbreviate measure units. | |
b331163b | 49 | * @stable ICU 53 |
57a6839d A |
50 | */ |
51 | UMEASFMT_WIDTH_SHORT, | |
52 | ||
53 | /** | |
54 | * Use symbols for measure units when possible. | |
b331163b | 55 | * @stable ICU 53 |
57a6839d A |
56 | */ |
57 | UMEASFMT_WIDTH_NARROW, | |
58 | ||
59 | /** | |
60 | * Completely omit measure units when possible. For example, format | |
61 | * '5 hours, 37 minutes' as '5:37' | |
b331163b | 62 | * @stable ICU 53 |
57a6839d A |
63 | */ |
64 | UMEASFMT_WIDTH_NUMERIC, | |
65 | ||
f3c0d7a5 | 66 | #ifndef U_HIDE_DEPRECATED_API |
57a6839d | 67 | /** |
f3c0d7a5 A |
68 | * One more than the highest normal UMeasureFormatWidth value. |
69 | * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420. | |
57a6839d | 70 | */ |
f3c0d7a5 A |
71 | UMEASFMT_WIDTH_COUNT = 4 |
72 | #endif // U_HIDE_DEPRECATED_API | |
73 | #ifndef U_HIDE_INTERNAL_API | |
74 | , | |
2ca993e8 A |
75 | /** |
76 | * Apple-specific | |
77 | * Shorter, between SHORT and NARROW (SHORT without space in unit pattern) | |
78 | * @draft ICU 57 | |
79 | */ | |
80 | UMEASFMT_WIDTH_SHORTER = 8 | |
f3c0d7a5 | 81 | #endif /* U_HIDE_INTERNAL_API */ |
57a6839d | 82 | }; |
b331163b | 83 | /** @stable ICU 53 */ |
57a6839d | 84 | typedef enum UMeasureFormatWidth UMeasureFormatWidth; |
57a6839d | 85 | |
f3c0d7a5 | 86 | #if U_SHOW_CPLUSPLUS_API |
374ca955 A |
87 | U_NAMESPACE_BEGIN |
88 | ||
b331163b A |
89 | class Measure; |
90 | class MeasureUnit; | |
57a6839d A |
91 | class NumberFormat; |
92 | class PluralRules; | |
93 | class MeasureFormatCacheData; | |
94 | class SharedNumberFormat; | |
95 | class SharedPluralRules; | |
96 | class QuantityFormatter; | |
2ca993e8 | 97 | class SimpleFormatter; |
57a6839d A |
98 | class ListFormatter; |
99 | class DateFormat; | |
2ca993e8 | 100 | class FieldPositionHandler; |
57a6839d | 101 | |
374ca955 | 102 | /** |
0f5d89e8 A |
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. | |
374ca955 A |
106 | * |
107 | * @see Format | |
108 | * @author Alan Liu | |
73c04bcf | 109 | * @stable ICU 3.0 |
374ca955 A |
110 | */ |
111 | class U_I18N_API MeasureFormat : public Format { | |
374ca955 | 112 | public: |
57a6839d A |
113 | using Format::parseObject; |
114 | using Format::format; | |
115 | ||
57a6839d A |
116 | /** |
117 | * Constructor. | |
0f5d89e8 A |
118 | * <p> |
119 | * <strong>NOTE:</strong> New users are strongly encouraged to use | |
3d1f044b | 120 | * {@link icu::number::NumberFormatter} instead of NumberFormat. |
b331163b | 121 | * @stable ICU 53 |
57a6839d A |
122 | */ |
123 | MeasureFormat( | |
124 | const Locale &locale, UMeasureFormatWidth width, UErrorCode &status); | |
125 | ||
126 | /** | |
127 | * Constructor. | |
0f5d89e8 A |
128 | * <p> |
129 | * <strong>NOTE:</strong> New users are strongly encouraged to use | |
3d1f044b | 130 | * {@link icu::number::NumberFormatter} instead of NumberFormat. |
b331163b | 131 | * @stable ICU 53 |
57a6839d A |
132 | */ |
133 | MeasureFormat( | |
134 | const Locale &locale, | |
135 | UMeasureFormatWidth width, | |
136 | NumberFormat *nfToAdopt, | |
137 | UErrorCode &status); | |
57a6839d A |
138 | |
139 | /** | |
140 | * Copy constructor. | |
b331163b | 141 | * @stable ICU 3.0 |
57a6839d A |
142 | */ |
143 | MeasureFormat(const MeasureFormat &other); | |
144 | ||
145 | /** | |
146 | * Assignment operator. | |
b331163b | 147 | * @stable ICU 3.0 |
57a6839d A |
148 | */ |
149 | MeasureFormat &operator=(const MeasureFormat &rhs); | |
150 | ||
4388f060 A |
151 | /** |
152 | * Destructor. | |
153 | * @stable ICU 3.0 | |
154 | */ | |
155 | virtual ~MeasureFormat(); | |
374ca955 | 156 | |
57a6839d A |
157 | /** |
158 | * Return true if given Format objects are semantically equal. | |
b331163b | 159 | * @stable ICU 53 |
57a6839d A |
160 | */ |
161 | virtual UBool operator==(const Format &other) const; | |
162 | ||
163 | /** | |
164 | * Clones this object polymorphically. | |
b331163b | 165 | * @stable ICU 53 |
57a6839d A |
166 | */ |
167 | virtual Format *clone() const; | |
168 | ||
169 | /** | |
170 | * Formats object to produce a string. | |
b331163b | 171 | * @stable ICU 53 |
57a6839d A |
172 | */ |
173 | virtual UnicodeString &format( | |
174 | const Formattable &obj, | |
175 | UnicodeString &appendTo, | |
176 | FieldPosition &pos, | |
177 | UErrorCode &status) const; | |
178 | ||
179 | /** | |
180 | * Parse a string to produce an object. This implementation sets | |
181 | * status to U_UNSUPPORTED_ERROR. | |
182 | * | |
183 | * @draft ICU 53 | |
184 | */ | |
185 | virtual void parseObject( | |
186 | const UnicodeString &source, | |
187 | Formattable &reslt, | |
188 | ParsePosition &pos) const; | |
189 | ||
57a6839d A |
190 | /** |
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 | |
203 | * | |
b331163b | 204 | * @stable ICU 53 |
57a6839d A |
205 | */ |
206 | UnicodeString &formatMeasures( | |
207 | const Measure *measures, | |
208 | int32_t measureCount, | |
209 | UnicodeString &appendTo, | |
210 | FieldPosition &pos, | |
211 | UErrorCode &status) const; | |
b331163b A |
212 | |
213 | #ifndef U_HIDE_INTERNAL_API | |
2ca993e8 A |
214 | /** |
215 | * Apple-specific for now. | |
216 | * Like formatMeasures above, but with a | |
217 | * FieldPositionIterator* instead of a FieldPosition& | |
218 | * | |
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 | |
227 | * | |
228 | * @internal. | |
229 | */ | |
230 | UnicodeString &formatMeasures( | |
231 | const Measure *measures, | |
232 | int32_t measureCount, | |
233 | UnicodeString &appendTo, | |
234 | FieldPositionIterator* posIter, | |
235 | UErrorCode &status) const; | |
236 | ||
b331163b A |
237 | /** |
238 | * Apple-specific for now | |
239 | * @internal. | |
240 | */ | |
241 | UMeasureFormatWidth getWidth(void) const; | |
2ca993e8 | 242 | |
b331163b | 243 | #endif /* U_HIDE_INTERNAL_API */ |
57a6839d A |
244 | |
245 | ||
b331163b A |
246 | /** |
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 | |
3d1f044b | 251 | * `*%MeasureUnit::createSecond(status)`. |
b331163b A |
252 | * @param appendTo formatted string appended here. |
253 | * @param pos the field position. | |
254 | * @param status the error. | |
255 | * @return appendTo reference | |
256 | * | |
2ca993e8 | 257 | * @stable ICU 55 |
b331163b A |
258 | */ |
259 | UnicodeString &formatMeasurePerUnit( | |
260 | const Measure &measure, | |
261 | const MeasureUnit &perUnit, | |
262 | UnicodeString &appendTo, | |
263 | FieldPosition &pos, | |
264 | UErrorCode &status) const; | |
265 | ||
f3c0d7a5 A |
266 | /** |
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 | |
3d1f044b | 272 | * the MeasureFormat constructor, or null if there is no display name available |
f3c0d7a5 A |
273 | * for the specified unit. |
274 | * | |
0f5d89e8 | 275 | * @stable ICU 58 |
f3c0d7a5 A |
276 | */ |
277 | UnicodeString getUnitDisplayName(const MeasureUnit& unit, UErrorCode &status) const; | |
f3c0d7a5 | 278 | |
b331163b | 279 | |
374ca955 A |
280 | /** |
281 | * Return a formatter for CurrencyAmount objects in the given | |
282 | * locale. | |
0f5d89e8 A |
283 | * <p> |
284 | * <strong>NOTE:</strong> New users are strongly encouraged to use | |
3d1f044b | 285 | * {@link icu::number::NumberFormatter} instead of NumberFormat. |
374ca955 A |
286 | * @param locale desired locale |
287 | * @param ec input-output error code | |
288 | * @return a formatter object, or NULL upon error | |
73c04bcf | 289 | * @stable ICU 3.0 |
374ca955 A |
290 | */ |
291 | static MeasureFormat* U_EXPORT2 createCurrencyFormat(const Locale& locale, | |
292 | UErrorCode& ec); | |
293 | ||
294 | /** | |
295 | * Return a formatter for CurrencyAmount objects in the default | |
296 | * locale. | |
0f5d89e8 A |
297 | * <p> |
298 | * <strong>NOTE:</strong> New users are strongly encouraged to use | |
3d1f044b | 299 | * {@link icu::number::NumberFormatter} instead of NumberFormat. |
374ca955 A |
300 | * @param ec input-output error code |
301 | * @return a formatter object, or NULL upon error | |
73c04bcf | 302 | * @stable ICU 3.0 |
374ca955 A |
303 | */ |
304 | static MeasureFormat* U_EXPORT2 createCurrencyFormat(UErrorCode& ec); | |
305 | ||
57a6839d A |
306 | /** |
307 | * Return the class ID for this class. This is useful only for comparing to | |
308 | * a return value from getDynamicClassID(). For example: | |
309 | * <pre> | |
310 | * . Base* polymorphic_pointer = createPolymorphicObject(); | |
311 | * . if (polymorphic_pointer->getDynamicClassID() == | |
312 | * . erived::getStaticClassID()) ... | |
313 | * </pre> | |
314 | * @return The class ID for all objects of this class. | |
b331163b | 315 | * @stable ICU 53 |
57a6839d A |
316 | */ |
317 | static UClassID U_EXPORT2 getStaticClassID(void); | |
318 | ||
319 | /** | |
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. | |
324 | * | |
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. | |
b331163b | 328 | * @stable ICU 53 |
57a6839d A |
329 | */ |
330 | virtual UClassID getDynamicClassID(void) const; | |
374ca955 | 331 | |
57a6839d | 332 | protected: |
374ca955 A |
333 | /** |
334 | * Default constructor. | |
73c04bcf | 335 | * @stable ICU 3.0 |
374ca955 A |
336 | */ |
337 | MeasureFormat(); | |
57a6839d A |
338 | |
339 | #ifndef U_HIDE_INTERNAL_API | |
340 | ||
57a6839d A |
341 | /** |
342 | * ICU use only. | |
343 | * Initialize or change MeasureFormat class from subclass. | |
344 | * @internal. | |
345 | */ | |
346 | void initMeasureFormat( | |
347 | const Locale &locale, | |
348 | UMeasureFormatWidth width, | |
349 | NumberFormat *nfToAdopt, | |
350 | UErrorCode &status); | |
57a6839d A |
351 | /** |
352 | * ICU use only. | |
353 | * Allows subclass to change locale. Note that this method also changes | |
354 | * the NumberFormat object. Returns TRUE if locale changed; FALSE if no | |
355 | * change was made. | |
356 | * @internal. | |
357 | */ | |
358 | UBool setMeasureFormatLocale(const Locale &locale, UErrorCode &status); | |
359 | ||
360 | public: | |
361 | // Apple-only, temporarily public for Apple use | |
362 | /** | |
363 | * ICU use only. | |
364 | * Let subclass change NumberFormat. | |
2ca993e8 | 365 | * @internal Apple |
57a6839d A |
366 | */ |
367 | void adoptNumberFormat(NumberFormat *nfToAdopt, UErrorCode &status); | |
368 | ||
2ca993e8 A |
369 | /** |
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, | |
373 | * length will be 0) | |
374 | * @return Reference to result | |
375 | * | |
376 | * @internal Apple | |
377 | */ | |
378 | UnicodeString &getUnitName( | |
379 | const MeasureUnit* unit, | |
380 | UnicodeString &result ) const; | |
381 | ||
382 | /** | |
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, | |
388 | * length will be 0) | |
389 | * @return Reference to result | |
390 | * | |
391 | * @internal Apple | |
392 | */ | |
393 | UnicodeString &getMultipleUnitNames( | |
394 | const MeasureUnit** units, | |
395 | int32_t unitCount, | |
396 | UAMeasureNameListStyle listStyle, | |
397 | UnicodeString &result ) const; | |
398 | ||
57a6839d A |
399 | protected: |
400 | /** | |
401 | * ICU use only. | |
402 | * @internal. | |
403 | */ | |
3d1f044b A |
404 | const NumberFormat &getNumberFormatInternal() const; |
405 | ||
406 | /** | |
407 | * ICU use only. | |
408 | * Always returns the short form currency formatter. | |
409 | * @internal. | |
410 | */ | |
411 | const NumberFormat& getCurrencyFormatInternal() const; | |
57a6839d A |
412 | |
413 | /** | |
414 | * ICU use only. | |
415 | * @internal. | |
416 | */ | |
417 | const PluralRules &getPluralRules() const; | |
418 | ||
419 | /** | |
420 | * ICU use only. | |
421 | * @internal. | |
422 | */ | |
423 | Locale getLocale(UErrorCode &status) const; | |
424 | ||
425 | /** | |
426 | * ICU use only. | |
427 | * @internal. | |
428 | */ | |
429 | const char *getLocaleID(UErrorCode &status) const; | |
430 | ||
431 | #endif /* U_HIDE_INTERNAL_API */ | |
432 | ||
433 | private: | |
434 | const MeasureFormatCacheData *cache; | |
435 | const SharedNumberFormat *numberFormat; | |
436 | const SharedPluralRules *pluralRules; | |
3d1f044b | 437 | UMeasureFormatWidth fWidth; |
2ca993e8 | 438 | UBool stripPatternSpaces; |
57a6839d A |
439 | |
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; | |
2ca993e8 | 444 | ListFormatter *listFormatterStd; // standard list style, option for display names; Apple specific |
57a6839d | 445 | |
57a6839d A |
446 | UnicodeString &formatMeasure( |
447 | const Measure &measure, | |
448 | const NumberFormat &nf, | |
449 | UnicodeString &appendTo, | |
450 | FieldPosition &pos, | |
451 | UErrorCode &status) const; | |
452 | ||
453 | UnicodeString &formatMeasuresSlowTrack( | |
454 | const Measure *measures, | |
455 | int32_t measureCount, | |
456 | UnicodeString& appendTo, | |
457 | FieldPosition& pos, | |
458 | UErrorCode& status) const; | |
459 | ||
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, | |
2ca993e8 | 465 | FieldPositionHandler& handler, |
57a6839d A |
466 | UErrorCode &status) const; |
467 | ||
468 | UnicodeString &formatNumeric( | |
469 | UDate date, | |
470 | const DateFormat &dateFmt, | |
471 | UDateFormatField smallestField, | |
472 | const Formattable &smallestAmount, | |
473 | UnicodeString &appendTo, | |
2ca993e8 | 474 | FieldPositionHandler& handler, |
57a6839d | 475 | UErrorCode &status) const; |
374ca955 A |
476 | }; |
477 | ||
478 | U_NAMESPACE_END | |
f3c0d7a5 | 479 | #endif // U_SHOW_CPLUSPLUS_API |
374ca955 A |
480 | |
481 | #endif // #if !UCONFIG_NO_FORMATTING | |
482 | #endif // #ifndef MEASUREFORMAT_H |