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