measfmt.h 11 KB

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