• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 
4 #include "unicode/utypes.h"
5 
6 #if !UCONFIG_NO_FORMATTING && !UPRV_INCOMPLETE_CPP11_SUPPORT
7 #ifndef __NUMBER_FORMATIMPL_H__
8 #define __NUMBER_FORMATIMPL_H__
9 
10 #include "number_types.h"
11 #include "number_stringbuilder.h"
12 #include "number_patternstring.h"
13 #include "number_utils.h"
14 #include "number_patternmodifier.h"
15 #include "number_longnames.h"
16 #include "number_compact.h"
17 
18 U_NAMESPACE_BEGIN namespace number {
19 namespace impl {
20 
21 /**
22  * This is the "brain" of the number formatting pipeline. It ties all the pieces together, taking in a MacroProps and a
23  * DecimalQuantity and outputting a properly formatted number string.
24  */
25 class NumberFormatterImpl : public UMemory {
26   public:
27     /**
28      * Builds a "safe" MicroPropsGenerator, which is thread-safe and can be used repeatedly.
29      * The caller owns the returned NumberFormatterImpl.
30      */
31     static NumberFormatterImpl *fromMacros(const MacroProps &macros, UErrorCode &status);
32 
33     /**
34      * Builds and evaluates an "unsafe" MicroPropsGenerator, which is cheaper but can be used only once.
35      */
36     static void
37     applyStatic(const MacroProps &macros, DecimalQuantity &inValue, NumberStringBuilder &outString,
38                 UErrorCode &status);
39 
40     /**
41      * Evaluates the "safe" MicroPropsGenerator created by "fromMacros".
42      */
43     void apply(DecimalQuantity &inValue, NumberStringBuilder &outString, UErrorCode &status) const;
44 
45   private:
46     // Head of the MicroPropsGenerator linked list:
47     const MicroPropsGenerator *fMicroPropsGenerator = nullptr;
48 
49     // Tail of the list:
50     MicroProps fMicros;
51 
52     // Other fields possibly used by the number formatting pipeline:
53     // TODO: Convert some of these LocalPointers to value objects to reduce the number of news?
54     LocalPointer<const DecimalFormatSymbols> fSymbols;
55     LocalPointer<const PluralRules> fRules;
56     LocalPointer<const ParsedPatternInfo> fPatternInfo;
57     LocalPointer<const ScientificHandler> fScientificHandler;
58     LocalPointer<const MutablePatternModifier> fPatternModifier;
59     LocalPointer<const ImmutablePatternModifier> fImmutablePatternModifier;
60     LocalPointer<const LongNameHandler> fLongNameHandler;
61     LocalPointer<const CompactHandler> fCompactHandler;
62 
63 
64     NumberFormatterImpl(const MacroProps &macros, bool safe, UErrorCode &status);
65 
66     void applyUnsafe(DecimalQuantity &inValue, NumberStringBuilder &outString, UErrorCode &status);
67 
68     /**
69      * If rulesPtr is non-null, return it.  Otherwise, return a PluralRules owned by this object for the
70      * specified locale, creating it if necessary.
71      */
72     const PluralRules *
73     resolvePluralRules(const PluralRules *rulesPtr, const Locale &locale, UErrorCode &status);
74 
75     /**
76      * Synthesizes the MacroProps into a MicroPropsGenerator. All information, including the locale, is encoded into the
77      * MicroPropsGenerator, except for the quantity itself, which is left abstract and must be provided to the returned
78      * MicroPropsGenerator instance.
79      *
80      * @see MicroPropsGenerator
81      * @param macros
82      *            The {@link MacroProps} to consume. This method does not mutate the MacroProps instance.
83      * @param safe
84      *            If true, the returned MicroPropsGenerator will be thread-safe. If false, the returned value will
85      *            <em>not</em> be thread-safe, intended for a single "one-shot" use only. Building the thread-safe
86      *            object is more expensive.
87      */
88     const MicroPropsGenerator *
89     macrosToMicroGenerator(const MacroProps &macros, bool safe, UErrorCode &status);
90 
91     /**
92      * Synthesizes the output string from a MicroProps and DecimalQuantity.
93      *
94      * @param micros
95      *            The MicroProps after the quantity has been consumed. Will not be mutated.
96      * @param quantity
97      *            The DecimalQuantity to be rendered. May be mutated.
98      * @param string
99      *            The output string. Will be mutated.
100      */
101     static int32_t
102     microsToString(const MicroProps &micros, DecimalQuantity &quantity, NumberStringBuilder &string,
103                    UErrorCode &status);
104 
105     static int32_t
106     writeNumber(const MicroProps &micros, DecimalQuantity &quantity, NumberStringBuilder &string,
107                 UErrorCode &status);
108 
109     static int32_t
110     writeIntegerDigits(const MicroProps &micros, DecimalQuantity &quantity, NumberStringBuilder &string,
111                        UErrorCode &status);
112 
113     static int32_t
114     writeFractionDigits(const MicroProps &micros, DecimalQuantity &quantity, NumberStringBuilder &string,
115                         UErrorCode &status);
116 };
117 
118 }  // namespace impl
119 }  // namespace number
120 U_NAMESPACE_END
121 
122 
123 #endif //__NUMBER_FORMATIMPL_H__
124 
125 #endif /* #if !UCONFIG_NO_FORMATTING */
126