]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/fmtable.h
e825cb693c83687870c32f9a318f34cd6f30c780
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
39 class DecimalQuantity
;
44 * Formattable objects can be passed to the Format class or
45 * its subclasses for formatting. Formattable is a thin wrapper
46 * class which interconverts between the primitive numeric types
47 * (double, long, etc.) as well as UDate and UnicodeString.
49 * <p>Internally, a Formattable object is a union of primitive types.
50 * As such, it can only store one flavor of data at a time. To
51 * determine what flavor of data it contains, use the getType method.
53 * <p>As of ICU 3.0, Formattable may also wrap a UObject pointer,
54 * which it owns. This allows an instance of any ICU class to be
55 * encapsulated in a Formattable. For legacy reasons and for
56 * efficiency, primitive numeric types are still stored directly
57 * within a Formattable.
59 * <p>The Formattable class is not suitable for subclassing.
61 * <p>See UFormattable for a C wrapper.
63 class U_I18N_API Formattable
: public UObject
{
66 * This enum is only used to let callers distinguish between
67 * the Formattable(UDate) constructor and the Formattable(double)
68 * constructor; the compiler cannot distinguish the signatures,
69 * since UDate is currently typedefed to be either double or long.
70 * If UDate is changed later to be a bonafide class
71 * or struct, then we no longer need this enum.
74 enum ISDATE
{ kIsDate
};
80 Formattable(); // Type kLong, value 0
83 * Creates a Formattable object with a UDate instance.
84 * @param d the UDate instance.
85 * @param flag the flag to indicate this is a date. Always set it to kIsDate
88 Formattable(UDate d
, ISDATE flag
);
91 * Creates a Formattable object with a double number.
92 * @param d the double number.
95 Formattable(double d
);
98 * Creates a Formattable object with a long number.
99 * @param l the long number.
102 Formattable(int32_t l
);
105 * Creates a Formattable object with an int64_t number
106 * @param ll the int64_t number.
109 Formattable(int64_t ll
);
111 #if !UCONFIG_NO_CONVERSION
113 * Creates a Formattable object with a char string pointer.
114 * Assumes that the char string is null terminated.
115 * @param strToCopy the char string.
118 Formattable(const char* strToCopy
);
122 * Creates a Formattable object of an appropriate numeric type from a
123 * a decimal number in string form. The Formattable will retain the
124 * full precision of the input in decimal format, even when it exceeds
125 * what can be represented by a double or int64_t.
127 * @param number the unformatted (not localized) string representation
128 * of the Decimal number.
129 * @param status the error code. Possible errors include U_INVALID_FORMAT_ERROR
130 * if the format of the string does not conform to that of a
134 Formattable(StringPiece number
, UErrorCode
&status
);
137 * Creates a Formattable object with a UnicodeString object to copy from.
138 * @param strToCopy the UnicodeString string.
141 Formattable(const UnicodeString
& strToCopy
);
144 * Creates a Formattable object with a UnicodeString object to adopt from.
145 * @param strToAdopt the UnicodeString string.
148 Formattable(UnicodeString
* strToAdopt
);
151 * Creates a Formattable object with an array of Formattable objects.
152 * @param arrayToCopy the Formattable object array.
153 * @param count the array count.
156 Formattable(const Formattable
* arrayToCopy
, int32_t count
);
159 * Creates a Formattable object that adopts the given UObject.
160 * @param objectToAdopt the UObject to set this object to
163 Formattable(UObject
* objectToAdopt
);
169 Formattable(const Formattable
&);
172 * Assignment operator.
173 * @param rhs The Formattable object to copy into this object.
176 Formattable
& operator=(const Formattable
&rhs
);
179 * Equality comparison.
180 * @param other the object to be compared with.
181 * @return TRUE if other are equal to this, FALSE otherwise.
184 UBool
operator==(const Formattable
&other
) const;
188 * @param other the object to be compared with.
189 * @return TRUE if other are unequal to this, FALSE otherwise.
192 UBool
operator!=(const Formattable
& other
) const
193 { return !operator==(other
); }
199 virtual ~Formattable();
203 * Clones can be used concurrently in multiple threads.
204 * If an error occurs, then NULL is returned.
205 * The caller must delete the clone.
207 * @return a clone of this object
209 * @see getDynamicClassID
212 Formattable
*clone() const;
215 * Selector for flavor of data type contained within a
216 * Formattable object. Formattable is a union of several
217 * different types, and at any time contains exactly one type.
222 * Selector indicating a UDate value. Use getDate to retrieve
229 * Selector indicating a double value. Use getDouble to
230 * retrieve the value.
236 * Selector indicating a 32-bit integer value. Use getLong to
237 * retrieve the value.
243 * Selector indicating a UnicodeString value. Use getString
244 * to retrieve the value.
250 * Selector indicating an array of Formattables. Use getArray
251 * to retrieve the value.
257 * Selector indicating a 64-bit integer value. Use getInt64
258 * to retrieve the value.
264 * Selector indicating a UObject value. Use getObject to
265 * retrieve the value.
272 * Gets the data type of this Formattable object.
273 * @return the data type of this Formattable object.
276 Type
getType(void) const;
279 * Returns TRUE if the data type of this Formattable object
280 * is kDouble, kLong, or kInt64
281 * @return TRUE if this is a pure numeric object
284 UBool
isNumeric() const;
287 * Gets the double value of this object. If this object is not of type
288 * kDouble then the result is undefined.
289 * @return the double value of this object.
292 double getDouble(void) const { return fValue
.fDouble
; }
295 * Gets the double value of this object. If this object is of type
296 * long, int64 or Decimal Number then a conversion is peformed, with
297 * possible loss of precision. If the type is kObject and the
298 * object is a Measure, then the result of
299 * getNumber().getDouble(status) is returned. If this object is
300 * neither a numeric type nor a Measure, then 0 is returned and
301 * the status is set to U_INVALID_FORMAT_ERROR.
302 * @param status the error code
303 * @return the double value of this object.
306 double getDouble(UErrorCode
& status
) const;
309 * Gets the long value of this object. If this object is not of type
310 * kLong then the result is undefined.
311 * @return the long value of this object.
314 int32_t getLong(void) const { return (int32_t)fValue
.fInt64
; }
317 * Gets the long value of this object. If the magnitude is too
318 * large to fit in a long, then the maximum or minimum long value,
319 * as appropriate, is returned and the status is set to
320 * U_INVALID_FORMAT_ERROR. If this object is of type kInt64 and
321 * it fits within a long, then no precision is lost. If it is of
322 * type kDouble, then a conversion is peformed, with
323 * truncation of any fractional part. If the type is kObject and
324 * the object is a Measure, then the result of
325 * getNumber().getLong(status) is returned. If this object is
326 * neither a numeric type nor a Measure, then 0 is returned and
327 * the status is set to U_INVALID_FORMAT_ERROR.
328 * @param status the error code
329 * @return the long value of this object.
332 int32_t getLong(UErrorCode
& status
) const;
335 * Gets the int64 value of this object. If this object is not of type
336 * kInt64 then the result is undefined.
337 * @return the int64 value of this object.
340 int64_t getInt64(void) const { return fValue
.fInt64
; }
343 * Gets the int64 value of this object. If this object is of a numeric
344 * type and the magnitude is too large to fit in an int64, then
345 * the maximum or minimum int64 value, as appropriate, is returned
346 * and the status is set to U_INVALID_FORMAT_ERROR. If the
347 * magnitude fits in an int64, then a casting conversion is
348 * peformed, with truncation of any fractional part. If the type
349 * is kObject and the object is a Measure, then the result of
350 * getNumber().getDouble(status) is returned. If this object is
351 * neither a numeric type nor a Measure, then 0 is returned and
352 * the status is set to U_INVALID_FORMAT_ERROR.
353 * @param status the error code
354 * @return the int64 value of this object.
357 int64_t getInt64(UErrorCode
& status
) const;
360 * Gets the Date value of this object. If this object is not of type
361 * kDate then the result is undefined.
362 * @return the Date value of this object.
365 UDate
getDate() const { return fValue
.fDate
; }
368 * Gets the Date value of this object. If the type is not a date,
369 * status is set to U_INVALID_FORMAT_ERROR and the return value is
371 * @param status the error code.
372 * @return the Date value of this object.
375 UDate
getDate(UErrorCode
& status
) const;
378 * Gets the string value of this object. If this object is not of type
379 * kString then the result is undefined.
380 * @param result Output param to receive the Date value of this object.
381 * @return A reference to 'result'.
384 UnicodeString
& getString(UnicodeString
& result
) const
385 { result
=*fValue
.fString
; return result
; }
388 * Gets the string value of this object. If the type is not a
389 * string, status is set to U_INVALID_FORMAT_ERROR and a bogus
390 * string is returned.
391 * @param result Output param to receive the Date value of this object.
392 * @param status the error code.
393 * @return A reference to 'result'.
396 UnicodeString
& getString(UnicodeString
& result
, UErrorCode
& status
) const;
399 * Gets a const reference to the string value of this object. If
400 * this object is not of type kString then the result is
402 * @return a const reference to the string value of this object.
405 inline const UnicodeString
& getString(void) const;
408 * Gets a const reference to the string value of this object. If
409 * the type is not a string, status is set to
410 * U_INVALID_FORMAT_ERROR and the result is a bogus string.
411 * @param status the error code.
412 * @return a const reference to the string value of this object.
415 const UnicodeString
& getString(UErrorCode
& status
) const;
418 * Gets a reference to the string value of this object. If this
419 * object is not of type kString then the result is undefined.
420 * @return a reference to the string value of this object.
423 inline UnicodeString
& getString(void);
426 * Gets a reference to the string value of this object. If the
427 * type is not a string, status is set to U_INVALID_FORMAT_ERROR
428 * and the result is a bogus string.
429 * @param status the error code.
430 * @return a reference to the string value of this object.
433 UnicodeString
& getString(UErrorCode
& status
);
436 * Gets the array value and count of this object. If this object
437 * is not of type kArray then the result is undefined.
438 * @param count fill-in with the count of this object.
439 * @return the array value of this object.
442 const Formattable
* getArray(int32_t& count
) const
443 { count
=fValue
.fArrayAndCount
.fCount
; return fValue
.fArrayAndCount
.fArray
; }
446 * Gets the array value and count of this object. If the type is
447 * not an array, status is set to U_INVALID_FORMAT_ERROR, count is
448 * set to 0, and the result is NULL.
449 * @param count fill-in with the count of this object.
450 * @param status the error code.
451 * @return the array value of this object.
454 const Formattable
* getArray(int32_t& count
, UErrorCode
& status
) const;
457 * Accesses the specified element in the array value of this
458 * Formattable object. If this object is not of type kArray then
459 * the result is undefined.
460 * @param index the specified index.
461 * @return the accessed element in the array.
464 Formattable
& operator[](int32_t index
) { return fValue
.fArrayAndCount
.fArray
[index
]; }
467 * Returns a pointer to the UObject contained within this
468 * formattable, or NULL if this object does not contain a UObject.
469 * @return a UObject pointer, or NULL
472 const UObject
* getObject() const;
475 * Returns a numeric string representation of the number contained within this
476 * formattable, or NULL if this object does not contain numeric type.
477 * For values obtained by parsing, the returned decimal number retains
478 * the full precision and range of the original input, unconstrained by
479 * the limits of a double floating point or a 64 bit int.
481 * This function is not thread safe, and therfore is not declared const,
482 * even though it is logically const.
484 * Possible errors include U_MEMORY_ALLOCATION_ERROR, and
485 * U_INVALID_STATE if the formattable object has not been set to
488 * @param status the error code.
489 * @return the unformatted string representation of a number.
492 StringPiece
getDecimalNumber(UErrorCode
&status
);
495 * Sets the double value of this object and changes the type to
497 * @param d the new double value to be set.
500 void setDouble(double d
);
503 * Sets the long value of this object and changes the type to
505 * @param l the new long value to be set.
508 void setLong(int32_t l
);
511 * Sets the int64 value of this object and changes the type to
513 * @param ll the new int64 value to be set.
516 void setInt64(int64_t ll
);
519 * Sets the Date value of this object and changes the type to
521 * @param d the new Date value to be set.
524 void setDate(UDate d
);
527 * Sets the string value of this object and changes the type to
529 * @param stringToCopy the new string value to be set.
532 void setString(const UnicodeString
& stringToCopy
);
535 * Sets the array value and count of this object and changes the
537 * @param array the array value.
538 * @param count the number of array elements to be copied.
541 void setArray(const Formattable
* array
, int32_t count
);
544 * Sets and adopts the string value and count of this object and
545 * changes the type to kArray.
546 * @param stringToAdopt the new string value to be adopted.
549 void adoptString(UnicodeString
* stringToAdopt
);
552 * Sets and adopts the array value and count of this object and
553 * changes the type to kArray.
556 void adoptArray(Formattable
* array
, int32_t count
);
559 * Sets and adopts the UObject value of this object and changes
560 * the type to kObject. After this call, the caller must not
561 * delete the given object.
562 * @param objectToAdopt the UObject value to be adopted
565 void adoptObject(UObject
* objectToAdopt
);
568 * Sets the the numeric value from a decimal number string, and changes
569 * the type to to a numeric type appropriate for the number.
570 * The syntax of the number is a "numeric string"
571 * as defined in the Decimal Arithmetic Specification, available at
572 * http://speleotrove.com/decimal
573 * The full precision and range of the input number will be retained,
574 * even when it exceeds what can be represented by a double or an int64.
576 * @param numberString a string representation of the unformatted decimal number.
577 * @param status the error code. Set to U_INVALID_FORMAT_ERROR if the
578 * incoming string is not a valid decimal number.
581 void setDecimalNumber(StringPiece numberString
,
585 * ICU "poor man's RTTI", returns a UClassID for the actual class.
589 virtual UClassID
getDynamicClassID() const;
592 * ICU "poor man's RTTI", returns a UClassID for this class.
596 static UClassID U_EXPORT2
getStaticClassID();
599 * Convert the UFormattable to a Formattable. Internally, this is a reinterpret_cast.
600 * @param fmt a valid UFormattable
601 * @return the UFormattable as a Formattable object pointer. This is an alias to the original
602 * UFormattable, and so is only valid while the original argument remains in scope.
605 static inline Formattable
*fromUFormattable(UFormattable
*fmt
);
608 * Convert the const UFormattable to a const Formattable. Internally, this is a reinterpret_cast.
609 * @param fmt a valid UFormattable
610 * @return the UFormattable as a Formattable object pointer. This is an alias to the original
611 * UFormattable, and so is only valid while the original argument remains in scope.
614 static inline const Formattable
*fromUFormattable(const UFormattable
*fmt
);
617 * Convert this object pointer to a UFormattable.
618 * @return this object as a UFormattable pointer. This is an alias to this object,
619 * and so is only valid while this object remains in scope.
622 inline UFormattable
*toUFormattable();
625 * Convert this object pointer to a UFormattable.
626 * @return this object as a UFormattable pointer. This is an alias to this object,
627 * and so is only valid while this object remains in scope.
630 inline const UFormattable
*toUFormattable() const;
632 #ifndef U_HIDE_DEPRECATED_API
634 * Deprecated variant of getLong(UErrorCode&).
635 * @param status the error code
636 * @return the long value of this object.
637 * @deprecated ICU 3.0 use getLong(UErrorCode&) instead
639 inline int32_t getLong(UErrorCode
* status
) const;
640 #endif /* U_HIDE_DEPRECATED_API */
642 #ifndef U_HIDE_INTERNAL_API
644 * Internal function, do not use.
645 * TODO: figure out how to make this be non-public.
646 * NumberFormat::format(Formattable, ...
647 * needs to get at the DecimalQuantity, if it exists, for
648 * big decimal formatting.
651 number::impl::DecimalQuantity
*getDecimalQuantity() const { return fDecimalQuantity
;}
654 * Export the value of this Formattable to a DecimalQuantity.
657 void populateDecimalQuantity(number::impl::DecimalQuantity
& output
, UErrorCode
& status
) const;
660 * Adopt, and set value from, a DecimalQuantity
661 * Internal Function, do not use.
662 * @param dq the DecimalQuantity to be adopted
665 void adoptDecimalQuantity(number::impl::DecimalQuantity
*dq
);
668 * Internal function to return the CharString pointer.
669 * @param status error code
670 * @return pointer to the CharString - may become invalid if the object is modified
673 CharString
*internalGetCharString(UErrorCode
&status
);
675 #endif /* U_HIDE_INTERNAL_API */
679 * Cleans up the memory for unwanted values. For example, the adopted
680 * string or array objects.
685 * Common initialization, for use by constructors.
689 UnicodeString
* getBogus() const;
693 UnicodeString
* fString
;
703 CharString
*fDecimalStr
;
705 number::impl::DecimalQuantity
*fDecimalQuantity
;
708 UnicodeString fBogus
; // Bogus string when it's needed.
711 inline UDate
Formattable::getDate(UErrorCode
& status
) const {
712 if (fType
!= kDate
) {
713 if (U_SUCCESS(status
)) {
714 status
= U_INVALID_FORMAT_ERROR
;
721 inline const UnicodeString
& Formattable::getString(void) const {
722 return *fValue
.fString
;
725 inline UnicodeString
& Formattable::getString(void) {
726 return *fValue
.fString
;
729 #ifndef U_HIDE_DEPRECATED_API
730 inline int32_t Formattable::getLong(UErrorCode
* status
) const {
731 return getLong(*status
);
733 #endif /* U_HIDE_DEPRECATED_API */
735 inline UFormattable
* Formattable::toUFormattable() {
736 return reinterpret_cast<UFormattable
*>(this);
739 inline const UFormattable
* Formattable::toUFormattable() const {
740 return reinterpret_cast<const UFormattable
*>(this);
743 inline Formattable
* Formattable::fromUFormattable(UFormattable
*fmt
) {
744 return reinterpret_cast<Formattable
*>(fmt
);
747 inline const Formattable
* Formattable::fromUFormattable(const UFormattable
*fmt
) {
748 return reinterpret_cast<const Formattable
*>(fmt
);
752 #endif // U_SHOW_CPLUSPLUS_API
754 #endif /* #if !UCONFIG_NO_FORMATTING */