]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/format.h
2 ********************************************************************************
3 * Copyright (C) 1997-2006, 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/parsepos.h"
38 #include "unicode/parseerr.h"
39 #include "unicode/locid.h"
44 * Base class for all formats. This is an abstract base class which
45 * specifies the protocol for classes which convert other objects or
46 * values, such as numeric values and dates, and their string
47 * representations. In some cases these representations may be
48 * localized or contain localized characters or strings. For example,
49 * a numeric formatter such as DecimalFormat may convert a numeric
50 * value such as 12345 to the string "$12,345". It may also parse
51 * the string back into a numeric value. A date and time formatter
52 * like SimpleDateFormat may represent a specific date, encoded
53 * numerically, as a string such as "Wednesday, February 26, 1997 AD".
55 * Many of the concrete subclasses of Format employ the notion of
56 * a pattern. A pattern is a string representation of the rules which
57 * govern the interconversion between values and strings. For example,
58 * a DecimalFormat object may be associated with the pattern
59 * "$#,##0.00;($#,##0.00)", which is a common US English format for
60 * currency values, yielding strings such as "$1,234.45" for 1234.45,
61 * and "($987.65)" for 987.6543. The specific syntax of a pattern
62 * is defined by each subclass.
64 * Even though many subclasses use patterns, the notion of a pattern
65 * is not inherent to Format classes in general, and is not part of
66 * the explicit base class protocol.
68 * Two complex formatting classes bear mentioning. These are
69 * MessageFormat and ChoiceFormat. ChoiceFormat is a subclass of
70 * NumberFormat which allows the user to format different number ranges
71 * as strings. For instance, 0 may be represented as "no files", 1 as
72 * "one file", and any number greater than 1 as "many files".
73 * MessageFormat is a formatter which utilizes other Format objects to
74 * format a string containing with multiple values. For instance,
75 * A MessageFormat object might produce the string "There are no files
76 * on the disk MyDisk on February 27, 1997." given the arguments 0,
77 * "MyDisk", and the date value of 2/27/97. See the ChoiceFormat
78 * and MessageFormat headers for further information.
80 * If formatting is unsuccessful, a failing UErrorCode is returned when
81 * the Format cannot format the type of object, otherwise if there is
82 * something illformed about the the Unicode replacement character
85 * If there is no match when parsing, a parse failure UErrorCode is
86 * retured for methods which take no ParsePosition. For the method
87 * that takes a ParsePosition, the index parameter is left unchanged.
89 * <em>User subclasses are not supported.</em> While clients may write
90 * subclasses, such code will not necessarily work and will not be
91 * guaranteed to work stably from release to release.
93 class U_I18N_API Format
: public UObject
{
102 * Return true if the given Format objects are semantically equal.
103 * Objects of different subclasses are considered unequal.
104 * @param other the object to be compared with.
105 * @return Return true if the given Format objects are semantically equal.
106 * Objects of different subclasses are considered unequal.
109 virtual UBool
operator==(const Format
& other
) const = 0;
112 * Return true if the given Format objects are not semantically
114 * @param other the object to be compared with.
115 * @return Return true if the given Format objects are not semantically.
118 UBool
operator!=(const Format
& other
) const { return !operator==(other
); }
121 * Clone this object polymorphically. The caller is responsible
122 * for deleting the result when done.
123 * @return A copy of the object
126 virtual Format
* clone() const = 0;
129 * Formats an object to produce a string.
131 * @param obj The object to format.
132 * @param appendTo Output parameter to receive result.
133 * Result is appended to existing contents.
134 * @param status Output parameter filled in with success or failure status.
135 * @return Reference to 'appendTo' parameter.
138 UnicodeString
& format(const Formattable
& obj
,
139 UnicodeString
& appendTo
,
140 UErrorCode
& status
) const;
143 * Format an object to produce a string. This is a pure virtual method which
144 * subclasses must implement. This method allows polymorphic formatting
145 * of Formattable objects. If a subclass of Format receives a Formattable
146 * object type it doesn't handle (e.g., if a numeric Formattable is passed
147 * to a DateFormat object) then it returns a failing UErrorCode.
149 * @param obj The object to format.
150 * @param appendTo Output parameter to receive result.
151 * Result is appended to existing contents.
152 * @param pos On input: an alignment field, if desired.
153 * On output: the offsets of the alignment field.
154 * @param status Output param filled with success/failure status.
155 * @return Reference to 'appendTo' parameter.
158 virtual UnicodeString
& format(const Formattable
& obj
,
159 UnicodeString
& appendTo
,
161 UErrorCode
& status
) const = 0;
164 * Parse a string to produce an object. This is a pure virtual
165 * method which subclasses must implement. This method allows
166 * polymorphic parsing of strings into Formattable objects.
168 * Before calling, set parse_pos.index to the offset you want to
169 * start parsing at in the source. After calling, parse_pos.index
170 * is the end of the text you parsed. If error occurs, index is
173 * When parsing, leading whitespace is discarded (with successful
174 * parse), while trailing whitespace is left as is.
178 * Parsing "_12_xy" (where _ represents a space) for a number,
179 * with index == 0 will result in the number 12, with
180 * parse_pos.index updated to 3 (just before the second space).
181 * Parsing a second time will result in a failing UErrorCode since
182 * "xy" is not a number, and leave index at 3.
184 * Subclasses will typically supply specific parse methods that
185 * return different types of values. Since methods can't overload
186 * on return types, these will typically be named "parse", while
187 * this polymorphic method will always be called parseObject. Any
188 * parse method that does not take a parse_pos should set status
189 * to an error value when no text in the required format is at the
192 * @param source The string to be parsed into an object.
193 * @param result Formattable to be set to the parse result.
194 * If parse fails, return contents are undefined.
195 * @param parse_pos The position to start parsing at. Upon return
196 * this param is set to the position after the
197 * last character successfully parsed. If the
198 * source is not parsed successfully, this param
199 * will remain unchanged.
202 virtual void parseObject(const UnicodeString
& source
,
204 ParsePosition
& parse_pos
) const = 0;
207 * Parses a string to produce an object. This is a convenience method
208 * which calls the pure virtual parseObject() method, and returns a
209 * failure UErrorCode if the ParsePosition indicates failure.
211 * @param source The string to be parsed into an object.
212 * @param result Formattable to be set to the parse result.
213 * If parse fails, return contents are undefined.
214 * @param status Output param to be filled with success/failure
218 void parseObject(const UnicodeString
& source
,
220 UErrorCode
& status
) const;
223 * Returns a unique class ID POLYMORPHICALLY. Pure virtual method.
224 * This method is to implement a simple version of RTTI, since not all
225 * C++ compilers support genuine RTTI. Polymorphic operator==() and
226 * clone() methods call this method.
227 * Concrete subclasses of Format must implement getDynamicClassID()
229 * @return The class ID for this object. All objects of a
230 * given class have the same class ID. Objects of
231 * other classes have different class IDs.
234 virtual UClassID
getDynamicClassID() const = 0;
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
242 Locale
getLocale(ULocDataLocaleType type
, UErrorCode
& status
) const;
244 /** Get the locale for this format object. You can choose between valid and actual locale.
245 * @param type type of the locale we're looking for (valid or actual)
246 * @param status error code for the operation
250 const char* getLocaleID(ULocDataLocaleType type
, UErrorCode
&status
) const;
253 /** @stable ICU 2.8 */
254 void setLocaleIDs(const char* valid
, const char* actual
);
258 * Default constructor for subclass use only. Does nothing.
266 Format(const Format
&); // Does nothing; for subclasses only
271 Format
& operator=(const Format
&); // Does nothing; for subclasses
275 * Simple function for initializing a UParseError from a UnicodeString.
277 * @param pattern The pattern to copy into the parseError
278 * @param pos The position in pattern where the error occured
279 * @param parseError The UParseError object to fill in
282 static void syntaxError(const UnicodeString
& pattern
,
284 UParseError
& parseError
);
287 char actualLocale
[ULOC_FULLNAME_CAPACITY
];
288 char validLocale
[ULOC_FULLNAME_CAPACITY
];
293 #endif /* #if !UCONFIG_NO_FORMATTING */