]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/unicode/fmtable.h
ICU-400.42.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / fmtable.h
CommitLineData
b75a7d8f
A
1/*
2********************************************************************************
73c04bcf 3* Copyright (C) 1997-2006, International Business Machines
b75a7d8f
A
4* Corporation and others. All Rights Reserved.
5********************************************************************************
6*
7* File FMTABLE.H
8*
9* Modification History:
10*
11* Date Name Description
12* 02/29/97 aliu Creation.
13********************************************************************************
14*/
15#ifndef FMTABLE_H
16#define FMTABLE_H
17
b75a7d8f 18#include "unicode/utypes.h"
374ca955 19#include "unicode/unistr.h"
73c04bcf
A
20/**
21 * \file
22 * \brief C++ API: Formattable is a thin wrapper for primitive numeric types.
23 */
b75a7d8f
A
24
25#if !UCONFIG_NO_FORMATTING
26
b75a7d8f
A
27U_NAMESPACE_BEGIN
28
29/**
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.
374ca955
A
34 *
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.
38 *
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.
44 *
45 * <p>The Formattable class is not suitable for subclassing.
b75a7d8f
A
46 */
47class U_I18N_API Formattable : public UObject {
48public:
49 /**
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.
374ca955 56 * @stable ICU 2.4
b75a7d8f
A
57 */
58 enum ISDATE { kIsDate };
59
60 /**
61 * Default constructor
374ca955 62 * @stable ICU 2.4
b75a7d8f
A
63 */
64 Formattable(); // Type kLong, value 0
374ca955 65
b75a7d8f
A
66 /**
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
70 * @stable ICU 2.0
71 */
72 Formattable(UDate d, ISDATE flag);
374ca955 73
b75a7d8f
A
74 /**
75 * Creates a Formattable object with a double number.
76 * @param d the double number.
77 * @stable ICU 2.0
78 */
79 Formattable(double d);
374ca955 80
b75a7d8f
A
81 /**
82 * Creates a Formattable object with a long number.
83 * @param l the long number.
84 * @stable ICU 2.0
85 */
86 Formattable(int32_t l);
374ca955
A
87
88 /**
89 * Creates a Formattable object with an int64_t number
90 * @param ll the int64_t number.
73c04bcf 91 * @stable ICU 2.8
374ca955
A
92 */
93 Formattable(int64_t ll);
94
73c04bcf 95#if !UCONFIG_NO_CONVERSION
b75a7d8f
A
96 /**
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.
100 * @stable ICU 2.0
101 */
102 Formattable(const char* strToCopy);
73c04bcf 103#endif
374ca955 104
b75a7d8f
A
105 /**
106 * Creates a Formattable object with a UnicodeString object to copy from.
107 * @param strToCopy the UnicodeString string.
108 * @stable ICU 2.0
109 */
110 Formattable(const UnicodeString& strToCopy);
374ca955 111
b75a7d8f
A
112 /**
113 * Creates a Formattable object with a UnicodeString object to adopt from.
114 * @param strToAdopt the UnicodeString string.
115 * @stable ICU 2.0
116 */
117 Formattable(UnicodeString* strToAdopt);
374ca955 118
b75a7d8f
A
119 /**
120 * Creates a Formattable object with an array of Formattable objects.
121 * @param arrayToCopy the Formattable object array.
122 * @param count the array count.
123 * @stable ICU 2.0
124 */
125 Formattable(const Formattable* arrayToCopy, int32_t count);
126
374ca955
A
127 /**
128 * Creates a Formattable object that adopts the given UObject.
129 * @param objectToAdopt the UObject to set this object to
73c04bcf 130 * @stable ICU 3.0
374ca955
A
131 */
132 Formattable(UObject* objectToAdopt);
133
b75a7d8f
A
134 /**
135 * Copy constructor.
136 * @stable ICU 2.0
137 */
138 Formattable(const Formattable&);
374ca955 139
b75a7d8f
A
140 /**
141 * Assignment operator.
142 * @param rhs The Formattable object to copy into this object.
143 * @stable ICU 2.0
144 */
145 Formattable& operator=(const Formattable &rhs);
374ca955 146
b75a7d8f
A
147 /**
148 * Equality comparison.
149 * @param other the object to be compared with.
150 * @return TRUE if other are equal to this, FALSE otherwise.
151 * @stable ICU 2.0
152 */
153 UBool operator==(const Formattable &other) const;
154
155 /**
156 * Equality operator.
157 * @param other the object to be compared with.
158 * @return TRUE if other are unequal to this, FALSE otherwise.
159 * @stable ICU 2.0
160 */
161 UBool operator!=(const Formattable& other) const
162 { return !operator==(other); }
163
164 /**
165 * Destructor.
166 * @stable ICU 2.0
167 */
168 virtual ~Formattable();
169
374ca955
A
170 /**
171 * Clone this object.
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.
175 *
176 * @return a clone of this object
177 *
178 * @see getDynamicClassID
73c04bcf 179 * @stable ICU 2.8
374ca955
A
180 */
181 Formattable *clone() const;
182
b75a7d8f 183 /**
374ca955
A
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.
187 * @stable ICU 2.4
b75a7d8f
A
188 */
189 enum Type {
374ca955
A
190 /**
191 * Selector indicating a UDate value. Use getDate to retrieve
192 * the value.
193 * @stable ICU 2.4
194 */
195 kDate,
196
197 /**
198 * Selector indicating a double value. Use getDouble to
199 * retrieve the value.
200 * @stable ICU 2.4
201 */
202 kDouble,
203
204 /**
205 * Selector indicating a 32-bit integer value. Use getLong to
206 * retrieve the value.
207 * @stable ICU 2.4
208 */
209 kLong,
210
211 /**
212 * Selector indicating a UnicodeString value. Use getString
213 * to retrieve the value.
214 * @stable ICU 2.4
215 */
216 kString,
217
218 /**
219 * Selector indicating an array of Formattables. Use getArray
220 * to retrieve the value.
221 * @stable ICU 2.4
222 */
223 kArray,
224
225 /**
226 * Selector indicating a 64-bit integer value. Use getInt64
227 * to retrieve the value.
73c04bcf 228 * @stable ICU 2.8
374ca955
A
229 */
230 kInt64,
231
232 /**
233 * Selector indicating a UObject value. Use getObject to
234 * retrieve the value.
73c04bcf 235 * @stable ICU 3.0
374ca955
A
236 */
237 kObject
238 };
b75a7d8f
A
239
240 /**
241 * Gets the data type of this Formattable object.
242 * @return the data type of this Formattable object.
243 * @stable ICU 2.0
244 */
245 Type getType(void) const;
246
247 /**
374ca955
A
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
73c04bcf 251 * @stable ICU 3.0
374ca955
A
252 */
253 UBool isNumeric() const;
254
255 /**
256 * Gets the double value of this object. If this object is not of type
257 * kDouble then the result is undefined.
b75a7d8f
A
258 * @return the double value of this object.
259 * @stable ICU 2.0
260 */
261 double getDouble(void) const { return fValue.fDouble; }
374ca955
A
262
263 /**
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.
73c04bcf 273 * @stable ICU 3.0
374ca955
A
274 */
275 double getDouble(UErrorCode& status) const;
276
b75a7d8f 277 /**
374ca955
A
278 * Gets the long value of this object. If this object is not of type
279 * kLong then the result is undefined.
b75a7d8f
A
280 * @return the long value of this object.
281 * @stable ICU 2.0
282 */
374ca955
A
283 int32_t getLong(void) const { return (int32_t)fValue.fInt64; }
284
285 /**
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.
73c04bcf 299 * @stable ICU 3.0
374ca955
A
300 */
301 int32_t getLong(UErrorCode& status) const;
302
303 /**
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.
73c04bcf 307 * @stable ICU 2.8
374ca955
A
308 */
309 int64_t getInt64(void) const { return fValue.fInt64; }
310
311 /**
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.
73c04bcf 324 * @stable ICU 3.0
374ca955
A
325 */
326 int64_t getInt64(UErrorCode& status) const;
327
b75a7d8f 328 /**
374ca955
A
329 * Gets the Date value of this object. If this object is not of type
330 * kDate then the result is undefined.
b75a7d8f
A
331 * @return the Date value of this object.
332 * @stable ICU 2.0
333 */
374ca955 334 UDate getDate() const { return fValue.fDate; }
b75a7d8f
A
335
336 /**
374ca955
A
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
339 * undefined.
340 * @param status the error code.
341 * @return the Date value of this object.
73c04bcf 342 * @stable ICU 3.0
374ca955
A
343 */
344 UDate getDate(UErrorCode& status) const;
345
346 /**
347 * Gets the string value of this object. If this object is not of type
348 * kString then the result is undefined.
b75a7d8f
A
349 * @param result Output param to receive the Date value of this object.
350 * @return A reference to 'result'.
351 * @stable ICU 2.0
352 */
353 UnicodeString& getString(UnicodeString& result) const
354 { result=*fValue.fString; return result; }
355
356 /**
374ca955
A
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'.
73c04bcf 363 * @stable ICU 3.0
374ca955
A
364 */
365 UnicodeString& getString(UnicodeString& result, UErrorCode& status) const;
366
367 /**
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
370 * undefined.
b75a7d8f
A
371 * @return a const reference to the string value of this object.
372 * @stable ICU 2.0
373 */
374 inline const UnicodeString& getString(void) const;
375
376 /**
374ca955
A
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.
73c04bcf 382 * @stable ICU 3.0
374ca955
A
383 */
384 const UnicodeString& getString(UErrorCode& status) const;
385
386 /**
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.
b75a7d8f
A
389 * @return a reference to the string value of this object.
390 * @stable ICU 2.0
391 */
392 inline UnicodeString& getString(void);
393
394 /**
374ca955
A
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.
73c04bcf 400 * @stable ICU 3.0
374ca955
A
401 */
402 UnicodeString& getString(UErrorCode& status);
403
404 /**
405 * Gets the array value and count of this object. If this object
406 * is not of type kArray then the result is undefined.
b75a7d8f
A
407 * @param count fill-in with the count of this object.
408 * @return the array value of this object.
409 * @stable ICU 2.0
410 */
411 const Formattable* getArray(int32_t& count) const
412 { count=fValue.fArrayAndCount.fCount; return fValue.fArrayAndCount.fArray; }
413
414 /**
374ca955
A
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.
73c04bcf 421 * @stable ICU 3.0
374ca955
A
422 */
423 const Formattable* getArray(int32_t& count, UErrorCode& status) const;
424
425 /**
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.
b75a7d8f
A
429 * @param index the specified index.
430 * @return the accessed element in the array.
431 * @stable ICU 2.0
432 */
433 Formattable& operator[](int32_t index) { return fValue.fArrayAndCount.fArray[index]; }
374ca955
A
434
435 /**
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
73c04bcf 439 * @stable ICU 3.0
374ca955
A
440 */
441 const UObject* getObject() const;
b75a7d8f
A
442
443 /**
374ca955
A
444 * Sets the double value of this object and changes the type to
445 * kDouble.
b75a7d8f
A
446 * @param d the new double value to be set.
447 * @stable ICU 2.0
448 */
449 void setDouble(double d);
374ca955 450
b75a7d8f 451 /**
374ca955
A
452 * Sets the long value of this object and changes the type to
453 * kLong.
b75a7d8f
A
454 * @param l the new long value to be set.
455 * @stable ICU 2.0
456 */
457 void setLong(int32_t l);
374ca955
A
458
459 /**
460 * Sets the int64 value of this object and changes the type to
461 * kInt64.
462 * @param ll the new int64 value to be set.
73c04bcf 463 * @stable ICU 2.8
374ca955
A
464 */
465 void setInt64(int64_t ll);
466
b75a7d8f 467 /**
374ca955
A
468 * Sets the Date value of this object and changes the type to
469 * kDate.
b75a7d8f
A
470 * @param d the new Date value to be set.
471 * @stable ICU 2.0
472 */
473 void setDate(UDate d);
374ca955 474
b75a7d8f 475 /**
374ca955
A
476 * Sets the string value of this object and changes the type to
477 * kString.
b75a7d8f
A
478 * @param stringToCopy the new string value to be set.
479 * @stable ICU 2.0
480 */
481 void setString(const UnicodeString& stringToCopy);
374ca955 482
b75a7d8f 483 /**
374ca955
A
484 * Sets the array value and count of this object and changes the
485 * type to kArray.
b75a7d8f
A
486 * @param array the array value.
487 * @param count the number of array elements to be copied.
488 * @stable ICU 2.0
489 */
490 void setArray(const Formattable* array, int32_t count);
374ca955 491
b75a7d8f 492 /**
374ca955
A
493 * Sets and adopts the string value and count of this object and
494 * changes the type to kArray.
b75a7d8f
A
495 * @param stringToAdopt the new string value to be adopted.
496 * @stable ICU 2.0
497 */
498 void adoptString(UnicodeString* stringToAdopt);
374ca955 499
b75a7d8f 500 /**
374ca955
A
501 * Sets and adopts the array value and count of this object and
502 * changes the type to kArray.
b75a7d8f
A
503 * @stable ICU 2.0
504 */
505 void adoptArray(Formattable* array, int32_t count);
374ca955
A
506
507 /**
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
73c04bcf 512 * @stable ICU 3.0
374ca955
A
513 */
514 void adoptObject(UObject* objectToAdopt);
515
b75a7d8f
A
516 /**
517 * ICU "poor man's RTTI", returns a UClassID for the actual class.
518 *
374ca955 519 * @stable ICU 2.2
b75a7d8f 520 */
374ca955 521 virtual UClassID getDynamicClassID() const;
b75a7d8f
A
522
523 /**
524 * ICU "poor man's RTTI", returns a UClassID for this class.
525 *
374ca955 526 * @stable ICU 2.2
b75a7d8f 527 */
374ca955
A
528 static UClassID U_EXPORT2 getStaticClassID();
529
530 /**
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
535 */
536 inline int32_t getLong(UErrorCode* status) const;
b75a7d8f
A
537
538private:
539 /**
540 * Cleans up the memory for unwanted values. For example, the adopted
541 * string or array objects.
542 */
543 void dispose(void);
544
374ca955
A
545 UnicodeString* getBogus() const;
546
b75a7d8f 547 union {
374ca955 548 UObject* fObject;
b75a7d8f
A
549 UnicodeString* fString;
550 double fDouble;
374ca955
A
551 int64_t fInt64;
552 UDate fDate;
553 struct {
b75a7d8f 554 Formattable* fArray;
374ca955 555 int32_t fCount;
b75a7d8f 556 } fArrayAndCount;
374ca955 557 } fValue;
b75a7d8f
A
558
559 Type fType;
374ca955 560 UnicodeString fBogus; // Bogus string when it's needed.
b75a7d8f
A
561};
562
374ca955
A
563inline UDate Formattable::getDate(UErrorCode& status) const {
564 if (fType != kDate) {
565 if (U_SUCCESS(status)) {
566 status = U_INVALID_FORMAT_ERROR;
567 }
568 return 0;
569 }
570 return fValue.fDate;
571}
572
b75a7d8f
A
573inline const UnicodeString& Formattable::getString(void) const {
574 return *fValue.fString;
575}
576
577inline UnicodeString& Formattable::getString(void) {
578 return *fValue.fString;
579}
580
374ca955
A
581inline int32_t Formattable::getLong(UErrorCode* status) const {
582 return getLong(*status);
583}
584
b75a7d8f
A
585U_NAMESPACE_END
586
587#endif /* #if !UCONFIG_NO_FORMATTING */
588
589#endif //_FMTABLE
590//eof
374ca955 591