measfmt.h 11 KB

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