measfmt.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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, and has much more limited
  81. * capabilities.
  82. *
  83. * @see Format
  84. * @author Alan Liu
  85. * @stable ICU 3.0
  86. */
  87. class U_I18N_API MeasureFormat : public Format {
  88. public:
  89. using Format::parseObject;
  90. using Format::format;
  91. /**
  92. * Constructor.
  93. * <p>
  94. * <strong>NOTE:</strong> New users are strongly encouraged to use
  95. * {@link icu::number::NumberFormatter} instead of NumberFormat.
  96. * @stable ICU 53
  97. */
  98. MeasureFormat(
  99. const Locale &locale, UMeasureFormatWidth width, UErrorCode &status);
  100. /**
  101. * Constructor.
  102. * <p>
  103. * <strong>NOTE:</strong> New users are strongly encouraged to use
  104. * {@link icu::number::NumberFormatter} instead of NumberFormat.
  105. * @stable ICU 53
  106. */
  107. MeasureFormat(
  108. const Locale &locale,
  109. UMeasureFormatWidth width,
  110. NumberFormat *nfToAdopt,
  111. UErrorCode &status);
  112. /**
  113. * Copy constructor.
  114. * @stable ICU 3.0
  115. */
  116. MeasureFormat(const MeasureFormat &other);
  117. /**
  118. * Assignment operator.
  119. * @stable ICU 3.0
  120. */
  121. MeasureFormat &operator=(const MeasureFormat &rhs);
  122. /**
  123. * Destructor.
  124. * @stable ICU 3.0
  125. */
  126. virtual ~MeasureFormat();
  127. /**
  128. * Return true if given Format objects are semantically equal.
  129. * @stable ICU 53
  130. */
  131. virtual bool operator==(const Format &other) const override;
  132. /**
  133. * Clones this object polymorphically.
  134. * @stable ICU 53
  135. */
  136. virtual MeasureFormat *clone() const override;
  137. /**
  138. * Formats object to produce a string.
  139. * @stable ICU 53
  140. */
  141. virtual UnicodeString &format(
  142. const Formattable &obj,
  143. UnicodeString &appendTo,
  144. FieldPosition &pos,
  145. UErrorCode &status) const override;
  146. #ifndef U_FORCE_HIDE_DRAFT_API
  147. /**
  148. * Parse a string to produce an object. This implementation sets
  149. * status to U_UNSUPPORTED_ERROR.
  150. *
  151. * @draft ICU 53
  152. */
  153. virtual void parseObject(
  154. const UnicodeString &source,
  155. Formattable &reslt,
  156. ParsePosition &pos) const override;
  157. #endif // U_FORCE_HIDE_DRAFT_API
  158. /**
  159. * Formats measure objects to produce a string. An example of such a
  160. * formatted string is 3 meters, 3.5 centimeters. Measure objects appear
  161. * in the formatted string in the same order they appear in the "measures"
  162. * array. The NumberFormat of this object is used only to format the amount
  163. * of the very last measure. The other amounts are formatted with zero
  164. * decimal places while rounding toward zero.
  165. * @param measures array of measure objects.
  166. * @param measureCount the number of measure objects.
  167. * @param appendTo formatted string appended here.
  168. * @param pos the field position.
  169. * @param status the error.
  170. * @return appendTo reference
  171. *
  172. * @stable ICU 53
  173. */
  174. UnicodeString &formatMeasures(
  175. const Measure *measures,
  176. int32_t measureCount,
  177. UnicodeString &appendTo,
  178. FieldPosition &pos,
  179. UErrorCode &status) const;
  180. /**
  181. * Formats a single measure per unit. An example of such a
  182. * formatted string is 3.5 meters per second.
  183. * @param measure The measure object. In above example, 3.5 meters.
  184. * @param perUnit The per unit. In above example, it is
  185. * `*%MeasureUnit::createSecond(status)`.
  186. * @param appendTo formatted string appended here.
  187. * @param pos the field position.
  188. * @param status the error.
  189. * @return appendTo reference
  190. *
  191. * @stable ICU 55
  192. */
  193. UnicodeString &formatMeasurePerUnit(
  194. const Measure &measure,
  195. const MeasureUnit &perUnit,
  196. UnicodeString &appendTo,
  197. FieldPosition &pos,
  198. UErrorCode &status) const;
  199. /**
  200. * Gets the display name of the specified {@link MeasureUnit} corresponding to the current
  201. * locale and format width.
  202. * @param unit The unit for which to get a display name.
  203. * @param status the error.
  204. * @return The display name in the locale and width specified in
  205. * the MeasureFormat constructor, or null if there is no display name available
  206. * for the specified unit.
  207. *
  208. * @stable ICU 58
  209. */
  210. UnicodeString getUnitDisplayName(const MeasureUnit& unit, UErrorCode &status) const;
  211. /**
  212. * Return a formatter for CurrencyAmount objects in the given
  213. * locale.
  214. * <p>
  215. * <strong>NOTE:</strong> New users are strongly encouraged to use
  216. * {@link icu::number::NumberFormatter} instead of NumberFormat.
  217. * @param locale desired locale
  218. * @param ec input-output error code
  219. * @return a formatter object, or nullptr upon error
  220. * @stable ICU 3.0
  221. */
  222. static MeasureFormat* U_EXPORT2 createCurrencyFormat(const Locale& locale,
  223. UErrorCode& ec);
  224. /**
  225. * Return a formatter for CurrencyAmount objects in the default
  226. * locale.
  227. * <p>
  228. * <strong>NOTE:</strong> New users are strongly encouraged to use
  229. * {@link icu::number::NumberFormatter} instead of NumberFormat.
  230. * @param ec input-output error code
  231. * @return a formatter object, or nullptr upon error
  232. * @stable ICU 3.0
  233. */
  234. static MeasureFormat* U_EXPORT2 createCurrencyFormat(UErrorCode& ec);
  235. /**
  236. * Return the class ID for this class. This is useful only for comparing to
  237. * a return value from getDynamicClassID(). For example:
  238. * <pre>
  239. * . Base* polymorphic_pointer = createPolymorphicObject();
  240. * . if (polymorphic_pointer->getDynamicClassID() ==
  241. * . erived::getStaticClassID()) ...
  242. * </pre>
  243. * @return The class ID for all objects of this class.
  244. * @stable ICU 53
  245. */
  246. static UClassID U_EXPORT2 getStaticClassID();
  247. /**
  248. * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
  249. * method is to implement a simple version of RTTI, since not all C++
  250. * compilers support genuine RTTI. Polymorphic operator==() and clone()
  251. * methods call this method.
  252. *
  253. * @return The class ID for this object. All objects of a
  254. * given class have the same class ID. Objects of
  255. * other classes have different class IDs.
  256. * @stable ICU 53
  257. */
  258. virtual UClassID getDynamicClassID() const override;
  259. protected:
  260. /**
  261. * Default constructor.
  262. * @stable ICU 3.0
  263. */
  264. MeasureFormat();
  265. #ifndef U_HIDE_INTERNAL_API
  266. /**
  267. * ICU use only.
  268. * Initialize or change MeasureFormat class from subclass.
  269. * @internal.
  270. */
  271. void initMeasureFormat(
  272. const Locale &locale,
  273. UMeasureFormatWidth width,
  274. NumberFormat *nfToAdopt,
  275. UErrorCode &status);
  276. /**
  277. * ICU use only.
  278. * Allows subclass to change locale. Note that this method also changes
  279. * the NumberFormat object. Returns true if locale changed; false if no
  280. * change was made.
  281. * @internal.
  282. */
  283. UBool setMeasureFormatLocale(const Locale &locale, UErrorCode &status);
  284. /**
  285. * ICU use only.
  286. * Let subclass change NumberFormat.
  287. * @internal.
  288. */
  289. void adoptNumberFormat(NumberFormat *nfToAdopt, UErrorCode &status);
  290. /**
  291. * ICU use only.
  292. * @internal.
  293. */
  294. const NumberFormat &getNumberFormatInternal() const;
  295. /**
  296. * ICU use only.
  297. * Always returns the short form currency formatter.
  298. * @internal.
  299. */
  300. const NumberFormat& getCurrencyFormatInternal() const;
  301. /**
  302. * ICU use only.
  303. * @internal.
  304. */
  305. const PluralRules &getPluralRules() const;
  306. /**
  307. * ICU use only.
  308. * @internal.
  309. */
  310. Locale getLocale(UErrorCode &status) const;
  311. /**
  312. * ICU use only.
  313. * @internal.
  314. */
  315. const char *getLocaleID(UErrorCode &status) const;
  316. #endif /* U_HIDE_INTERNAL_API */
  317. private:
  318. const MeasureFormatCacheData *cache;
  319. const SharedNumberFormat *numberFormat;
  320. const SharedPluralRules *pluralRules;
  321. UMeasureFormatWidth fWidth;
  322. // Declared outside of MeasureFormatSharedData because ListFormatter
  323. // objects are relatively cheap to copy; therefore, they don't need to be
  324. // shared across instances.
  325. ListFormatter *listFormatter;
  326. UnicodeString &formatMeasure(
  327. const Measure &measure,
  328. const NumberFormat &nf,
  329. UnicodeString &appendTo,
  330. FieldPosition &pos,
  331. UErrorCode &status) const;
  332. UnicodeString &formatMeasuresSlowTrack(
  333. const Measure *measures,
  334. int32_t measureCount,
  335. UnicodeString& appendTo,
  336. FieldPosition& pos,
  337. UErrorCode& status) const;
  338. UnicodeString &formatNumeric(
  339. const Formattable *hms, // always length 3: [0] is hour; [1] is
  340. // minute; [2] is second.
  341. int32_t bitMap, // 1=hour set, 2=minute set, 4=second set
  342. UnicodeString &appendTo,
  343. UErrorCode &status) const;
  344. };
  345. U_NAMESPACE_END
  346. #endif // #if !UCONFIG_NO_FORMATTING
  347. #endif /* U_SHOW_CPLUSPLUS_API */
  348. #endif // #ifndef MEASUREFORMAT_H