jpayne@69: // © 2016 and later: Unicode, Inc. and others. jpayne@69: // License & terms of use: http://www.unicode.org/copyright.html jpayne@69: /* jpayne@69: ******************************************************************************** jpayne@69: * Copyright (C) 1997-2014, International Business Machines jpayne@69: * Corporation and others. All Rights Reserved. jpayne@69: ******************************************************************************** jpayne@69: * jpayne@69: * File FMTABLE.H jpayne@69: * jpayne@69: * Modification History: jpayne@69: * jpayne@69: * Date Name Description jpayne@69: * 02/29/97 aliu Creation. jpayne@69: ******************************************************************************** jpayne@69: */ jpayne@69: #ifndef FMTABLE_H jpayne@69: #define FMTABLE_H jpayne@69: jpayne@69: #include "unicode/utypes.h" jpayne@69: jpayne@69: #if U_SHOW_CPLUSPLUS_API jpayne@69: jpayne@69: /** jpayne@69: * \file jpayne@69: * \brief C++ API: Formattable is a thin wrapper for primitive types used for formatting and parsing jpayne@69: */ jpayne@69: jpayne@69: #if !UCONFIG_NO_FORMATTING jpayne@69: jpayne@69: #include "unicode/unistr.h" jpayne@69: #include "unicode/stringpiece.h" jpayne@69: #include "unicode/uformattable.h" jpayne@69: jpayne@69: U_NAMESPACE_BEGIN jpayne@69: jpayne@69: class CharString; jpayne@69: namespace number { jpayne@69: namespace impl { jpayne@69: class DecimalQuantity; jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: /** jpayne@69: * Formattable objects can be passed to the Format class or jpayne@69: * its subclasses for formatting. Formattable is a thin wrapper jpayne@69: * class which interconverts between the primitive numeric types jpayne@69: * (double, long, etc.) as well as UDate and UnicodeString. jpayne@69: * jpayne@69: *
Internally, a Formattable object is a union of primitive types. jpayne@69: * As such, it can only store one flavor of data at a time. To jpayne@69: * determine what flavor of data it contains, use the getType method. jpayne@69: * jpayne@69: *
As of ICU 3.0, Formattable may also wrap a UObject pointer, jpayne@69: * which it owns. This allows an instance of any ICU class to be jpayne@69: * encapsulated in a Formattable. For legacy reasons and for jpayne@69: * efficiency, primitive numeric types are still stored directly jpayne@69: * within a Formattable. jpayne@69: * jpayne@69: *
The Formattable class is not suitable for subclassing. jpayne@69: * jpayne@69: *
See UFormattable for a C wrapper.
jpayne@69: */
jpayne@69: class U_I18N_API Formattable : public UObject {
jpayne@69: public:
jpayne@69: /**
jpayne@69: * This enum is only used to let callers distinguish between
jpayne@69: * the Formattable(UDate) constructor and the Formattable(double)
jpayne@69: * constructor; the compiler cannot distinguish the signatures,
jpayne@69: * since UDate is currently typedefed to be either double or long.
jpayne@69: * If UDate is changed later to be a bonafide class
jpayne@69: * or struct, then we no longer need this enum.
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: enum ISDATE { kIsDate };
jpayne@69:
jpayne@69: /**
jpayne@69: * Default constructor
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: Formattable(); // Type kLong, value 0
jpayne@69:
jpayne@69: /**
jpayne@69: * Creates a Formattable object with a UDate instance.
jpayne@69: * @param d the UDate instance.
jpayne@69: * @param flag the flag to indicate this is a date. Always set it to kIsDate
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: Formattable(UDate d, ISDATE flag);
jpayne@69:
jpayne@69: /**
jpayne@69: * Creates a Formattable object with a double number.
jpayne@69: * @param d the double number.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: Formattable(double d);
jpayne@69:
jpayne@69: /**
jpayne@69: * Creates a Formattable object with a long number.
jpayne@69: * @param l the long number.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: Formattable(int32_t l);
jpayne@69:
jpayne@69: /**
jpayne@69: * Creates a Formattable object with an int64_t number
jpayne@69: * @param ll the int64_t number.
jpayne@69: * @stable ICU 2.8
jpayne@69: */
jpayne@69: Formattable(int64_t ll);
jpayne@69:
jpayne@69: #if !UCONFIG_NO_CONVERSION
jpayne@69: /**
jpayne@69: * Creates a Formattable object with a char string pointer.
jpayne@69: * Assumes that the char string is null terminated.
jpayne@69: * @param strToCopy the char string.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: Formattable(const char* strToCopy);
jpayne@69: #endif
jpayne@69:
jpayne@69: /**
jpayne@69: * Creates a Formattable object of an appropriate numeric type from a
jpayne@69: * a decimal number in string form. The Formattable will retain the
jpayne@69: * full precision of the input in decimal format, even when it exceeds
jpayne@69: * what can be represented by a double or int64_t.
jpayne@69: *
jpayne@69: * @param number the unformatted (not localized) string representation
jpayne@69: * of the Decimal number.
jpayne@69: * @param status the error code. Possible errors include U_INVALID_FORMAT_ERROR
jpayne@69: * if the format of the string does not conform to that of a
jpayne@69: * decimal number.
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: Formattable(StringPiece number, UErrorCode &status);
jpayne@69:
jpayne@69: /**
jpayne@69: * Creates a Formattable object with a UnicodeString object to copy from.
jpayne@69: * @param strToCopy the UnicodeString string.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: Formattable(const UnicodeString& strToCopy);
jpayne@69:
jpayne@69: /**
jpayne@69: * Creates a Formattable object with a UnicodeString object to adopt from.
jpayne@69: * @param strToAdopt the UnicodeString string.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: Formattable(UnicodeString* strToAdopt);
jpayne@69:
jpayne@69: /**
jpayne@69: * Creates a Formattable object with an array of Formattable objects.
jpayne@69: * @param arrayToCopy the Formattable object array.
jpayne@69: * @param count the array count.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: Formattable(const Formattable* arrayToCopy, int32_t count);
jpayne@69:
jpayne@69: /**
jpayne@69: * Creates a Formattable object that adopts the given UObject.
jpayne@69: * @param objectToAdopt the UObject to set this object to
jpayne@69: * @stable ICU 3.0
jpayne@69: */
jpayne@69: Formattable(UObject* objectToAdopt);
jpayne@69:
jpayne@69: /**
jpayne@69: * Copy constructor.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: Formattable(const Formattable&);
jpayne@69:
jpayne@69: /**
jpayne@69: * Assignment operator.
jpayne@69: * @param rhs The Formattable object to copy into this object.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: Formattable& operator=(const Formattable &rhs);
jpayne@69:
jpayne@69: /**
jpayne@69: * Equality comparison.
jpayne@69: * @param other the object to be compared with.
jpayne@69: * @return TRUE if other are equal to this, FALSE otherwise.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: UBool operator==(const Formattable &other) const;
jpayne@69:
jpayne@69: /**
jpayne@69: * Equality operator.
jpayne@69: * @param other the object to be compared with.
jpayne@69: * @return TRUE if other are unequal to this, FALSE otherwise.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: UBool operator!=(const Formattable& other) const
jpayne@69: { return !operator==(other); }
jpayne@69:
jpayne@69: /**
jpayne@69: * Destructor.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: virtual ~Formattable();
jpayne@69:
jpayne@69: /**
jpayne@69: * Clone this object.
jpayne@69: * Clones can be used concurrently in multiple threads.
jpayne@69: * If an error occurs, then NULL is returned.
jpayne@69: * The caller must delete the clone.
jpayne@69: *
jpayne@69: * @return a clone of this object
jpayne@69: *
jpayne@69: * @see getDynamicClassID
jpayne@69: * @stable ICU 2.8
jpayne@69: */
jpayne@69: Formattable *clone() const;
jpayne@69:
jpayne@69: /**
jpayne@69: * Selector for flavor of data type contained within a
jpayne@69: * Formattable object. Formattable is a union of several
jpayne@69: * different types, and at any time contains exactly one type.
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: enum Type {
jpayne@69: /**
jpayne@69: * Selector indicating a UDate value. Use getDate to retrieve
jpayne@69: * the value.
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: kDate,
jpayne@69:
jpayne@69: /**
jpayne@69: * Selector indicating a double value. Use getDouble to
jpayne@69: * retrieve the value.
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: kDouble,
jpayne@69:
jpayne@69: /**
jpayne@69: * Selector indicating a 32-bit integer value. Use getLong to
jpayne@69: * retrieve the value.
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: kLong,
jpayne@69:
jpayne@69: /**
jpayne@69: * Selector indicating a UnicodeString value. Use getString
jpayne@69: * to retrieve the value.
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: kString,
jpayne@69:
jpayne@69: /**
jpayne@69: * Selector indicating an array of Formattables. Use getArray
jpayne@69: * to retrieve the value.
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: kArray,
jpayne@69:
jpayne@69: /**
jpayne@69: * Selector indicating a 64-bit integer value. Use getInt64
jpayne@69: * to retrieve the value.
jpayne@69: * @stable ICU 2.8
jpayne@69: */
jpayne@69: kInt64,
jpayne@69:
jpayne@69: /**
jpayne@69: * Selector indicating a UObject value. Use getObject to
jpayne@69: * retrieve the value.
jpayne@69: * @stable ICU 3.0
jpayne@69: */
jpayne@69: kObject
jpayne@69: };
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets the data type of this Formattable object.
jpayne@69: * @return the data type of this Formattable object.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: Type getType(void) const;
jpayne@69:
jpayne@69: /**
jpayne@69: * Returns TRUE if the data type of this Formattable object
jpayne@69: * is kDouble, kLong, or kInt64
jpayne@69: * @return TRUE if this is a pure numeric object
jpayne@69: * @stable ICU 3.0
jpayne@69: */
jpayne@69: UBool isNumeric() const;
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets the double value of this object. If this object is not of type
jpayne@69: * kDouble then the result is undefined.
jpayne@69: * @return the double value of this object.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: double getDouble(void) const { return fValue.fDouble; }
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets the double value of this object. If this object is of type
jpayne@69: * long, int64 or Decimal Number then a conversion is peformed, with
jpayne@69: * possible loss of precision. If the type is kObject and the
jpayne@69: * object is a Measure, then the result of
jpayne@69: * getNumber().getDouble(status) is returned. If this object is
jpayne@69: * neither a numeric type nor a Measure, then 0 is returned and
jpayne@69: * the status is set to U_INVALID_FORMAT_ERROR.
jpayne@69: * @param status the error code
jpayne@69: * @return the double value of this object.
jpayne@69: * @stable ICU 3.0
jpayne@69: */
jpayne@69: double getDouble(UErrorCode& status) const;
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets the long value of this object. If this object is not of type
jpayne@69: * kLong then the result is undefined.
jpayne@69: * @return the long value of this object.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: int32_t getLong(void) const { return (int32_t)fValue.fInt64; }
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets the long value of this object. If the magnitude is too
jpayne@69: * large to fit in a long, then the maximum or minimum long value,
jpayne@69: * as appropriate, is returned and the status is set to
jpayne@69: * U_INVALID_FORMAT_ERROR. If this object is of type kInt64 and
jpayne@69: * it fits within a long, then no precision is lost. If it is of
jpayne@69: * type kDouble, then a conversion is peformed, with
jpayne@69: * truncation of any fractional part. If the type is kObject and
jpayne@69: * the object is a Measure, then the result of
jpayne@69: * getNumber().getLong(status) is returned. If this object is
jpayne@69: * neither a numeric type nor a Measure, then 0 is returned and
jpayne@69: * the status is set to U_INVALID_FORMAT_ERROR.
jpayne@69: * @param status the error code
jpayne@69: * @return the long value of this object.
jpayne@69: * @stable ICU 3.0
jpayne@69: */
jpayne@69: int32_t getLong(UErrorCode& status) const;
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets the int64 value of this object. If this object is not of type
jpayne@69: * kInt64 then the result is undefined.
jpayne@69: * @return the int64 value of this object.
jpayne@69: * @stable ICU 2.8
jpayne@69: */
jpayne@69: int64_t getInt64(void) const { return fValue.fInt64; }
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets the int64 value of this object. If this object is of a numeric
jpayne@69: * type and the magnitude is too large to fit in an int64, then
jpayne@69: * the maximum or minimum int64 value, as appropriate, is returned
jpayne@69: * and the status is set to U_INVALID_FORMAT_ERROR. If the
jpayne@69: * magnitude fits in an int64, then a casting conversion is
jpayne@69: * peformed, with truncation of any fractional part. If the type
jpayne@69: * is kObject and the object is a Measure, then the result of
jpayne@69: * getNumber().getDouble(status) is returned. If this object is
jpayne@69: * neither a numeric type nor a Measure, then 0 is returned and
jpayne@69: * the status is set to U_INVALID_FORMAT_ERROR.
jpayne@69: * @param status the error code
jpayne@69: * @return the int64 value of this object.
jpayne@69: * @stable ICU 3.0
jpayne@69: */
jpayne@69: int64_t getInt64(UErrorCode& status) const;
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets the Date value of this object. If this object is not of type
jpayne@69: * kDate then the result is undefined.
jpayne@69: * @return the Date value of this object.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: UDate getDate() const { return fValue.fDate; }
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets the Date value of this object. If the type is not a date,
jpayne@69: * status is set to U_INVALID_FORMAT_ERROR and the return value is
jpayne@69: * undefined.
jpayne@69: * @param status the error code.
jpayne@69: * @return the Date value of this object.
jpayne@69: * @stable ICU 3.0
jpayne@69: */
jpayne@69: UDate getDate(UErrorCode& status) const;
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets the string value of this object. If this object is not of type
jpayne@69: * kString then the result is undefined.
jpayne@69: * @param result Output param to receive the Date value of this object.
jpayne@69: * @return A reference to 'result'.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: UnicodeString& getString(UnicodeString& result) const
jpayne@69: { result=*fValue.fString; return result; }
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets the string value of this object. If the type is not a
jpayne@69: * string, status is set to U_INVALID_FORMAT_ERROR and a bogus
jpayne@69: * string is returned.
jpayne@69: * @param result Output param to receive the Date value of this object.
jpayne@69: * @param status the error code.
jpayne@69: * @return A reference to 'result'.
jpayne@69: * @stable ICU 3.0
jpayne@69: */
jpayne@69: UnicodeString& getString(UnicodeString& result, UErrorCode& status) const;
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets a const reference to the string value of this object. If
jpayne@69: * this object is not of type kString then the result is
jpayne@69: * undefined.
jpayne@69: * @return a const reference to the string value of this object.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: inline const UnicodeString& getString(void) const;
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets a const reference to the string value of this object. If
jpayne@69: * the type is not a string, status is set to
jpayne@69: * U_INVALID_FORMAT_ERROR and the result is a bogus string.
jpayne@69: * @param status the error code.
jpayne@69: * @return a const reference to the string value of this object.
jpayne@69: * @stable ICU 3.0
jpayne@69: */
jpayne@69: const UnicodeString& getString(UErrorCode& status) const;
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets a reference to the string value of this object. If this
jpayne@69: * object is not of type kString then the result is undefined.
jpayne@69: * @return a reference to the string value of this object.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: inline UnicodeString& getString(void);
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets a reference to the string value of this object. If the
jpayne@69: * type is not a string, status is set to U_INVALID_FORMAT_ERROR
jpayne@69: * and the result is a bogus string.
jpayne@69: * @param status the error code.
jpayne@69: * @return a reference to the string value of this object.
jpayne@69: * @stable ICU 3.0
jpayne@69: */
jpayne@69: UnicodeString& getString(UErrorCode& status);
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets the array value and count of this object. If this object
jpayne@69: * is not of type kArray then the result is undefined.
jpayne@69: * @param count fill-in with the count of this object.
jpayne@69: * @return the array value of this object.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: const Formattable* getArray(int32_t& count) const
jpayne@69: { count=fValue.fArrayAndCount.fCount; return fValue.fArrayAndCount.fArray; }
jpayne@69:
jpayne@69: /**
jpayne@69: * Gets the array value and count of this object. If the type is
jpayne@69: * not an array, status is set to U_INVALID_FORMAT_ERROR, count is
jpayne@69: * set to 0, and the result is NULL.
jpayne@69: * @param count fill-in with the count of this object.
jpayne@69: * @param status the error code.
jpayne@69: * @return the array value of this object.
jpayne@69: * @stable ICU 3.0
jpayne@69: */
jpayne@69: const Formattable* getArray(int32_t& count, UErrorCode& status) const;
jpayne@69:
jpayne@69: /**
jpayne@69: * Accesses the specified element in the array value of this
jpayne@69: * Formattable object. If this object is not of type kArray then
jpayne@69: * the result is undefined.
jpayne@69: * @param index the specified index.
jpayne@69: * @return the accessed element in the array.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: Formattable& operator[](int32_t index) { return fValue.fArrayAndCount.fArray[index]; }
jpayne@69:
jpayne@69: /**
jpayne@69: * Returns a pointer to the UObject contained within this
jpayne@69: * formattable, or NULL if this object does not contain a UObject.
jpayne@69: * @return a UObject pointer, or NULL
jpayne@69: * @stable ICU 3.0
jpayne@69: */
jpayne@69: const UObject* getObject() const;
jpayne@69:
jpayne@69: /**
jpayne@69: * Returns a numeric string representation of the number contained within this
jpayne@69: * formattable, or NULL if this object does not contain numeric type.
jpayne@69: * For values obtained by parsing, the returned decimal number retains
jpayne@69: * the full precision and range of the original input, unconstrained by
jpayne@69: * the limits of a double floating point or a 64 bit int.
jpayne@69: *
jpayne@69: * This function is not thread safe, and therfore is not declared const,
jpayne@69: * even though it is logically const.
jpayne@69: *
jpayne@69: * Possible errors include U_MEMORY_ALLOCATION_ERROR, and
jpayne@69: * U_INVALID_STATE if the formattable object has not been set to
jpayne@69: * a numeric type.
jpayne@69: *
jpayne@69: * @param status the error code.
jpayne@69: * @return the unformatted string representation of a number.
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: StringPiece getDecimalNumber(UErrorCode &status);
jpayne@69:
jpayne@69: /**
jpayne@69: * Sets the double value of this object and changes the type to
jpayne@69: * kDouble.
jpayne@69: * @param d the new double value to be set.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: void setDouble(double d);
jpayne@69:
jpayne@69: /**
jpayne@69: * Sets the long value of this object and changes the type to
jpayne@69: * kLong.
jpayne@69: * @param l the new long value to be set.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: void setLong(int32_t l);
jpayne@69:
jpayne@69: /**
jpayne@69: * Sets the int64 value of this object and changes the type to
jpayne@69: * kInt64.
jpayne@69: * @param ll the new int64 value to be set.
jpayne@69: * @stable ICU 2.8
jpayne@69: */
jpayne@69: void setInt64(int64_t ll);
jpayne@69:
jpayne@69: /**
jpayne@69: * Sets the Date value of this object and changes the type to
jpayne@69: * kDate.
jpayne@69: * @param d the new Date value to be set.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: void setDate(UDate d);
jpayne@69:
jpayne@69: /**
jpayne@69: * Sets the string value of this object and changes the type to
jpayne@69: * kString.
jpayne@69: * @param stringToCopy the new string value to be set.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: void setString(const UnicodeString& stringToCopy);
jpayne@69:
jpayne@69: /**
jpayne@69: * Sets the array value and count of this object and changes the
jpayne@69: * type to kArray.
jpayne@69: * @param array the array value.
jpayne@69: * @param count the number of array elements to be copied.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: void setArray(const Formattable* array, int32_t count);
jpayne@69:
jpayne@69: /**
jpayne@69: * Sets and adopts the string value and count of this object and
jpayne@69: * changes the type to kArray.
jpayne@69: * @param stringToAdopt the new string value to be adopted.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: void adoptString(UnicodeString* stringToAdopt);
jpayne@69:
jpayne@69: /**
jpayne@69: * Sets and adopts the array value and count of this object and
jpayne@69: * changes the type to kArray.
jpayne@69: * @stable ICU 2.0
jpayne@69: */
jpayne@69: void adoptArray(Formattable* array, int32_t count);
jpayne@69:
jpayne@69: /**
jpayne@69: * Sets and adopts the UObject value of this object and changes
jpayne@69: * the type to kObject. After this call, the caller must not
jpayne@69: * delete the given object.
jpayne@69: * @param objectToAdopt the UObject value to be adopted
jpayne@69: * @stable ICU 3.0
jpayne@69: */
jpayne@69: void adoptObject(UObject* objectToAdopt);
jpayne@69:
jpayne@69: /**
jpayne@69: * Sets the the numeric value from a decimal number string, and changes
jpayne@69: * the type to to a numeric type appropriate for the number.
jpayne@69: * The syntax of the number is a "numeric string"
jpayne@69: * as defined in the Decimal Arithmetic Specification, available at
jpayne@69: * http://speleotrove.com/decimal
jpayne@69: * The full precision and range of the input number will be retained,
jpayne@69: * even when it exceeds what can be represented by a double or an int64.
jpayne@69: *
jpayne@69: * @param numberString a string representation of the unformatted decimal number.
jpayne@69: * @param status the error code. Set to U_INVALID_FORMAT_ERROR if the
jpayne@69: * incoming string is not a valid decimal number.
jpayne@69: * @stable ICU 4.4
jpayne@69: */
jpayne@69: void setDecimalNumber(StringPiece numberString,
jpayne@69: UErrorCode &status);
jpayne@69:
jpayne@69: /**
jpayne@69: * ICU "poor man's RTTI", returns a UClassID for the actual class.
jpayne@69: *
jpayne@69: * @stable ICU 2.2
jpayne@69: */
jpayne@69: virtual UClassID getDynamicClassID() const;
jpayne@69:
jpayne@69: /**
jpayne@69: * ICU "poor man's RTTI", returns a UClassID for this class.
jpayne@69: *
jpayne@69: * @stable ICU 2.2
jpayne@69: */
jpayne@69: static UClassID U_EXPORT2 getStaticClassID();
jpayne@69:
jpayne@69: /**
jpayne@69: * Convert the UFormattable to a Formattable. Internally, this is a reinterpret_cast.
jpayne@69: * @param fmt a valid UFormattable
jpayne@69: * @return the UFormattable as a Formattable object pointer. This is an alias to the original
jpayne@69: * UFormattable, and so is only valid while the original argument remains in scope.
jpayne@69: * @stable ICU 52
jpayne@69: */
jpayne@69: static inline Formattable *fromUFormattable(UFormattable *fmt);
jpayne@69:
jpayne@69: /**
jpayne@69: * Convert the const UFormattable to a const Formattable. Internally, this is a reinterpret_cast.
jpayne@69: * @param fmt a valid UFormattable
jpayne@69: * @return the UFormattable as a Formattable object pointer. This is an alias to the original
jpayne@69: * UFormattable, and so is only valid while the original argument remains in scope.
jpayne@69: * @stable ICU 52
jpayne@69: */
jpayne@69: static inline const Formattable *fromUFormattable(const UFormattable *fmt);
jpayne@69:
jpayne@69: /**
jpayne@69: * Convert this object pointer to a UFormattable.
jpayne@69: * @return this object as a UFormattable pointer. This is an alias to this object,
jpayne@69: * and so is only valid while this object remains in scope.
jpayne@69: * @stable ICU 52
jpayne@69: */
jpayne@69: inline UFormattable *toUFormattable();
jpayne@69:
jpayne@69: /**
jpayne@69: * Convert this object pointer to a UFormattable.
jpayne@69: * @return this object as a UFormattable pointer. This is an alias to this object,
jpayne@69: * and so is only valid while this object remains in scope.
jpayne@69: * @stable ICU 52
jpayne@69: */
jpayne@69: inline const UFormattable *toUFormattable() const;
jpayne@69:
jpayne@69: #ifndef U_HIDE_DEPRECATED_API
jpayne@69: /**
jpayne@69: * Deprecated variant of getLong(UErrorCode&).
jpayne@69: * @param status the error code
jpayne@69: * @return the long value of this object.
jpayne@69: * @deprecated ICU 3.0 use getLong(UErrorCode&) instead
jpayne@69: */
jpayne@69: inline int32_t getLong(UErrorCode* status) const;
jpayne@69: #endif /* U_HIDE_DEPRECATED_API */
jpayne@69:
jpayne@69: #ifndef U_HIDE_INTERNAL_API
jpayne@69: /**
jpayne@69: * Internal function, do not use.
jpayne@69: * TODO: figure out how to make this be non-public.
jpayne@69: * NumberFormat::format(Formattable, ...
jpayne@69: * needs to get at the DecimalQuantity, if it exists, for
jpayne@69: * big decimal formatting.
jpayne@69: * @internal
jpayne@69: */
jpayne@69: number::impl::DecimalQuantity *getDecimalQuantity() const { return fDecimalQuantity;}
jpayne@69:
jpayne@69: /**
jpayne@69: * Export the value of this Formattable to a DecimalQuantity.
jpayne@69: * @internal
jpayne@69: */
jpayne@69: void populateDecimalQuantity(number::impl::DecimalQuantity& output, UErrorCode& status) const;
jpayne@69:
jpayne@69: /**
jpayne@69: * Adopt, and set value from, a DecimalQuantity
jpayne@69: * Internal Function, do not use.
jpayne@69: * @param dq the DecimalQuantity to be adopted
jpayne@69: * @internal
jpayne@69: */
jpayne@69: void adoptDecimalQuantity(number::impl::DecimalQuantity *dq);
jpayne@69:
jpayne@69: /**
jpayne@69: * Internal function to return the CharString pointer.
jpayne@69: * @param status error code
jpayne@69: * @return pointer to the CharString - may become invalid if the object is modified
jpayne@69: * @internal
jpayne@69: */
jpayne@69: CharString *internalGetCharString(UErrorCode &status);
jpayne@69:
jpayne@69: #endif /* U_HIDE_INTERNAL_API */
jpayne@69:
jpayne@69: private:
jpayne@69: /**
jpayne@69: * Cleans up the memory for unwanted values. For example, the adopted
jpayne@69: * string or array objects.
jpayne@69: */
jpayne@69: void dispose(void);
jpayne@69:
jpayne@69: /**
jpayne@69: * Common initialization, for use by constructors.
jpayne@69: */
jpayne@69: void init();
jpayne@69:
jpayne@69: UnicodeString* getBogus() const;
jpayne@69:
jpayne@69: union {
jpayne@69: UObject* fObject;
jpayne@69: UnicodeString* fString;
jpayne@69: double fDouble;
jpayne@69: int64_t fInt64;
jpayne@69: UDate fDate;
jpayne@69: struct {
jpayne@69: Formattable* fArray;
jpayne@69: int32_t fCount;
jpayne@69: } fArrayAndCount;
jpayne@69: } fValue;
jpayne@69:
jpayne@69: CharString *fDecimalStr;
jpayne@69:
jpayne@69: number::impl::DecimalQuantity *fDecimalQuantity;
jpayne@69:
jpayne@69: Type fType;
jpayne@69: UnicodeString fBogus; // Bogus string when it's needed.
jpayne@69: };
jpayne@69:
jpayne@69: inline UDate Formattable::getDate(UErrorCode& status) const {
jpayne@69: if (fType != kDate) {
jpayne@69: if (U_SUCCESS(status)) {
jpayne@69: status = U_INVALID_FORMAT_ERROR;
jpayne@69: }
jpayne@69: return 0;
jpayne@69: }
jpayne@69: return fValue.fDate;
jpayne@69: }
jpayne@69:
jpayne@69: inline const UnicodeString& Formattable::getString(void) const {
jpayne@69: return *fValue.fString;
jpayne@69: }
jpayne@69:
jpayne@69: inline UnicodeString& Formattable::getString(void) {
jpayne@69: return *fValue.fString;
jpayne@69: }
jpayne@69:
jpayne@69: #ifndef U_HIDE_DEPRECATED_API
jpayne@69: inline int32_t Formattable::getLong(UErrorCode* status) const {
jpayne@69: return getLong(*status);
jpayne@69: }
jpayne@69: #endif /* U_HIDE_DEPRECATED_API */
jpayne@69:
jpayne@69: inline UFormattable* Formattable::toUFormattable() {
jpayne@69: return reinterpret_cast