]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/fmtable.h
2 ********************************************************************************
3 * Copyright (C) 1997-2010, 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
35 * Formattable objects can be passed to the Format class or
36 * its subclasses for formatting. Formattable is a thin wrapper
37 * class which interconverts between the primitive numeric types
38 * (double, long, etc.) as well as UDate and UnicodeString.
40 * <p>Internally, a Formattable object is a union of primitive types.
41 * As such, it can only store one flavor of data at a time. To
42 * determine what flavor of data it contains, use the getType method.
44 * <p>As of ICU 3.0, Formattable may also wrap a UObject pointer,
45 * which it owns. This allows an instance of any ICU class to be
46 * encapsulated in a Formattable. For legacy reasons and for
47 * efficiency, primitive numeric types are still stored directly
48 * within a Formattable.
50 * <p>The Formattable class is not suitable for subclassing.
52 class U_I18N_API Formattable
: public UObject
{
55 * This enum is only used to let callers distinguish between
56 * the Formattable(UDate) constructor and the Formattable(double)
57 * constructor; the compiler cannot distinguish the signatures,
58 * since UDate is currently typedefed to be either double or long.
59 * If UDate is changed later to be a bonafide class
60 * or struct, then we no longer need this enum.
63 enum ISDATE
{ kIsDate
};
69 Formattable(); // Type kLong, value 0
72 * Creates a Formattable object with a UDate instance.
73 * @param d the UDate instance.
74 * @param flag the flag to indicate this is a date. Always set it to kIsDate
77 Formattable(UDate d
, ISDATE flag
);
80 * Creates a Formattable object with a double number.
81 * @param d the double number.
84 Formattable(double d
);
87 * Creates a Formattable object with a long number.
88 * @param l the long number.
91 Formattable(int32_t l
);
94 * Creates a Formattable object with an int64_t number
95 * @param ll the int64_t number.
98 Formattable(int64_t ll
);
100 #if !UCONFIG_NO_CONVERSION
102 * Creates a Formattable object with a char string pointer.
103 * Assumes that the char string is null terminated.
104 * @param strToCopy the char string.
107 Formattable(const char* strToCopy
);
111 * Creates a Formattable object of an appropriate numeric type from a
112 * a decimal number in string form. The Formattable will retain the
113 * full precision of the input in decimal format, even when it exceeds
114 * what can be represented by a double of int64_t.
116 * @param number the unformatted (not localized) string representation
117 * of the Decimal number.
118 * @param status the error code. Possible errors include U_INVALID_FORMAT_ERROR
119 * if the format of the string does not conform to that of a
123 Formattable(const StringPiece
&number
, UErrorCode
&status
);
126 * Creates a Formattable object with a UnicodeString object to copy from.
127 * @param strToCopy the UnicodeString string.
130 Formattable(const UnicodeString
& strToCopy
);
133 * Creates a Formattable object with a UnicodeString object to adopt from.
134 * @param strToAdopt the UnicodeString string.
137 Formattable(UnicodeString
* strToAdopt
);
140 * Creates a Formattable object with an array of Formattable objects.
141 * @param arrayToCopy the Formattable object array.
142 * @param count the array count.
145 Formattable(const Formattable
* arrayToCopy
, int32_t count
);
148 * Creates a Formattable object that adopts the given UObject.
149 * @param objectToAdopt the UObject to set this object to
152 Formattable(UObject
* objectToAdopt
);
158 Formattable(const Formattable
&);
161 * Assignment operator.
162 * @param rhs The Formattable object to copy into this object.
165 Formattable
& operator=(const Formattable
&rhs
);
168 * Equality comparison.
169 * @param other the object to be compared with.
170 * @return TRUE if other are equal to this, FALSE otherwise.
173 UBool
operator==(const Formattable
&other
) const;
177 * @param other the object to be compared with.
178 * @return TRUE if other are unequal to this, FALSE otherwise.
181 UBool
operator!=(const Formattable
& other
) const
182 { return !operator==(other
); }
188 virtual ~Formattable();
192 * Clones can be used concurrently in multiple threads.
193 * If an error occurs, then NULL is returned.
194 * The caller must delete the clone.
196 * @return a clone of this object
198 * @see getDynamicClassID
201 Formattable
*clone() const;
204 * Selector for flavor of data type contained within a
205 * Formattable object. Formattable is a union of several
206 * different types, and at any time contains exactly one type.
211 * Selector indicating a UDate value. Use getDate to retrieve
218 * Selector indicating a double value. Use getDouble to
219 * retrieve the value.
225 * Selector indicating a 32-bit integer value. Use getLong to
226 * retrieve the value.
232 * Selector indicating a UnicodeString value. Use getString
233 * to retrieve the value.
239 * Selector indicating an array of Formattables. Use getArray
240 * to retrieve the value.
246 * Selector indicating a 64-bit integer value. Use getInt64
247 * to retrieve the value.
253 * Selector indicating a UObject value. Use getObject to
254 * retrieve the value.
261 * Gets the data type of this Formattable object.
262 * @return the data type of this Formattable object.
265 Type
getType(void) const;
268 * Returns TRUE if the data type of this Formattable object
269 * is kDouble, kLong, kInt64 or kDecimalNumber.
270 * @return TRUE if this is a pure numeric object
273 UBool
isNumeric() const;
276 * Gets the double value of this object. If this object is not of type
277 * kDouble then the result is undefined.
278 * @return the double value of this object.
281 double getDouble(void) const { return fValue
.fDouble
; }
284 * Gets the double value of this object. If this object is of type
285 * long, int64 or Decimal Number then a conversion is peformed, with
286 * possible loss of precision. If the type is kObject and the
287 * object is a Measure, then the result of
288 * getNumber().getDouble(status) is returned. If this object is
289 * neither a numeric type nor a Measure, then 0 is returned and
290 * the status is set to U_INVALID_FORMAT_ERROR.
291 * @param status the error code
292 * @return the double value of this object.
295 double getDouble(UErrorCode
& status
) const;
298 * Gets the long value of this object. If this object is not of type
299 * kLong then the result is undefined.
300 * @return the long value of this object.
303 int32_t getLong(void) const { return (int32_t)fValue
.fInt64
; }
306 * Gets the long value of this object. If the magnitude is too
307 * large to fit in a long, then the maximum or minimum long value,
308 * as appropriate, is returned and the status is set to
309 * U_INVALID_FORMAT_ERROR. If this object is of type kInt64 and
310 * it fits within a long, then no precision is lost. If it is of
311 * type kDouble or kDecimalNumber, then a conversion is peformed, with
312 * truncation of any fractional part. If the type is kObject and
313 * the object is a Measure, then the result of
314 * getNumber().getLong(status) is returned. If this object is
315 * neither a numeric type nor a Measure, then 0 is returned and
316 * the status is set to U_INVALID_FORMAT_ERROR.
317 * @param status the error code
318 * @return the long value of this object.
321 int32_t getLong(UErrorCode
& status
) const;
324 * Gets the int64 value of this object. If this object is not of type
325 * kInt64 then the result is undefined.
326 * @return the int64 value of this object.
329 int64_t getInt64(void) const { return fValue
.fInt64
; }
332 * Gets the int64 value of this object. If this object is of a numeric
333 * type and the magnitude is too large to fit in an int64, then
334 * the maximum or minimum int64 value, as appropriate, is returned
335 * and the status is set to U_INVALID_FORMAT_ERROR. If the
336 * magnitude fits in an int64, then a casting conversion is
337 * peformed, with truncation of any fractional part. If the type
338 * is kObject and the object is a Measure, then the result of
339 * getNumber().getDouble(status) is returned. If this object is
340 * neither a numeric type nor a Measure, then 0 is returned and
341 * the status is set to U_INVALID_FORMAT_ERROR.
342 * @param status the error code
343 * @return the int64 value of this object.
346 int64_t getInt64(UErrorCode
& status
) const;
349 * Gets the Date value of this object. If this object is not of type
350 * kDate then the result is undefined.
351 * @return the Date value of this object.
354 UDate
getDate() const { return fValue
.fDate
; }
357 * Gets the Date value of this object. If the type is not a date,
358 * status is set to U_INVALID_FORMAT_ERROR and the return value is
360 * @param status the error code.
361 * @return the Date value of this object.
364 UDate
getDate(UErrorCode
& status
) const;
367 * Gets the string value of this object. If this object is not of type
368 * kString then the result is undefined.
369 * @param result Output param to receive the Date value of this object.
370 * @return A reference to 'result'.
373 UnicodeString
& getString(UnicodeString
& result
) const
374 { result
=*fValue
.fString
; return result
; }
377 * Gets the string value of this object. If the type is not a
378 * string, status is set to U_INVALID_FORMAT_ERROR and a bogus
379 * string is returned.
380 * @param result Output param to receive the Date value of this object.
381 * @param status the error code.
382 * @return A reference to 'result'.
385 UnicodeString
& getString(UnicodeString
& result
, UErrorCode
& status
) const;
388 * Gets a const reference to the string value of this object. If
389 * this object is not of type kString then the result is
391 * @return a const reference to the string value of this object.
394 inline const UnicodeString
& getString(void) const;
397 * Gets a const reference to the string value of this object. If
398 * the type is not a string, status is set to
399 * U_INVALID_FORMAT_ERROR and the result is a bogus string.
400 * @param status the error code.
401 * @return a const reference to the string value of this object.
404 const UnicodeString
& getString(UErrorCode
& status
) const;
407 * Gets a reference to the string value of this object. If this
408 * object is not of type kString then the result is undefined.
409 * @return a reference to the string value of this object.
412 inline UnicodeString
& getString(void);
415 * Gets a reference to the string value of this object. If the
416 * type is not a string, status is set to U_INVALID_FORMAT_ERROR
417 * and the result is a bogus string.
418 * @param status the error code.
419 * @return a reference to the string value of this object.
422 UnicodeString
& getString(UErrorCode
& status
);
425 * Gets the array value and count of this object. If this object
426 * is not of type kArray then the result is undefined.
427 * @param count fill-in with the count of this object.
428 * @return the array value of this object.
431 const Formattable
* getArray(int32_t& count
) const
432 { count
=fValue
.fArrayAndCount
.fCount
; return fValue
.fArrayAndCount
.fArray
; }
435 * Gets the array value and count of this object. If the type is
436 * not an array, status is set to U_INVALID_FORMAT_ERROR, count is
437 * set to 0, and the result is NULL.
438 * @param count fill-in with the count of this object.
439 * @param status the error code.
440 * @return the array value of this object.
443 const Formattable
* getArray(int32_t& count
, UErrorCode
& status
) const;
446 * Accesses the specified element in the array value of this
447 * Formattable object. If this object is not of type kArray then
448 * the result is undefined.
449 * @param index the specified index.
450 * @return the accessed element in the array.
453 Formattable
& operator[](int32_t index
) { return fValue
.fArrayAndCount
.fArray
[index
]; }
456 * Returns a pointer to the UObject contained within this
457 * formattable, or NULL if this object does not contain a UObject.
458 * @return a UObject pointer, or NULL
461 const UObject
* getObject() const;
464 * Returns a numeric string representation of the number contained within this
465 * formattable, or NULL if this object does not contain numeric type.
466 * For values obtained by parsing, the returned decimal number retains
467 * the full precision and range of the original input, unconstrained by
468 * the limits of a double floating point or a 64 bit int.
470 * This function is not thread safe, and therfore is not declared const,
471 * even though it is logically const.
473 * Possible errors include U_MEMORY_ALLOCATION_ERROR, and
474 * U_INVALID_STATE if the formattable object has not been set to
477 * @param status the error code.
478 * @return the unformatted string representation of a number.
481 StringPiece
getDecimalNumber(UErrorCode
&status
);
484 * Sets the double value of this object and changes the type to
486 * @param d the new double value to be set.
489 void setDouble(double d
);
492 * Sets the long value of this object and changes the type to
494 * @param l the new long value to be set.
497 void setLong(int32_t l
);
500 * Sets the int64 value of this object and changes the type to
502 * @param ll the new int64 value to be set.
505 void setInt64(int64_t ll
);
508 * Sets the Date value of this object and changes the type to
510 * @param d the new Date value to be set.
513 void setDate(UDate d
);
516 * Sets the string value of this object and changes the type to
518 * @param stringToCopy the new string value to be set.
521 void setString(const UnicodeString
& stringToCopy
);
524 * Sets the array value and count of this object and changes the
526 * @param array the array value.
527 * @param count the number of array elements to be copied.
530 void setArray(const Formattable
* array
, int32_t count
);
533 * Sets and adopts the string value and count of this object and
534 * changes the type to kArray.
535 * @param stringToAdopt the new string value to be adopted.
538 void adoptString(UnicodeString
* stringToAdopt
);
541 * Sets and adopts the array value and count of this object and
542 * changes the type to kArray.
545 void adoptArray(Formattable
* array
, int32_t count
);
548 * Sets and adopts the UObject value of this object and changes
549 * the type to kObject. After this call, the caller must not
550 * delete the given object.
551 * @param objectToAdopt the UObject value to be adopted
554 void adoptObject(UObject
* objectToAdopt
);
557 * Sets the the numeric value from a decimal number string, and changes
558 * the type to to a numeric type appropriate for the number.
559 * The syntax of the number is a "numeric string"
560 * as defined in the Decimal Arithmetic Specification, available at
561 * http://speleotrove.com/decimal
562 * The full precision and range of the input number will be retained,
563 * even when it exceeds what can be represented by a double or an int64.
565 * @param numberString a string representation of the unformatted decimal number.
566 * @param status the error code. Set to U_INVALID_FORMAT_ERROR if the
567 * incoming string is not a valid decimal number.
570 void setDecimalNumber(const StringPiece
&numberString
,
574 * ICU "poor man's RTTI", returns a UClassID for the actual class.
578 virtual UClassID
getDynamicClassID() const;
581 * ICU "poor man's RTTI", returns a UClassID for this class.
585 static UClassID U_EXPORT2
getStaticClassID();
588 * Deprecated variant of getLong(UErrorCode&).
589 * @param status the error code
590 * @return the long value of this object.
591 * @deprecated ICU 3.0 use getLong(UErrorCode&) instead
593 inline int32_t getLong(UErrorCode
* status
) const;
596 * Internal function, do not use.
597 * TODO: figure out how to make this be non-public.
598 * NumberFormat::format(Formattable, ...
599 * needs to get at the DigitList, if it exists, for
600 * big decimal formatting.
603 DigitList
*getDigitList() const { return fDecimalNum
;};
606 * Adopt, and set value from, a DigitList
607 * Internal Function, do not use.
608 * @param dl the Digit List to be adopted
611 void adoptDigitList(DigitList
*dl
);
615 * Cleans up the memory for unwanted values. For example, the adopted
616 * string or array objects.
621 * Common initialization, for use by constructors.
625 UnicodeString
* getBogus() const;
629 UnicodeString
* fString
;
639 CharString
*fDecimalStr
;
640 DigitList
*fDecimalNum
;
643 UnicodeString fBogus
; // Bogus string when it's needed.
646 inline UDate
Formattable::getDate(UErrorCode
& status
) const {
647 if (fType
!= kDate
) {
648 if (U_SUCCESS(status
)) {
649 status
= U_INVALID_FORMAT_ERROR
;
656 inline const UnicodeString
& Formattable::getString(void) const {
657 return *fValue
.fString
;
660 inline UnicodeString
& Formattable::getString(void) {
661 return *fValue
.fString
;
664 inline int32_t Formattable::getLong(UErrorCode
* status
) const {
665 return getLong(*status
);
670 #endif /* #if !UCONFIG_NO_FORMATTING */