1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #include "unicode/utypes.h"
6 #if !UCONFIG_NO_FORMATTING
7 #ifndef __NUMBER_MAPPER_H__
8 #define __NUMBER_MAPPER_H__
11 #include "number_types.h"
12 #include "unicode/currpinf.h"
13 #include "standardplural.h"
14 #include "number_patternstring.h"
15 #include "number_currencysymbols.h"
16 #include "numparse_impl.h"
23 class PropertiesAffixPatternProvider
: public AffixPatternProvider
, public UMemory
{
25 bool isBogus() const {
33 void setTo(const DecimalFormatProperties
& properties
, UErrorCode
& status
);
35 PropertiesAffixPatternProvider() = default; // puts instance in valid but undefined state
37 PropertiesAffixPatternProvider(const DecimalFormatProperties
& properties
, UErrorCode
& status
) {
38 setTo(properties
, status
);
41 // AffixPatternProvider Methods:
43 char16_t charAt(int32_t flags
, int32_t i
) const U_OVERRIDE
;
45 int32_t length(int32_t flags
) const U_OVERRIDE
;
47 UnicodeString
getString(int32_t flags
) const U_OVERRIDE
;
49 bool hasCurrencySign() const U_OVERRIDE
;
51 bool positiveHasPlusSign() const U_OVERRIDE
;
53 bool hasNegativeSubpattern() const U_OVERRIDE
;
55 bool negativeHasMinusSign() const U_OVERRIDE
;
57 bool containsSymbolType(AffixPatternType
, UErrorCode
&) const U_OVERRIDE
;
59 bool hasBody() const U_OVERRIDE
;
62 UnicodeString posPrefix
;
63 UnicodeString posSuffix
;
64 UnicodeString negPrefix
;
65 UnicodeString negSuffix
;
67 const UnicodeString
& getStringInternal(int32_t flags
) const;
73 class CurrencyPluralInfoAffixProvider
: public AffixPatternProvider
, public UMemory
{
75 bool isBogus() const {
83 void setTo(const CurrencyPluralInfo
& cpi
, const DecimalFormatProperties
& properties
,
86 // AffixPatternProvider Methods:
88 char16_t charAt(int32_t flags
, int32_t i
) const U_OVERRIDE
;
90 int32_t length(int32_t flags
) const U_OVERRIDE
;
92 UnicodeString
getString(int32_t flags
) const U_OVERRIDE
;
94 bool hasCurrencySign() const U_OVERRIDE
;
96 bool positiveHasPlusSign() const U_OVERRIDE
;
98 bool hasNegativeSubpattern() const U_OVERRIDE
;
100 bool negativeHasMinusSign() const U_OVERRIDE
;
102 bool containsSymbolType(AffixPatternType
, UErrorCode
&) const U_OVERRIDE
;
104 bool hasBody() const U_OVERRIDE
;
107 PropertiesAffixPatternProvider affixesByPlural
[StandardPlural::COUNT
];
114 * A struct for ownership of a few objects needed for formatting.
116 struct DecimalFormatWarehouse
{
117 PropertiesAffixPatternProvider propertiesAPP
;
118 CurrencyPluralInfoAffixProvider currencyPluralInfoAPP
;
119 CurrencySymbols currencySymbols
;
124 * Internal fields for DecimalFormat.
125 * TODO: Make some of these fields by value instead of by LocalPointer?
127 struct DecimalFormatFields
: public UMemory
{
128 /** The property bag corresponding to user-specified settings and settings from the pattern string. */
129 LocalPointer
<DecimalFormatProperties
> properties
;
131 /** The symbols for the current locale. */
132 LocalPointer
<const DecimalFormatSymbols
> symbols
;
135 * The pre-computed formatter object. Setters cause this to be re-computed atomically. The {@link
136 * #format} method uses the formatter directly without needing to synchronize.
138 LocalPointer
<const LocalizedNumberFormatter
> formatter
;
140 /** The lazy-computed parser for .parse() */
141 std::atomic
<::icu::numparse::impl::NumberParserImpl
*> atomicParser
= {};
143 /** The lazy-computed parser for .parseCurrency() */
144 std::atomic
<::icu::numparse::impl::NumberParserImpl
*> atomicCurrencyParser
= {};
146 /** Small object ownership warehouse for the formatter and parser */
147 DecimalFormatWarehouse warehouse
;
149 /** The effective properties as exported from the formatter object. Used by some getters. */
150 LocalPointer
<DecimalFormatProperties
> exportedProperties
;
153 bool canUseFastFormat
= false;
154 struct FastFormatData
{
156 char16_t cpGroupingSeparator
;
157 char16_t cpMinusSign
;
165 * Utilities for converting between a DecimalFormatProperties and a MacroProps.
167 class NumberPropertyMapper
{
169 /** Convenience method to create a NumberFormatter directly from Properties. */
170 static UnlocalizedNumberFormatter
create(const DecimalFormatProperties
& properties
,
171 const DecimalFormatSymbols
& symbols
,
172 DecimalFormatWarehouse
& warehouse
, UErrorCode
& status
);
174 /** Convenience method to create a NumberFormatter directly from Properties. */
175 static UnlocalizedNumberFormatter
create(const DecimalFormatProperties
& properties
,
176 const DecimalFormatSymbols
& symbols
,
177 DecimalFormatWarehouse
& warehouse
,
178 DecimalFormatProperties
& exportedProperties
,
182 * Creates a new {@link MacroProps} object based on the content of a {@link DecimalFormatProperties}
183 * object. In other words, maps Properties to MacroProps. This function is used by the
184 * JDK-compatibility API to call into the ICU 60 fluent number formatting pipeline.
187 * The property bag to be mapped.
189 * The symbols associated with the property bag.
190 * @param exportedProperties
191 * A property bag in which to store validated properties. Used by some DecimalFormat
193 * @return A new MacroProps containing all of the information in the Properties.
195 static MacroProps
oldToNew(const DecimalFormatProperties
& properties
,
196 const DecimalFormatSymbols
& symbols
, DecimalFormatWarehouse
& warehouse
,
197 DecimalFormatProperties
* exportedProperties
, UErrorCode
& status
);
202 } // namespace numparse
205 #endif //__NUMBER_MAPPER_H__
206 #endif /* #if !UCONFIG_NO_FORMATTING */