ICU 77.1  77.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
formattednumber.h
Go to the documentation of this file.
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"
17 
25 U_NAMESPACE_BEGIN
26 
27 class FieldPositionIteratorHandler;
28 class SimpleDateFormat;
29 
30 namespace number { // icu::number
31 
32 namespace impl {
33 class DecimalQuantity;
34 class UFormattedNumberData;
35 struct UFormattedNumberImpl;
36 } // icu::number::impl
37 
38 
39 
49  public:
50 
56  : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
57 
63 
68  virtual ~FormattedNumber() override;
69 
71  FormattedNumber(const FormattedNumber&) = delete;
72 
75 
81 
82  // Copybrief: this method is older than the parent method
90  UnicodeString toString(UErrorCode& status) const override;
91 
92  // Copydoc: this method is new in ICU 64
94  UnicodeString toTempString(UErrorCode& status) const override;
95 
96  // Copybrief: this method is older than the parent method
104  Appendable &appendTo(Appendable& appendable, UErrorCode& status) const override;
105 
106  // Copydoc: this method is new in ICU 64
108  UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const override;
109 
128  template<typename StringClass>
129  inline StringClass toDecimalNumber(UErrorCode& status) const;
130 
143 
152 
153 #ifndef U_HIDE_INTERNAL_API
154 
159  void getDecimalQuantity(impl::DecimalQuantity& output, UErrorCode& status) const;
160 
165  void getAllFieldPositionsImpl(FieldPositionIteratorHandler& fpih, UErrorCode& status) const;
166 
167 #endif /* U_HIDE_INTERNAL_API */
168 
169  private:
170  // Can't use LocalPointer because UFormattedNumberData is forward-declared
171  impl::UFormattedNumberData *fData;
172 
173  // Error code for the terminal methods
174  UErrorCode fErrorCode;
175 
180  explicit FormattedNumber(impl::UFormattedNumberData *results)
181  : fData(results), fErrorCode(U_ZERO_ERROR) {}
182 
183  explicit FormattedNumber(UErrorCode errorCode)
184  : fData(nullptr), fErrorCode(errorCode) {}
185 
186  void toDecimalNumber(ByteSink& sink, UErrorCode& status) const;
187 
188  // To give LocalizedNumberFormatter format methods access to this class's constructor:
189  friend class LocalizedNumberFormatter;
190  friend class SimpleNumberFormatter;
191 
192  // To give C API access to internals
193  friend struct impl::UFormattedNumberImpl;
194 
195  // To give access to the data pointer for non-heap allocation
196  friend class icu::SimpleDateFormat;
197 };
198 
199 template<typename StringClass>
200 StringClass FormattedNumber::toDecimalNumber(UErrorCode& status) const {
201  StringClass result;
202  StringByteSink<StringClass> sink(&result);
203  toDecimalNumber(sink, status);
204  return result;
205 }
206 
207 } // namespace number
208 U_NAMESPACE_END
209 
210 #endif /* #if !UCONFIG_NO_FORMATTING */
211 
212 #endif /* U_SHOW_CPLUSPLUS_API */
213 
214 #endif // __FORMATTEDNUMBER_H__
215 
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:54
A ByteSink can be filled with bytes.
Definition: bytestream.h:53
Represents a span of a string containing a given field.
An abstract formatted value: a string with associated field attributes.
A unit such as length, mass, volume, currency, etc.
Definition: measunit.h:405
SimpleDateFormat is a concrete class for formatting and parsing dates in a language-independent manne...
Definition: smpdtfmt.h:385
Implementation of ByteSink that writes to a "string".
Definition: bytestream.h:267
UMemory is the common ICU base class.
Definition: uobject.h:115
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
The result of a number formatting operation.
void getDecimalQuantity(impl::DecimalQuantity &output, UErrorCode &status) const
Gets the raw DecimalQuantity for plural rule selection.
UDisplayOptionsNounClass getNounClass(UErrorCode &status) const
Gets the noun class of the formatted output.
FormattedNumber(const FormattedNumber &)=delete
Copying not supported; use move constructor instead.
MeasureUnit getOutputUnit(UErrorCode &status) const
Gets the resolved output unit.
FormattedNumber()
Default constructor; makes an empty FormattedNumber.
FormattedNumber & operator=(const FormattedNumber &)=delete
Copying not supported; use move assignment instead.
void getAllFieldPositionsImpl(FieldPositionIteratorHandler &fpih, UErrorCode &status) const
Populates the mutable builder type FieldPositionIteratorHandler.
virtual ~FormattedNumber() override
Destruct an instance of FormattedNumber.
UnicodeString toString(UErrorCode &status) const override
Returns the formatted string as a self-contained UnicodeString.
Appendable & appendTo(Appendable &appendable, UErrorCode &status) const override
Appends the formatted string to an Appendable.
FormattedNumber(FormattedNumber &&src) noexcept
Move constructor: Leaves the source FormattedNumber in an undefined state.
UBool nextPosition(ConstrainedFieldPosition &cfpos, UErrorCode &status) const override
Iterates over field positions in the FormattedValue.
UnicodeString toTempString(UErrorCode &status) const override
Returns the formatted string as a read-only alias to memory owned by the FormattedValue.
FormattedNumber & operator=(FormattedNumber &&src) noexcept
Move assignment: Leaves the source FormattedNumber in an undefined state.
C++ API: Abstract operations for localized strings.
C++ API: A unit for measuring a quantity.
C API: Display options (enum types, values, helper functions)
UDisplayOptionsNounClass
Represents all the grammatical noun classes that are supported by CLDR.
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:247
C++ API: Common ICU base class UObject.
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:430
@ U_INVALID_STATE_ERROR
Requested operation can not be completed with ICU in its current state.
Definition: utypes.h:494
@ U_ZERO_ERROR
No error, no warning.
Definition: utypes.h:465
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:316