Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************* |
michael@0 | 3 | * Copyright (C) 2008-2013, Google, International Business Machines Corporation |
michael@0 | 4 | * and others. All Rights Reserved. |
michael@0 | 5 | ******************************************************************************* |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #ifndef __TMUTFMT_H__ |
michael@0 | 9 | #define __TMUTFMT_H__ |
michael@0 | 10 | |
michael@0 | 11 | #include "unicode/utypes.h" |
michael@0 | 12 | |
michael@0 | 13 | /** |
michael@0 | 14 | * \file |
michael@0 | 15 | * \brief C++ API: Format and parse duration in single time unit |
michael@0 | 16 | */ |
michael@0 | 17 | |
michael@0 | 18 | |
michael@0 | 19 | #if !UCONFIG_NO_FORMATTING |
michael@0 | 20 | |
michael@0 | 21 | #include "unicode/unistr.h" |
michael@0 | 22 | #include "unicode/tmunit.h" |
michael@0 | 23 | #include "unicode/tmutamt.h" |
michael@0 | 24 | #include "unicode/measfmt.h" |
michael@0 | 25 | #include "unicode/numfmt.h" |
michael@0 | 26 | #include "unicode/plurrule.h" |
michael@0 | 27 | |
michael@0 | 28 | /** |
michael@0 | 29 | * Constants for various styles. |
michael@0 | 30 | * There are 2 styles: full name and abbreviated name. |
michael@0 | 31 | * For example, for English, the full name for hour duration is "3 hours", |
michael@0 | 32 | * and the abbreviated name is "3 hrs". |
michael@0 | 33 | * @stable ICU 4.8 |
michael@0 | 34 | */ |
michael@0 | 35 | enum UTimeUnitFormatStyle { |
michael@0 | 36 | /** @stable ICU 4.8 */ |
michael@0 | 37 | UTMUTFMT_FULL_STYLE, |
michael@0 | 38 | /** @stable ICU 4.8 */ |
michael@0 | 39 | UTMUTFMT_ABBREVIATED_STYLE, |
michael@0 | 40 | /** @stable ICU 4.8 */ |
michael@0 | 41 | UTMUTFMT_FORMAT_STYLE_COUNT |
michael@0 | 42 | }; |
michael@0 | 43 | typedef enum UTimeUnitFormatStyle UTimeUnitFormatStyle; /**< @stable ICU 4.8 */ |
michael@0 | 44 | |
michael@0 | 45 | U_NAMESPACE_BEGIN |
michael@0 | 46 | |
michael@0 | 47 | class Hashtable; |
michael@0 | 48 | class UVector; |
michael@0 | 49 | |
michael@0 | 50 | /** |
michael@0 | 51 | * Format or parse a TimeUnitAmount, using plural rules for the units where available. |
michael@0 | 52 | * |
michael@0 | 53 | * <P> |
michael@0 | 54 | * Code Sample: |
michael@0 | 55 | * <pre> |
michael@0 | 56 | * // create time unit amount instance - a combination of Number and time unit |
michael@0 | 57 | * UErrorCode status = U_ZERO_ERROR; |
michael@0 | 58 | * TimeUnitAmount* source = new TimeUnitAmount(2, TimeUnit::UTIMEUNIT_YEAR, status); |
michael@0 | 59 | * // create time unit format instance |
michael@0 | 60 | * TimeUnitFormat* format = new TimeUnitFormat(Locale("en"), status); |
michael@0 | 61 | * // format a time unit amount |
michael@0 | 62 | * UnicodeString formatted; |
michael@0 | 63 | * Formattable formattable; |
michael@0 | 64 | * if (U_SUCCESS(status)) { |
michael@0 | 65 | * formattable.adoptObject(source); |
michael@0 | 66 | * formatted = ((Format*)format)->format(formattable, formatted, status); |
michael@0 | 67 | * Formattable result; |
michael@0 | 68 | * ((Format*)format)->parseObject(formatted, result, status); |
michael@0 | 69 | * if (U_SUCCESS(status)) { |
michael@0 | 70 | * assert (result == formattable); |
michael@0 | 71 | * } |
michael@0 | 72 | * } |
michael@0 | 73 | * </pre> |
michael@0 | 74 | * |
michael@0 | 75 | * <P> |
michael@0 | 76 | * @see TimeUnitAmount |
michael@0 | 77 | * @see TimeUnitFormat |
michael@0 | 78 | * @stable ICU 4.2 |
michael@0 | 79 | */ |
michael@0 | 80 | class U_I18N_API TimeUnitFormat: public MeasureFormat { |
michael@0 | 81 | public: |
michael@0 | 82 | |
michael@0 | 83 | /** |
michael@0 | 84 | * Create TimeUnitFormat with default locale, and full name style. |
michael@0 | 85 | * Use setLocale and/or setFormat to modify. |
michael@0 | 86 | * @stable ICU 4.2 |
michael@0 | 87 | */ |
michael@0 | 88 | TimeUnitFormat(UErrorCode& status); |
michael@0 | 89 | |
michael@0 | 90 | /** |
michael@0 | 91 | * Create TimeUnitFormat given locale, and full name style. |
michael@0 | 92 | * @stable ICU 4.2 |
michael@0 | 93 | */ |
michael@0 | 94 | TimeUnitFormat(const Locale& locale, UErrorCode& status); |
michael@0 | 95 | |
michael@0 | 96 | /** |
michael@0 | 97 | * Create TimeUnitFormat given locale and style. |
michael@0 | 98 | * @stable ICU 4.8 |
michael@0 | 99 | */ |
michael@0 | 100 | TimeUnitFormat(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status); |
michael@0 | 101 | |
michael@0 | 102 | /** |
michael@0 | 103 | * Copy constructor. |
michael@0 | 104 | * @stable ICU 4.2 |
michael@0 | 105 | */ |
michael@0 | 106 | TimeUnitFormat(const TimeUnitFormat&); |
michael@0 | 107 | |
michael@0 | 108 | /** |
michael@0 | 109 | * deconstructor |
michael@0 | 110 | * @stable ICU 4.2 |
michael@0 | 111 | */ |
michael@0 | 112 | virtual ~TimeUnitFormat(); |
michael@0 | 113 | |
michael@0 | 114 | /** |
michael@0 | 115 | * Clone this Format object polymorphically. The caller owns the result and |
michael@0 | 116 | * should delete it when done. |
michael@0 | 117 | * @return A copy of the object. |
michael@0 | 118 | * @stable ICU 4.2 |
michael@0 | 119 | */ |
michael@0 | 120 | virtual Format* clone(void) const; |
michael@0 | 121 | |
michael@0 | 122 | /** |
michael@0 | 123 | * Assignment operator |
michael@0 | 124 | * @stable ICU 4.2 |
michael@0 | 125 | */ |
michael@0 | 126 | TimeUnitFormat& operator=(const TimeUnitFormat& other); |
michael@0 | 127 | |
michael@0 | 128 | |
michael@0 | 129 | /** |
michael@0 | 130 | * Return true if the given Format objects are semantically equal. Objects |
michael@0 | 131 | * of different subclasses are considered unequal. |
michael@0 | 132 | * @param other the object to be compared with. |
michael@0 | 133 | * @return true if the given Format objects are semantically equal. |
michael@0 | 134 | * @stable ICU 4.2 |
michael@0 | 135 | */ |
michael@0 | 136 | virtual UBool operator==(const Format& other) const; |
michael@0 | 137 | |
michael@0 | 138 | /** |
michael@0 | 139 | * Return true if the given Format objects are not semantically equal. |
michael@0 | 140 | * Objects of different subclasses are considered unequal. |
michael@0 | 141 | * @param other the object to be compared with. |
michael@0 | 142 | * @return true if the given Format objects are not semantically equal. |
michael@0 | 143 | * @stable ICU 4.2 |
michael@0 | 144 | */ |
michael@0 | 145 | UBool operator!=(const Format& other) const; |
michael@0 | 146 | |
michael@0 | 147 | /** |
michael@0 | 148 | * Set the locale used for formatting or parsing. |
michael@0 | 149 | * @param locale the locale to be set |
michael@0 | 150 | * @param status output param set to success/failure code on exit |
michael@0 | 151 | * @stable ICU 4.2 |
michael@0 | 152 | */ |
michael@0 | 153 | void setLocale(const Locale& locale, UErrorCode& status); |
michael@0 | 154 | |
michael@0 | 155 | |
michael@0 | 156 | /** |
michael@0 | 157 | * Set the number format used for formatting or parsing. |
michael@0 | 158 | * @param format the number formatter to be set |
michael@0 | 159 | * @param status output param set to success/failure code on exit |
michael@0 | 160 | * @stable ICU 4.2 |
michael@0 | 161 | */ |
michael@0 | 162 | void setNumberFormat(const NumberFormat& format, UErrorCode& status); |
michael@0 | 163 | |
michael@0 | 164 | |
michael@0 | 165 | using MeasureFormat::format; |
michael@0 | 166 | |
michael@0 | 167 | /** |
michael@0 | 168 | * Format a TimeUnitAmount. |
michael@0 | 169 | * If the formattable object is not a time unit amount object, |
michael@0 | 170 | * or the number in time unit amount is not a double type or long type |
michael@0 | 171 | * numeric, it returns a failing status: U_ILLEGAL_ARGUMENT_ERROR. |
michael@0 | 172 | * @see Format#format(const Formattable&, UnicodeString&, FieldPosition&, UErrorCode&) const |
michael@0 | 173 | * @stable ICU 4.2 |
michael@0 | 174 | */ |
michael@0 | 175 | virtual UnicodeString& format(const Formattable& obj, |
michael@0 | 176 | UnicodeString& toAppendTo, |
michael@0 | 177 | FieldPosition& pos, |
michael@0 | 178 | UErrorCode& status) const; |
michael@0 | 179 | |
michael@0 | 180 | /** |
michael@0 | 181 | * Parse a TimeUnitAmount. |
michael@0 | 182 | * @see Format#parseObject(const UnicodeString&, Formattable&, ParsePosition&) const; |
michael@0 | 183 | * @stable ICU 4.2 |
michael@0 | 184 | */ |
michael@0 | 185 | virtual void parseObject(const UnicodeString& source, |
michael@0 | 186 | Formattable& result, |
michael@0 | 187 | ParsePosition& pos) const; |
michael@0 | 188 | |
michael@0 | 189 | /** |
michael@0 | 190 | * Return the class ID for this class. This is useful only for comparing to |
michael@0 | 191 | * a return value from getDynamicClassID(). For example: |
michael@0 | 192 | * <pre> |
michael@0 | 193 | * . Base* polymorphic_pointer = createPolymorphicObject(); |
michael@0 | 194 | * . if (polymorphic_pointer->getDynamicClassID() == |
michael@0 | 195 | * . erived::getStaticClassID()) ... |
michael@0 | 196 | * </pre> |
michael@0 | 197 | * @return The class ID for all objects of this class. |
michael@0 | 198 | * @stable ICU 4.2 |
michael@0 | 199 | */ |
michael@0 | 200 | static UClassID U_EXPORT2 getStaticClassID(void); |
michael@0 | 201 | |
michael@0 | 202 | /** |
michael@0 | 203 | * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This |
michael@0 | 204 | * method is to implement a simple version of RTTI, since not all C++ |
michael@0 | 205 | * compilers support genuine RTTI. Polymorphic operator==() and clone() |
michael@0 | 206 | * methods call this method. |
michael@0 | 207 | * |
michael@0 | 208 | * @return The class ID for this object. All objects of a |
michael@0 | 209 | * given class have the same class ID. Objects of |
michael@0 | 210 | * other classes have different class IDs. |
michael@0 | 211 | * @stable ICU 4.2 |
michael@0 | 212 | */ |
michael@0 | 213 | virtual UClassID getDynamicClassID(void) const; |
michael@0 | 214 | |
michael@0 | 215 | private: |
michael@0 | 216 | NumberFormat* fNumberFormat; |
michael@0 | 217 | Locale fLocale; |
michael@0 | 218 | Hashtable* fTimeUnitToCountToPatterns[TimeUnit::UTIMEUNIT_FIELD_COUNT]; |
michael@0 | 219 | PluralRules* fPluralRules; |
michael@0 | 220 | UTimeUnitFormatStyle fStyle; |
michael@0 | 221 | |
michael@0 | 222 | void create(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status); |
michael@0 | 223 | |
michael@0 | 224 | // it might actually be simpler to make them Decimal Formats later. |
michael@0 | 225 | // initialize all private data members |
michael@0 | 226 | void setup(UErrorCode& status); |
michael@0 | 227 | |
michael@0 | 228 | // initialize data member without fill in data for fTimeUnitToCountToPattern |
michael@0 | 229 | void initDataMembers(UErrorCode& status); |
michael@0 | 230 | |
michael@0 | 231 | // initialize fTimeUnitToCountToPatterns from current locale's resource. |
michael@0 | 232 | void readFromCurrentLocale(UTimeUnitFormatStyle style, const char* key, const UVector& pluralCounts, |
michael@0 | 233 | UErrorCode& status); |
michael@0 | 234 | |
michael@0 | 235 | // check completeness of fTimeUnitToCountToPatterns against all time units, |
michael@0 | 236 | // and all plural rules, fill in fallback as necessary. |
michael@0 | 237 | void checkConsistency(UTimeUnitFormatStyle style, const char* key, UErrorCode& status); |
michael@0 | 238 | |
michael@0 | 239 | // fill in fTimeUnitToCountToPatterns from locale fall-back chain |
michael@0 | 240 | void searchInLocaleChain(UTimeUnitFormatStyle style, const char* key, const char* localeName, |
michael@0 | 241 | TimeUnit::UTimeUnitFields field, const UnicodeString&, |
michael@0 | 242 | const char*, Hashtable*, UErrorCode&); |
michael@0 | 243 | |
michael@0 | 244 | // initialize hash table |
michael@0 | 245 | Hashtable* initHash(UErrorCode& status); |
michael@0 | 246 | |
michael@0 | 247 | // delete hash table |
michael@0 | 248 | void deleteHash(Hashtable* htable); |
michael@0 | 249 | |
michael@0 | 250 | // copy hash table |
michael@0 | 251 | void copyHash(const Hashtable* source, Hashtable* target, UErrorCode& status); |
michael@0 | 252 | // get time unit name, such as "year", from time unit field enum, such as |
michael@0 | 253 | // UTIMEUNIT_YEAR. |
michael@0 | 254 | static const char* getTimeUnitName(TimeUnit::UTimeUnitFields field, UErrorCode& status); |
michael@0 | 255 | |
michael@0 | 256 | }; |
michael@0 | 257 | |
michael@0 | 258 | |
michael@0 | 259 | |
michael@0 | 260 | inline UBool |
michael@0 | 261 | TimeUnitFormat::operator!=(const Format& other) const { |
michael@0 | 262 | return !operator==(other); |
michael@0 | 263 | } |
michael@0 | 264 | |
michael@0 | 265 | |
michael@0 | 266 | |
michael@0 | 267 | U_NAMESPACE_END |
michael@0 | 268 | |
michael@0 | 269 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
michael@0 | 270 | |
michael@0 | 271 | #endif // __TMUTFMT_H__ |
michael@0 | 272 | //eof |