1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ********************************************************************************
5 * Copyright (C) 1997-2014, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 ********************************************************************************
11 * Modification History:
13 * Date Name Description
14 * 02/29/97 aliu Creation.
15 ********************************************************************************
20 #include "unicode/utypes.h"
24 * \brief C++ API: Formattable is a thin wrapper for primitive types used for formatting and parsing
27 #if !UCONFIG_NO_FORMATTING
29 #include "unicode/unistr.h"
30 #include "unicode/stringpiece.h"
31 #include "unicode/uformattable.h"
33 #if U_SHOW_CPLUSPLUS_API
40 class DecimalQuantity
;
45 * \def UNUM_INTERNAL_STACKARRAY_SIZE
48 #if U_PLATFORM == U_PF_OS400
49 #define UNUM_INTERNAL_STACKARRAY_SIZE 144
51 #define UNUM_INTERNAL_STACKARRAY_SIZE 128
55 * Formattable objects can be passed to the Format class or
56 * its subclasses for formatting. Formattable is a thin wrapper
57 * class which interconverts between the primitive numeric types
58 * (double, long, etc.) as well as UDate and UnicodeString.
60 * <p>Internally, a Formattable object is a union of primitive types.
61 * As such, it can only store one flavor of data at a time. To
62 * determine what flavor of data it contains, use the getType method.
64 * <p>As of ICU 3.0, Formattable may also wrap a UObject pointer,
65 * which it owns. This allows an instance of any ICU class to be
66 * encapsulated in a Formattable. For legacy reasons and for
67 * efficiency, primitive numeric types are still stored directly
68 * within a Formattable.
70 * <p>The Formattable class is not suitable for subclassing.
72 * <p>See UFormattable for a C wrapper.
74 class U_I18N_API Formattable
: public UObject
{
77 * This enum is only used to let callers distinguish between
78 * the Formattable(UDate) constructor and the Formattable(double)
79 * constructor; the compiler cannot distinguish the signatures,
80 * since UDate is currently typedefed to be either double or long.
81 * If UDate is changed later to be a bonafide class
82 * or struct, then we no longer need this enum.
85 enum ISDATE
{ kIsDate
};
91 Formattable(); // Type kLong, value 0
94 * Creates a Formattable object with a UDate instance.
95 * @param d the UDate instance.
96 * @param flag the flag to indicate this is a date. Always set it to kIsDate
99 Formattable(UDate d
, ISDATE flag
);
102 * Creates a Formattable object with a double number.
103 * @param d the double number.
106 Formattable(double d
);
109 * Creates a Formattable object with a long number.
110 * @param l the long number.
113 Formattable(int32_t l
);
116 * Creates a Formattable object with an int64_t number
117 * @param ll the int64_t number.
120 Formattable(int64_t ll
);
122 #if !UCONFIG_NO_CONVERSION
124 * Creates a Formattable object with a char string pointer.
125 * Assumes that the char string is null terminated.
126 * @param strToCopy the char string.
129 Formattable(const char* strToCopy
);
133 * Creates a Formattable object of an appropriate numeric type from a
134 * a decimal number in string form. The Formattable will retain the
135 * full precision of the input in decimal format, even when it exceeds
136 * what can be represented by a double or int64_t.
138 * @param number the unformatted (not localized) string representation
139 * of the Decimal number.
140 * @param status the error code. Possible errors include U_INVALID_FORMAT_ERROR
141 * if the format of the string does not conform to that of a
145 Formattable(StringPiece number
, UErrorCode
&status
);
148 * Creates a Formattable object with a UnicodeString object to copy from.
149 * @param strToCopy the UnicodeString string.
152 Formattable(const UnicodeString
& strToCopy
);
155 * Creates a Formattable object with a UnicodeString object to adopt from.
156 * @param strToAdopt the UnicodeString string.
159 Formattable(UnicodeString
* strToAdopt
);
162 * Creates a Formattable object with an array of Formattable objects.
163 * @param arrayToCopy the Formattable object array.
164 * @param count the array count.
167 Formattable(const Formattable
* arrayToCopy
, int32_t count
);
170 * Creates a Formattable object that adopts the given UObject.
171 * @param objectToAdopt the UObject to set this object to
174 Formattable(UObject
* objectToAdopt
);
180 Formattable(const Formattable
&);
183 * Assignment operator.
184 * @param rhs The Formattable object to copy into this object.
187 Formattable
& operator=(const Formattable
&rhs
);
190 * Equality comparison.
191 * @param other the object to be compared with.
192 * @return TRUE if other are equal to this, FALSE otherwise.
195 UBool
operator==(const Formattable
&other
) const;
199 * @param other the object to be compared with.
200 * @return TRUE if other are unequal to this, FALSE otherwise.
203 UBool
operator!=(const Formattable
& other
) const
204 { return !operator==(other
); }
210 virtual ~Formattable();
214 * Clones can be used concurrently in multiple threads.
215 * If an error occurs, then NULL is returned.
216 * The caller must delete the clone.
218 * @return a clone of this object
220 * @see getDynamicClassID
223 Formattable
*clone() const;
226 * Selector for flavor of data type contained within a
227 * Formattable object. Formattable is a union of several
228 * different types, and at any time contains exactly one type.
233 * Selector indicating a UDate value. Use getDate to retrieve
240 * Selector indicating a double value. Use getDouble to
241 * retrieve the value.
247 * Selector indicating a 32-bit integer value. Use getLong to
248 * retrieve the value.
254 * Selector indicating a UnicodeString value. Use getString
255 * to retrieve the value.
261 * Selector indicating an array of Formattables. Use getArray
262 * to retrieve the value.
268 * Selector indicating a 64-bit integer value. Use getInt64
269 * to retrieve the value.
275 * Selector indicating a UObject value. Use getObject to
276 * retrieve the value.
283 * Gets the data type of this Formattable object.
284 * @return the data type of this Formattable object.
287 Type
getType(void) const;
290 * Returns TRUE if the data type of this Formattable object
291 * is kDouble, kLong, or kInt64
292 * @return TRUE if this is a pure numeric object
295 UBool
isNumeric() const;
298 * Gets the double value of this object. If this object is not of type
299 * kDouble then the result is undefined.
300 * @return the double value of this object.
303 double getDouble(void) const { return fValue
.fDouble
; }
306 * Gets the double value of this object. If this object is of type
307 * long, int64 or Decimal Number then a conversion is peformed, with
308 * possible loss of precision. If the type is kObject and the
309 * object is a Measure, then the result of
310 * getNumber().getDouble(status) is returned. If this object is
311 * neither a numeric type nor a Measure, then 0 is returned and
312 * the status is set to U_INVALID_FORMAT_ERROR.
313 * @param status the error code
314 * @return the double value of this object.
317 double getDouble(UErrorCode
& status
) const;
320 * Gets the long value of this object. If this object is not of type
321 * kLong then the result is undefined.
322 * @return the long value of this object.
325 int32_t getLong(void) const { return (int32_t)fValue
.fInt64
; }
328 * Gets the long value of this object. If the magnitude is too
329 * large to fit in a long, then the maximum or minimum long value,
330 * as appropriate, is returned and the status is set to
331 * U_INVALID_FORMAT_ERROR. If this object is of type kInt64 and
332 * it fits within a long, then no precision is lost. If it is of
333 * type kDouble, then a conversion is peformed, with
334 * truncation of any fractional part. If the type is kObject and
335 * the object is a Measure, then the result of
336 * getNumber().getLong(status) is returned. If this object is
337 * neither a numeric type nor a Measure, then 0 is returned and
338 * the status is set to U_INVALID_FORMAT_ERROR.
339 * @param status the error code
340 * @return the long value of this object.
343 int32_t getLong(UErrorCode
& status
) const;
346 * Gets the int64 value of this object. If this object is not of type
347 * kInt64 then the result is undefined.
348 * @return the int64 value of this object.
351 int64_t getInt64(void) const { return fValue
.fInt64
; }
354 * Gets the int64 value of this object. If this object is of a numeric
355 * type and the magnitude is too large to fit in an int64, then
356 * the maximum or minimum int64 value, as appropriate, is returned
357 * and the status is set to U_INVALID_FORMAT_ERROR. If the
358 * magnitude fits in an int64, then a casting conversion is
359 * peformed, with truncation of any fractional part. If the type
360 * is kObject and the object is a Measure, then the result of
361 * getNumber().getDouble(status) is returned. If this object is
362 * neither a numeric type nor a Measure, then 0 is returned and
363 * the status is set to U_INVALID_FORMAT_ERROR.
364 * @param status the error code
365 * @return the int64 value of this object.
368 int64_t getInt64(UErrorCode
& status
) const;
371 * Gets the Date value of this object. If this object is not of type
372 * kDate then the result is undefined.
373 * @return the Date value of this object.
376 UDate
getDate() const { return fValue
.fDate
; }
379 * Gets the Date value of this object. If the type is not a date,
380 * status is set to U_INVALID_FORMAT_ERROR and the return value is
382 * @param status the error code.
383 * @return the Date value of this object.
386 UDate
getDate(UErrorCode
& status
) const;
389 * Gets the string value of this object. If this object is not of type
390 * kString then the result is undefined.
391 * @param result Output param to receive the Date value of this object.
392 * @return A reference to 'result'.
395 UnicodeString
& getString(UnicodeString
& result
) const
396 { result
=*fValue
.fString
; return result
; }
399 * Gets the string value of this object. If the type is not a
400 * string, status is set to U_INVALID_FORMAT_ERROR and a bogus
401 * string is returned.
402 * @param result Output param to receive the Date value of this object.
403 * @param status the error code.
404 * @return A reference to 'result'.
407 UnicodeString
& getString(UnicodeString
& result
, UErrorCode
& status
) const;
410 * Gets a const reference to the string value of this object. If
411 * this object is not of type kString then the result is
413 * @return a const reference to the string value of this object.
416 inline const UnicodeString
& getString(void) const;
419 * Gets a const reference to the string value of this object. If
420 * the type is not a string, status is set to
421 * U_INVALID_FORMAT_ERROR and the result is a bogus string.
422 * @param status the error code.
423 * @return a const reference to the string value of this object.
426 const UnicodeString
& getString(UErrorCode
& status
) const;
429 * Gets a reference to the string value of this object. If this
430 * object is not of type kString then the result is undefined.
431 * @return a reference to the string value of this object.
434 inline UnicodeString
& getString(void);
437 * Gets a reference to the string value of this object. If the
438 * type is not a string, status is set to U_INVALID_FORMAT_ERROR
439 * and the result is a bogus string.
440 * @param status the error code.
441 * @return a reference to the string value of this object.
444 UnicodeString
& getString(UErrorCode
& status
);
447 * Gets the array value and count of this object. If this object
448 * is not of type kArray then the result is undefined.
449 * @param count fill-in with the count of this object.
450 * @return the array value of this object.
453 const Formattable
* getArray(int32_t& count
) const
454 { count
=fValue
.fArrayAndCount
.fCount
; return fValue
.fArrayAndCount
.fArray
; }
457 * Gets the array value and count of this object. If the type is
458 * not an array, status is set to U_INVALID_FORMAT_ERROR, count is
459 * set to 0, and the result is NULL.
460 * @param count fill-in with the count of this object.
461 * @param status the error code.
462 * @return the array value of this object.
465 const Formattable
* getArray(int32_t& count
, UErrorCode
& status
) const;
468 * Accesses the specified element in the array value of this
469 * Formattable object. If this object is not of type kArray then
470 * the result is undefined.
471 * @param index the specified index.
472 * @return the accessed element in the array.
475 Formattable
& operator[](int32_t index
) { return fValue
.fArrayAndCount
.fArray
[index
]; }
478 * Returns a pointer to the UObject contained within this
479 * formattable, or NULL if this object does not contain a UObject.
480 * @return a UObject pointer, or NULL
483 const UObject
* getObject() const;
486 * Returns a numeric string representation of the number contained within this
487 * formattable, or NULL if this object does not contain numeric type.
488 * For values obtained by parsing, the returned decimal number retains
489 * the full precision and range of the original input, unconstrained by
490 * the limits of a double floating point or a 64 bit int.
492 * This function is not thread safe, and therfore is not declared const,
493 * even though it is logically const.
495 * Possible errors include U_MEMORY_ALLOCATION_ERROR, and
496 * U_INVALID_STATE if the formattable object has not been set to
499 * @param status the error code.
500 * @return the unformatted string representation of a number.
503 StringPiece
getDecimalNumber(UErrorCode
&status
);
506 * Sets the double value of this object and changes the type to
508 * @param d the new double value to be set.
511 void setDouble(double d
);
514 * Sets the long value of this object and changes the type to
516 * @param l the new long value to be set.
519 void setLong(int32_t l
);
522 * Sets the int64 value of this object and changes the type to
524 * @param ll the new int64 value to be set.
527 void setInt64(int64_t ll
);
530 * Sets the Date value of this object and changes the type to
532 * @param d the new Date value to be set.
535 void setDate(UDate d
);
538 * Sets the string value of this object and changes the type to
540 * @param stringToCopy the new string value to be set.
543 void setString(const UnicodeString
& stringToCopy
);
546 * Sets the array value and count of this object and changes the
548 * @param array the array value.
549 * @param count the number of array elements to be copied.
552 void setArray(const Formattable
* array
, int32_t count
);
555 * Sets and adopts the string value and count of this object and
556 * changes the type to kArray.
557 * @param stringToAdopt the new string value to be adopted.
560 void adoptString(UnicodeString
* stringToAdopt
);
563 * Sets and adopts the array value and count of this object and
564 * changes the type to kArray.
567 void adoptArray(Formattable
* array
, int32_t count
);
570 * Sets and adopts the UObject value of this object and changes
571 * the type to kObject. After this call, the caller must not
572 * delete the given object.
573 * @param objectToAdopt the UObject value to be adopted
576 void adoptObject(UObject
* objectToAdopt
);
579 * Sets the the numeric value from a decimal number string, and changes
580 * the type to to a numeric type appropriate for the number.
581 * The syntax of the number is a "numeric string"
582 * as defined in the Decimal Arithmetic Specification, available at
583 * http://speleotrove.com/decimal
584 * The full precision and range of the input number will be retained,
585 * even when it exceeds what can be represented by a double or an int64.
587 * @param numberString a string representation of the unformatted decimal number.
588 * @param status the error code. Set to U_INVALID_FORMAT_ERROR if the
589 * incoming string is not a valid decimal number.
592 void setDecimalNumber(StringPiece numberString
,
596 * ICU "poor man's RTTI", returns a UClassID for the actual class.
600 virtual UClassID
getDynamicClassID() const;
603 * ICU "poor man's RTTI", returns a UClassID for this class.
607 static UClassID U_EXPORT2
getStaticClassID();
610 * Convert the UFormattable to a Formattable. Internally, this is a reinterpret_cast.
611 * @param fmt a valid UFormattable
612 * @return the UFormattable as a Formattable object pointer. This is an alias to the original
613 * UFormattable, and so is only valid while the original argument remains in scope.
616 static inline Formattable
*fromUFormattable(UFormattable
*fmt
);
619 * Convert the const UFormattable to a const Formattable. Internally, this is a reinterpret_cast.
620 * @param fmt a valid UFormattable
621 * @return the UFormattable as a Formattable object pointer. This is an alias to the original
622 * UFormattable, and so is only valid while the original argument remains in scope.
625 static inline const Formattable
*fromUFormattable(const UFormattable
*fmt
);
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 UFormattable
*toUFormattable();
636 * Convert this object pointer to a UFormattable.
637 * @return this object as a UFormattable pointer. This is an alias to this object,
638 * and so is only valid while this object remains in scope.
641 inline const UFormattable
*toUFormattable() const;
643 #ifndef U_HIDE_DEPRECATED_API
645 * Deprecated variant of getLong(UErrorCode&).
646 * @param status the error code
647 * @return the long value of this object.
648 * @deprecated ICU 3.0 use getLong(UErrorCode&) instead
650 inline int32_t getLong(UErrorCode
* status
) const;
651 #endif /* U_HIDE_DEPRECATED_API */
653 #ifndef U_HIDE_INTERNAL_API
655 * Internal function, do not use.
656 * TODO: figure out how to make this be non-public.
657 * NumberFormat::format(Formattable, ...
658 * needs to get at the DigitList, if it exists, for
659 * big decimal formatting.
662 DigitList
*getDigitList() const { return fDecimalNum
;}
667 DigitList
*getInternalDigitList();
670 * Internal function, do not use.
671 * TODO: figure out how to make this be non-public.
672 * NumberFormat::format(Formattable, ...
673 * needs to get at the DecimalQuantity, if it exists, for
674 * big decimal formatting.
677 number::impl::DecimalQuantity
*getDecimalQuantity() const { return fDecimalQuantity
;}
680 * Export the value of this Formattable to a DecimalQuantity.
683 void populateDecimalQuantity(number::impl::DecimalQuantity
& output
, UErrorCode
& status
) const;
686 * Adopt, and set value from, a DigitList
687 * Internal Function, do not use.
688 * @param dl the Digit List to be adopted
691 void adoptDigitList(DigitList
*dl
);
694 * Adopt, and set value from, a DecimalQuantity
695 * Internal Function, do not use.
696 * @param dl the DecimalQuantity to be adopted
699 void adoptDecimalQuantity(number::impl::DecimalQuantity
*dq
);
702 * Internal function to return the CharString pointer.
703 * @param status error code
704 * @return pointer to the CharString - may become invalid if the object is modified
707 CharString
*internalGetCharString(UErrorCode
&status
);
709 #endif /* U_HIDE_INTERNAL_API */
713 * Cleans up the memory for unwanted values. For example, the adopted
714 * string or array objects.
719 * Common initialization, for use by constructors.
723 UnicodeString
* getBogus() const;
727 UnicodeString
* fString
;
737 CharString
*fDecimalStr
;
739 DigitList
*fDecimalNum
;
741 char fStackData
[UNUM_INTERNAL_STACKARRAY_SIZE
]; // must be big enough for DigitList
743 number::impl::DecimalQuantity
*fDecimalQuantity
;
746 UnicodeString fBogus
; // Bogus string when it's needed.
749 inline UDate
Formattable::getDate(UErrorCode
& status
) const {
750 if (fType
!= kDate
) {
751 if (U_SUCCESS(status
)) {
752 status
= U_INVALID_FORMAT_ERROR
;
759 inline const UnicodeString
& Formattable::getString(void) const {
760 return *fValue
.fString
;
763 inline UnicodeString
& Formattable::getString(void) {
764 return *fValue
.fString
;
767 #ifndef U_HIDE_DEPRECATED_API
768 inline int32_t Formattable::getLong(UErrorCode
* status
) const {
769 return getLong(*status
);
771 #endif /* U_HIDE_DEPRECATED_API */
773 inline UFormattable
* Formattable::toUFormattable() {
774 return reinterpret_cast<UFormattable
*>(this);
777 inline const UFormattable
* Formattable::toUFormattable() const {
778 return reinterpret_cast<const UFormattable
*>(this);
781 inline Formattable
* Formattable::fromUFormattable(UFormattable
*fmt
) {
782 return reinterpret_cast<Formattable
*>(fmt
);
785 inline const Formattable
* Formattable::fromUFormattable(const UFormattable
*fmt
) {
786 return reinterpret_cast<const Formattable
*>(fmt
);
790 #endif // U_SHOW_CPLUSPLUS_API
792 #endif /* #if !UCONFIG_NO_FORMATTING */