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