|
1 /* |
|
2 ******************************************************************************** |
|
3 * Copyright (C) 2013, International Business Machines Corporation and others. |
|
4 * All Rights Reserved. |
|
5 ******************************************************************************** |
|
6 * |
|
7 * File UFORMATTABLE.H |
|
8 * |
|
9 * Modification History: |
|
10 * |
|
11 * Date Name Description |
|
12 * 2013 Jun 7 srl New |
|
13 ******************************************************************************** |
|
14 */ |
|
15 |
|
16 /** |
|
17 * \file |
|
18 * \brief C API: UFormattable is a thin wrapper for primitive types used for formatting and parsing. |
|
19 * |
|
20 * This is a C interface to the icu::Formattable class. Static functions on this class convert |
|
21 * to and from this interface (via reinterpret_cast). Note that Formattables (and thus UFormattables) |
|
22 * are mutable, and many operations (even getters) may actually modify the internal state. For this |
|
23 * reason, UFormattables are not thread safe, and should not be shared between threads. |
|
24 * |
|
25 * See {@link unum_parseToUFormattable} for example code. |
|
26 */ |
|
27 |
|
28 #ifndef UFORMATTABLE_H |
|
29 #define UFORMATTABLE_H |
|
30 |
|
31 #include "unicode/utypes.h" |
|
32 |
|
33 #if !UCONFIG_NO_FORMATTING |
|
34 |
|
35 #ifndef U_HIDE_DRAFT_API |
|
36 |
|
37 #include "unicode/localpointer.h" |
|
38 |
|
39 /** |
|
40 * Enum designating the type of a UFormattable instance. |
|
41 * Practically, this indicates which of the getters would return without conversion |
|
42 * or error. |
|
43 * @see icu::Formattable::Type |
|
44 * @draft ICU 52 |
|
45 */ |
|
46 typedef enum UFormattableType { |
|
47 UFMT_DATE = 0, /**< ufmt_getDate() will return without conversion. @see ufmt_getDate*/ |
|
48 UFMT_DOUBLE, /**< ufmt_getDouble() will return without conversion. @see ufmt_getDouble*/ |
|
49 UFMT_LONG, /**< ufmt_getLong() will return without conversion. @see ufmt_getLong */ |
|
50 UFMT_STRING, /**< ufmt_getUChars() will return without conversion. @see ufmt_getUChars*/ |
|
51 UFMT_ARRAY, /**< ufmt_countArray() and ufmt_getArray() will return the value. @see ufmt_getArrayItemByIndex */ |
|
52 UFMT_INT64, /**< ufmt_getInt64() will return without conversion. @see ufmt_getInt64 */ |
|
53 UFMT_OBJECT, /**< ufmt_getObject() will return without conversion. @see ufmt_getObject*/ |
|
54 UFMT_COUNT /**< Count of defined UFormattableType values */ |
|
55 } UFormattableType; |
|
56 |
|
57 |
|
58 /** |
|
59 * Opaque type representing various types of data which may be used for formatting |
|
60 * and parsing operations. |
|
61 * @see icu::Formattable |
|
62 * @draft ICU 52 |
|
63 */ |
|
64 typedef void *UFormattable; |
|
65 |
|
66 /** |
|
67 * Initialize a UFormattable, to type UNUM_LONG, value 0 |
|
68 * may return error if memory allocation failed. |
|
69 * parameter status error code. |
|
70 * See {@link unum_parseToUFormattable} for example code. |
|
71 * @draft ICU 52 |
|
72 * @return the new UFormattable |
|
73 * @see ufmt_close |
|
74 * @see icu::Formattable::Formattable() |
|
75 */ |
|
76 U_DRAFT UFormattable* U_EXPORT2 |
|
77 ufmt_open(UErrorCode* status); |
|
78 |
|
79 /** |
|
80 * Cleanup any additional memory allocated by this UFormattable. |
|
81 * @param fmt the formatter |
|
82 * @draft ICU 52 |
|
83 * @see ufmt_open |
|
84 */ |
|
85 U_DRAFT void U_EXPORT2 |
|
86 ufmt_close(UFormattable* fmt); |
|
87 |
|
88 #if U_SHOW_CPLUSPLUS_API |
|
89 |
|
90 U_NAMESPACE_BEGIN |
|
91 |
|
92 /** |
|
93 * \class LocalUFormattablePointer |
|
94 * "Smart pointer" class, closes a UFormattable via ufmt_close(). |
|
95 * For most methods see the LocalPointerBase base class. |
|
96 * |
|
97 * @see LocalPointerBase |
|
98 * @see LocalPointer |
|
99 * @draft ICU 52 |
|
100 */ |
|
101 U_DEFINE_LOCAL_OPEN_POINTER(LocalUFormattablePointer, UFormattable, ufmt_close); |
|
102 |
|
103 U_NAMESPACE_END |
|
104 |
|
105 #endif |
|
106 |
|
107 /** |
|
108 * Return the type of this object |
|
109 * @param fmt the UFormattable object |
|
110 * @param status status code - U_ILLEGAL_ARGUMENT_ERROR is returned if the UFormattable contains data not supported by |
|
111 * the API |
|
112 * @return the value as a UFormattableType |
|
113 * @see ufmt_isNumeric |
|
114 * @see icu::Formattable::getType() const |
|
115 * @draft ICU 52 |
|
116 */ |
|
117 U_DRAFT UFormattableType U_EXPORT2 |
|
118 ufmt_getType(const UFormattable* fmt, UErrorCode *status); |
|
119 |
|
120 /** |
|
121 * Return whether the object is numeric. |
|
122 * @param fmt the UFormattable object |
|
123 * @return true if the object is a double, long, or int64 value, else false. |
|
124 * @see ufmt_getType |
|
125 * @see icu::Formattable::isNumeric() const |
|
126 * @draft ICU 52 |
|
127 */ |
|
128 U_DRAFT UBool U_EXPORT2 |
|
129 ufmt_isNumeric(const UFormattable* fmt); |
|
130 |
|
131 /** |
|
132 * Gets the UDate value of this object. If the type is not of type UFMT_DATE, |
|
133 * status is set to U_INVALID_FORMAT_ERROR and the return value is |
|
134 * undefined. |
|
135 * @param fmt the UFormattable object |
|
136 * @param status the error code - any conversion or format errors |
|
137 * @return the value |
|
138 * @draft ICU 52 |
|
139 * @see icu::Formattable::getDate(UErrorCode&) const |
|
140 */ |
|
141 U_DRAFT UDate U_EXPORT2 |
|
142 ufmt_getDate(const UFormattable* fmt, UErrorCode *status); |
|
143 |
|
144 /** |
|
145 * Gets the double value of this object. If the type is not a UFMT_DOUBLE, or |
|
146 * if there are additional significant digits than fit in a double type, |
|
147 * a conversion is performed with possible loss of precision. |
|
148 * If the type is UFMT_OBJECT and the |
|
149 * object is a Measure, then the result of |
|
150 * getNumber().getDouble(status) is returned. If this object is |
|
151 * neither a numeric type nor a Measure, then 0 is returned and |
|
152 * the status is set to U_INVALID_FORMAT_ERROR. |
|
153 * @param fmt the UFormattable object |
|
154 * @param status the error code - any conversion or format errors |
|
155 * @return the value |
|
156 * @draft ICU 52 |
|
157 * @see icu::Formattable::getDouble(UErrorCode&) const |
|
158 */ |
|
159 U_DRAFT double U_EXPORT2 |
|
160 ufmt_getDouble(UFormattable* fmt, UErrorCode *status); |
|
161 |
|
162 /** |
|
163 * Gets the long (int32_t) value of this object. If the magnitude is too |
|
164 * large to fit in a long, then the maximum or minimum long value, |
|
165 * as appropriate, is returned and the status is set to |
|
166 * U_INVALID_FORMAT_ERROR. If this object is of type UFMT_INT64 and |
|
167 * it fits within a long, then no precision is lost. If it is of |
|
168 * type kDouble or kDecimalNumber, then a conversion is peformed, with |
|
169 * truncation of any fractional part. If the type is UFMT_OBJECT and |
|
170 * the object is a Measure, then the result of |
|
171 * getNumber().getLong(status) is returned. If this object is |
|
172 * neither a numeric type nor a Measure, then 0 is returned and |
|
173 * the status is set to U_INVALID_FORMAT_ERROR. |
|
174 * @param fmt the UFormattable object |
|
175 * @param status the error code - any conversion or format errors |
|
176 * @return the value |
|
177 * @draft ICU 52 |
|
178 * @see icu::Formattable::getLong(UErrorCode&) const |
|
179 */ |
|
180 U_DRAFT int32_t U_EXPORT2 |
|
181 ufmt_getLong(UFormattable* fmt, UErrorCode *status); |
|
182 |
|
183 |
|
184 /** |
|
185 * Gets the int64_t value of this object. If this object is of a numeric |
|
186 * type and the magnitude is too large to fit in an int64, then |
|
187 * the maximum or minimum int64 value, as appropriate, is returned |
|
188 * and the status is set to U_INVALID_FORMAT_ERROR. If the |
|
189 * magnitude fits in an int64, then a casting conversion is |
|
190 * peformed, with truncation of any fractional part. If the type |
|
191 * is UFMT_OBJECT and the object is a Measure, then the result of |
|
192 * getNumber().getDouble(status) is returned. If this object is |
|
193 * neither a numeric type nor a Measure, then 0 is returned and |
|
194 * the status is set to U_INVALID_FORMAT_ERROR. |
|
195 * @param fmt the UFormattable object |
|
196 * @param status the error code - any conversion or format errors |
|
197 * @return the value |
|
198 * @draft ICU 52 |
|
199 * @see icu::Formattable::getInt64(UErrorCode&) const |
|
200 */ |
|
201 U_DRAFT int64_t U_EXPORT2 |
|
202 ufmt_getInt64(UFormattable* fmt, UErrorCode *status); |
|
203 |
|
204 /** |
|
205 * Returns a pointer to the UObject contained within this |
|
206 * formattable (as a const void*), or NULL if this object |
|
207 * is not of type UFMT_OBJECT. |
|
208 * @param fmt the UFormattable object |
|
209 * @param status the error code - any conversion or format errors |
|
210 * @return the value as a const void*. It is a polymorphic C++ object. |
|
211 * @draft ICU 52 |
|
212 * @see icu::Formattable::getObject() const |
|
213 */ |
|
214 U_DRAFT const void *U_EXPORT2 |
|
215 ufmt_getObject(const UFormattable* fmt, UErrorCode *status); |
|
216 |
|
217 /** |
|
218 * Gets the string value of this object as a UChar string. If the type is not a |
|
219 * string, status is set to U_INVALID_FORMAT_ERROR and a NULL pointer is returned. |
|
220 * This function is not thread safe and may modify the UFormattable if need be to terminate the string. |
|
221 * The returned pointer is not valid if any other functions are called on this UFormattable, or if the UFormattable is closed. |
|
222 * @param fmt the UFormattable object |
|
223 * @param status the error code - any conversion or format errors |
|
224 * @param len if non null, contains the string length on return |
|
225 * @return the null terminated string value - must not be referenced after any other functions are called on this UFormattable. |
|
226 * @draft ICU 52 |
|
227 * @see icu::Formattable::getString(UnicodeString&)const |
|
228 */ |
|
229 U_DRAFT const UChar* U_EXPORT2 |
|
230 ufmt_getUChars(UFormattable* fmt, int32_t *len, UErrorCode *status); |
|
231 |
|
232 /** |
|
233 * Get the number of array objects contained, if an array type UFMT_ARRAY |
|
234 * @param fmt the UFormattable object |
|
235 * @param status the error code - any conversion or format errors. U_ILLEGAL_ARGUMENT_ERROR if not an array type. |
|
236 * @return the number of array objects or undefined if not an array type |
|
237 * @draft ICU 52 |
|
238 * @see ufmt_getArrayItemByIndex |
|
239 */ |
|
240 U_DRAFT int32_t U_EXPORT2 |
|
241 ufmt_getArrayLength(const UFormattable* fmt, UErrorCode *status); |
|
242 |
|
243 /** |
|
244 * Get the specified value from the array of UFormattables. Invalid if the object is not an array type UFMT_ARRAY |
|
245 * @param fmt the UFormattable object |
|
246 * @param n the number of the array to return (0 based). |
|
247 * @param status the error code - any conversion or format errors. Returns an error if n is out of bounds. |
|
248 * @return the nth array value, only valid while the containing UFormattable is valid. NULL if not an array. |
|
249 * @draft ICU 52 |
|
250 * @see icu::Formattable::getArray(int32_t&, UErrorCode&) const |
|
251 */ |
|
252 U_DRAFT UFormattable * U_EXPORT2 |
|
253 ufmt_getArrayItemByIndex(UFormattable* fmt, int32_t n, UErrorCode *status); |
|
254 |
|
255 /** |
|
256 * Returns a numeric string representation of the number contained within this |
|
257 * formattable, or NULL if this object does not contain numeric type. |
|
258 * For values obtained by parsing, the returned decimal number retains |
|
259 * the full precision and range of the original input, unconstrained by |
|
260 * the limits of a double floating point or a 64 bit int. |
|
261 * |
|
262 * This function is not thread safe, and therfore is not declared const, |
|
263 * even though it is logically const. |
|
264 * The resulting buffer is owned by the UFormattable and is invalid if any other functions are |
|
265 * called on the UFormattable. |
|
266 * |
|
267 * Possible errors include U_MEMORY_ALLOCATION_ERROR, and |
|
268 * U_INVALID_STATE if the formattable object has not been set to |
|
269 * a numeric type. |
|
270 * @param fmt the UFormattable object |
|
271 * @param len if non-null, on exit contains the string length (not including the terminating null) |
|
272 * @param status the error code |
|
273 * @return the character buffer as a NULL terminated string, which is owned by the object and must not be accessed if any other functions are called on this object. |
|
274 * @draft ICU 52 |
|
275 * @see icu::Formattable::getDecimalNumber(UErrorCode&) |
|
276 */ |
|
277 U_DRAFT const char * U_EXPORT2 |
|
278 ufmt_getDecNumChars(UFormattable *fmt, int32_t *len, UErrorCode *status); |
|
279 #endif /* U_HIDE_DRAFT_API */ |
|
280 |
|
281 #endif |
|
282 |
|
283 #endif |