formattednumber.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // © 2022 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. #ifndef __FORMATTEDNUMBER_H__
  4. #define __FORMATTEDNUMBER_H__
  5. #include "unicode/utypes.h"
  6. #if U_SHOW_CPLUSPLUS_API
  7. #if !UCONFIG_NO_FORMATTING
  8. #include "unicode/uobject.h"
  9. #include "unicode/formattedvalue.h"
  10. #include "unicode/measunit.h"
  11. #include "unicode/udisplayoptions.h"
  12. /**
  13. * \file
  14. * \brief C API: Formatted number result from various number formatting functions.
  15. *
  16. * See also {@link icu::FormattedValue} for additional things you can do with a FormattedNumber.
  17. */
  18. U_NAMESPACE_BEGIN
  19. class FieldPositionIteratorHandler;
  20. class SimpleDateFormat;
  21. namespace number { // icu::number
  22. namespace impl {
  23. class DecimalQuantity;
  24. class UFormattedNumberData;
  25. struct UFormattedNumberImpl;
  26. } // icu::number::impl
  27. /**
  28. * The result of a number formatting operation. This class allows the result to be exported in several data types,
  29. * including a UnicodeString and a FieldPositionIterator.
  30. *
  31. * Instances of this class are immutable and thread-safe.
  32. *
  33. * @stable ICU 60
  34. */
  35. class U_I18N_API FormattedNumber : public UMemory, public FormattedValue {
  36. public:
  37. /**
  38. * Default constructor; makes an empty FormattedNumber.
  39. * @stable ICU 64
  40. */
  41. FormattedNumber()
  42. : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
  43. /**
  44. * Move constructor: Leaves the source FormattedNumber in an undefined state.
  45. * @stable ICU 62
  46. */
  47. FormattedNumber(FormattedNumber&& src) noexcept;
  48. /**
  49. * Destruct an instance of FormattedNumber.
  50. * @stable ICU 60
  51. */
  52. virtual ~FormattedNumber() override;
  53. /** Copying not supported; use move constructor instead. */
  54. FormattedNumber(const FormattedNumber&) = delete;
  55. /** Copying not supported; use move assignment instead. */
  56. FormattedNumber& operator=(const FormattedNumber&) = delete;
  57. /**
  58. * Move assignment: Leaves the source FormattedNumber in an undefined state.
  59. * @stable ICU 62
  60. */
  61. FormattedNumber& operator=(FormattedNumber&& src) noexcept;
  62. // Copybrief: this method is older than the parent method
  63. /**
  64. * @copybrief FormattedValue::toString()
  65. *
  66. * For more information, see FormattedValue::toString()
  67. *
  68. * @stable ICU 62
  69. */
  70. UnicodeString toString(UErrorCode& status) const override;
  71. // Copydoc: this method is new in ICU 64
  72. /** @copydoc FormattedValue::toTempString() */
  73. UnicodeString toTempString(UErrorCode& status) const override;
  74. // Copybrief: this method is older than the parent method
  75. /**
  76. * @copybrief FormattedValue::appendTo()
  77. *
  78. * For more information, see FormattedValue::appendTo()
  79. *
  80. * @stable ICU 62
  81. */
  82. Appendable &appendTo(Appendable& appendable, UErrorCode& status) const override;
  83. // Copydoc: this method is new in ICU 64
  84. /** @copydoc FormattedValue::nextPosition() */
  85. UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const override;
  86. /**
  87. * Export the formatted number as a "numeric string" conforming to the
  88. * syntax defined in the Decimal Arithmetic Specification, available at
  89. * http://speleotrove.com/decimal
  90. *
  91. * This endpoint is useful for obtaining the exact number being printed
  92. * after scaling and rounding have been applied by the number formatter.
  93. *
  94. * Example call site:
  95. *
  96. * auto decimalNumber = fn.toDecimalNumber<std::string>(status);
  97. *
  98. * @tparam StringClass A string class compatible with StringByteSink;
  99. * for example, std::string.
  100. * @param status Set if an error occurs.
  101. * @return A StringClass containing the numeric string.
  102. * @stable ICU 65
  103. */
  104. template<typename StringClass>
  105. inline StringClass toDecimalNumber(UErrorCode& status) const;
  106. /**
  107. * Gets the resolved output unit.
  108. *
  109. * The output unit is dependent upon the localized preferences for the usage
  110. * specified via NumberFormatterSettings::usage(), and may be a unit with
  111. * UMEASURE_UNIT_MIXED unit complexity (MeasureUnit::getComplexity()), such
  112. * as "foot-and-inch" or "hour-and-minute-and-second".
  113. *
  114. * @return `MeasureUnit`.
  115. * @stable ICU 68
  116. */
  117. MeasureUnit getOutputUnit(UErrorCode& status) const;
  118. /**
  119. * Gets the noun class of the formatted output. Returns `UNDEFINED` when the noun class
  120. * is not supported yet.
  121. *
  122. * @return UDisplayOptionsNounClass
  123. * @stable ICU 72
  124. */
  125. UDisplayOptionsNounClass getNounClass(UErrorCode &status) const;
  126. #ifndef U_HIDE_INTERNAL_API
  127. /**
  128. * Gets the raw DecimalQuantity for plural rule selection.
  129. * @internal
  130. */
  131. void getDecimalQuantity(impl::DecimalQuantity& output, UErrorCode& status) const;
  132. /**
  133. * Populates the mutable builder type FieldPositionIteratorHandler.
  134. * @internal
  135. */
  136. void getAllFieldPositionsImpl(FieldPositionIteratorHandler& fpih, UErrorCode& status) const;
  137. #endif /* U_HIDE_INTERNAL_API */
  138. private:
  139. // Can't use LocalPointer because UFormattedNumberData is forward-declared
  140. impl::UFormattedNumberData *fData;
  141. // Error code for the terminal methods
  142. UErrorCode fErrorCode;
  143. /**
  144. * Internal constructor from data type. Adopts the data pointer.
  145. * @internal (private)
  146. */
  147. explicit FormattedNumber(impl::UFormattedNumberData *results)
  148. : fData(results), fErrorCode(U_ZERO_ERROR) {}
  149. explicit FormattedNumber(UErrorCode errorCode)
  150. : fData(nullptr), fErrorCode(errorCode) {}
  151. void toDecimalNumber(ByteSink& sink, UErrorCode& status) const;
  152. // To give LocalizedNumberFormatter format methods access to this class's constructor:
  153. friend class LocalizedNumberFormatter;
  154. friend class SimpleNumberFormatter;
  155. // To give C API access to internals
  156. friend struct impl::UFormattedNumberImpl;
  157. // To give access to the data pointer for non-heap allocation
  158. friend class icu::SimpleDateFormat;
  159. };
  160. template<typename StringClass>
  161. StringClass FormattedNumber::toDecimalNumber(UErrorCode& status) const {
  162. StringClass result;
  163. StringByteSink<StringClass> sink(&result);
  164. toDecimalNumber(sink, status);
  165. return result;
  166. }
  167. } // namespace number
  168. U_NAMESPACE_END
  169. #endif /* #if !UCONFIG_NO_FORMATTING */
  170. #endif /* U_SHOW_CPLUSPLUS_API */
  171. #endif // __FORMATTEDNUMBER_H__