fmtable.h 24 KB

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