fmtable.h 24 KB

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