michael@0: /* michael@0: ******************************************************************************** michael@0: * Copyright (C) 2013, International Business Machines Corporation and others. michael@0: * All Rights Reserved. michael@0: ******************************************************************************** michael@0: * michael@0: * File UFORMATTABLE.H michael@0: * michael@0: * Modification History: michael@0: * michael@0: * Date Name Description michael@0: * 2013 Jun 7 srl New michael@0: ******************************************************************************** michael@0: */ michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C API: UFormattable is a thin wrapper for primitive types used for formatting and parsing. michael@0: * michael@0: * This is a C interface to the icu::Formattable class. Static functions on this class convert michael@0: * to and from this interface (via reinterpret_cast). Note that Formattables (and thus UFormattables) michael@0: * are mutable, and many operations (even getters) may actually modify the internal state. For this michael@0: * reason, UFormattables are not thread safe, and should not be shared between threads. michael@0: * michael@0: * See {@link unum_parseToUFormattable} for example code. michael@0: */ michael@0: michael@0: #ifndef UFORMATTABLE_H michael@0: #define UFORMATTABLE_H michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #ifndef U_HIDE_DRAFT_API michael@0: michael@0: #include "unicode/localpointer.h" michael@0: michael@0: /** michael@0: * Enum designating the type of a UFormattable instance. michael@0: * Practically, this indicates which of the getters would return without conversion michael@0: * or error. michael@0: * @see icu::Formattable::Type michael@0: * @draft ICU 52 michael@0: */ michael@0: typedef enum UFormattableType { michael@0: UFMT_DATE = 0, /**< ufmt_getDate() will return without conversion. @see ufmt_getDate*/ michael@0: UFMT_DOUBLE, /**< ufmt_getDouble() will return without conversion. @see ufmt_getDouble*/ michael@0: UFMT_LONG, /**< ufmt_getLong() will return without conversion. @see ufmt_getLong */ michael@0: UFMT_STRING, /**< ufmt_getUChars() will return without conversion. @see ufmt_getUChars*/ michael@0: UFMT_ARRAY, /**< ufmt_countArray() and ufmt_getArray() will return the value. @see ufmt_getArrayItemByIndex */ michael@0: UFMT_INT64, /**< ufmt_getInt64() will return without conversion. @see ufmt_getInt64 */ michael@0: UFMT_OBJECT, /**< ufmt_getObject() will return without conversion. @see ufmt_getObject*/ michael@0: UFMT_COUNT /**< Count of defined UFormattableType values */ michael@0: } UFormattableType; michael@0: michael@0: michael@0: /** michael@0: * Opaque type representing various types of data which may be used for formatting michael@0: * and parsing operations. michael@0: * @see icu::Formattable michael@0: * @draft ICU 52 michael@0: */ michael@0: typedef void *UFormattable; michael@0: michael@0: /** michael@0: * Initialize a UFormattable, to type UNUM_LONG, value 0 michael@0: * may return error if memory allocation failed. michael@0: * parameter status error code. michael@0: * See {@link unum_parseToUFormattable} for example code. michael@0: * @draft ICU 52 michael@0: * @return the new UFormattable michael@0: * @see ufmt_close michael@0: * @see icu::Formattable::Formattable() michael@0: */ michael@0: U_DRAFT UFormattable* U_EXPORT2 michael@0: ufmt_open(UErrorCode* status); michael@0: michael@0: /** michael@0: * Cleanup any additional memory allocated by this UFormattable. michael@0: * @param fmt the formatter michael@0: * @draft ICU 52 michael@0: * @see ufmt_open michael@0: */ michael@0: U_DRAFT void U_EXPORT2 michael@0: ufmt_close(UFormattable* fmt); michael@0: michael@0: #if U_SHOW_CPLUSPLUS_API michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: /** michael@0: * \class LocalUFormattablePointer michael@0: * "Smart pointer" class, closes a UFormattable via ufmt_close(). michael@0: * For most methods see the LocalPointerBase base class. michael@0: * michael@0: * @see LocalPointerBase michael@0: * @see LocalPointer michael@0: * @draft ICU 52 michael@0: */ michael@0: U_DEFINE_LOCAL_OPEN_POINTER(LocalUFormattablePointer, UFormattable, ufmt_close); michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif michael@0: michael@0: /** michael@0: * Return the type of this object michael@0: * @param fmt the UFormattable object michael@0: * @param status status code - U_ILLEGAL_ARGUMENT_ERROR is returned if the UFormattable contains data not supported by michael@0: * the API michael@0: * @return the value as a UFormattableType michael@0: * @see ufmt_isNumeric michael@0: * @see icu::Formattable::getType() const michael@0: * @draft ICU 52 michael@0: */ michael@0: U_DRAFT UFormattableType U_EXPORT2 michael@0: ufmt_getType(const UFormattable* fmt, UErrorCode *status); michael@0: michael@0: /** michael@0: * Return whether the object is numeric. michael@0: * @param fmt the UFormattable object michael@0: * @return true if the object is a double, long, or int64 value, else false. michael@0: * @see ufmt_getType michael@0: * @see icu::Formattable::isNumeric() const michael@0: * @draft ICU 52 michael@0: */ michael@0: U_DRAFT UBool U_EXPORT2 michael@0: ufmt_isNumeric(const UFormattable* fmt); michael@0: michael@0: /** michael@0: * Gets the UDate value of this object. If the type is not of type UFMT_DATE, michael@0: * status is set to U_INVALID_FORMAT_ERROR and the return value is michael@0: * undefined. michael@0: * @param fmt the UFormattable object michael@0: * @param status the error code - any conversion or format errors michael@0: * @return the value michael@0: * @draft ICU 52 michael@0: * @see icu::Formattable::getDate(UErrorCode&) const michael@0: */ michael@0: U_DRAFT UDate U_EXPORT2 michael@0: ufmt_getDate(const UFormattable* fmt, UErrorCode *status); michael@0: michael@0: /** michael@0: * Gets the double value of this object. If the type is not a UFMT_DOUBLE, or michael@0: * if there are additional significant digits than fit in a double type, michael@0: * a conversion is performed with possible loss of precision. michael@0: * If the type is UFMT_OBJECT and the michael@0: * object is a Measure, then the result of michael@0: * getNumber().getDouble(status) is returned. If this object is michael@0: * neither a numeric type nor a Measure, then 0 is returned and michael@0: * the status is set to U_INVALID_FORMAT_ERROR. michael@0: * @param fmt the UFormattable object michael@0: * @param status the error code - any conversion or format errors michael@0: * @return the value michael@0: * @draft ICU 52 michael@0: * @see icu::Formattable::getDouble(UErrorCode&) const michael@0: */ michael@0: U_DRAFT double U_EXPORT2 michael@0: ufmt_getDouble(UFormattable* fmt, UErrorCode *status); michael@0: michael@0: /** michael@0: * Gets the long (int32_t) value of this object. If the magnitude is too michael@0: * large to fit in a long, then the maximum or minimum long value, michael@0: * as appropriate, is returned and the status is set to michael@0: * U_INVALID_FORMAT_ERROR. If this object is of type UFMT_INT64 and michael@0: * it fits within a long, then no precision is lost. If it is of michael@0: * type kDouble or kDecimalNumber, then a conversion is peformed, with michael@0: * truncation of any fractional part. If the type is UFMT_OBJECT and michael@0: * the object is a Measure, then the result of michael@0: * getNumber().getLong(status) is returned. If this object is michael@0: * neither a numeric type nor a Measure, then 0 is returned and michael@0: * the status is set to U_INVALID_FORMAT_ERROR. michael@0: * @param fmt the UFormattable object michael@0: * @param status the error code - any conversion or format errors michael@0: * @return the value michael@0: * @draft ICU 52 michael@0: * @see icu::Formattable::getLong(UErrorCode&) const michael@0: */ michael@0: U_DRAFT int32_t U_EXPORT2 michael@0: ufmt_getLong(UFormattable* fmt, UErrorCode *status); michael@0: michael@0: michael@0: /** michael@0: * Gets the int64_t value of this object. If this object is of a numeric michael@0: * type and the magnitude is too large to fit in an int64, then michael@0: * the maximum or minimum int64 value, as appropriate, is returned michael@0: * and the status is set to U_INVALID_FORMAT_ERROR. If the michael@0: * magnitude fits in an int64, then a casting conversion is michael@0: * peformed, with truncation of any fractional part. If the type michael@0: * is UFMT_OBJECT and the object is a Measure, then the result of michael@0: * getNumber().getDouble(status) is returned. If this object is michael@0: * neither a numeric type nor a Measure, then 0 is returned and michael@0: * the status is set to U_INVALID_FORMAT_ERROR. michael@0: * @param fmt the UFormattable object michael@0: * @param status the error code - any conversion or format errors michael@0: * @return the value michael@0: * @draft ICU 52 michael@0: * @see icu::Formattable::getInt64(UErrorCode&) const michael@0: */ michael@0: U_DRAFT int64_t U_EXPORT2 michael@0: ufmt_getInt64(UFormattable* fmt, UErrorCode *status); michael@0: michael@0: /** michael@0: * Returns a pointer to the UObject contained within this michael@0: * formattable (as a const void*), or NULL if this object michael@0: * is not of type UFMT_OBJECT. michael@0: * @param fmt the UFormattable object michael@0: * @param status the error code - any conversion or format errors michael@0: * @return the value as a const void*. It is a polymorphic C++ object. michael@0: * @draft ICU 52 michael@0: * @see icu::Formattable::getObject() const michael@0: */ michael@0: U_DRAFT const void *U_EXPORT2 michael@0: ufmt_getObject(const UFormattable* fmt, UErrorCode *status); michael@0: michael@0: /** michael@0: * Gets the string value of this object as a UChar string. If the type is not a michael@0: * string, status is set to U_INVALID_FORMAT_ERROR and a NULL pointer is returned. michael@0: * This function is not thread safe and may modify the UFormattable if need be to terminate the string. michael@0: * The returned pointer is not valid if any other functions are called on this UFormattable, or if the UFormattable is closed. michael@0: * @param fmt the UFormattable object michael@0: * @param status the error code - any conversion or format errors michael@0: * @param len if non null, contains the string length on return michael@0: * @return the null terminated string value - must not be referenced after any other functions are called on this UFormattable. michael@0: * @draft ICU 52 michael@0: * @see icu::Formattable::getString(UnicodeString&)const michael@0: */ michael@0: U_DRAFT const UChar* U_EXPORT2 michael@0: ufmt_getUChars(UFormattable* fmt, int32_t *len, UErrorCode *status); michael@0: michael@0: /** michael@0: * Get the number of array objects contained, if an array type UFMT_ARRAY michael@0: * @param fmt the UFormattable object michael@0: * @param status the error code - any conversion or format errors. U_ILLEGAL_ARGUMENT_ERROR if not an array type. michael@0: * @return the number of array objects or undefined if not an array type michael@0: * @draft ICU 52 michael@0: * @see ufmt_getArrayItemByIndex michael@0: */ michael@0: U_DRAFT int32_t U_EXPORT2 michael@0: ufmt_getArrayLength(const UFormattable* fmt, UErrorCode *status); michael@0: michael@0: /** michael@0: * Get the specified value from the array of UFormattables. Invalid if the object is not an array type UFMT_ARRAY michael@0: * @param fmt the UFormattable object michael@0: * @param n the number of the array to return (0 based). michael@0: * @param status the error code - any conversion or format errors. Returns an error if n is out of bounds. michael@0: * @return the nth array value, only valid while the containing UFormattable is valid. NULL if not an array. michael@0: * @draft ICU 52 michael@0: * @see icu::Formattable::getArray(int32_t&, UErrorCode&) const michael@0: */ michael@0: U_DRAFT UFormattable * U_EXPORT2 michael@0: ufmt_getArrayItemByIndex(UFormattable* fmt, int32_t n, UErrorCode *status); michael@0: michael@0: /** michael@0: * Returns a numeric string representation of the number contained within this michael@0: * formattable, or NULL if this object does not contain numeric type. michael@0: * For values obtained by parsing, the returned decimal number retains michael@0: * the full precision and range of the original input, unconstrained by michael@0: * the limits of a double floating point or a 64 bit int. michael@0: * michael@0: * This function is not thread safe, and therfore is not declared const, michael@0: * even though it is logically const. michael@0: * The resulting buffer is owned by the UFormattable and is invalid if any other functions are michael@0: * called on the UFormattable. michael@0: * michael@0: * Possible errors include U_MEMORY_ALLOCATION_ERROR, and michael@0: * U_INVALID_STATE if the formattable object has not been set to michael@0: * a numeric type. michael@0: * @param fmt the UFormattable object michael@0: * @param len if non-null, on exit contains the string length (not including the terminating null) michael@0: * @param status the error code michael@0: * @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. michael@0: * @draft ICU 52 michael@0: * @see icu::Formattable::getDecimalNumber(UErrorCode&) michael@0: */ michael@0: U_DRAFT const char * U_EXPORT2 michael@0: ufmt_getDecNumChars(UFormattable *fmt, int32_t *len, UErrorCode *status); michael@0: #endif /* U_HIDE_DRAFT_API */ michael@0: michael@0: #endif michael@0: michael@0: #endif