Debian Codegraph @

intl/icu/source/i18n/unicode/uformattable.h

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