]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/unicode/fmtable.h
ICU-64243.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / fmtable.h
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f
A
3/*
4********************************************************************************
b331163b 5* Copyright (C) 1997-2014, International Business Machines
b75a7d8f
A
6* Corporation and others. All Rights Reserved.
7********************************************************************************
8*
9* File FMTABLE.H
10*
11* Modification History:
12*
13* Date Name Description
14* 02/29/97 aliu Creation.
15********************************************************************************
16*/
17#ifndef FMTABLE_H
18#define FMTABLE_H
19
b75a7d8f 20#include "unicode/utypes.h"
729e4ab9 21
73c04bcf 22/**
57a6839d
A
23 * \file
24 * \brief C++ API: Formattable is a thin wrapper for primitive types used for formatting and parsing
73c04bcf 25 */
b75a7d8f
A
26
27#if !UCONFIG_NO_FORMATTING
28
57a6839d
A
29#include "unicode/unistr.h"
30#include "unicode/stringpiece.h"
31#include "unicode/uformattable.h"
32
f3c0d7a5 33#if U_SHOW_CPLUSPLUS_API
b75a7d8f
A
34U_NAMESPACE_BEGIN
35
729e4ab9 36class CharString;
0f5d89e8
A
37namespace number {
38namespace impl {
39class DecimalQuantity;
40}
41}
729e4ab9 42
b75a7d8f
A
43/**
44 * Formattable objects can be passed to the Format class or
45 * its subclasses for formatting. Formattable is a thin wrapper
46 * class which interconverts between the primitive numeric types
47 * (double, long, etc.) as well as UDate and UnicodeString.
374ca955
A
48 *
49 * <p>Internally, a Formattable object is a union of primitive types.
50 * As such, it can only store one flavor of data at a time. To
51 * determine what flavor of data it contains, use the getType method.
52 *
53 * <p>As of ICU 3.0, Formattable may also wrap a UObject pointer,
54 * which it owns. This allows an instance of any ICU class to be
55 * encapsulated in a Formattable. For legacy reasons and for
56 * efficiency, primitive numeric types are still stored directly
57 * within a Formattable.
58 *
59 * <p>The Formattable class is not suitable for subclassing.
57a6839d
A
60 *
61 * <p>See UFormattable for a C wrapper.
b75a7d8f
A
62 */
63class U_I18N_API Formattable : public UObject {
64public:
65 /**
66 * This enum is only used to let callers distinguish between
67 * the Formattable(UDate) constructor and the Formattable(double)
68 * constructor; the compiler cannot distinguish the signatures,
69 * since UDate is currently typedefed to be either double or long.
70 * If UDate is changed later to be a bonafide class
71 * or struct, then we no longer need this enum.
374ca955 72 * @stable ICU 2.4
b75a7d8f
A
73 */
74 enum ISDATE { kIsDate };
75
76 /**
77 * Default constructor
374ca955 78 * @stable ICU 2.4
b75a7d8f
A
79 */
80 Formattable(); // Type kLong, value 0
374ca955 81
b75a7d8f
A
82 /**
83 * Creates a Formattable object with a UDate instance.
84 * @param d the UDate instance.
85 * @param flag the flag to indicate this is a date. Always set it to kIsDate
57a6839d 86 * @stable ICU 2.0
b75a7d8f
A
87 */
88 Formattable(UDate d, ISDATE flag);
374ca955 89
b75a7d8f
A
90 /**
91 * Creates a Formattable object with a double number.
92 * @param d the double number.
93 * @stable ICU 2.0
94 */
95 Formattable(double d);
374ca955 96
b75a7d8f
A
97 /**
98 * Creates a Formattable object with a long number.
99 * @param l the long number.
100 * @stable ICU 2.0
101 */
102 Formattable(int32_t l);
374ca955
A
103
104 /**
105 * Creates a Formattable object with an int64_t number
106 * @param ll the int64_t number.
73c04bcf 107 * @stable ICU 2.8
374ca955
A
108 */
109 Formattable(int64_t ll);
110
73c04bcf 111#if !UCONFIG_NO_CONVERSION
b75a7d8f
A
112 /**
113 * Creates a Formattable object with a char string pointer.
114 * Assumes that the char string is null terminated.
115 * @param strToCopy the char string.
116 * @stable ICU 2.0
117 */
118 Formattable(const char* strToCopy);
73c04bcf 119#endif
374ca955 120
729e4ab9
A
121 /**
122 * Creates a Formattable object of an appropriate numeric type from a
123 * a decimal number in string form. The Formattable will retain the
124 * full precision of the input in decimal format, even when it exceeds
57a6839d 125 * what can be represented by a double or int64_t.
729e4ab9
A
126 *
127 * @param number the unformatted (not localized) string representation
128 * of the Decimal number.
129 * @param status the error code. Possible errors include U_INVALID_FORMAT_ERROR
130 * if the format of the string does not conform to that of a
131 * decimal number.
132 * @stable ICU 4.4
133 */
f3c0d7a5 134 Formattable(StringPiece number, UErrorCode &status);
729e4ab9 135
b75a7d8f
A
136 /**
137 * Creates a Formattable object with a UnicodeString object to copy from.
138 * @param strToCopy the UnicodeString string.
139 * @stable ICU 2.0
140 */
141 Formattable(const UnicodeString& strToCopy);
374ca955 142
b75a7d8f
A
143 /**
144 * Creates a Formattable object with a UnicodeString object to adopt from.
145 * @param strToAdopt the UnicodeString string.
146 * @stable ICU 2.0
147 */
148 Formattable(UnicodeString* strToAdopt);
374ca955 149
b75a7d8f
A
150 /**
151 * Creates a Formattable object with an array of Formattable objects.
152 * @param arrayToCopy the Formattable object array.
153 * @param count the array count.
154 * @stable ICU 2.0
155 */
156 Formattable(const Formattable* arrayToCopy, int32_t count);
157
374ca955
A
158 /**
159 * Creates a Formattable object that adopts the given UObject.
160 * @param objectToAdopt the UObject to set this object to
73c04bcf 161 * @stable ICU 3.0
374ca955
A
162 */
163 Formattable(UObject* objectToAdopt);
164
b75a7d8f
A
165 /**
166 * Copy constructor.
167 * @stable ICU 2.0
168 */
169 Formattable(const Formattable&);
374ca955 170
b75a7d8f
A
171 /**
172 * Assignment operator.
173 * @param rhs The Formattable object to copy into this object.
174 * @stable ICU 2.0
175 */
176 Formattable& operator=(const Formattable &rhs);
374ca955 177
b75a7d8f
A
178 /**
179 * Equality comparison.
180 * @param other the object to be compared with.
181 * @return TRUE if other are equal to this, FALSE otherwise.
182 * @stable ICU 2.0
183 */
184 UBool operator==(const Formattable &other) const;
57a6839d
A
185
186 /**
b75a7d8f
A
187 * Equality operator.
188 * @param other the object to be compared with.
189 * @return TRUE if other are unequal to this, FALSE otherwise.
190 * @stable ICU 2.0
191 */
192 UBool operator!=(const Formattable& other) const
193 { return !operator==(other); }
194
57a6839d 195 /**
b75a7d8f
A
196 * Destructor.
197 * @stable ICU 2.0
198 */
199 virtual ~Formattable();
200
374ca955
A
201 /**
202 * Clone this object.
203 * Clones can be used concurrently in multiple threads.
204 * If an error occurs, then NULL is returned.
205 * The caller must delete the clone.
206 *
207 * @return a clone of this object
208 *
209 * @see getDynamicClassID
73c04bcf 210 * @stable ICU 2.8
374ca955
A
211 */
212 Formattable *clone() const;
213
57a6839d 214 /**
374ca955
A
215 * Selector for flavor of data type contained within a
216 * Formattable object. Formattable is a union of several
217 * different types, and at any time contains exactly one type.
218 * @stable ICU 2.4
b75a7d8f
A
219 */
220 enum Type {
374ca955
A
221 /**
222 * Selector indicating a UDate value. Use getDate to retrieve
223 * the value.
224 * @stable ICU 2.4
225 */
226 kDate,
227
228 /**
229 * Selector indicating a double value. Use getDouble to
230 * retrieve the value.
231 * @stable ICU 2.4
232 */
233 kDouble,
234
235 /**
236 * Selector indicating a 32-bit integer value. Use getLong to
237 * retrieve the value.
238 * @stable ICU 2.4
239 */
240 kLong,
241
242 /**
243 * Selector indicating a UnicodeString value. Use getString
244 * to retrieve the value.
245 * @stable ICU 2.4
246 */
247 kString,
248
249 /**
250 * Selector indicating an array of Formattables. Use getArray
251 * to retrieve the value.
252 * @stable ICU 2.4
253 */
254 kArray,
255
256 /**
257 * Selector indicating a 64-bit integer value. Use getInt64
258 * to retrieve the value.
73c04bcf 259 * @stable ICU 2.8
374ca955
A
260 */
261 kInt64,
262
263 /**
264 * Selector indicating a UObject value. Use getObject to
265 * retrieve the value.
73c04bcf 266 * @stable ICU 3.0
374ca955
A
267 */
268 kObject
269 };
b75a7d8f
A
270
271 /**
272 * Gets the data type of this Formattable object.
273 * @return the data type of this Formattable object.
274 * @stable ICU 2.0
275 */
276 Type getType(void) const;
57a6839d 277
b75a7d8f 278 /**
374ca955 279 * Returns TRUE if the data type of this Formattable object
57a6839d 280 * is kDouble, kLong, or kInt64
374ca955 281 * @return TRUE if this is a pure numeric object
73c04bcf 282 * @stable ICU 3.0
374ca955
A
283 */
284 UBool isNumeric() const;
57a6839d 285
374ca955
A
286 /**
287 * Gets the double value of this object. If this object is not of type
288 * kDouble then the result is undefined.
b75a7d8f
A
289 * @return the double value of this object.
290 * @stable ICU 2.0
57a6839d 291 */
b75a7d8f 292 double getDouble(void) const { return fValue.fDouble; }
374ca955
A
293
294 /**
295 * Gets the double value of this object. If this object is of type
729e4ab9 296 * long, int64 or Decimal Number then a conversion is peformed, with
374ca955
A
297 * possible loss of precision. If the type is kObject and the
298 * object is a Measure, then the result of
299 * getNumber().getDouble(status) is returned. If this object is
300 * neither a numeric type nor a Measure, then 0 is returned and
301 * the status is set to U_INVALID_FORMAT_ERROR.
302 * @param status the error code
303 * @return the double value of this object.
73c04bcf 304 * @stable ICU 3.0
57a6839d 305 */
374ca955
A
306 double getDouble(UErrorCode& status) const;
307
b75a7d8f 308 /**
374ca955
A
309 * Gets the long value of this object. If this object is not of type
310 * kLong then the result is undefined.
b75a7d8f
A
311 * @return the long value of this object.
312 * @stable ICU 2.0
57a6839d 313 */
374ca955
A
314 int32_t getLong(void) const { return (int32_t)fValue.fInt64; }
315
316 /**
317 * Gets the long value of this object. If the magnitude is too
318 * large to fit in a long, then the maximum or minimum long value,
319 * as appropriate, is returned and the status is set to
320 * U_INVALID_FORMAT_ERROR. If this object is of type kInt64 and
321 * it fits within a long, then no precision is lost. If it is of
57a6839d 322 * type kDouble, then a conversion is peformed, with
374ca955
A
323 * truncation of any fractional part. If the type is kObject and
324 * the object is a Measure, then the result of
325 * getNumber().getLong(status) is returned. If this object is
326 * neither a numeric type nor a Measure, then 0 is returned and
327 * the status is set to U_INVALID_FORMAT_ERROR.
328 * @param status the error code
329 * @return the long value of this object.
73c04bcf 330 * @stable ICU 3.0
57a6839d 331 */
374ca955
A
332 int32_t getLong(UErrorCode& status) const;
333
334 /**
335 * Gets the int64 value of this object. If this object is not of type
336 * kInt64 then the result is undefined.
337 * @return the int64 value of this object.
73c04bcf 338 * @stable ICU 2.8
57a6839d 339 */
374ca955
A
340 int64_t getInt64(void) const { return fValue.fInt64; }
341
342 /**
729e4ab9
A
343 * Gets the int64 value of this object. If this object is of a numeric
344 * type and the magnitude is too large to fit in an int64, then
374ca955
A
345 * the maximum or minimum int64 value, as appropriate, is returned
346 * and the status is set to U_INVALID_FORMAT_ERROR. If the
347 * magnitude fits in an int64, then a casting conversion is
348 * peformed, with truncation of any fractional part. If the type
349 * is kObject and the object is a Measure, then the result of
350 * getNumber().getDouble(status) is returned. If this object is
351 * neither a numeric type nor a Measure, then 0 is returned and
352 * the status is set to U_INVALID_FORMAT_ERROR.
353 * @param status the error code
354 * @return the int64 value of this object.
73c04bcf 355 * @stable ICU 3.0
57a6839d 356 */
374ca955
A
357 int64_t getInt64(UErrorCode& status) const;
358
b75a7d8f 359 /**
374ca955
A
360 * Gets the Date value of this object. If this object is not of type
361 * kDate then the result is undefined.
b75a7d8f
A
362 * @return the Date value of this object.
363 * @stable ICU 2.0
57a6839d 364 */
374ca955 365 UDate getDate() const { return fValue.fDate; }
b75a7d8f
A
366
367 /**
374ca955
A
368 * Gets the Date value of this object. If the type is not a date,
369 * status is set to U_INVALID_FORMAT_ERROR and the return value is
370 * undefined.
371 * @param status the error code.
372 * @return the Date value of this object.
73c04bcf 373 * @stable ICU 3.0
57a6839d 374 */
374ca955
A
375 UDate getDate(UErrorCode& status) const;
376
377 /**
378 * Gets the string value of this object. If this object is not of type
379 * kString then the result is undefined.
b75a7d8f
A
380 * @param result Output param to receive the Date value of this object.
381 * @return A reference to 'result'.
382 * @stable ICU 2.0
57a6839d 383 */
b75a7d8f
A
384 UnicodeString& getString(UnicodeString& result) const
385 { result=*fValue.fString; return result; }
386
387 /**
374ca955
A
388 * Gets the string value of this object. If the type is not a
389 * string, status is set to U_INVALID_FORMAT_ERROR and a bogus
390 * string is returned.
391 * @param result Output param to receive the Date value of this object.
57a6839d 392 * @param status the error code.
374ca955 393 * @return A reference to 'result'.
73c04bcf 394 * @stable ICU 3.0
57a6839d 395 */
374ca955
A
396 UnicodeString& getString(UnicodeString& result, UErrorCode& status) const;
397
398 /**
399 * Gets a const reference to the string value of this object. If
400 * this object is not of type kString then the result is
401 * undefined.
b75a7d8f
A
402 * @return a const reference to the string value of this object.
403 * @stable ICU 2.0
404 */
405 inline const UnicodeString& getString(void) const;
406
407 /**
374ca955
A
408 * Gets a const reference to the string value of this object. If
409 * the type is not a string, status is set to
410 * U_INVALID_FORMAT_ERROR and the result is a bogus string.
411 * @param status the error code.
412 * @return a const reference to the string value of this object.
73c04bcf 413 * @stable ICU 3.0
374ca955
A
414 */
415 const UnicodeString& getString(UErrorCode& status) const;
416
417 /**
418 * Gets a reference to the string value of this object. If this
419 * object is not of type kString then the result is undefined.
b75a7d8f
A
420 * @return a reference to the string value of this object.
421 * @stable ICU 2.0
422 */
423 inline UnicodeString& getString(void);
424
425 /**
374ca955
A
426 * Gets a reference to the string value of this object. If the
427 * type is not a string, status is set to U_INVALID_FORMAT_ERROR
428 * and the result is a bogus string.
57a6839d 429 * @param status the error code.
374ca955 430 * @return a reference to the string value of this object.
73c04bcf 431 * @stable ICU 3.0
374ca955
A
432 */
433 UnicodeString& getString(UErrorCode& status);
434
435 /**
436 * Gets the array value and count of this object. If this object
437 * is not of type kArray then the result is undefined.
b75a7d8f
A
438 * @param count fill-in with the count of this object.
439 * @return the array value of this object.
440 * @stable ICU 2.0
57a6839d 441 */
b75a7d8f
A
442 const Formattable* getArray(int32_t& count) const
443 { count=fValue.fArrayAndCount.fCount; return fValue.fArrayAndCount.fArray; }
444
445 /**
374ca955
A
446 * Gets the array value and count of this object. If the type is
447 * not an array, status is set to U_INVALID_FORMAT_ERROR, count is
448 * set to 0, and the result is NULL.
449 * @param count fill-in with the count of this object.
57a6839d 450 * @param status the error code.
374ca955 451 * @return the array value of this object.
73c04bcf 452 * @stable ICU 3.0
57a6839d 453 */
374ca955
A
454 const Formattable* getArray(int32_t& count, UErrorCode& status) const;
455
456 /**
457 * Accesses the specified element in the array value of this
458 * Formattable object. If this object is not of type kArray then
459 * the result is undefined.
b75a7d8f
A
460 * @param index the specified index.
461 * @return the accessed element in the array.
462 * @stable ICU 2.0
463 */
464 Formattable& operator[](int32_t index) { return fValue.fArrayAndCount.fArray[index]; }
57a6839d 465
374ca955
A
466 /**
467 * Returns a pointer to the UObject contained within this
468 * formattable, or NULL if this object does not contain a UObject.
469 * @return a UObject pointer, or NULL
73c04bcf 470 * @stable ICU 3.0
374ca955
A
471 */
472 const UObject* getObject() const;
b75a7d8f
A
473
474 /**
729e4ab9
A
475 * Returns a numeric string representation of the number contained within this
476 * formattable, or NULL if this object does not contain numeric type.
477 * For values obtained by parsing, the returned decimal number retains
478 * the full precision and range of the original input, unconstrained by
479 * the limits of a double floating point or a 64 bit int.
57a6839d 480 *
729e4ab9
A
481 * This function is not thread safe, and therfore is not declared const,
482 * even though it is logically const.
483 *
484 * Possible errors include U_MEMORY_ALLOCATION_ERROR, and
485 * U_INVALID_STATE if the formattable object has not been set to
486 * a numeric type.
487 *
488 * @param status the error code.
489 * @return the unformatted string representation of a number.
490 * @stable ICU 4.4
491 */
492 StringPiece getDecimalNumber(UErrorCode &status);
493
494 /**
374ca955
A
495 * Sets the double value of this object and changes the type to
496 * kDouble.
b75a7d8f
A
497 * @param d the new double value to be set.
498 * @stable ICU 2.0
57a6839d 499 */
b75a7d8f 500 void setDouble(double d);
374ca955 501
b75a7d8f 502 /**
374ca955
A
503 * Sets the long value of this object and changes the type to
504 * kLong.
b75a7d8f
A
505 * @param l the new long value to be set.
506 * @stable ICU 2.0
57a6839d 507 */
b75a7d8f 508 void setLong(int32_t l);
374ca955
A
509
510 /**
511 * Sets the int64 value of this object and changes the type to
512 * kInt64.
513 * @param ll the new int64 value to be set.
73c04bcf 514 * @stable ICU 2.8
57a6839d 515 */
374ca955
A
516 void setInt64(int64_t ll);
517
b75a7d8f 518 /**
374ca955
A
519 * Sets the Date value of this object and changes the type to
520 * kDate.
b75a7d8f
A
521 * @param d the new Date value to be set.
522 * @stable ICU 2.0
57a6839d 523 */
b75a7d8f 524 void setDate(UDate d);
374ca955 525
b75a7d8f 526 /**
374ca955
A
527 * Sets the string value of this object and changes the type to
528 * kString.
b75a7d8f
A
529 * @param stringToCopy the new string value to be set.
530 * @stable ICU 2.0
57a6839d 531 */
b75a7d8f 532 void setString(const UnicodeString& stringToCopy);
374ca955 533
b75a7d8f 534 /**
374ca955
A
535 * Sets the array value and count of this object and changes the
536 * type to kArray.
b75a7d8f
A
537 * @param array the array value.
538 * @param count the number of array elements to be copied.
539 * @stable ICU 2.0
57a6839d 540 */
b75a7d8f 541 void setArray(const Formattable* array, int32_t count);
374ca955 542
b75a7d8f 543 /**
374ca955
A
544 * Sets and adopts the string value and count of this object and
545 * changes the type to kArray.
b75a7d8f
A
546 * @param stringToAdopt the new string value to be adopted.
547 * @stable ICU 2.0
57a6839d 548 */
b75a7d8f 549 void adoptString(UnicodeString* stringToAdopt);
374ca955 550
b75a7d8f 551 /**
374ca955
A
552 * Sets and adopts the array value and count of this object and
553 * changes the type to kArray.
b75a7d8f 554 * @stable ICU 2.0
57a6839d 555 */
b75a7d8f 556 void adoptArray(Formattable* array, int32_t count);
57a6839d 557
374ca955
A
558 /**
559 * Sets and adopts the UObject value of this object and changes
560 * the type to kObject. After this call, the caller must not
561 * delete the given object.
562 * @param objectToAdopt the UObject value to be adopted
73c04bcf 563 * @stable ICU 3.0
374ca955
A
564 */
565 void adoptObject(UObject* objectToAdopt);
566
729e4ab9
A
567 /**
568 * Sets the the numeric value from a decimal number string, and changes
57a6839d 569 * the type to to a numeric type appropriate for the number.
729e4ab9
A
570 * The syntax of the number is a "numeric string"
571 * as defined in the Decimal Arithmetic Specification, available at
572 * http://speleotrove.com/decimal
573 * The full precision and range of the input number will be retained,
574 * even when it exceeds what can be represented by a double or an int64.
575 *
576 * @param numberString a string representation of the unformatted decimal number.
577 * @param status the error code. Set to U_INVALID_FORMAT_ERROR if the
578 * incoming string is not a valid decimal number.
579 * @stable ICU 4.4
580 */
f3c0d7a5 581 void setDecimalNumber(StringPiece numberString,
729e4ab9
A
582 UErrorCode &status);
583
b75a7d8f
A
584 /**
585 * ICU "poor man's RTTI", returns a UClassID for the actual class.
586 *
374ca955 587 * @stable ICU 2.2
b75a7d8f 588 */
374ca955 589 virtual UClassID getDynamicClassID() const;
b75a7d8f
A
590
591 /**
592 * ICU "poor man's RTTI", returns a UClassID for this class.
593 *
374ca955 594 * @stable ICU 2.2
b75a7d8f 595 */
374ca955
A
596 static UClassID U_EXPORT2 getStaticClassID();
597
57a6839d
A
598 /**
599 * Convert the UFormattable to a Formattable. Internally, this is a reinterpret_cast.
600 * @param fmt a valid UFormattable
601 * @return the UFormattable as a Formattable object pointer. This is an alias to the original
602 * UFormattable, and so is only valid while the original argument remains in scope.
b331163b 603 * @stable ICU 52
57a6839d
A
604 */
605 static inline Formattable *fromUFormattable(UFormattable *fmt);
606
607 /**
608 * Convert the const UFormattable to a const Formattable. Internally, this is a reinterpret_cast.
609 * @param fmt a valid UFormattable
610 * @return the UFormattable as a Formattable object pointer. This is an alias to the original
611 * UFormattable, and so is only valid while the original argument remains in scope.
b331163b 612 * @stable ICU 52
57a6839d
A
613 */
614 static inline const Formattable *fromUFormattable(const UFormattable *fmt);
615
616 /**
617 * Convert this object pointer to a UFormattable.
618 * @return this object as a UFormattable pointer. This is an alias to this object,
619 * and so is only valid while this object remains in scope.
b331163b 620 * @stable ICU 52
57a6839d
A
621 */
622 inline UFormattable *toUFormattable();
623
624 /**
625 * Convert this object pointer to a UFormattable.
626 * @return this object as a UFormattable pointer. This is an alias to this object,
627 * and so is only valid while this object remains in scope.
b331163b 628 * @stable ICU 52
57a6839d
A
629 */
630 inline const UFormattable *toUFormattable() const;
57a6839d 631
4388f060 632#ifndef U_HIDE_DEPRECATED_API
374ca955
A
633 /**
634 * Deprecated variant of getLong(UErrorCode&).
635 * @param status the error code
636 * @return the long value of this object.
637 * @deprecated ICU 3.0 use getLong(UErrorCode&) instead
57a6839d 638 */
374ca955 639 inline int32_t getLong(UErrorCode* status) const;
4388f060 640#endif /* U_HIDE_DEPRECATED_API */
b75a7d8f 641
4388f060 642#ifndef U_HIDE_INTERNAL_API
0f5d89e8
A
643 /**
644 * Internal function, do not use.
645 * TODO: figure out how to make this be non-public.
646 * NumberFormat::format(Formattable, ...
647 * needs to get at the DecimalQuantity, if it exists, for
648 * big decimal formatting.
649 * @internal
650 */
651 number::impl::DecimalQuantity *getDecimalQuantity() const { return fDecimalQuantity;}
652
653 /**
654 * Export the value of this Formattable to a DecimalQuantity.
655 * @internal
656 */
657 void populateDecimalQuantity(number::impl::DecimalQuantity& output, UErrorCode& status) const;
658
0f5d89e8
A
659 /**
660 * Adopt, and set value from, a DecimalQuantity
661 * Internal Function, do not use.
3d1f044b 662 * @param dq the DecimalQuantity to be adopted
0f5d89e8
A
663 * @internal
664 */
665 void adoptDecimalQuantity(number::impl::DecimalQuantity *dq);
666
57a6839d
A
667 /**
668 * Internal function to return the CharString pointer.
669 * @param status error code
670 * @return pointer to the CharString - may become invalid if the object is modified
671 * @internal
672 */
673 CharString *internalGetCharString(UErrorCode &status);
674
4388f060 675#endif /* U_HIDE_INTERNAL_API */
729e4ab9 676
b75a7d8f
A
677private:
678 /**
679 * Cleans up the memory for unwanted values. For example, the adopted
680 * string or array objects.
681 */
682 void dispose(void);
683
729e4ab9
A
684 /**
685 * Common initialization, for use by constructors.
686 */
687 void init();
688
374ca955
A
689 UnicodeString* getBogus() const;
690
b75a7d8f 691 union {
374ca955 692 UObject* fObject;
b75a7d8f
A
693 UnicodeString* fString;
694 double fDouble;
374ca955
A
695 int64_t fInt64;
696 UDate fDate;
697 struct {
b75a7d8f 698 Formattable* fArray;
374ca955 699 int32_t fCount;
b75a7d8f 700 } fArrayAndCount;
374ca955 701 } fValue;
b75a7d8f 702
729e4ab9 703 CharString *fDecimalStr;
51004dcb 704
0f5d89e8
A
705 number::impl::DecimalQuantity *fDecimalQuantity;
706
b75a7d8f 707 Type fType;
374ca955 708 UnicodeString fBogus; // Bogus string when it's needed.
b75a7d8f
A
709};
710
374ca955
A
711inline UDate Formattable::getDate(UErrorCode& status) const {
712 if (fType != kDate) {
713 if (U_SUCCESS(status)) {
714 status = U_INVALID_FORMAT_ERROR;
715 }
716 return 0;
717 }
718 return fValue.fDate;
719}
720
b75a7d8f
A
721inline const UnicodeString& Formattable::getString(void) const {
722 return *fValue.fString;
723}
724
725inline UnicodeString& Formattable::getString(void) {
726 return *fValue.fString;
727}
728
57a6839d 729#ifndef U_HIDE_DEPRECATED_API
374ca955
A
730inline int32_t Formattable::getLong(UErrorCode* status) const {
731 return getLong(*status);
732}
57a6839d
A
733#endif /* U_HIDE_DEPRECATED_API */
734
57a6839d
A
735inline UFormattable* Formattable::toUFormattable() {
736 return reinterpret_cast<UFormattable*>(this);
737}
738
739inline const UFormattable* Formattable::toUFormattable() const {
740 return reinterpret_cast<const UFormattable*>(this);
741}
742
743inline Formattable* Formattable::fromUFormattable(UFormattable *fmt) {
744 return reinterpret_cast<Formattable *>(fmt);
745}
4388f060 746
57a6839d
A
747inline const Formattable* Formattable::fromUFormattable(const UFormattable *fmt) {
748 return reinterpret_cast<const Formattable *>(fmt);
749}
374ca955 750
b75a7d8f 751U_NAMESPACE_END
f3c0d7a5 752#endif // U_SHOW_CPLUSPLUS_API
b75a7d8f
A
753
754#endif /* #if !UCONFIG_NO_FORMATTING */
755
756#endif //_FMTABLE
757//eof