format.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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) 1997-2011, International Business Machines Corporation and others.
  6. * All Rights Reserved.
  7. ********************************************************************************
  8. *
  9. * File FORMAT.H
  10. *
  11. * Modification History:
  12. *
  13. * Date Name Description
  14. * 02/19/97 aliu Converted from java.
  15. * 03/17/97 clhuang Updated per C++ implementation.
  16. * 03/27/97 helena Updated to pass the simple test after code review.
  17. ********************************************************************************
  18. */
  19. // *****************************************************************************
  20. // This file was generated from the java source file Format.java
  21. // *****************************************************************************
  22. #ifndef FORMAT_H
  23. #define FORMAT_H
  24. #include "utypes.h"
  25. /**
  26. * \file
  27. * \brief C++ API: Base class for all formats.
  28. */
  29. #if !UCONFIG_NO_FORMATTING
  30. #include "unistr.h"
  31. #include "fmtable.h"
  32. #include "fieldpos.h"
  33. #include "fpositer.h"
  34. #include "parsepos.h"
  35. #include "parseerr.h"
  36. #include "locid.h"
  37. U_NAMESPACE_BEGIN
  38. /**
  39. * Base class for all formats. This is an abstract base class which
  40. * specifies the protocol for classes which convert other objects or
  41. * values, such as numeric values and dates, and their string
  42. * representations. In some cases these representations may be
  43. * localized or contain localized characters or strings. For example,
  44. * a numeric formatter such as DecimalFormat may convert a numeric
  45. * value such as 12345 to the string "$12,345". It may also parse
  46. * the string back into a numeric value. A date and time formatter
  47. * like SimpleDateFormat may represent a specific date, encoded
  48. * numerically, as a string such as "Wednesday, February 26, 1997 AD".
  49. * <P>
  50. * Many of the concrete subclasses of Format employ the notion of
  51. * a pattern. A pattern is a string representation of the rules which
  52. * govern the interconversion between values and strings. For example,
  53. * a DecimalFormat object may be associated with the pattern
  54. * "$#,##0.00;($#,##0.00)", which is a common US English format for
  55. * currency values, yielding strings such as "$1,234.45" for 1234.45,
  56. * and "($987.65)" for 987.6543. The specific syntax of a pattern
  57. * is defined by each subclass.
  58. * <P>
  59. * Even though many subclasses use patterns, the notion of a pattern
  60. * is not inherent to Format classes in general, and is not part of
  61. * the explicit base class protocol.
  62. * <P>
  63. * Two complex formatting classes bear mentioning. These are
  64. * MessageFormat and ChoiceFormat. ChoiceFormat is a subclass of
  65. * NumberFormat which allows the user to format different number ranges
  66. * as strings. For instance, 0 may be represented as "no files", 1 as
  67. * "one file", and any number greater than 1 as "many files".
  68. * MessageFormat is a formatter which utilizes other Format objects to
  69. * format a string containing with multiple values. For instance,
  70. * A MessageFormat object might produce the string "There are no files
  71. * on the disk MyDisk on February 27, 1997." given the arguments 0,
  72. * "MyDisk", and the date value of 2/27/97. See the ChoiceFormat
  73. * and MessageFormat headers for further information.
  74. * <P>
  75. * If formatting is unsuccessful, a failing UErrorCode is returned when
  76. * the Format cannot format the type of object, otherwise if there is
  77. * something illformed about the the Unicode replacement character
  78. * 0xFFFD is returned.
  79. * <P>
  80. * If there is no match when parsing, a parse failure UErrorCode is
  81. * retured for methods which take no ParsePosition. For the method
  82. * that takes a ParsePosition, the index parameter is left unchanged.
  83. * <P>
  84. * <em>User subclasses are not supported.</em> While clients may write
  85. * subclasses, such code will not necessarily work and will not be
  86. * guaranteed to work stably from release to release.
  87. */
  88. class U_I18N_API Format : public UObject {
  89. public:
  90. /** Destructor
  91. * @stable ICU 2.4
  92. */
  93. virtual ~Format();
  94. /**
  95. * Return true if the given Format objects are semantically equal.
  96. * Objects of different subclasses are considered unequal.
  97. * @param other the object to be compared with.
  98. * @return Return true if the given Format objects are semantically equal.
  99. * Objects of different subclasses are considered unequal.
  100. * @stable ICU 2.0
  101. */
  102. virtual UBool operator==(const Format& other) const = 0;
  103. /**
  104. * Return true if the given Format objects are not semantically
  105. * equal.
  106. * @param other the object to be compared with.
  107. * @return Return true if the given Format objects are not semantically.
  108. * @stable ICU 2.0
  109. */
  110. UBool operator!=(const Format& other) const { return !operator==(other); }
  111. /**
  112. * Clone this object polymorphically. The caller is responsible
  113. * for deleting the result when done.
  114. * @return A copy of the object
  115. * @stable ICU 2.0
  116. */
  117. virtual Format* clone() const = 0;
  118. /**
  119. * Formats an object to produce a string.
  120. *
  121. * @param obj The object to format.
  122. * @param appendTo Output parameter to receive result.
  123. * Result is appended to existing contents.
  124. * @param status Output parameter filled in with success or failure status.
  125. * @return Reference to 'appendTo' parameter.
  126. * @stable ICU 2.0
  127. */
  128. UnicodeString& format(const Formattable& obj,
  129. UnicodeString& appendTo,
  130. UErrorCode& status) const;
  131. /**
  132. * Format an object to produce a string. This is a pure virtual method which
  133. * subclasses must implement. This method allows polymorphic formatting
  134. * of Formattable objects. If a subclass of Format receives a Formattable
  135. * object type it doesn't handle (e.g., if a numeric Formattable is passed
  136. * to a DateFormat object) then it returns a failing UErrorCode.
  137. *
  138. * @param obj The object to format.
  139. * @param appendTo Output parameter to receive result.
  140. * Result is appended to existing contents.
  141. * @param pos On input: an alignment field, if desired.
  142. * On output: the offsets of the alignment field.
  143. * @param status Output param filled with success/failure status.
  144. * @return Reference to 'appendTo' parameter.
  145. * @stable ICU 2.0
  146. */
  147. virtual UnicodeString& format(const Formattable& obj,
  148. UnicodeString& appendTo,
  149. FieldPosition& pos,
  150. UErrorCode& status) const = 0;
  151. /**
  152. * Format an object to produce a string. Subclasses should override this
  153. * method. This method allows polymorphic formatting of Formattable objects.
  154. * If a subclass of Format receives a Formattable object type it doesn't
  155. * handle (e.g., if a numeric Formattable is passed to a DateFormat object)
  156. * then it returns a failing UErrorCode.
  157. *
  158. * @param obj The object to format.
  159. * @param appendTo Output parameter to receive result.
  160. * Result is appended to existing contents.
  161. * @param posIter On return, can be used to iterate over positions
  162. * of fields generated by this format call.
  163. * @param status Output param filled with success/failure status.
  164. * @return Reference to 'appendTo' parameter.
  165. * @stable ICU 4.4
  166. */
  167. virtual UnicodeString& format(const Formattable& obj,
  168. UnicodeString& appendTo,
  169. FieldPositionIterator* posIter,
  170. UErrorCode& status) const;
  171. /**
  172. * Parse a string to produce an object. This is a pure virtual
  173. * method which subclasses must implement. This method allows
  174. * polymorphic parsing of strings into Formattable objects.
  175. * <P>
  176. * Before calling, set parse_pos.index to the offset you want to
  177. * start parsing at in the source. After calling, parse_pos.index
  178. * is the end of the text you parsed. If error occurs, index is
  179. * unchanged.
  180. * <P>
  181. * When parsing, leading whitespace is discarded (with successful
  182. * parse), while trailing whitespace is left as is.
  183. * <P>
  184. * Example:
  185. * <P>
  186. * Parsing "_12_xy" (where _ represents a space) for a number,
  187. * with index == 0 will result in the number 12, with
  188. * parse_pos.index updated to 3 (just before the second space).
  189. * Parsing a second time will result in a failing UErrorCode since
  190. * "xy" is not a number, and leave index at 3.
  191. * <P>
  192. * Subclasses will typically supply specific parse methods that
  193. * return different types of values. Since methods can't overload
  194. * on return types, these will typically be named "parse", while
  195. * this polymorphic method will always be called parseObject. Any
  196. * parse method that does not take a parse_pos should set status
  197. * to an error value when no text in the required format is at the
  198. * start position.
  199. *
  200. * @param source The string to be parsed into an object.
  201. * @param result Formattable to be set to the parse result.
  202. * If parse fails, return contents are undefined.
  203. * @param parse_pos The position to start parsing at. Upon return
  204. * this param is set to the position after the
  205. * last character successfully parsed. If the
  206. * source is not parsed successfully, this param
  207. * will remain unchanged.
  208. * @stable ICU 2.0
  209. */
  210. virtual void parseObject(const UnicodeString& source,
  211. Formattable& result,
  212. ParsePosition& parse_pos) const = 0;
  213. /**
  214. * Parses a string to produce an object. This is a convenience method
  215. * which calls the pure virtual parseObject() method, and returns a
  216. * failure UErrorCode if the ParsePosition indicates failure.
  217. *
  218. * @param source The string to be parsed into an object.
  219. * @param result Formattable to be set to the parse result.
  220. * If parse fails, return contents are undefined.
  221. * @param status Output param to be filled with success/failure
  222. * result code.
  223. * @stable ICU 2.0
  224. */
  225. void parseObject(const UnicodeString& source,
  226. Formattable& result,
  227. UErrorCode& status) const;
  228. /** Get the locale for this format object. You can choose between valid and actual locale.
  229. * @param type type of the locale we're looking for (valid or actual)
  230. * @param status error code for the operation
  231. * @return the locale
  232. * @stable ICU 2.8
  233. */
  234. Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
  235. #ifndef U_HIDE_INTERNAL_API
  236. /** Get the locale for this format object. You can choose between valid and actual locale.
  237. * @param type type of the locale we're looking for (valid or actual)
  238. * @param status error code for the operation
  239. * @return the locale
  240. * @internal
  241. */
  242. const char* getLocaleID(ULocDataLocaleType type, UErrorCode &status) const;
  243. #endif /* U_HIDE_INTERNAL_API */
  244. protected:
  245. /** @stable ICU 2.8 */
  246. void setLocaleIDs(const char* valid, const char* actual);
  247. protected:
  248. /**
  249. * Default constructor for subclass use only. Does nothing.
  250. * @stable ICU 2.0
  251. */
  252. Format();
  253. /**
  254. * @stable ICU 2.0
  255. */
  256. Format(const Format&); // Does nothing; for subclasses only
  257. /**
  258. * @stable ICU 2.0
  259. */
  260. Format& operator=(const Format&); // Does nothing; for subclasses
  261. /**
  262. * Simple function for initializing a UParseError from a UnicodeString.
  263. *
  264. * @param pattern The pattern to copy into the parseError
  265. * @param pos The position in pattern where the error occured
  266. * @param parseError The UParseError object to fill in
  267. * @stable ICU 2.4
  268. */
  269. static void syntaxError(const UnicodeString& pattern,
  270. int32_t pos,
  271. UParseError& parseError);
  272. private:
  273. char actualLocale[ULOC_FULLNAME_CAPACITY];
  274. char validLocale[ULOC_FULLNAME_CAPACITY];
  275. };
  276. U_NAMESPACE_END
  277. #endif /* #if !UCONFIG_NO_FORMATTING */
  278. #endif // _FORMAT
  279. //eof