Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:58:50

0001 // © 2016 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 /*
0004 ********************************************************************************
0005 * Copyright (C) 2013-2014, International Business Machines Corporation and others.
0006 * All Rights Reserved.
0007 ********************************************************************************
0008 *
0009 * File UFORMATTABLE.H
0010 *
0011 * Modification History:
0012 *
0013 *   Date        Name        Description
0014 *   2013 Jun 7  srl         New
0015 ********************************************************************************
0016 */
0017 
0018 /**
0019  * \file
0020  * \brief C API: UFormattable is a thin wrapper for primitive types used for formatting and parsing.
0021  *
0022  * This is a C interface to the icu::Formattable class. Static functions on this class convert
0023  * to and from this interface (via reinterpret_cast).  Note that Formattables (and thus UFormattables)
0024  * are mutable, and many operations (even getters) may actually modify the internal state. For this
0025  * reason, UFormattables are not thread safe, and should not be shared between threads.
0026  *
0027  * See {@link unum_parseToUFormattable} for example code.
0028  */
0029 
0030 #ifndef UFORMATTABLE_H
0031 #define UFORMATTABLE_H
0032 
0033 #include "unicode/utypes.h"
0034 
0035 #if !UCONFIG_NO_FORMATTING
0036 
0037 #include "unicode/localpointer.h"
0038 
0039 /**
0040  * Enum designating the type of a UFormattable instance.
0041  * Practically, this indicates which of the getters would return without conversion
0042  * or error.
0043  * @see icu::Formattable::Type
0044  * @stable ICU 52
0045  */
0046 typedef enum UFormattableType {
0047   UFMT_DATE = 0, /**< ufmt_getDate() will return without conversion. @see ufmt_getDate*/
0048   UFMT_DOUBLE,   /**< ufmt_getDouble() will return without conversion.  @see ufmt_getDouble*/
0049   UFMT_LONG,     /**< ufmt_getLong() will return without conversion. @see ufmt_getLong */
0050   UFMT_STRING,   /**< ufmt_getUChars() will return without conversion.  @see ufmt_getUChars*/
0051   UFMT_ARRAY,    /**< ufmt_countArray() and ufmt_getArray() will return the value.  @see ufmt_getArrayItemByIndex */
0052   UFMT_INT64,    /**< ufmt_getInt64() will return without conversion. @see ufmt_getInt64 */
0053   UFMT_OBJECT,   /**< ufmt_getObject() will return without conversion.  @see ufmt_getObject*/
0054 #ifndef U_HIDE_DEPRECATED_API
0055     /**
0056      * One more than the highest normal UFormattableType value.
0057      * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
0058      */
0059     UFMT_COUNT
0060 #endif  /* U_HIDE_DEPRECATED_API */
0061 } UFormattableType;
0062 
0063 
0064 /**
0065  * Opaque type representing various types of data which may be used for formatting
0066  * and parsing operations.
0067  * @see icu::Formattable
0068  * @stable ICU 52
0069  */
0070 typedef void *UFormattable;
0071 
0072 /**
0073  * Initialize a UFormattable, to type UNUM_LONG, value 0
0074  * may return error if memory allocation failed.
0075  * parameter status error code.
0076  * See {@link unum_parseToUFormattable} for example code.
0077  * @stable ICU 52
0078  * @return the new UFormattable
0079  * @see ufmt_close
0080  * @see icu::Formattable::Formattable()
0081  */
0082 U_STABLE UFormattable* U_EXPORT2
0083 ufmt_open(UErrorCode* status);
0084 
0085 /**
0086  * Cleanup any additional memory allocated by this UFormattable.
0087  * @param fmt the formatter
0088  * @stable ICU 52
0089  * @see ufmt_open
0090  */
0091 U_STABLE void U_EXPORT2
0092 ufmt_close(UFormattable* fmt);
0093 
0094 #if U_SHOW_CPLUSPLUS_API
0095 
0096 U_NAMESPACE_BEGIN
0097 
0098 /**
0099  * \class LocalUFormattablePointer
0100  * "Smart pointer" class, closes a UFormattable via ufmt_close().
0101  * For most methods see the LocalPointerBase base class.
0102  *
0103  * @see LocalPointerBase
0104  * @see LocalPointer
0105  * @stable ICU 52
0106  */
0107 U_DEFINE_LOCAL_OPEN_POINTER(LocalUFormattablePointer, UFormattable, ufmt_close);
0108 
0109 U_NAMESPACE_END
0110 
0111 #endif
0112 
0113 /**
0114  * Return the type of this object
0115  * @param fmt the UFormattable object
0116  * @param status status code - U_ILLEGAL_ARGUMENT_ERROR is returned if the UFormattable contains data not supported by
0117  * the API
0118  * @return the value as a UFormattableType
0119  * @see ufmt_isNumeric
0120  * @see icu::Formattable::getType() const
0121  * @stable ICU 52
0122  */
0123 U_STABLE UFormattableType U_EXPORT2
0124 ufmt_getType(const UFormattable* fmt, UErrorCode *status);
0125 
0126 /**
0127  * Return whether the object is numeric.
0128  * @param fmt the UFormattable object
0129  * @return true if the object is a double, long, or int64 value, else false.
0130  * @see ufmt_getType
0131  * @see icu::Formattable::isNumeric() const
0132  * @stable ICU 52
0133  */
0134 U_STABLE UBool U_EXPORT2
0135 ufmt_isNumeric(const UFormattable* fmt);
0136 
0137 /**
0138  * Gets the UDate value of this object.  If the type is not of type UFMT_DATE,
0139  * status is set to U_INVALID_FORMAT_ERROR and the return value is
0140  * undefined.
0141  * @param fmt the UFormattable object
0142  * @param status the error code - any conversion or format errors
0143  * @return the value
0144  * @stable ICU 52
0145  * @see icu::Formattable::getDate(UErrorCode&) const
0146  */
0147 U_STABLE UDate U_EXPORT2
0148 ufmt_getDate(const UFormattable* fmt, UErrorCode *status);
0149 
0150 /**
0151  * Gets the double value of this object. If the type is not a UFMT_DOUBLE, or
0152  * if there are additional significant digits than fit in a double type,
0153  * a conversion is performed with  possible loss of precision.
0154  * If the type is UFMT_OBJECT and the
0155  * object is a Measure, then the result of
0156  * getNumber().getDouble(status) is returned.  If this object is
0157  * neither a numeric type nor a Measure, then 0 is returned and
0158  * the status is set to U_INVALID_FORMAT_ERROR.
0159  * @param fmt the UFormattable object
0160  * @param status the error code - any conversion or format errors
0161  * @return the value
0162  * @stable ICU 52
0163  * @see icu::Formattable::getDouble(UErrorCode&) const
0164  */
0165 U_STABLE double U_EXPORT2
0166 ufmt_getDouble(UFormattable* fmt, UErrorCode *status);
0167 
0168 /**
0169  * Gets the long (int32_t) value of this object. If the magnitude is too
0170  * large to fit in a long, then the maximum or minimum long value,
0171  * as appropriate, is returned and the status is set to
0172  * U_INVALID_FORMAT_ERROR.  If this object is of type UFMT_INT64 and
0173  * it fits within a long, then no precision is lost.  If it is of
0174  * type kDouble or kDecimalNumber, then a conversion is peformed, with
0175  * truncation of any fractional part.  If the type is UFMT_OBJECT and
0176  * the object is a Measure, then the result of
0177  * getNumber().getLong(status) is returned.  If this object is
0178  * neither a numeric type nor a Measure, then 0 is returned and
0179  * the status is set to U_INVALID_FORMAT_ERROR.
0180  * @param fmt the UFormattable object
0181  * @param status the error code - any conversion or format errors
0182  * @return the value
0183  * @stable ICU 52
0184  * @see icu::Formattable::getLong(UErrorCode&) const
0185  */
0186 U_STABLE int32_t U_EXPORT2
0187 ufmt_getLong(UFormattable* fmt, UErrorCode *status);
0188 
0189 
0190 /**
0191  * Gets the int64_t value of this object. If this object is of a numeric
0192  * type and the magnitude is too large to fit in an int64, then
0193  * the maximum or minimum int64 value, as appropriate, is returned
0194  * and the status is set to U_INVALID_FORMAT_ERROR.  If the
0195  * magnitude fits in an int64, then a casting conversion is
0196  * peformed, with truncation of any fractional part.  If the type
0197  * is UFMT_OBJECT and the object is a Measure, then the result of
0198  * getNumber().getDouble(status) is returned.  If this object is
0199  * neither a numeric type nor a Measure, then 0 is returned and
0200  * the status is set to U_INVALID_FORMAT_ERROR.
0201  * @param fmt the UFormattable object
0202  * @param status the error code - any conversion or format errors
0203  * @return the value
0204  * @stable ICU 52
0205  * @see icu::Formattable::getInt64(UErrorCode&) const
0206  */
0207 U_STABLE int64_t U_EXPORT2
0208 ufmt_getInt64(UFormattable* fmt, UErrorCode *status);
0209 
0210 /**
0211  * Returns a pointer to the UObject contained within this
0212  * formattable (as a const void*), or NULL if this object
0213  * is not of type UFMT_OBJECT.
0214  * @param fmt the UFormattable object
0215  * @param status the error code - any conversion or format errors
0216  * @return the value as a const void*. It is a polymorphic C++ object.
0217  * @stable ICU 52
0218  * @see icu::Formattable::getObject() const
0219  */
0220 U_STABLE const void *U_EXPORT2
0221 ufmt_getObject(const UFormattable* fmt, UErrorCode *status);
0222 
0223 /**
0224  * Gets the string value of this object as a UChar string. If the type is not a
0225  * string, status is set to U_INVALID_FORMAT_ERROR and a NULL pointer is returned.
0226  * This function is not thread safe and may modify the UFormattable if need be to terminate the string.
0227  * The returned pointer is not valid if any other functions are called on this UFormattable, or if the UFormattable is closed.
0228  * @param fmt the UFormattable object
0229  * @param status the error code - any conversion or format errors
0230  * @param len if non null, contains the string length on return
0231  * @return the null terminated string value - must not be referenced after any other functions are called on this UFormattable.
0232  * @stable ICU 52
0233  * @see icu::Formattable::getString(UnicodeString&)const
0234  */
0235 U_STABLE const UChar* U_EXPORT2
0236 ufmt_getUChars(UFormattable* fmt, int32_t *len, UErrorCode *status);
0237 
0238 /**
0239  * Get the number of array objects contained, if an array type UFMT_ARRAY
0240  * @param fmt the UFormattable object
0241  * @param status the error code - any conversion or format errors. U_ILLEGAL_ARGUMENT_ERROR if not an array type.
0242  * @return the number of array objects or undefined if not an array type
0243  * @stable ICU 52
0244  * @see ufmt_getArrayItemByIndex
0245  */
0246 U_STABLE int32_t U_EXPORT2
0247 ufmt_getArrayLength(const UFormattable* fmt, UErrorCode *status);
0248 
0249 /**
0250  * Get the specified value from the array of UFormattables. Invalid if the object is not an array type UFMT_ARRAY
0251  * @param fmt the UFormattable object
0252  * @param n the number of the array to return (0 based).
0253  * @param status the error code - any conversion or format errors. Returns an error if n is out of bounds.
0254  * @return the nth array value, only valid while the containing UFormattable is valid. NULL if not an array.
0255  * @stable ICU 52
0256  * @see icu::Formattable::getArray(int32_t&, UErrorCode&) const
0257  */
0258 U_STABLE UFormattable * U_EXPORT2
0259 ufmt_getArrayItemByIndex(UFormattable* fmt, int32_t n, UErrorCode *status);
0260 
0261 /**
0262  * Returns a numeric string representation of the number contained within this
0263  * formattable, or NULL if this object does not contain numeric type.
0264  * For values obtained by parsing, the returned decimal number retains
0265  * the full precision and range of the original input, unconstrained by
0266  * the limits of a double floating point or a 64 bit int.
0267  *
0268  * This function is not thread safe, and therfore is not declared const,
0269  * even though it is logically const.
0270  * The resulting buffer is owned by the UFormattable and is invalid if any other functions are
0271  * called on the UFormattable.
0272  *
0273  * Possible errors include U_MEMORY_ALLOCATION_ERROR, and
0274  * U_INVALID_STATE if the formattable object has not been set to
0275  * a numeric type.
0276  * @param fmt the UFormattable object
0277  * @param len if non-null, on exit contains the string length (not including the terminating null)
0278  * @param status the error code
0279  * @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.
0280  * @stable ICU 52
0281  * @see icu::Formattable::getDecimalNumber(UErrorCode&)
0282  */
0283 U_STABLE const char * U_EXPORT2
0284 ufmt_getDecNumChars(UFormattable *fmt, int32_t *len, UErrorCode *status);
0285 
0286 #endif
0287 
0288 #endif