measfmt.h 10 KB

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