2 ********************************************************************************
3 * Copyright (C) 1997-2013, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ********************************************************************************
9 * Modification History:
11 * Date Name Description
12 * 02/29/97 aliu Creation.
13 ********************************************************************************
18 #include "unicode/utypes.h"
19 #include "unicode/unistr.h"
20 #include "unicode/stringpiece.h"
24 * \brief C++ API: Formattable is a thin wrapper for primitive numeric types.
27 #if !UCONFIG_NO_FORMATTING
34 #ifndef U_HIDE_INTERNAL_API
36 * \def UNUM_INTERNAL_STACKARRAY_SIZE
39 #if U_PLATFORM == U_PF_OS400
40 #define UNUM_INTERNAL_STACKARRAY_SIZE 144
42 #define UNUM_INTERNAL_STACKARRAY_SIZE 128
44 #endif /* U_HIDE_INTERNAL_API */
47 * Formattable objects can be passed to the Format class or
48 * its subclasses for formatting. Formattable is a thin wrapper
49 * class which interconverts between the primitive numeric types
50 * (double, long, etc.) as well as UDate and UnicodeString.
52 * <p>Internally, a Formattable object is a union of primitive types.
53 * As such, it can only store one flavor of data at a time. To
54 * determine what flavor of data it contains, use the getType method.
56 * <p>As of ICU 3.0, Formattable may also wrap a UObject pointer,
57 * which it owns. This allows an instance of any ICU class to be
58 * encapsulated in a Formattable. For legacy reasons and for
59 * efficiency, primitive numeric types are still stored directly
60 * within a Formattable.
62 * <p>The Formattable class is not suitable for subclassing.
64 class U_I18N_API Formattable
: public UObject
{
67 * This enum is only used to let callers distinguish between
68 * the Formattable(UDate) constructor and the Formattable(double)
69 * constructor; the compiler cannot distinguish the signatures,
70 * since UDate is currently typedefed to be either double or long.
71 * If UDate is changed later to be a bonafide class
72 * or struct, then we no longer need this enum.
75 enum ISDATE
{ kIsDate
};
81 Formattable(); // Type kLong, value 0
84 * Creates a Formattable object with a UDate instance.
85 * @param d the UDate instance.
86 * @param flag the flag to indicate this is a date. Always set it to kIsDate
89 Formattable(UDate d
, ISDATE flag
);
92 * Creates a Formattable object with a double number.
93 * @param d the double number.
96 Formattable(double d
);
99 * Creates a Formattable object with a long number.
100 * @param l the long number.
103 Formattable(int32_t l
);
106 * Creates a Formattable object with an int64_t number
107 * @param ll the int64_t number.
110 Formattable(int64_t ll
);
112 #if !UCONFIG_NO_CONVERSION
114 * Creates a Formattable object with a char string pointer.
115 * Assumes that the char string is null terminated.
116 * @param strToCopy the char string.
119 Formattable(const char* strToCopy
);
123 * Creates a Formattable object of an appropriate numeric type from a
124 * a decimal number in string form. The Formattable will retain the
125 * full precision of the input in decimal format, even when it exceeds
126 * what can be represented by a double of int64_t.
128 * @param number the unformatted (not localized) string representation
129 * of the Decimal number.
130 * @param status the error code. Possible errors include U_INVALID_FORMAT_ERROR
131 * if the format of the string does not conform to that of a
135 Formattable(const StringPiece
&number
, UErrorCode
&status
);
138 * Creates a Formattable object with a UnicodeString object to copy from.
139 * @param strToCopy the UnicodeString string.
142 Formattable(const UnicodeString
& strToCopy
);
145 * Creates a Formattable object with a UnicodeString object to adopt from.
146 * @param strToAdopt the UnicodeString string.
149 Formattable(UnicodeString
* strToAdopt
);
152 * Creates a Formattable object with an array of Formattable objects.
153 * @param arrayToCopy the Formattable object array.
154 * @param count the array count.
157 Formattable(const Formattable
* arrayToCopy
, int32_t count
);
160 * Creates a Formattable object that adopts the given UObject.
161 * @param objectToAdopt the UObject to set this object to
164 Formattable(UObject
* objectToAdopt
);
170 Formattable(const Formattable
&);
173 * Assignment operator.
174 * @param rhs The Formattable object to copy into this object.
177 Formattable
& operator=(const Formattable
&rhs
);
180 * Equality comparison.
181 * @param other the object to be compared with.
182 * @return TRUE if other are equal to this, FALSE otherwise.
185 UBool
operator==(const Formattable
&other
) const;
189 * @param other the object to be compared with.
190 * @return TRUE if other are unequal to this, FALSE otherwise.
193 UBool
operator!=(const Formattable
& other
) const
194 { return !operator==(other
); }
200 virtual ~Formattable();
204 * Clones can be used concurrently in multiple threads.
205 * If an error occurs, then NULL is returned.
206 * The caller must delete the clone.
208 * @return a clone of this object
210 * @see getDynamicClassID
213 Formattable
*clone() const;
216 * Selector for flavor of data type contained within a
217 * Formattable object. Formattable is a union of several
218 * different types, and at any time contains exactly one type.
223 * Selector indicating a UDate value. Use getDate to retrieve
230 * Selector indicating a double value. Use getDouble to
231 * retrieve the value.
237 * Selector indicating a 32-bit integer value. Use getLong to
238 * retrieve the value.
244 * Selector indicating a UnicodeString value. Use getString
245 * to retrieve the value.
251 * Selector indicating an array of Formattables. Use getArray
252 * to retrieve the value.
258 * Selector indicating a 64-bit integer value. Use getInt64
259 * to retrieve the value.
265 * Selector indicating a UObject value. Use getObject to
266 * retrieve the value.
273 * Gets the data type of this Formattable object.
274 * @return the data type of this Formattable object.
277 Type
getType(void) const;
280 * Returns TRUE if the data type of this Formattable object
281 * is kDouble, kLong, kInt64 or kDecimalNumber.
282 * @return TRUE if this is a pure numeric object
285 UBool
isNumeric() const;
288 * Gets the double value of this object. If this object is not of type
289 * kDouble then the result is undefined.
290 * @return the double value of this object.
293 double getDouble(void) const { return fValue
.fDouble
; }
296 * Gets the double value of this object. If this object is of type
297 * long, int64 or Decimal Number then a conversion is peformed, with
298 * possible loss of precision. If the type is kObject and the
299 * object is a Measure, then the result of
300 * getNumber().getDouble(status) is returned. If this object is
301 * neither a numeric type nor a Measure, then 0 is returned and
302 * the status is set to U_INVALID_FORMAT_ERROR.
303 * @param status the error code
304 * @return the double value of this object.
307 double getDouble(UErrorCode
& status
) const;
310 * Gets the long value of this object. If this object is not of type
311 * kLong then the result is undefined.
312 * @return the long value of this object.
315 int32_t getLong(void) const { return (int32_t)fValue
.fInt64
; }
318 * Gets the long value of this object. If the magnitude is too
319 * large to fit in a long, then the maximum or minimum long value,
320 * as appropriate, is returned and the status is set to
321 * U_INVALID_FORMAT_ERROR. If this object is of type kInt64 and
322 * it fits within a long, then no precision is lost. If it is of
323 * type kDouble or kDecimalNumber, then a conversion is peformed, with
324 * truncation of any fractional part. If the type is kObject and
325 * the object is a Measure, then the result of
326 * getNumber().getLong(status) is returned. If this object is
327 * neither a numeric type nor a Measure, then 0 is returned and
328 * the status is set to U_INVALID_FORMAT_ERROR.
329 * @param status the error code
330 * @return the long value of this object.
333 int32_t getLong(UErrorCode
& status
) const;
336 * Gets the int64 value of this object. If this object is not of type
337 * kInt64 then the result is undefined.
338 * @return the int64 value of this object.
341 int64_t getInt64(void) const { return fValue
.fInt64
; }
344 * Gets the int64 value of this object. If this object is of a numeric
345 * type and the magnitude is too large to fit in an int64, then
346 * the maximum or minimum int64 value, as appropriate, is returned
347 * and the status is set to U_INVALID_FORMAT_ERROR. If the
348 * magnitude fits in an int64, then a casting conversion is
349 * peformed, with truncation of any fractional part. If the type
350 * is kObject and the object is a Measure, then the result of
351 * getNumber().getDouble(status) is returned. If this object is
352 * neither a numeric type nor a Measure, then 0 is returned and
353 * the status is set to U_INVALID_FORMAT_ERROR.
354 * @param status the error code
355 * @return the int64 value of this object.
358 int64_t getInt64(UErrorCode
& status
) const;
361 * Gets the Date value of this object. If this object is not of type
362 * kDate then the result is undefined.
363 * @return the Date value of this object.
366 UDate
getDate() const { return fValue
.fDate
; }
369 * Gets the Date value of this object. If the type is not a date,
370 * status is set to U_INVALID_FORMAT_ERROR and the return value is
372 * @param status the error code.
373 * @return the Date value of this object.
376 UDate
getDate(UErrorCode
& status
) const;
379 * Gets the string value of this object. If this object is not of type
380 * kString then the result is undefined.
381 * @param result Output param to receive the Date value of this object.
382 * @return A reference to 'result'.
385 UnicodeString
& getString(UnicodeString
& result
) const
386 { result
=*fValue
.fString
; return result
; }
389 * Gets the string value of this object. If the type is not a
390 * string, status is set to U_INVALID_FORMAT_ERROR and a bogus
391 * string is returned.
392 * @param result Output param to receive the Date value of this object.
393 * @param status the error code.
394 * @return A reference to 'result'.
397 UnicodeString
& getString(UnicodeString
& result
, UErrorCode
& status
) const;
400 * Gets a const reference to the string value of this object. If
401 * this object is not of type kString then the result is
403 * @return a const reference to the string value of this object.
406 inline const UnicodeString
& getString(void) const;
409 * Gets a const reference to the string value of this object. If
410 * the type is not a string, status is set to
411 * U_INVALID_FORMAT_ERROR and the result is a bogus string.
412 * @param status the error code.
413 * @return a const reference to the string value of this object.
416 const UnicodeString
& getString(UErrorCode
& status
) const;
419 * Gets a reference to the string value of this object. If this
420 * object is not of type kString then the result is undefined.
421 * @return a reference to the string value of this object.
424 inline UnicodeString
& getString(void);
427 * Gets a reference to the string value of this object. If the
428 * type is not a string, status is set to U_INVALID_FORMAT_ERROR
429 * and the result is a bogus string.
430 * @param status the error code.
431 * @return a reference to the string value of this object.
434 UnicodeString
& getString(UErrorCode
& status
);
437 * Gets the array value and count of this object. If this object
438 * is not of type kArray then the result is undefined.
439 * @param count fill-in with the count of this object.
440 * @return the array value of this object.
443 const Formattable
* getArray(int32_t& count
) const
444 { count
=fValue
.fArrayAndCount
.fCount
; return fValue
.fArrayAndCount
.fArray
; }
447 * Gets the array value and count of this object. If the type is
448 * not an array, status is set to U_INVALID_FORMAT_ERROR, count is
449 * set to 0, and the result is NULL.
450 * @param count fill-in with the count of this object.
451 * @param status the error code.
452 * @return the array value of this object.
455 const Formattable
* getArray(int32_t& count
, UErrorCode
& status
) const;
458 * Accesses the specified element in the array value of this
459 * Formattable object. If this object is not of type kArray then
460 * the result is undefined.
461 * @param index the specified index.
462 * @return the accessed element in the array.
465 Formattable
& operator[](int32_t index
) { return fValue
.fArrayAndCount
.fArray
[index
]; }
468 * Returns a pointer to the UObject contained within this
469 * formattable, or NULL if this object does not contain a UObject.
470 * @return a UObject pointer, or NULL
473 const UObject
* getObject() const;
476 * Returns a numeric string representation of the number contained within this
477 * formattable, or NULL if this object does not contain numeric type.
478 * For values obtained by parsing, the returned decimal number retains
479 * the full precision and range of the original input, unconstrained by
480 * the limits of a double floating point or a 64 bit int.
482 * This function is not thread safe, and therfore is not declared const,
483 * even though it is logically const.
485 * Possible errors include U_MEMORY_ALLOCATION_ERROR, and
486 * U_INVALID_STATE if the formattable object has not been set to
489 * @param status the error code.
490 * @return the unformatted string representation of a number.
493 StringPiece
getDecimalNumber(UErrorCode
&status
);
496 * Sets the double value of this object and changes the type to
498 * @param d the new double value to be set.
501 void setDouble(double d
);
504 * Sets the long value of this object and changes the type to
506 * @param l the new long value to be set.
509 void setLong(int32_t l
);
512 * Sets the int64 value of this object and changes the type to
514 * @param ll the new int64 value to be set.
517 void setInt64(int64_t ll
);
520 * Sets the Date value of this object and changes the type to
522 * @param d the new Date value to be set.
525 void setDate(UDate d
);
528 * Sets the string value of this object and changes the type to
530 * @param stringToCopy the new string value to be set.
533 void setString(const UnicodeString
& stringToCopy
);
536 * Sets the array value and count of this object and changes the
538 * @param array the array value.
539 * @param count the number of array elements to be copied.
542 void setArray(const Formattable
* array
, int32_t count
);
545 * Sets and adopts the string value and count of this object and
546 * changes the type to kArray.
547 * @param stringToAdopt the new string value to be adopted.
550 void adoptString(UnicodeString
* stringToAdopt
);
553 * Sets and adopts the array value and count of this object and
554 * changes the type to kArray.
557 void adoptArray(Formattable
* array
, int32_t count
);
560 * Sets and adopts the UObject value of this object and changes
561 * the type to kObject. After this call, the caller must not
562 * delete the given object.
563 * @param objectToAdopt the UObject value to be adopted
566 void adoptObject(UObject
* objectToAdopt
);
569 * Sets the the numeric value from a decimal number string, and changes
570 * the type to to a numeric type appropriate for the number.
571 * The syntax of the number is a "numeric string"
572 * as defined in the Decimal Arithmetic Specification, available at
573 * http://speleotrove.com/decimal
574 * The full precision and range of the input number will be retained,
575 * even when it exceeds what can be represented by a double or an int64.
577 * @param numberString a string representation of the unformatted decimal number.
578 * @param status the error code. Set to U_INVALID_FORMAT_ERROR if the
579 * incoming string is not a valid decimal number.
582 void setDecimalNumber(const StringPiece
&numberString
,
586 * ICU "poor man's RTTI", returns a UClassID for the actual class.
590 virtual UClassID
getDynamicClassID() const;
593 * ICU "poor man's RTTI", returns a UClassID for this class.
597 static UClassID U_EXPORT2
getStaticClassID();
599 #ifndef U_HIDE_DEPRECATED_API
601 * Deprecated variant of getLong(UErrorCode&).
602 * @param status the error code
603 * @return the long value of this object.
604 * @deprecated ICU 3.0 use getLong(UErrorCode&) instead
606 inline int32_t getLong(UErrorCode
* status
) const;
607 #endif /* U_HIDE_DEPRECATED_API */
609 #ifndef U_HIDE_INTERNAL_API
611 * Internal function, do not use.
612 * TODO: figure out how to make this be non-public.
613 * NumberFormat::format(Formattable, ...
614 * needs to get at the DigitList, if it exists, for
615 * big decimal formatting.
618 DigitList
*getDigitList() const { return fDecimalNum
;}
623 DigitList
*getInternalDigitList();
626 * Adopt, and set value from, a DigitList
627 * Internal Function, do not use.
628 * @param dl the Digit List to be adopted
631 void adoptDigitList(DigitList
*dl
);
632 #endif /* U_HIDE_INTERNAL_API */
636 * Cleans up the memory for unwanted values. For example, the adopted
637 * string or array objects.
642 * Common initialization, for use by constructors.
646 UnicodeString
* getBogus() const;
650 UnicodeString
* fString
;
660 CharString
*fDecimalStr
;
662 DigitList
*fDecimalNum
;
664 char fStackData
[UNUM_INTERNAL_STACKARRAY_SIZE
]; // must be big enough for DigitList
667 UnicodeString fBogus
; // Bogus string when it's needed.
670 inline UDate
Formattable::getDate(UErrorCode
& status
) const {
671 if (fType
!= kDate
) {
672 if (U_SUCCESS(status
)) {
673 status
= U_INVALID_FORMAT_ERROR
;
680 inline const UnicodeString
& Formattable::getString(void) const {
681 return *fValue
.fString
;
684 inline UnicodeString
& Formattable::getString(void) {
685 return *fValue
.fString
;
688 inline int32_t Formattable::getLong(UErrorCode
* status
) const {
689 return getLong(*status
);
695 #endif /* #if !UCONFIG_NO_FORMATTING */