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