fmtable.h 24 KB

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