2 ********************************************************************************
3 * Copyright (C) 1997-2014, 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"
22 * \brief C++ API: Formattable is a thin wrapper for primitive types used for formatting and parsing
25 #if !UCONFIG_NO_FORMATTING
27 #include "unicode/unistr.h"
28 #include "unicode/stringpiece.h"
29 #include "unicode/uformattable.h"
37 * \def UNUM_INTERNAL_STACKARRAY_SIZE
40 #if U_PLATFORM == U_PF_OS400
41 #define UNUM_INTERNAL_STACKARRAY_SIZE 144
43 #define UNUM_INTERNAL_STACKARRAY_SIZE 128
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 * <p>See UFormattable for a C wrapper.
66 class U_I18N_API Formattable
: public UObject
{
69 * This enum is only used to let callers distinguish between
70 * the Formattable(UDate) constructor and the Formattable(double)
71 * constructor; the compiler cannot distinguish the signatures,
72 * since UDate is currently typedefed to be either double or long.
73 * If UDate is changed later to be a bonafide class
74 * or struct, then we no longer need this enum.
77 enum ISDATE
{ kIsDate
};
83 Formattable(); // Type kLong, value 0
86 * Creates a Formattable object with a UDate instance.
87 * @param d the UDate instance.
88 * @param flag the flag to indicate this is a date. Always set it to kIsDate
91 Formattable(UDate d
, ISDATE flag
);
94 * Creates a Formattable object with a double number.
95 * @param d the double number.
98 Formattable(double d
);
101 * Creates a Formattable object with a long number.
102 * @param l the long number.
105 Formattable(int32_t l
);
108 * Creates a Formattable object with an int64_t number
109 * @param ll the int64_t number.
112 Formattable(int64_t ll
);
114 #if !UCONFIG_NO_CONVERSION
116 * Creates a Formattable object with a char string pointer.
117 * Assumes that the char string is null terminated.
118 * @param strToCopy the char string.
121 Formattable(const char* strToCopy
);
125 * Creates a Formattable object of an appropriate numeric type from a
126 * a decimal number in string form. The Formattable will retain the
127 * full precision of the input in decimal format, even when it exceeds
128 * what can be represented by a double or int64_t.
130 * @param number the unformatted (not localized) string representation
131 * of the Decimal number.
132 * @param status the error code. Possible errors include U_INVALID_FORMAT_ERROR
133 * if the format of the string does not conform to that of a
137 Formattable(const StringPiece
&number
, UErrorCode
&status
);
140 * Creates a Formattable object with a UnicodeString object to copy from.
141 * @param strToCopy the UnicodeString string.
144 Formattable(const UnicodeString
& strToCopy
);
147 * Creates a Formattable object with a UnicodeString object to adopt from.
148 * @param strToAdopt the UnicodeString string.
151 Formattable(UnicodeString
* strToAdopt
);
154 * Creates a Formattable object with an array of Formattable objects.
155 * @param arrayToCopy the Formattable object array.
156 * @param count the array count.
159 Formattable(const Formattable
* arrayToCopy
, int32_t count
);
162 * Creates a Formattable object that adopts the given UObject.
163 * @param objectToAdopt the UObject to set this object to
166 Formattable(UObject
* objectToAdopt
);
172 Formattable(const Formattable
&);
175 * Assignment operator.
176 * @param rhs The Formattable object to copy into this object.
179 Formattable
& operator=(const Formattable
&rhs
);
182 * Equality comparison.
183 * @param other the object to be compared with.
184 * @return TRUE if other are equal to this, FALSE otherwise.
187 UBool
operator==(const Formattable
&other
) const;
191 * @param other the object to be compared with.
192 * @return TRUE if other are unequal to this, FALSE otherwise.
195 UBool
operator!=(const Formattable
& other
) const
196 { return !operator==(other
); }
202 virtual ~Formattable();
206 * Clones can be used concurrently in multiple threads.
207 * If an error occurs, then NULL is returned.
208 * The caller must delete the clone.
210 * @return a clone of this object
212 * @see getDynamicClassID
215 Formattable
*clone() const;
218 * Selector for flavor of data type contained within a
219 * Formattable object. Formattable is a union of several
220 * different types, and at any time contains exactly one type.
225 * Selector indicating a UDate value. Use getDate to retrieve
232 * Selector indicating a double value. Use getDouble to
233 * retrieve the value.
239 * Selector indicating a 32-bit integer value. Use getLong to
240 * retrieve the value.
246 * Selector indicating a UnicodeString value. Use getString
247 * to retrieve the value.
253 * Selector indicating an array of Formattables. Use getArray
254 * to retrieve the value.
260 * Selector indicating a 64-bit integer value. Use getInt64
261 * to retrieve the value.
267 * Selector indicating a UObject value. Use getObject to
268 * retrieve the value.
275 * Gets the data type of this Formattable object.
276 * @return the data type of this Formattable object.
279 Type
getType(void) const;
282 * Returns TRUE if the data type of this Formattable object
283 * is kDouble, kLong, or kInt64
284 * @return TRUE if this is a pure numeric object
287 UBool
isNumeric() const;
290 * Gets the double value of this object. If this object is not of type
291 * kDouble then the result is undefined.
292 * @return the double value of this object.
295 double getDouble(void) const { return fValue
.fDouble
; }
298 * Gets the double value of this object. If this object is of type
299 * long, int64 or Decimal Number then a conversion is peformed, with
300 * possible loss of precision. If the type is kObject and the
301 * object is a Measure, then the result of
302 * getNumber().getDouble(status) is returned. If this object is
303 * neither a numeric type nor a Measure, then 0 is returned and
304 * the status is set to U_INVALID_FORMAT_ERROR.
305 * @param status the error code
306 * @return the double value of this object.
309 double getDouble(UErrorCode
& status
) const;
312 * Gets the long value of this object. If this object is not of type
313 * kLong then the result is undefined.
314 * @return the long value of this object.
317 int32_t getLong(void) const { return (int32_t)fValue
.fInt64
; }
320 * Gets the long value of this object. If the magnitude is too
321 * large to fit in a long, then the maximum or minimum long value,
322 * as appropriate, is returned and the status is set to
323 * U_INVALID_FORMAT_ERROR. If this object is of type kInt64 and
324 * it fits within a long, then no precision is lost. If it is of
325 * type kDouble, then a conversion is peformed, with
326 * truncation of any fractional part. If the type is kObject and
327 * the object is a Measure, then the result of
328 * getNumber().getLong(status) is returned. If this object is
329 * neither a numeric type nor a Measure, then 0 is returned and
330 * the status is set to U_INVALID_FORMAT_ERROR.
331 * @param status the error code
332 * @return the long value of this object.
335 int32_t getLong(UErrorCode
& status
) const;
338 * Gets the int64 value of this object. If this object is not of type
339 * kInt64 then the result is undefined.
340 * @return the int64 value of this object.
343 int64_t getInt64(void) const { return fValue
.fInt64
; }
346 * Gets the int64 value of this object. If this object is of a numeric
347 * type and the magnitude is too large to fit in an int64, then
348 * the maximum or minimum int64 value, as appropriate, is returned
349 * and the status is set to U_INVALID_FORMAT_ERROR. If the
350 * magnitude fits in an int64, then a casting conversion is
351 * peformed, with truncation of any fractional part. If the type
352 * is kObject and the object is a Measure, then the result of
353 * getNumber().getDouble(status) is returned. If this object is
354 * neither a numeric type nor a Measure, then 0 is returned and
355 * the status is set to U_INVALID_FORMAT_ERROR.
356 * @param status the error code
357 * @return the int64 value of this object.
360 int64_t getInt64(UErrorCode
& status
) const;
363 * Gets the Date value of this object. If this object is not of type
364 * kDate then the result is undefined.
365 * @return the Date value of this object.
368 UDate
getDate() const { return fValue
.fDate
; }
371 * Gets the Date value of this object. If the type is not a date,
372 * status is set to U_INVALID_FORMAT_ERROR and the return value is
374 * @param status the error code.
375 * @return the Date value of this object.
378 UDate
getDate(UErrorCode
& status
) const;
381 * Gets the string value of this object. If this object is not of type
382 * kString then the result is undefined.
383 * @param result Output param to receive the Date value of this object.
384 * @return A reference to 'result'.
387 UnicodeString
& getString(UnicodeString
& result
) const
388 { result
=*fValue
.fString
; return result
; }
391 * Gets the string value of this object. If the type is not a
392 * string, status is set to U_INVALID_FORMAT_ERROR and a bogus
393 * string is returned.
394 * @param result Output param to receive the Date value of this object.
395 * @param status the error code.
396 * @return A reference to 'result'.
399 UnicodeString
& getString(UnicodeString
& result
, UErrorCode
& status
) const;
402 * Gets a const reference to the string value of this object. If
403 * this object is not of type kString then the result is
405 * @return a const reference to the string value of this object.
408 inline const UnicodeString
& getString(void) const;
411 * Gets a const reference to the string value of this object. If
412 * the type is not a string, status is set to
413 * U_INVALID_FORMAT_ERROR and the result is a bogus string.
414 * @param status the error code.
415 * @return a const reference to the string value of this object.
418 const UnicodeString
& getString(UErrorCode
& status
) const;
421 * Gets a reference to the string value of this object. If this
422 * object is not of type kString then the result is undefined.
423 * @return a reference to the string value of this object.
426 inline UnicodeString
& getString(void);
429 * Gets a reference to the string value of this object. If the
430 * type is not a string, status is set to U_INVALID_FORMAT_ERROR
431 * and the result is a bogus string.
432 * @param status the error code.
433 * @return a reference to the string value of this object.
436 UnicodeString
& getString(UErrorCode
& status
);
439 * Gets the array value and count of this object. If this object
440 * is not of type kArray then the result is undefined.
441 * @param count fill-in with the count of this object.
442 * @return the array value of this object.
445 const Formattable
* getArray(int32_t& count
) const
446 { count
=fValue
.fArrayAndCount
.fCount
; return fValue
.fArrayAndCount
.fArray
; }
449 * Gets the array value and count of this object. If the type is
450 * not an array, status is set to U_INVALID_FORMAT_ERROR, count is
451 * set to 0, and the result is NULL.
452 * @param count fill-in with the count of this object.
453 * @param status the error code.
454 * @return the array value of this object.
457 const Formattable
* getArray(int32_t& count
, UErrorCode
& status
) const;
460 * Accesses the specified element in the array value of this
461 * Formattable object. If this object is not of type kArray then
462 * the result is undefined.
463 * @param index the specified index.
464 * @return the accessed element in the array.
467 Formattable
& operator[](int32_t index
) { return fValue
.fArrayAndCount
.fArray
[index
]; }
470 * Returns a pointer to the UObject contained within this
471 * formattable, or NULL if this object does not contain a UObject.
472 * @return a UObject pointer, or NULL
475 const UObject
* getObject() const;
478 * Returns a numeric string representation of the number contained within this
479 * formattable, or NULL if this object does not contain numeric type.
480 * For values obtained by parsing, the returned decimal number retains
481 * the full precision and range of the original input, unconstrained by
482 * the limits of a double floating point or a 64 bit int.
484 * This function is not thread safe, and therfore is not declared const,
485 * even though it is logically const.
487 * Possible errors include U_MEMORY_ALLOCATION_ERROR, and
488 * U_INVALID_STATE if the formattable object has not been set to
491 * @param status the error code.
492 * @return the unformatted string representation of a number.
495 StringPiece
getDecimalNumber(UErrorCode
&status
);
498 * Sets the double value of this object and changes the type to
500 * @param d the new double value to be set.
503 void setDouble(double d
);
506 * Sets the long value of this object and changes the type to
508 * @param l the new long value to be set.
511 void setLong(int32_t l
);
514 * Sets the int64 value of this object and changes the type to
516 * @param ll the new int64 value to be set.
519 void setInt64(int64_t ll
);
522 * Sets the Date value of this object and changes the type to
524 * @param d the new Date value to be set.
527 void setDate(UDate d
);
530 * Sets the string value of this object and changes the type to
532 * @param stringToCopy the new string value to be set.
535 void setString(const UnicodeString
& stringToCopy
);
538 * Sets the array value and count of this object and changes the
540 * @param array the array value.
541 * @param count the number of array elements to be copied.
544 void setArray(const Formattable
* array
, int32_t count
);
547 * Sets and adopts the string value and count of this object and
548 * changes the type to kArray.
549 * @param stringToAdopt the new string value to be adopted.
552 void adoptString(UnicodeString
* stringToAdopt
);
555 * Sets and adopts the array value and count of this object and
556 * changes the type to kArray.
559 void adoptArray(Formattable
* array
, int32_t count
);
562 * Sets and adopts the UObject value of this object and changes
563 * the type to kObject. After this call, the caller must not
564 * delete the given object.
565 * @param objectToAdopt the UObject value to be adopted
568 void adoptObject(UObject
* objectToAdopt
);
571 * Sets the the numeric value from a decimal number string, and changes
572 * the type to to a numeric type appropriate for the number.
573 * The syntax of the number is a "numeric string"
574 * as defined in the Decimal Arithmetic Specification, available at
575 * http://speleotrove.com/decimal
576 * The full precision and range of the input number will be retained,
577 * even when it exceeds what can be represented by a double or an int64.
579 * @param numberString a string representation of the unformatted decimal number.
580 * @param status the error code. Set to U_INVALID_FORMAT_ERROR if the
581 * incoming string is not a valid decimal number.
584 void setDecimalNumber(const StringPiece
&numberString
,
588 * ICU "poor man's RTTI", returns a UClassID for the actual class.
592 virtual UClassID
getDynamicClassID() const;
595 * ICU "poor man's RTTI", returns a UClassID for this class.
599 static UClassID U_EXPORT2
getStaticClassID();
602 * Convert the UFormattable to a Formattable. Internally, this is a reinterpret_cast.
603 * @param fmt a valid UFormattable
604 * @return the UFormattable as a Formattable object pointer. This is an alias to the original
605 * UFormattable, and so is only valid while the original argument remains in scope.
608 static inline Formattable
*fromUFormattable(UFormattable
*fmt
);
611 * Convert the const UFormattable to a const Formattable. Internally, this is a reinterpret_cast.
612 * @param fmt a valid UFormattable
613 * @return the UFormattable as a Formattable object pointer. This is an alias to the original
614 * UFormattable, and so is only valid while the original argument remains in scope.
617 static inline const Formattable
*fromUFormattable(const UFormattable
*fmt
);
620 * Convert this object pointer to a UFormattable.
621 * @return this object as a UFormattable pointer. This is an alias to this object,
622 * and so is only valid while this object remains in scope.
625 inline UFormattable
*toUFormattable();
628 * Convert this object pointer to a UFormattable.
629 * @return this object as a UFormattable pointer. This is an alias to this object,
630 * and so is only valid while this object remains in scope.
633 inline const UFormattable
*toUFormattable() const;
635 #ifndef U_HIDE_DEPRECATED_API
637 * Deprecated variant of getLong(UErrorCode&).
638 * @param status the error code
639 * @return the long value of this object.
640 * @deprecated ICU 3.0 use getLong(UErrorCode&) instead
642 inline int32_t getLong(UErrorCode
* status
) const;
643 #endif /* U_HIDE_DEPRECATED_API */
645 #ifndef U_HIDE_INTERNAL_API
647 * Internal function, do not use.
648 * TODO: figure out how to make this be non-public.
649 * NumberFormat::format(Formattable, ...
650 * needs to get at the DigitList, if it exists, for
651 * big decimal formatting.
654 DigitList
*getDigitList() const { return fDecimalNum
;}
659 DigitList
*getInternalDigitList();
662 * Adopt, and set value from, a DigitList
663 * Internal Function, do not use.
664 * @param dl the Digit List to be adopted
667 void adoptDigitList(DigitList
*dl
);
670 * Internal function to return the CharString pointer.
671 * @param status error code
672 * @return pointer to the CharString - may become invalid if the object is modified
675 CharString
*internalGetCharString(UErrorCode
&status
);
677 #endif /* U_HIDE_INTERNAL_API */
681 * Cleans up the memory for unwanted values. For example, the adopted
682 * string or array objects.
687 * Common initialization, for use by constructors.
691 UnicodeString
* getBogus() const;
695 UnicodeString
* fString
;
705 CharString
*fDecimalStr
;
707 DigitList
*fDecimalNum
;
709 char fStackData
[UNUM_INTERNAL_STACKARRAY_SIZE
]; // must be big enough for DigitList
712 UnicodeString fBogus
; // Bogus string when it's needed.
715 inline UDate
Formattable::getDate(UErrorCode
& status
) const {
716 if (fType
!= kDate
) {
717 if (U_SUCCESS(status
)) {
718 status
= U_INVALID_FORMAT_ERROR
;
725 inline const UnicodeString
& Formattable::getString(void) const {
726 return *fValue
.fString
;
729 inline UnicodeString
& Formattable::getString(void) {
730 return *fValue
.fString
;
733 #ifndef U_HIDE_DEPRECATED_API
734 inline int32_t Formattable::getLong(UErrorCode
* status
) const {
735 return getLong(*status
);
737 #endif /* U_HIDE_DEPRECATED_API */
739 inline UFormattable
* Formattable::toUFormattable() {
740 return reinterpret_cast<UFormattable
*>(this);
743 inline const UFormattable
* Formattable::toUFormattable() const {
744 return reinterpret_cast<const UFormattable
*>(this);
747 inline Formattable
* Formattable::fromUFormattable(UFormattable
*fmt
) {
748 return reinterpret_cast<Formattable
*>(fmt
);
751 inline const Formattable
* Formattable::fromUFormattable(const UFormattable
*fmt
) {
752 return reinterpret_cast<const Formattable
*>(fmt
);
757 #endif /* #if !UCONFIG_NO_FORMATTING */