]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/format.h
2 ********************************************************************************
3 * Copyright (C) 1997-2011, International Business Machines Corporation and others.
5 ********************************************************************************
9 * Modification History:
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 ********************************************************************************
17 // *****************************************************************************
18 // This file was generated from the java source file Format.java
19 // *****************************************************************************
25 #include "unicode/utypes.h"
29 * \brief C++ API: Base class for all formats.
32 #if !UCONFIG_NO_FORMATTING
34 #include "unicode/unistr.h"
35 #include "unicode/fmtable.h"
36 #include "unicode/fieldpos.h"
37 #include "unicode/fpositer.h"
38 #include "unicode/parsepos.h"
39 #include "unicode/parseerr.h"
40 #include "unicode/locid.h"
45 * Base class for all formats. This is an abstract base class which
46 * specifies the protocol for classes which convert other objects or
47 * values, such as numeric values and dates, and their string
48 * representations. In some cases these representations may be
49 * localized or contain localized characters or strings. For example,
50 * a numeric formatter such as DecimalFormat may convert a numeric
51 * value such as 12345 to the string "$12,345". It may also parse
52 * the string back into a numeric value. A date and time formatter
53 * like SimpleDateFormat may represent a specific date, encoded
54 * numerically, as a string such as "Wednesday, February 26, 1997 AD".
56 * Many of the concrete subclasses of Format employ the notion of
57 * a pattern. A pattern is a string representation of the rules which
58 * govern the interconversion between values and strings. For example,
59 * a DecimalFormat object may be associated with the pattern
60 * "$#,##0.00;($#,##0.00)", which is a common US English format for
61 * currency values, yielding strings such as "$1,234.45" for 1234.45,
62 * and "($987.65)" for 987.6543. The specific syntax of a pattern
63 * is defined by each subclass.
65 * Even though many subclasses use patterns, the notion of a pattern
66 * is not inherent to Format classes in general, and is not part of
67 * the explicit base class protocol.
69 * Two complex formatting classes bear mentioning. These are
70 * MessageFormat and ChoiceFormat. ChoiceFormat is a subclass of
71 * NumberFormat which allows the user to format different number ranges
72 * as strings. For instance, 0 may be represented as "no files", 1 as
73 * "one file", and any number greater than 1 as "many files".
74 * MessageFormat is a formatter which utilizes other Format objects to
75 * format a string containing with multiple values. For instance,
76 * A MessageFormat object might produce the string "There are no files
77 * on the disk MyDisk on February 27, 1997." given the arguments 0,
78 * "MyDisk", and the date value of 2/27/97. See the ChoiceFormat
79 * and MessageFormat headers for further information.
81 * If formatting is unsuccessful, a failing UErrorCode is returned when
82 * the Format cannot format the type of object, otherwise if there is
83 * something illformed about the the Unicode replacement character
86 * If there is no match when parsing, a parse failure UErrorCode is
87 * retured for methods which take no ParsePosition. For the method
88 * that takes a ParsePosition, the index parameter is left unchanged.
90 * <em>User subclasses are not supported.</em> While clients may write
91 * subclasses, such code will not necessarily work and will not be
92 * guaranteed to work stably from release to release.
94 class U_I18N_API Format
: public UObject
{
103 * Return true if the given Format objects are semantically equal.
104 * Objects of different subclasses are considered unequal.
105 * @param other the object to be compared with.
106 * @return Return true if the given Format objects are semantically equal.
107 * Objects of different subclasses are considered unequal.
110 virtual UBool
operator==(const Format
& other
) const = 0;
113 * Return true if the given Format objects are not semantically
115 * @param other the object to be compared with.
116 * @return Return true if the given Format objects are not semantically.
119 UBool
operator!=(const Format
& other
) const { return !operator==(other
); }
122 * Clone this object polymorphically. The caller is responsible
123 * for deleting the result when done.
124 * @return A copy of the object
127 virtual Format
* clone() const = 0;
130 * Formats an object to produce a string.
132 * @param obj The object to format.
133 * @param appendTo Output parameter to receive result.
134 * Result is appended to existing contents.
135 * @param status Output parameter filled in with success or failure status.
136 * @return Reference to 'appendTo' parameter.
139 UnicodeString
& format(const Formattable
& obj
,
140 UnicodeString
& appendTo
,
141 UErrorCode
& status
) const;
144 * Format an object to produce a string. This is a pure virtual method which
145 * subclasses must implement. This method allows polymorphic formatting
146 * of Formattable objects. If a subclass of Format receives a Formattable
147 * object type it doesn't handle (e.g., if a numeric Formattable is passed
148 * to a DateFormat object) then it returns a failing UErrorCode.
150 * @param obj The object to format.
151 * @param appendTo Output parameter to receive result.
152 * Result is appended to existing contents.
153 * @param pos On input: an alignment field, if desired.
154 * On output: the offsets of the alignment field.
155 * @param status Output param filled with success/failure status.
156 * @return Reference to 'appendTo' parameter.
159 virtual UnicodeString
& format(const Formattable
& obj
,
160 UnicodeString
& appendTo
,
162 UErrorCode
& status
) const = 0;
164 * Format an object to produce a string. Subclasses should override this
165 * method. This method allows polymorphic formatting of Formattable objects.
166 * If a subclass of Format receives a Formattable object type it doesn't
167 * handle (e.g., if a numeric Formattable is passed to a DateFormat object)
168 * then it returns a failing UErrorCode.
170 * @param obj The object to format.
171 * @param appendTo Output parameter to receive result.
172 * Result is appended to existing contents.
173 * @param posIter On return, can be used to iterate over positions
174 * of fields generated by this format call.
175 * @param status Output param filled with success/failure status.
176 * @return Reference to 'appendTo' parameter.
179 virtual UnicodeString
& format(const Formattable
& obj
,
180 UnicodeString
& appendTo
,
181 FieldPositionIterator
* posIter
,
182 UErrorCode
& status
) const;
185 * Parse a string to produce an object. This is a pure virtual
186 * method which subclasses must implement. This method allows
187 * polymorphic parsing of strings into Formattable objects.
189 * Before calling, set parse_pos.index to the offset you want to
190 * start parsing at in the source. After calling, parse_pos.index
191 * is the end of the text you parsed. If error occurs, index is
194 * When parsing, leading whitespace is discarded (with successful
195 * parse), while trailing whitespace is left as is.
199 * Parsing "_12_xy" (where _ represents a space) for a number,
200 * with index == 0 will result in the number 12, with
201 * parse_pos.index updated to 3 (just before the second space).
202 * Parsing a second time will result in a failing UErrorCode since
203 * "xy" is not a number, and leave index at 3.
205 * Subclasses will typically supply specific parse methods that
206 * return different types of values. Since methods can't overload
207 * on return types, these will typically be named "parse", while
208 * this polymorphic method will always be called parseObject. Any
209 * parse method that does not take a parse_pos should set status
210 * to an error value when no text in the required format is at the
213 * @param source The string to be parsed into an object.
214 * @param result Formattable to be set to the parse result.
215 * If parse fails, return contents are undefined.
216 * @param parse_pos The position to start parsing at. Upon return
217 * this param is set to the position after the
218 * last character successfully parsed. If the
219 * source is not parsed successfully, this param
220 * will remain unchanged.
223 virtual void parseObject(const UnicodeString
& source
,
225 ParsePosition
& parse_pos
) const = 0;
228 * Parses a string to produce an object. This is a convenience method
229 * which calls the pure virtual parseObject() method, and returns a
230 * failure UErrorCode if the ParsePosition indicates failure.
232 * @param source The string to be parsed into an object.
233 * @param result Formattable to be set to the parse result.
234 * If parse fails, return contents are undefined.
235 * @param status Output param to be filled with success/failure
239 void parseObject(const UnicodeString
& source
,
241 UErrorCode
& status
) const;
243 /** Get the locale for this format object. You can choose between valid and actual locale.
244 * @param type type of the locale we're looking for (valid or actual)
245 * @param status error code for the operation
249 Locale
getLocale(ULocDataLocaleType type
, UErrorCode
& status
) const;
251 #ifndef U_HIDE_INTERNAL_API
252 /** Get the locale for this format object. You can choose between valid and actual locale.
253 * @param type type of the locale we're looking for (valid or actual)
254 * @param status error code for the operation
258 const char* getLocaleID(ULocDataLocaleType type
, UErrorCode
&status
) const;
259 #endif /* U_HIDE_INTERNAL_API */
262 /** @stable ICU 2.8 */
263 void setLocaleIDs(const char* valid
, const char* actual
);
267 * Default constructor for subclass use only. Does nothing.
275 Format(const Format
&); // Does nothing; for subclasses only
280 Format
& operator=(const Format
&); // Does nothing; for subclasses
284 * Simple function for initializing a UParseError from a UnicodeString.
286 * @param pattern The pattern to copy into the parseError
287 * @param pos The position in pattern where the error occured
288 * @param parseError The UParseError object to fill in
291 static void syntaxError(const UnicodeString
& pattern
,
293 UParseError
& parseError
);
296 char actualLocale
[ULOC_FULLNAME_CAPACITY
];
297 char validLocale
[ULOC_FULLNAME_CAPACITY
];
302 #endif /* #if !UCONFIG_NO_FORMATTING */