]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/fmtable.h
2 ********************************************************************************
3 * Copyright (C) 1997-2004, 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"
21 #if !UCONFIG_NO_FORMATTING
26 * Formattable objects can be passed to the Format class or
27 * its subclasses for formatting. Formattable is a thin wrapper
28 * class which interconverts between the primitive numeric types
29 * (double, long, etc.) as well as UDate and UnicodeString.
31 * <p>Internally, a Formattable object is a union of primitive types.
32 * As such, it can only store one flavor of data at a time. To
33 * determine what flavor of data it contains, use the getType method.
35 * <p>As of ICU 3.0, Formattable may also wrap a UObject pointer,
36 * which it owns. This allows an instance of any ICU class to be
37 * encapsulated in a Formattable. For legacy reasons and for
38 * efficiency, primitive numeric types are still stored directly
39 * within a Formattable.
41 * <p>The Formattable class is not suitable for subclassing.
43 class U_I18N_API Formattable
: public UObject
{
46 * This enum is only used to let callers distinguish between
47 * the Formattable(UDate) constructor and the Formattable(double)
48 * constructor; the compiler cannot distinguish the signatures,
49 * since UDate is currently typedefed to be either double or long.
50 * If UDate is changed later to be a bonafide class
51 * or struct, then we no longer need this enum.
54 enum ISDATE
{ kIsDate
};
60 Formattable(); // Type kLong, value 0
63 * Creates a Formattable object with a UDate instance.
64 * @param d the UDate instance.
65 * @param flag the flag to indicate this is a date. Always set it to kIsDate
68 Formattable(UDate d
, ISDATE flag
);
71 * Creates a Formattable object with a double number.
72 * @param d the double number.
75 Formattable(double d
);
78 * Creates a Formattable object with a long number.
79 * @param l the long number.
82 Formattable(int32_t l
);
85 * Creates a Formattable object with an int64_t number
86 * @param ll the int64_t number.
89 Formattable(int64_t ll
);
92 * Creates a Formattable object with a char string pointer.
93 * Assumes that the char string is null terminated.
94 * @param strToCopy the char string.
97 Formattable(const char* strToCopy
);
100 * Creates a Formattable object with a UnicodeString object to copy from.
101 * @param strToCopy the UnicodeString string.
104 Formattable(const UnicodeString
& strToCopy
);
107 * Creates a Formattable object with a UnicodeString object to adopt from.
108 * @param strToAdopt the UnicodeString string.
111 Formattable(UnicodeString
* strToAdopt
);
114 * Creates a Formattable object with an array of Formattable objects.
115 * @param arrayToCopy the Formattable object array.
116 * @param count the array count.
119 Formattable(const Formattable
* arrayToCopy
, int32_t count
);
122 * Creates a Formattable object that adopts the given UObject.
123 * @param objectToAdopt the UObject to set this object to
126 Formattable(UObject
* objectToAdopt
);
132 Formattable(const Formattable
&);
135 * Assignment operator.
136 * @param rhs The Formattable object to copy into this object.
139 Formattable
& operator=(const Formattable
&rhs
);
142 * Equality comparison.
143 * @param other the object to be compared with.
144 * @return TRUE if other are equal to this, FALSE otherwise.
147 UBool
operator==(const Formattable
&other
) const;
151 * @param other the object to be compared with.
152 * @return TRUE if other are unequal to this, FALSE otherwise.
155 UBool
operator!=(const Formattable
& other
) const
156 { return !operator==(other
); }
162 virtual ~Formattable();
166 * Clones can be used concurrently in multiple threads.
167 * If an error occurs, then NULL is returned.
168 * The caller must delete the clone.
170 * @return a clone of this object
172 * @see getDynamicClassID
175 Formattable
*clone() const;
178 * Selector for flavor of data type contained within a
179 * Formattable object. Formattable is a union of several
180 * different types, and at any time contains exactly one type.
185 * Selector indicating a UDate value. Use getDate to retrieve
192 * Selector indicating a double value. Use getDouble to
193 * retrieve the value.
199 * Selector indicating a 32-bit integer value. Use getLong to
200 * retrieve the value.
206 * Selector indicating a UnicodeString value. Use getString
207 * to retrieve the value.
213 * Selector indicating an array of Formattables. Use getArray
214 * to retrieve the value.
220 * Selector indicating a 64-bit integer value. Use getInt64
221 * to retrieve the value.
227 * Selector indicating a UObject value. Use getObject to
228 * retrieve the value.
235 * Gets the data type of this Formattable object.
236 * @return the data type of this Formattable object.
239 Type
getType(void) const;
242 * Returns TRUE if the data type of this Formattable object
243 * is kDouble, kLong, or kInt64.
244 * @return TRUE if this is a pure numeric object
247 UBool
isNumeric() const;
250 * Gets the double value of this object. If this object is not of type
251 * kDouble then the result is undefined.
252 * @return the double value of this object.
255 double getDouble(void) const { return fValue
.fDouble
; }
258 * Gets the double value of this object. If this object is of type
259 * long or int64 then a casting conversion is peformed, with
260 * possible loss of precision. If the type is kObject and the
261 * object is a Measure, then the result of
262 * getNumber().getDouble(status) is returned. If this object is
263 * neither a numeric type nor a Measure, then 0 is returned and
264 * the status is set to U_INVALID_FORMAT_ERROR.
265 * @param status the error code
266 * @return the double value of this object.
269 double getDouble(UErrorCode
& status
) const;
272 * Gets the long value of this object. If this object is not of type
273 * kLong then the result is undefined.
274 * @return the long value of this object.
277 int32_t getLong(void) const { return (int32_t)fValue
.fInt64
; }
280 * Gets the long value of this object. If the magnitude is too
281 * large to fit in a long, then the maximum or minimum long value,
282 * as appropriate, is returned and the status is set to
283 * U_INVALID_FORMAT_ERROR. If this object is of type kInt64 and
284 * it fits within a long, then no precision is lost. If it is of
285 * type kDouble, then a casting conversion is peformed, with
286 * truncation of any fractional part. If the type is kObject and
287 * the object is a Measure, then the result of
288 * getNumber().getLong(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 long value of this object.
295 int32_t getLong(UErrorCode
& status
) const;
298 * Gets the int64 value of this object. If this object is not of type
299 * kInt64 then the result is undefined.
300 * @return the int64 value of this object.
303 int64_t getInt64(void) const { return fValue
.fInt64
; }
306 * Gets the int64 value of this object. If this object is of type
307 * kDouble and the magnitude is too large to fit in an int64, then
308 * the maximum or minimum int64 value, as appropriate, is returned
309 * and the status is set to U_INVALID_FORMAT_ERROR. If the
310 * magnitude fits in an int64, then a casting conversion is
311 * peformed, with truncation of any fractional part. If the type
312 * is kObject and the object is a Measure, then the result of
313 * getNumber().getDouble(status) is returned. If this object is
314 * neither a numeric type nor a Measure, then 0 is returned and
315 * the status is set to U_INVALID_FORMAT_ERROR.
316 * @param status the error code
317 * @return the int64 value of this object.
320 int64_t getInt64(UErrorCode
& status
) const;
323 * Gets the Date value of this object. If this object is not of type
324 * kDate then the result is undefined.
325 * @return the Date value of this object.
328 UDate
getDate() const { return fValue
.fDate
; }
331 * Gets the Date value of this object. If the type is not a date,
332 * status is set to U_INVALID_FORMAT_ERROR and the return value is
334 * @param status the error code.
335 * @return the Date value of this object.
338 UDate
getDate(UErrorCode
& status
) const;
341 * Gets the string value of this object. If this object is not of type
342 * kString then the result is undefined.
343 * @param result Output param to receive the Date value of this object.
344 * @return A reference to 'result'.
347 UnicodeString
& getString(UnicodeString
& result
) const
348 { result
=*fValue
.fString
; return result
; }
351 * Gets the string value of this object. If the type is not a
352 * string, status is set to U_INVALID_FORMAT_ERROR and a bogus
353 * string is returned.
354 * @param result Output param to receive the Date value of this object.
355 * @param status the error code.
356 * @return A reference to 'result'.
359 UnicodeString
& getString(UnicodeString
& result
, UErrorCode
& status
) const;
362 * Gets a const reference to the string value of this object. If
363 * this object is not of type kString then the result is
365 * @return a const reference to the string value of this object.
368 inline const UnicodeString
& getString(void) const;
371 * Gets a const reference to the string value of this object. If
372 * the type is not a string, status is set to
373 * U_INVALID_FORMAT_ERROR and the result is a bogus string.
374 * @param status the error code.
375 * @return a const reference to the string value of this object.
378 const UnicodeString
& getString(UErrorCode
& status
) const;
381 * Gets a reference to the string value of this object. If this
382 * object is not of type kString then the result is undefined.
383 * @return a reference to the string value of this object.
386 inline UnicodeString
& getString(void);
389 * Gets a reference to the string value of this object. If the
390 * type is not a string, status is set to U_INVALID_FORMAT_ERROR
391 * and the result is a bogus string.
392 * @param status the error code.
393 * @return a reference to the string value of this object.
396 UnicodeString
& getString(UErrorCode
& status
);
399 * Gets the array value and count of this object. If this object
400 * is not of type kArray then the result is undefined.
401 * @param count fill-in with the count of this object.
402 * @return the array value of this object.
405 const Formattable
* getArray(int32_t& count
) const
406 { count
=fValue
.fArrayAndCount
.fCount
; return fValue
.fArrayAndCount
.fArray
; }
409 * Gets the array value and count of this object. If the type is
410 * not an array, status is set to U_INVALID_FORMAT_ERROR, count is
411 * set to 0, and the result is NULL.
412 * @param count fill-in with the count of this object.
413 * @param status the error code.
414 * @return the array value of this object.
417 const Formattable
* getArray(int32_t& count
, UErrorCode
& status
) const;
420 * Accesses the specified element in the array value of this
421 * Formattable object. If this object is not of type kArray then
422 * the result is undefined.
423 * @param index the specified index.
424 * @return the accessed element in the array.
427 Formattable
& operator[](int32_t index
) { return fValue
.fArrayAndCount
.fArray
[index
]; }
430 * Returns a pointer to the UObject contained within this
431 * formattable, or NULL if this object does not contain a UObject.
432 * @return a UObject pointer, or NULL
435 const UObject
* getObject() const;
438 * Sets the double value of this object and changes the type to
440 * @param d the new double value to be set.
443 void setDouble(double d
);
446 * Sets the long value of this object and changes the type to
448 * @param l the new long value to be set.
451 void setLong(int32_t l
);
454 * Sets the int64 value of this object and changes the type to
456 * @param ll the new int64 value to be set.
459 void setInt64(int64_t ll
);
462 * Sets the Date value of this object and changes the type to
464 * @param d the new Date value to be set.
467 void setDate(UDate d
);
470 * Sets the string value of this object and changes the type to
472 * @param stringToCopy the new string value to be set.
475 void setString(const UnicodeString
& stringToCopy
);
478 * Sets the array value and count of this object and changes the
480 * @param array the array value.
481 * @param count the number of array elements to be copied.
484 void setArray(const Formattable
* array
, int32_t count
);
487 * Sets and adopts the string value and count of this object and
488 * changes the type to kArray.
489 * @param stringToAdopt the new string value to be adopted.
492 void adoptString(UnicodeString
* stringToAdopt
);
495 * Sets and adopts the array value and count of this object and
496 * changes the type to kArray.
499 void adoptArray(Formattable
* array
, int32_t count
);
502 * Sets and adopts the UObject value of this object and changes
503 * the type to kObject. After this call, the caller must not
504 * delete the given object.
505 * @param objectToAdopt the UObject value to be adopted
508 void adoptObject(UObject
* objectToAdopt
);
511 * ICU "poor man's RTTI", returns a UClassID for the actual class.
515 virtual UClassID
getDynamicClassID() const;
518 * ICU "poor man's RTTI", returns a UClassID for this class.
522 static UClassID U_EXPORT2
getStaticClassID();
525 * Deprecated variant of getLong(UErrorCode&).
526 * @param status the error code
527 * @return the long value of this object.
528 * @deprecated ICU 3.0 use getLong(UErrorCode&) instead
530 inline int32_t getLong(UErrorCode
* status
) const;
534 * Cleans up the memory for unwanted values. For example, the adopted
535 * string or array objects.
540 * Creates a new Formattable array and copies the values from the specified
542 * @param array the original array
543 * @param count the original array count
544 * @return the new Formattable array.
546 static Formattable
* createArrayCopy(const Formattable
* array
, int32_t count
);
548 UnicodeString
* getBogus() const;
552 UnicodeString
* fString
;
563 UnicodeString fBogus
; // Bogus string when it's needed.
567 Formattable::createArrayCopy(const Formattable
* array
, int32_t count
)
569 Formattable
*result
= new Formattable
[count
];
570 for (int32_t i
=0; i
<count
; ++i
) result
[i
] = array
[i
]; // Don't memcpy!
574 inline UDate
Formattable::getDate(UErrorCode
& status
) const {
575 if (fType
!= kDate
) {
576 if (U_SUCCESS(status
)) {
577 status
= U_INVALID_FORMAT_ERROR
;
584 inline const UnicodeString
& Formattable::getString(void) const {
585 return *fValue
.fString
;
588 inline UnicodeString
& Formattable::getString(void) {
589 return *fValue
.fString
;
592 inline int32_t Formattable::getLong(UErrorCode
* status
) const {
593 return getLong(*status
);
598 #endif /* #if !UCONFIG_NO_FORMATTING */