format.h 12 KB

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