OLD | NEW |
1 /* | 1 /* |
2 ********************************************************************** | 2 ********************************************************************** |
3 * Copyright (c) 2004-2011, International Business Machines | 3 * Copyright (c) 2004-2014, International Business Machines |
4 * Corporation and others. All Rights Reserved. | 4 * Corporation and others. All Rights Reserved. |
5 ********************************************************************** | 5 ********************************************************************** |
6 * Author: Alan Liu | 6 * Author: Alan Liu |
7 * Created: April 20, 2004 | 7 * Created: April 20, 2004 |
8 * Since: ICU 3.0 | 8 * Since: ICU 3.0 |
9 ********************************************************************** | 9 ********************************************************************** |
10 */ | 10 */ |
11 #ifndef MEASUREFORMAT_H | 11 #ifndef MEASUREFORMAT_H |
12 #define MEASUREFORMAT_H | 12 #define MEASUREFORMAT_H |
13 | 13 |
14 #include "unicode/utypes.h" | 14 #include "unicode/utypes.h" |
15 | 15 |
16 #if !UCONFIG_NO_FORMATTING | 16 #if !UCONFIG_NO_FORMATTING |
17 | 17 |
18 #include "unicode/format.h" | 18 #include "unicode/format.h" |
| 19 #include "unicode/udat.h" |
19 | 20 |
20 /** | 21 /** |
21 * \file | 22 * \file |
22 * \brief C++ API: Formatter for measure objects. | 23 * \brief C++ API: Formatter for measure objects. |
23 */ | 24 */ |
24 | 25 |
| 26 /** |
| 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" |
| 32 * @draft ICU 53 |
| 33 */ |
| 34 enum UMeasureFormatWidth { |
| 35 |
| 36 #ifndef U_HIDE_DRAFT_API |
| 37 // Wide, short, and narrow must be first and in this order. |
| 38 /** |
| 39 * Spell out measure units. |
| 40 * @draft ICU 53 |
| 41 */ |
| 42 UMEASFMT_WIDTH_WIDE, |
| 43 |
| 44 /** |
| 45 * Abbreviate measure units. |
| 46 * @draft ICU 53 |
| 47 */ |
| 48 UMEASFMT_WIDTH_SHORT, |
| 49 |
| 50 /** |
| 51 * Use symbols for measure units when possible. |
| 52 * @draft ICU 53 |
| 53 */ |
| 54 UMEASFMT_WIDTH_NARROW, |
| 55 |
| 56 /** |
| 57 * Completely omit measure units when possible. For example, format |
| 58 * '5 hours, 37 minutes' as '5:37' |
| 59 * @draft ICU 53 |
| 60 */ |
| 61 UMEASFMT_WIDTH_NUMERIC, |
| 62 #endif /* U_HIDE_DRAFT_API */ |
| 63 |
| 64 /** |
| 65 * Count of values in this enum. |
| 66 * @draft ICU 53 |
| 67 */ |
| 68 UMEASFMT_WIDTH_COUNT = 4 |
| 69 }; |
| 70 /** @draft ICU 53 */ |
| 71 typedef enum UMeasureFormatWidth UMeasureFormatWidth; |
| 72 |
25 U_NAMESPACE_BEGIN | 73 U_NAMESPACE_BEGIN |
26 | 74 |
| 75 class Measure; |
| 76 class MeasureUnit; |
| 77 class NumberFormat; |
| 78 class PluralRules; |
| 79 class MeasureFormatCacheData; |
| 80 class SharedNumberFormat; |
| 81 class SharedPluralRules; |
| 82 class QuantityFormatter; |
| 83 class SimplePatternFormatter; |
| 84 class ListFormatter; |
| 85 class DateFormat; |
| 86 |
27 /** | 87 /** |
28 * | 88 * |
29 * A formatter for measure objects. This is an abstract base class. | 89 * A formatter for measure objects. |
30 * | |
31 * <p>To format or parse a measure object, first create a formatter | |
32 * object using a MeasureFormat factory method. Then use that | |
33 * object's format and parse methods. | |
34 * | |
35 * <p>This is an abstract class. | |
36 * | 90 * |
37 * @see Format | 91 * @see Format |
38 * @author Alan Liu | 92 * @author Alan Liu |
39 * @stable ICU 3.0 | 93 * @stable ICU 3.0 |
40 */ | 94 */ |
41 class U_I18N_API MeasureFormat : public Format { | 95 class U_I18N_API MeasureFormat : public Format { |
42 public: | 96 public: |
| 97 using Format::parseObject; |
| 98 using Format::format; |
| 99 |
| 100 #ifndef U_HIDE_DRAFT_API |
| 101 /** |
| 102 * Constructor. |
| 103 * @draft ICU 53 |
| 104 */ |
| 105 MeasureFormat( |
| 106 const Locale &locale, UMeasureFormatWidth width, UErrorCode &status)
; |
| 107 |
| 108 /** |
| 109 * Constructor. |
| 110 * @draft ICU 53 |
| 111 */ |
| 112 MeasureFormat( |
| 113 const Locale &locale, |
| 114 UMeasureFormatWidth width, |
| 115 NumberFormat *nfToAdopt, |
| 116 UErrorCode &status); |
| 117 #endif /* U_HIDE_DRAFT_API */ |
| 118 |
| 119 /** |
| 120 * Copy constructor. |
| 121 * @draft ICU 53 |
| 122 */ |
| 123 MeasureFormat(const MeasureFormat &other); |
| 124 |
| 125 /** |
| 126 * Assignment operator. |
| 127 * @draft ICU 53 |
| 128 */ |
| 129 MeasureFormat &operator=(const MeasureFormat &rhs); |
| 130 |
43 /** | 131 /** |
44 * Destructor. | 132 * Destructor. |
45 * @stable ICU 3.0 | 133 * @stable ICU 3.0 |
46 */ | 134 */ |
47 virtual ~MeasureFormat(); | 135 virtual ~MeasureFormat(); |
48 | 136 |
49 /** | 137 /** |
| 138 * Return true if given Format objects are semantically equal. |
| 139 * @draft ICU 53 |
| 140 */ |
| 141 virtual UBool operator==(const Format &other) const; |
| 142 |
| 143 /** |
| 144 * Clones this object polymorphically. |
| 145 * @draft ICU 53 |
| 146 */ |
| 147 virtual Format *clone() const; |
| 148 |
| 149 /** |
| 150 * Formats object to produce a string. |
| 151 * @draft ICU 53 |
| 152 */ |
| 153 virtual UnicodeString &format( |
| 154 const Formattable &obj, |
| 155 UnicodeString &appendTo, |
| 156 FieldPosition &pos, |
| 157 UErrorCode &status) const; |
| 158 |
| 159 /** |
| 160 * Parse a string to produce an object. This implementation sets |
| 161 * status to U_UNSUPPORTED_ERROR. |
| 162 * |
| 163 * @draft ICU 53 |
| 164 */ |
| 165 virtual void parseObject( |
| 166 const UnicodeString &source, |
| 167 Formattable &reslt, |
| 168 ParsePosition &pos) const; |
| 169 |
| 170 #ifndef U_HIDE_DRAFT_API |
| 171 /** |
| 172 * Formats measure objects to produce a string. An example of such a |
| 173 * formatted string is 3 meters, 3.5 centimeters. Measure objects appear |
| 174 * in the formatted string in the same order they appear in the "measures" |
| 175 * array. The NumberFormat of this object is used only to format the amount |
| 176 * of the very last measure. The other amounts are formatted with zero |
| 177 * decimal places while rounding toward zero. |
| 178 * @param measures array of measure objects. |
| 179 * @param measureCount the number of measure objects. |
| 180 * @param appendTo formatted string appended here. |
| 181 * @param pos the field position. |
| 182 * @param status the error. |
| 183 * @return appendTo reference |
| 184 * |
| 185 * @draft ICU 53 |
| 186 */ |
| 187 UnicodeString &formatMeasures( |
| 188 const Measure *measures, |
| 189 int32_t measureCount, |
| 190 UnicodeString &appendTo, |
| 191 FieldPosition &pos, |
| 192 UErrorCode &status) const; |
| 193 #endif /* U_HIDE_DRAFT_API */ |
| 194 |
| 195 #ifndef U_HIDE_INTERNAL_API |
| 196 /** |
| 197 * Works like formatMeasures but adds a per unit. An example of such a |
| 198 * formatted string is 3 meters, 3.5 centimeters per second. |
| 199 * @param measures array of measure objects. |
| 200 * @param measureCount the number of measure objects. |
| 201 * @param perUnit The per unit. In the example formatted string, |
| 202 * it is *MeasureUnit::createSecond(status). |
| 203 * @param appendTo formatted string appended here. |
| 204 * @param pos the field position. |
| 205 * @param status the error. |
| 206 * @return appendTo reference |
| 207 * |
| 208 * @internal Technology preview |
| 209 */ |
| 210 UnicodeString &formatMeasuresPer( |
| 211 const Measure *measures, |
| 212 int32_t measureCount, |
| 213 const MeasureUnit &perUnit, |
| 214 UnicodeString &appendTo, |
| 215 FieldPosition &pos, |
| 216 UErrorCode &status) const; |
| 217 #endif /* U_HIDE_INTERNAL_API */ |
| 218 |
| 219 /** |
50 * Return a formatter for CurrencyAmount objects in the given | 220 * Return a formatter for CurrencyAmount objects in the given |
51 * locale. | 221 * locale. |
52 * @param locale desired locale | 222 * @param locale desired locale |
53 * @param ec input-output error code | 223 * @param ec input-output error code |
54 * @return a formatter object, or NULL upon error | 224 * @return a formatter object, or NULL upon error |
55 * @stable ICU 3.0 | 225 * @stable ICU 3.0 |
56 */ | 226 */ |
57 static MeasureFormat* U_EXPORT2 createCurrencyFormat(const Locale& locale, | 227 static MeasureFormat* U_EXPORT2 createCurrencyFormat(const Locale& locale, |
58 UErrorCode& ec); | 228 UErrorCode& ec); |
59 | 229 |
60 /** | 230 /** |
61 * Return a formatter for CurrencyAmount objects in the default | 231 * Return a formatter for CurrencyAmount objects in the default |
62 * locale. | 232 * locale. |
63 * @param ec input-output error code | 233 * @param ec input-output error code |
64 * @return a formatter object, or NULL upon error | 234 * @return a formatter object, or NULL upon error |
65 * @stable ICU 3.0 | 235 * @stable ICU 3.0 |
66 */ | 236 */ |
67 static MeasureFormat* U_EXPORT2 createCurrencyFormat(UErrorCode& ec); | 237 static MeasureFormat* U_EXPORT2 createCurrencyFormat(UErrorCode& ec); |
68 | 238 |
| 239 /** |
| 240 * Return the class ID for this class. This is useful only for comparing to |
| 241 * a return value from getDynamicClassID(). For example: |
| 242 * <pre> |
| 243 * . Base* polymorphic_pointer = createPolymorphicObject(); |
| 244 * . if (polymorphic_pointer->getDynamicClassID() == |
| 245 * . erived::getStaticClassID()) ... |
| 246 * </pre> |
| 247 * @return The class ID for all objects of this class. |
| 248 * @draft ICU 53 |
| 249 */ |
| 250 static UClassID U_EXPORT2 getStaticClassID(void); |
| 251 |
| 252 /** |
| 253 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This |
| 254 * method is to implement a simple version of RTTI, since not all C++ |
| 255 * compilers support genuine RTTI. Polymorphic operator==() and clone() |
| 256 * methods call this method. |
| 257 * |
| 258 * @return The class ID for this object. All objects of a |
| 259 * given class have the same class ID. Objects of |
| 260 * other classes have different class IDs. |
| 261 * @draft ICU 53 |
| 262 */ |
| 263 virtual UClassID getDynamicClassID(void) const; |
| 264 |
69 protected: | 265 protected: |
70 | |
71 /** | 266 /** |
72 * Default constructor. | 267 * Default constructor. |
73 * @stable ICU 3.0 | 268 * @stable ICU 3.0 |
74 */ | 269 */ |
75 MeasureFormat(); | 270 MeasureFormat(); |
| 271 |
| 272 #ifndef U_HIDE_INTERNAL_API |
| 273 |
| 274 /** |
| 275 * ICU use only. |
| 276 * Initialize or change MeasureFormat class from subclass. |
| 277 * @internal. |
| 278 */ |
| 279 void initMeasureFormat( |
| 280 const Locale &locale, |
| 281 UMeasureFormatWidth width, |
| 282 NumberFormat *nfToAdopt, |
| 283 UErrorCode &status); |
| 284 /** |
| 285 * ICU use only. |
| 286 * Allows subclass to change locale. Note that this method also changes |
| 287 * the NumberFormat object. Returns TRUE if locale changed; FALSE if no |
| 288 * change was made. |
| 289 * @internal. |
| 290 */ |
| 291 UBool setMeasureFormatLocale(const Locale &locale, UErrorCode &status); |
| 292 |
| 293 /** |
| 294 * ICU use only. |
| 295 * Let subclass change NumberFormat. |
| 296 * @internal. |
| 297 */ |
| 298 void adoptNumberFormat(NumberFormat *nfToAdopt, UErrorCode &status); |
| 299 |
| 300 /** |
| 301 * ICU use only. |
| 302 * @internal. |
| 303 */ |
| 304 const NumberFormat &getNumberFormat() const; |
| 305 |
| 306 /** |
| 307 * ICU use only. |
| 308 * @internal. |
| 309 */ |
| 310 const PluralRules &getPluralRules() const; |
| 311 |
| 312 /** |
| 313 * ICU use only. |
| 314 * @internal. |
| 315 */ |
| 316 Locale getLocale(UErrorCode &status) const; |
| 317 |
| 318 /** |
| 319 * ICU use only. |
| 320 * @internal. |
| 321 */ |
| 322 const char *getLocaleID(UErrorCode &status) const; |
| 323 |
| 324 #endif /* U_HIDE_INTERNAL_API */ |
| 325 |
| 326 private: |
| 327 const MeasureFormatCacheData *cache; |
| 328 const SharedNumberFormat *numberFormat; |
| 329 const SharedPluralRules *pluralRules; |
| 330 UMeasureFormatWidth width; |
| 331 |
| 332 // Declared outside of MeasureFormatSharedData because ListFormatter |
| 333 // objects are relatively cheap to copy; therefore, they don't need to be |
| 334 // shared across instances. |
| 335 ListFormatter *listFormatter; |
| 336 |
| 337 const QuantityFormatter *getQuantityFormatter( |
| 338 int32_t index, |
| 339 int32_t widthIndex, |
| 340 UErrorCode &status) const; |
| 341 |
| 342 const SimplePatternFormatter *getPerUnitFormatter( |
| 343 int32_t index, |
| 344 int32_t widthIndex) const; |
| 345 |
| 346 const SimplePatternFormatter *getPerFormatter( |
| 347 int32_t widthIndex, |
| 348 UErrorCode &status) const; |
| 349 |
| 350 int32_t withPerUnit( |
| 351 const UnicodeString &formatted, |
| 352 const MeasureUnit &perUnit, |
| 353 UnicodeString &appendTo, |
| 354 UErrorCode &status) const; |
| 355 |
| 356 UnicodeString &formatMeasure( |
| 357 const Measure &measure, |
| 358 const NumberFormat &nf, |
| 359 UnicodeString &appendTo, |
| 360 FieldPosition &pos, |
| 361 UErrorCode &status) const; |
| 362 |
| 363 UnicodeString &formatMeasuresSlowTrack( |
| 364 const Measure *measures, |
| 365 int32_t measureCount, |
| 366 UnicodeString& appendTo, |
| 367 FieldPosition& pos, |
| 368 UErrorCode& status) const; |
| 369 |
| 370 UnicodeString &formatNumeric( |
| 371 const Formattable *hms, // always length 3: [0] is hour; [1] is |
| 372 // minute; [2] is second. |
| 373 int32_t bitMap, // 1=hour set, 2=minute set, 4=second set |
| 374 UnicodeString &appendTo, |
| 375 UErrorCode &status) const; |
| 376 |
| 377 UnicodeString &formatNumeric( |
| 378 UDate date, |
| 379 const DateFormat &dateFmt, |
| 380 UDateFormatField smallestField, |
| 381 const Formattable &smallestAmount, |
| 382 UnicodeString &appendTo, |
| 383 UErrorCode &status) const; |
76 }; | 384 }; |
77 | 385 |
78 U_NAMESPACE_END | 386 U_NAMESPACE_END |
79 | 387 |
80 #endif // #if !UCONFIG_NO_FORMATTING | 388 #endif // #if !UCONFIG_NO_FORMATTING |
81 #endif // #ifndef MEASUREFORMAT_H | 389 #endif // #ifndef MEASUREFORMAT_H |
OLD | NEW |