]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/fmtable.h
2 ********************************************************************************
3 * Copyright (C) 1997-2006, 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"
22 * \brief C++ API: Formattable is a thin wrapper for primitive numeric types.
25 #if !UCONFIG_NO_FORMATTING
30 * Formattable objects can be passed to the Format class or
31 * its subclasses for formatting. Formattable is a thin wrapper
32 * class which interconverts between the primitive numeric types
33 * (double, long, etc.) as well as UDate and UnicodeString.
35 * <p>Internally, a Formattable object is a union of primitive types.
36 * As such, it can only store one flavor of data at a time. To
37 * determine what flavor of data it contains, use the getType method.
39 * <p>As of ICU 3.0, Formattable may also wrap a UObject pointer,
40 * which it owns. This allows an instance of any ICU class to be
41 * encapsulated in a Formattable. For legacy reasons and for
42 * efficiency, primitive numeric types are still stored directly
43 * within a Formattable.
45 * <p>The Formattable class is not suitable for subclassing.
47 class U_I18N_API Formattable
: public UObject
{
50 * This enum is only used to let callers distinguish between
51 * the Formattable(UDate) constructor and the Formattable(double)
52 * constructor; the compiler cannot distinguish the signatures,
53 * since UDate is currently typedefed to be either double or long.
54 * If UDate is changed later to be a bonafide class
55 * or struct, then we no longer need this enum.
58 enum ISDATE
{ kIsDate
};
64 Formattable(); // Type kLong, value 0
67 * Creates a Formattable object with a UDate instance.
68 * @param d the UDate instance.
69 * @param flag the flag to indicate this is a date. Always set it to kIsDate
72 Formattable(UDate d
, ISDATE flag
);
75 * Creates a Formattable object with a double number.
76 * @param d the double number.
79 Formattable(double d
);
82 * Creates a Formattable object with a long number.
83 * @param l the long number.
86 Formattable(int32_t l
);
89 * Creates a Formattable object with an int64_t number
90 * @param ll the int64_t number.
93 Formattable(int64_t ll
);
95 #if !UCONFIG_NO_CONVERSION
97 * Creates a Formattable object with a char string pointer.
98 * Assumes that the char string is null terminated.
99 * @param strToCopy the char string.
102 Formattable(const char* strToCopy
);
106 * Creates a Formattable object with a UnicodeString object to copy from.
107 * @param strToCopy the UnicodeString string.
110 Formattable(const UnicodeString
& strToCopy
);
113 * Creates a Formattable object with a UnicodeString object to adopt from.
114 * @param strToAdopt the UnicodeString string.
117 Formattable(UnicodeString
* strToAdopt
);
120 * Creates a Formattable object with an array of Formattable objects.
121 * @param arrayToCopy the Formattable object array.
122 * @param count the array count.
125 Formattable(const Formattable
* arrayToCopy
, int32_t count
);
128 * Creates a Formattable object that adopts the given UObject.
129 * @param objectToAdopt the UObject to set this object to
132 Formattable(UObject
* objectToAdopt
);
138 Formattable(const Formattable
&);
141 * Assignment operator.
142 * @param rhs The Formattable object to copy into this object.
145 Formattable
& operator=(const Formattable
&rhs
);
148 * Equality comparison.
149 * @param other the object to be compared with.
150 * @return TRUE if other are equal to this, FALSE otherwise.
153 UBool
operator==(const Formattable
&other
) const;
157 * @param other the object to be compared with.
158 * @return TRUE if other are unequal to this, FALSE otherwise.
161 UBool
operator!=(const Formattable
& other
) const
162 { return !operator==(other
); }
168 virtual ~Formattable();
172 * Clones can be used concurrently in multiple threads.
173 * If an error occurs, then NULL is returned.
174 * The caller must delete the clone.
176 * @return a clone of this object
178 * @see getDynamicClassID
181 Formattable
*clone() const;
184 * Selector for flavor of data type contained within a
185 * Formattable object. Formattable is a union of several
186 * different types, and at any time contains exactly one type.
191 * Selector indicating a UDate value. Use getDate to retrieve
198 * Selector indicating a double value. Use getDouble to
199 * retrieve the value.
205 * Selector indicating a 32-bit integer value. Use getLong to
206 * retrieve the value.
212 * Selector indicating a UnicodeString value. Use getString
213 * to retrieve the value.
219 * Selector indicating an array of Formattables. Use getArray
220 * to retrieve the value.
226 * Selector indicating a 64-bit integer value. Use getInt64
227 * to retrieve the value.
233 * Selector indicating a UObject value. Use getObject to
234 * retrieve the value.
241 * Gets the data type of this Formattable object.
242 * @return the data type of this Formattable object.
245 Type
getType(void) const;
248 * Returns TRUE if the data type of this Formattable object
249 * is kDouble, kLong, or kInt64.
250 * @return TRUE if this is a pure numeric object
253 UBool
isNumeric() const;
256 * Gets the double value of this object. If this object is not of type
257 * kDouble then the result is undefined.
258 * @return the double value of this object.
261 double getDouble(void) const { return fValue
.fDouble
; }
264 * Gets the double value of this object. If this object is of type
265 * long or int64 then a casting conversion is peformed, with
266 * possible loss of precision. If the type is kObject and the
267 * object is a Measure, then the result of
268 * getNumber().getDouble(status) is returned. If this object is
269 * neither a numeric type nor a Measure, then 0 is returned and
270 * the status is set to U_INVALID_FORMAT_ERROR.
271 * @param status the error code
272 * @return the double value of this object.
275 double getDouble(UErrorCode
& status
) const;
278 * Gets the long value of this object. If this object is not of type
279 * kLong then the result is undefined.
280 * @return the long value of this object.
283 int32_t getLong(void) const { return (int32_t)fValue
.fInt64
; }
286 * Gets the long value of this object. If the magnitude is too
287 * large to fit in a long, then the maximum or minimum long value,
288 * as appropriate, is returned and the status is set to
289 * U_INVALID_FORMAT_ERROR. If this object is of type kInt64 and
290 * it fits within a long, then no precision is lost. If it is of
291 * type kDouble, then a casting conversion is peformed, with
292 * truncation of any fractional part. If the type is kObject and
293 * the object is a Measure, then the result of
294 * getNumber().getLong(status) is returned. If this object is
295 * neither a numeric type nor a Measure, then 0 is returned and
296 * the status is set to U_INVALID_FORMAT_ERROR.
297 * @param status the error code
298 * @return the long value of this object.
301 int32_t getLong(UErrorCode
& status
) const;
304 * Gets the int64 value of this object. If this object is not of type
305 * kInt64 then the result is undefined.
306 * @return the int64 value of this object.
309 int64_t getInt64(void) const { return fValue
.fInt64
; }
312 * Gets the int64 value of this object. If this object is of type
313 * kDouble and the magnitude is too large to fit in an int64, then
314 * the maximum or minimum int64 value, as appropriate, is returned
315 * and the status is set to U_INVALID_FORMAT_ERROR. If the
316 * magnitude fits in an int64, then a casting conversion is
317 * peformed, with truncation of any fractional part. If the type
318 * is kObject and the object is a Measure, then the result of
319 * getNumber().getDouble(status) is returned. If this object is
320 * neither a numeric type nor a Measure, then 0 is returned and
321 * the status is set to U_INVALID_FORMAT_ERROR.
322 * @param status the error code
323 * @return the int64 value of this object.
326 int64_t getInt64(UErrorCode
& status
) const;
329 * Gets the Date value of this object. If this object is not of type
330 * kDate then the result is undefined.
331 * @return the Date value of this object.
334 UDate
getDate() const { return fValue
.fDate
; }
337 * Gets the Date value of this object. If the type is not a date,
338 * status is set to U_INVALID_FORMAT_ERROR and the return value is
340 * @param status the error code.
341 * @return the Date value of this object.
344 UDate
getDate(UErrorCode
& status
) const;
347 * Gets the string value of this object. If this object is not of type
348 * kString then the result is undefined.
349 * @param result Output param to receive the Date value of this object.
350 * @return A reference to 'result'.
353 UnicodeString
& getString(UnicodeString
& result
) const
354 { result
=*fValue
.fString
; return result
; }
357 * Gets the string value of this object. If the type is not a
358 * string, status is set to U_INVALID_FORMAT_ERROR and a bogus
359 * string is returned.
360 * @param result Output param to receive the Date value of this object.
361 * @param status the error code.
362 * @return A reference to 'result'.
365 UnicodeString
& getString(UnicodeString
& result
, UErrorCode
& status
) const;
368 * Gets a const reference to the string value of this object. If
369 * this object is not of type kString then the result is
371 * @return a const reference to the string value of this object.
374 inline const UnicodeString
& getString(void) const;
377 * Gets a const reference to the string value of this object. If
378 * the type is not a string, status is set to
379 * U_INVALID_FORMAT_ERROR and the result is a bogus string.
380 * @param status the error code.
381 * @return a const reference to the string value of this object.
384 const UnicodeString
& getString(UErrorCode
& status
) const;
387 * Gets a reference to the string value of this object. If this
388 * object is not of type kString then the result is undefined.
389 * @return a reference to the string value of this object.
392 inline UnicodeString
& getString(void);
395 * Gets a reference to the string value of this object. If the
396 * type is not a string, status is set to U_INVALID_FORMAT_ERROR
397 * and the result is a bogus string.
398 * @param status the error code.
399 * @return a reference to the string value of this object.
402 UnicodeString
& getString(UErrorCode
& status
);
405 * Gets the array value and count of this object. If this object
406 * is not of type kArray then the result is undefined.
407 * @param count fill-in with the count of this object.
408 * @return the array value of this object.
411 const Formattable
* getArray(int32_t& count
) const
412 { count
=fValue
.fArrayAndCount
.fCount
; return fValue
.fArrayAndCount
.fArray
; }
415 * Gets the array value and count of this object. If the type is
416 * not an array, status is set to U_INVALID_FORMAT_ERROR, count is
417 * set to 0, and the result is NULL.
418 * @param count fill-in with the count of this object.
419 * @param status the error code.
420 * @return the array value of this object.
423 const Formattable
* getArray(int32_t& count
, UErrorCode
& status
) const;
426 * Accesses the specified element in the array value of this
427 * Formattable object. If this object is not of type kArray then
428 * the result is undefined.
429 * @param index the specified index.
430 * @return the accessed element in the array.
433 Formattable
& operator[](int32_t index
) { return fValue
.fArrayAndCount
.fArray
[index
]; }
436 * Returns a pointer to the UObject contained within this
437 * formattable, or NULL if this object does not contain a UObject.
438 * @return a UObject pointer, or NULL
441 const UObject
* getObject() const;
444 * Sets the double value of this object and changes the type to
446 * @param d the new double value to be set.
449 void setDouble(double d
);
452 * Sets the long value of this object and changes the type to
454 * @param l the new long value to be set.
457 void setLong(int32_t l
);
460 * Sets the int64 value of this object and changes the type to
462 * @param ll the new int64 value to be set.
465 void setInt64(int64_t ll
);
468 * Sets the Date value of this object and changes the type to
470 * @param d the new Date value to be set.
473 void setDate(UDate d
);
476 * Sets the string value of this object and changes the type to
478 * @param stringToCopy the new string value to be set.
481 void setString(const UnicodeString
& stringToCopy
);
484 * Sets the array value and count of this object and changes the
486 * @param array the array value.
487 * @param count the number of array elements to be copied.
490 void setArray(const Formattable
* array
, int32_t count
);
493 * Sets and adopts the string value and count of this object and
494 * changes the type to kArray.
495 * @param stringToAdopt the new string value to be adopted.
498 void adoptString(UnicodeString
* stringToAdopt
);
501 * Sets and adopts the array value and count of this object and
502 * changes the type to kArray.
505 void adoptArray(Formattable
* array
, int32_t count
);
508 * Sets and adopts the UObject value of this object and changes
509 * the type to kObject. After this call, the caller must not
510 * delete the given object.
511 * @param objectToAdopt the UObject value to be adopted
514 void adoptObject(UObject
* objectToAdopt
);
517 * ICU "poor man's RTTI", returns a UClassID for the actual class.
521 virtual UClassID
getDynamicClassID() const;
524 * ICU "poor man's RTTI", returns a UClassID for this class.
528 static UClassID U_EXPORT2
getStaticClassID();
531 * Deprecated variant of getLong(UErrorCode&).
532 * @param status the error code
533 * @return the long value of this object.
534 * @deprecated ICU 3.0 use getLong(UErrorCode&) instead
536 inline int32_t getLong(UErrorCode
* status
) const;
540 * Cleans up the memory for unwanted values. For example, the adopted
541 * string or array objects.
545 UnicodeString
* getBogus() const;
549 UnicodeString
* fString
;
560 UnicodeString fBogus
; // Bogus string when it's needed.
563 inline UDate
Formattable::getDate(UErrorCode
& status
) const {
564 if (fType
!= kDate
) {
565 if (U_SUCCESS(status
)) {
566 status
= U_INVALID_FORMAT_ERROR
;
573 inline const UnicodeString
& Formattable::getString(void) const {
574 return *fValue
.fString
;
577 inline UnicodeString
& Formattable::getString(void) {
578 return *fValue
.fString
;
581 inline int32_t Formattable::getLong(UErrorCode
* status
) const {
582 return getLong(*status
);
587 #endif /* #if !UCONFIG_NO_FORMATTING */