]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/fmtable.h
7bec4f6906e7ee4e1de0e8eb26192115a0796c6f
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"
22 #if U_SHOW_CPLUSPLUS_API
26 * \brief C++ API: Formattable is a thin wrapper for primitive types used for formatting and parsing
29 #if !UCONFIG_NO_FORMATTING
31 #include "unicode/unistr.h"
32 #include "unicode/stringpiece.h"
33 #include "unicode/uformattable.h"
40 class DecimalQuantity
;
45 * Formattable objects can be passed to the Format class or
46 * its subclasses for formatting. Formattable is a thin wrapper
47 * class which interconverts between the primitive numeric types
48 * (double, long, etc.) as well as UDate and UnicodeString.
50 * <p>Internally, a Formattable object is a union of primitive types.
51 * As such, it can only store one flavor of data at a time. To
52 * determine what flavor of data it contains, use the getType method.
54 * <p>As of ICU 3.0, Formattable may also wrap a UObject pointer,
55 * which it owns. This allows an instance of any ICU class to be
56 * encapsulated in a Formattable. For legacy reasons and for
57 * efficiency, primitive numeric types are still stored directly
58 * within a Formattable.
60 * <p>The Formattable class is not suitable for subclassing.
62 * <p>See UFormattable for a C wrapper.
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 or 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(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, or kInt64
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, 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(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();
600 * Convert the UFormattable to a Formattable. Internally, this is a reinterpret_cast.
601 * @param fmt a valid UFormattable
602 * @return the UFormattable as a Formattable object pointer. This is an alias to the original
603 * UFormattable, and so is only valid while the original argument remains in scope.
606 static inline Formattable
*fromUFormattable(UFormattable
*fmt
);
609 * Convert the const UFormattable to a const Formattable. Internally, this is a reinterpret_cast.
610 * @param fmt a valid UFormattable
611 * @return the UFormattable as a Formattable object pointer. This is an alias to the original
612 * UFormattable, and so is only valid while the original argument remains in scope.
615 static inline const Formattable
*fromUFormattable(const UFormattable
*fmt
);
618 * Convert this object pointer to a UFormattable.
619 * @return this object as a UFormattable pointer. This is an alias to this object,
620 * and so is only valid while this object remains in scope.
623 inline UFormattable
*toUFormattable();
626 * Convert this object pointer to a UFormattable.
627 * @return this object as a UFormattable pointer. This is an alias to this object,
628 * and so is only valid while this object remains in scope.
631 inline const UFormattable
*toUFormattable() const;
633 #ifndef U_HIDE_DEPRECATED_API
635 * Deprecated variant of getLong(UErrorCode&).
636 * @param status the error code
637 * @return the long value of this object.
638 * @deprecated ICU 3.0 use getLong(UErrorCode&) instead
640 inline int32_t getLong(UErrorCode
* status
) const;
641 #endif /* U_HIDE_DEPRECATED_API */
643 #ifndef U_HIDE_INTERNAL_API
645 * Internal function, do not use.
646 * TODO: figure out how to make this be non-public.
647 * NumberFormat::format(Formattable, ...
648 * needs to get at the DecimalQuantity, if it exists, for
649 * big decimal formatting.
652 number::impl::DecimalQuantity
*getDecimalQuantity() const { return fDecimalQuantity
;}
655 * Export the value of this Formattable to a DecimalQuantity.
658 void populateDecimalQuantity(number::impl::DecimalQuantity
& output
, UErrorCode
& status
) const;
661 * Adopt, and set value from, a DecimalQuantity
662 * Internal Function, do not use.
663 * @param dq the DecimalQuantity to be adopted
666 void adoptDecimalQuantity(number::impl::DecimalQuantity
*dq
);
669 * Internal function to return the CharString pointer.
670 * @param status error code
671 * @return pointer to the CharString - may become invalid if the object is modified
674 CharString
*internalGetCharString(UErrorCode
&status
);
676 #endif /* U_HIDE_INTERNAL_API */
680 * Cleans up the memory for unwanted values. For example, the adopted
681 * string or array objects.
686 * Common initialization, for use by constructors.
690 UnicodeString
* getBogus() const;
694 UnicodeString
* fString
;
704 CharString
*fDecimalStr
;
706 number::impl::DecimalQuantity
*fDecimalQuantity
;
709 UnicodeString fBogus
; // Bogus string when it's needed.
712 inline UDate
Formattable::getDate(UErrorCode
& status
) const {
713 if (fType
!= kDate
) {
714 if (U_SUCCESS(status
)) {
715 status
= U_INVALID_FORMAT_ERROR
;
722 inline const UnicodeString
& Formattable::getString(void) const {
723 return *fValue
.fString
;
726 inline UnicodeString
& Formattable::getString(void) {
727 return *fValue
.fString
;
730 #ifndef U_HIDE_DEPRECATED_API
731 inline int32_t Formattable::getLong(UErrorCode
* status
) const {
732 return getLong(*status
);
734 #endif /* U_HIDE_DEPRECATED_API */
736 inline UFormattable
* Formattable::toUFormattable() {
737 return reinterpret_cast<UFormattable
*>(this);
740 inline const UFormattable
* Formattable::toUFormattable() const {
741 return reinterpret_cast<const UFormattable
*>(this);
744 inline Formattable
* Formattable::fromUFormattable(UFormattable
*fmt
) {
745 return reinterpret_cast<Formattable
*>(fmt
);
748 inline const Formattable
* Formattable::fromUFormattable(const UFormattable
*fmt
) {
749 return reinterpret_cast<const Formattable
*>(fmt
);
754 #endif /* #if !UCONFIG_NO_FORMATTING */
756 #endif /* U_SHOW_CPLUSPLUS_API */