1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/i18n/unicode/tmutfmt.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,272 @@ 1.4 +/* 1.5 + ******************************************************************************* 1.6 + * Copyright (C) 2008-2013, Google, International Business Machines Corporation 1.7 + * and others. All Rights Reserved. 1.8 + ******************************************************************************* 1.9 + */ 1.10 + 1.11 +#ifndef __TMUTFMT_H__ 1.12 +#define __TMUTFMT_H__ 1.13 + 1.14 +#include "unicode/utypes.h" 1.15 + 1.16 +/** 1.17 + * \file 1.18 + * \brief C++ API: Format and parse duration in single time unit 1.19 + */ 1.20 + 1.21 + 1.22 +#if !UCONFIG_NO_FORMATTING 1.23 + 1.24 +#include "unicode/unistr.h" 1.25 +#include "unicode/tmunit.h" 1.26 +#include "unicode/tmutamt.h" 1.27 +#include "unicode/measfmt.h" 1.28 +#include "unicode/numfmt.h" 1.29 +#include "unicode/plurrule.h" 1.30 + 1.31 +/** 1.32 + * Constants for various styles. 1.33 + * There are 2 styles: full name and abbreviated name. 1.34 + * For example, for English, the full name for hour duration is "3 hours", 1.35 + * and the abbreviated name is "3 hrs". 1.36 + * @stable ICU 4.8 1.37 + */ 1.38 +enum UTimeUnitFormatStyle { 1.39 + /** @stable ICU 4.8 */ 1.40 + UTMUTFMT_FULL_STYLE, 1.41 + /** @stable ICU 4.8 */ 1.42 + UTMUTFMT_ABBREVIATED_STYLE, 1.43 + /** @stable ICU 4.8 */ 1.44 + UTMUTFMT_FORMAT_STYLE_COUNT 1.45 +}; 1.46 +typedef enum UTimeUnitFormatStyle UTimeUnitFormatStyle; /**< @stable ICU 4.8 */ 1.47 + 1.48 +U_NAMESPACE_BEGIN 1.49 + 1.50 +class Hashtable; 1.51 +class UVector; 1.52 + 1.53 +/** 1.54 + * Format or parse a TimeUnitAmount, using plural rules for the units where available. 1.55 + * 1.56 + * <P> 1.57 + * Code Sample: 1.58 + * <pre> 1.59 + * // create time unit amount instance - a combination of Number and time unit 1.60 + * UErrorCode status = U_ZERO_ERROR; 1.61 + * TimeUnitAmount* source = new TimeUnitAmount(2, TimeUnit::UTIMEUNIT_YEAR, status); 1.62 + * // create time unit format instance 1.63 + * TimeUnitFormat* format = new TimeUnitFormat(Locale("en"), status); 1.64 + * // format a time unit amount 1.65 + * UnicodeString formatted; 1.66 + * Formattable formattable; 1.67 + * if (U_SUCCESS(status)) { 1.68 + * formattable.adoptObject(source); 1.69 + * formatted = ((Format*)format)->format(formattable, formatted, status); 1.70 + * Formattable result; 1.71 + * ((Format*)format)->parseObject(formatted, result, status); 1.72 + * if (U_SUCCESS(status)) { 1.73 + * assert (result == formattable); 1.74 + * } 1.75 + * } 1.76 + * </pre> 1.77 + * 1.78 + * <P> 1.79 + * @see TimeUnitAmount 1.80 + * @see TimeUnitFormat 1.81 + * @stable ICU 4.2 1.82 + */ 1.83 +class U_I18N_API TimeUnitFormat: public MeasureFormat { 1.84 +public: 1.85 + 1.86 + /** 1.87 + * Create TimeUnitFormat with default locale, and full name style. 1.88 + * Use setLocale and/or setFormat to modify. 1.89 + * @stable ICU 4.2 1.90 + */ 1.91 + TimeUnitFormat(UErrorCode& status); 1.92 + 1.93 + /** 1.94 + * Create TimeUnitFormat given locale, and full name style. 1.95 + * @stable ICU 4.2 1.96 + */ 1.97 + TimeUnitFormat(const Locale& locale, UErrorCode& status); 1.98 + 1.99 + /** 1.100 + * Create TimeUnitFormat given locale and style. 1.101 + * @stable ICU 4.8 1.102 + */ 1.103 + TimeUnitFormat(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status); 1.104 + 1.105 + /** 1.106 + * Copy constructor. 1.107 + * @stable ICU 4.2 1.108 + */ 1.109 + TimeUnitFormat(const TimeUnitFormat&); 1.110 + 1.111 + /** 1.112 + * deconstructor 1.113 + * @stable ICU 4.2 1.114 + */ 1.115 + virtual ~TimeUnitFormat(); 1.116 + 1.117 + /** 1.118 + * Clone this Format object polymorphically. The caller owns the result and 1.119 + * should delete it when done. 1.120 + * @return A copy of the object. 1.121 + * @stable ICU 4.2 1.122 + */ 1.123 + virtual Format* clone(void) const; 1.124 + 1.125 + /** 1.126 + * Assignment operator 1.127 + * @stable ICU 4.2 1.128 + */ 1.129 + TimeUnitFormat& operator=(const TimeUnitFormat& other); 1.130 + 1.131 + 1.132 + /** 1.133 + * Return true if the given Format objects are semantically equal. Objects 1.134 + * of different subclasses are considered unequal. 1.135 + * @param other the object to be compared with. 1.136 + * @return true if the given Format objects are semantically equal. 1.137 + * @stable ICU 4.2 1.138 + */ 1.139 + virtual UBool operator==(const Format& other) const; 1.140 + 1.141 + /** 1.142 + * Return true if the given Format objects are not semantically equal. 1.143 + * Objects of different subclasses are considered unequal. 1.144 + * @param other the object to be compared with. 1.145 + * @return true if the given Format objects are not semantically equal. 1.146 + * @stable ICU 4.2 1.147 + */ 1.148 + UBool operator!=(const Format& other) const; 1.149 + 1.150 + /** 1.151 + * Set the locale used for formatting or parsing. 1.152 + * @param locale the locale to be set 1.153 + * @param status output param set to success/failure code on exit 1.154 + * @stable ICU 4.2 1.155 + */ 1.156 + void setLocale(const Locale& locale, UErrorCode& status); 1.157 + 1.158 + 1.159 + /** 1.160 + * Set the number format used for formatting or parsing. 1.161 + * @param format the number formatter to be set 1.162 + * @param status output param set to success/failure code on exit 1.163 + * @stable ICU 4.2 1.164 + */ 1.165 + void setNumberFormat(const NumberFormat& format, UErrorCode& status); 1.166 + 1.167 + 1.168 + using MeasureFormat::format; 1.169 + 1.170 + /** 1.171 + * Format a TimeUnitAmount. 1.172 + * If the formattable object is not a time unit amount object, 1.173 + * or the number in time unit amount is not a double type or long type 1.174 + * numeric, it returns a failing status: U_ILLEGAL_ARGUMENT_ERROR. 1.175 + * @see Format#format(const Formattable&, UnicodeString&, FieldPosition&, UErrorCode&) const 1.176 + * @stable ICU 4.2 1.177 + */ 1.178 + virtual UnicodeString& format(const Formattable& obj, 1.179 + UnicodeString& toAppendTo, 1.180 + FieldPosition& pos, 1.181 + UErrorCode& status) const; 1.182 + 1.183 + /** 1.184 + * Parse a TimeUnitAmount. 1.185 + * @see Format#parseObject(const UnicodeString&, Formattable&, ParsePosition&) const; 1.186 + * @stable ICU 4.2 1.187 + */ 1.188 + virtual void parseObject(const UnicodeString& source, 1.189 + Formattable& result, 1.190 + ParsePosition& pos) const; 1.191 + 1.192 + /** 1.193 + * Return the class ID for this class. This is useful only for comparing to 1.194 + * a return value from getDynamicClassID(). For example: 1.195 + * <pre> 1.196 + * . Base* polymorphic_pointer = createPolymorphicObject(); 1.197 + * . if (polymorphic_pointer->getDynamicClassID() == 1.198 + * . erived::getStaticClassID()) ... 1.199 + * </pre> 1.200 + * @return The class ID for all objects of this class. 1.201 + * @stable ICU 4.2 1.202 + */ 1.203 + static UClassID U_EXPORT2 getStaticClassID(void); 1.204 + 1.205 + /** 1.206 + * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This 1.207 + * method is to implement a simple version of RTTI, since not all C++ 1.208 + * compilers support genuine RTTI. Polymorphic operator==() and clone() 1.209 + * methods call this method. 1.210 + * 1.211 + * @return The class ID for this object. All objects of a 1.212 + * given class have the same class ID. Objects of 1.213 + * other classes have different class IDs. 1.214 + * @stable ICU 4.2 1.215 + */ 1.216 + virtual UClassID getDynamicClassID(void) const; 1.217 + 1.218 +private: 1.219 + NumberFormat* fNumberFormat; 1.220 + Locale fLocale; 1.221 + Hashtable* fTimeUnitToCountToPatterns[TimeUnit::UTIMEUNIT_FIELD_COUNT]; 1.222 + PluralRules* fPluralRules; 1.223 + UTimeUnitFormatStyle fStyle; 1.224 + 1.225 + void create(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status); 1.226 + 1.227 + // it might actually be simpler to make them Decimal Formats later. 1.228 + // initialize all private data members 1.229 + void setup(UErrorCode& status); 1.230 + 1.231 + // initialize data member without fill in data for fTimeUnitToCountToPattern 1.232 + void initDataMembers(UErrorCode& status); 1.233 + 1.234 + // initialize fTimeUnitToCountToPatterns from current locale's resource. 1.235 + void readFromCurrentLocale(UTimeUnitFormatStyle style, const char* key, const UVector& pluralCounts, 1.236 + UErrorCode& status); 1.237 + 1.238 + // check completeness of fTimeUnitToCountToPatterns against all time units, 1.239 + // and all plural rules, fill in fallback as necessary. 1.240 + void checkConsistency(UTimeUnitFormatStyle style, const char* key, UErrorCode& status); 1.241 + 1.242 + // fill in fTimeUnitToCountToPatterns from locale fall-back chain 1.243 + void searchInLocaleChain(UTimeUnitFormatStyle style, const char* key, const char* localeName, 1.244 + TimeUnit::UTimeUnitFields field, const UnicodeString&, 1.245 + const char*, Hashtable*, UErrorCode&); 1.246 + 1.247 + // initialize hash table 1.248 + Hashtable* initHash(UErrorCode& status); 1.249 + 1.250 + // delete hash table 1.251 + void deleteHash(Hashtable* htable); 1.252 + 1.253 + // copy hash table 1.254 + void copyHash(const Hashtable* source, Hashtable* target, UErrorCode& status); 1.255 + // get time unit name, such as "year", from time unit field enum, such as 1.256 + // UTIMEUNIT_YEAR. 1.257 + static const char* getTimeUnitName(TimeUnit::UTimeUnitFields field, UErrorCode& status); 1.258 + 1.259 +}; 1.260 + 1.261 + 1.262 + 1.263 +inline UBool 1.264 +TimeUnitFormat::operator!=(const Format& other) const { 1.265 + return !operator==(other); 1.266 +} 1.267 + 1.268 + 1.269 + 1.270 +U_NAMESPACE_END 1.271 + 1.272 +#endif /* #if !UCONFIG_NO_FORMATTING */ 1.273 + 1.274 +#endif // __TMUTFMT_H__ 1.275 +//eof