format.h 13 KB

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