xref: /aosp_15_r20/external/icu/libicu/cts_headers/number_usageprefs.h (revision 0e209d3975ff4a8c132096b14b0e9364a753506e)
1 // © 2020 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
7 #ifndef __NUMBER_USAGEPREFS_H__
8 #define __NUMBER_USAGEPREFS_H__
9 
10 #include "cmemory.h"
11 #include "number_types.h"
12 #include "unicode/listformatter.h"
13 #include "unicode/localpointer.h"
14 #include "unicode/locid.h"
15 #include "unicode/measunit.h"
16 #include "unicode/stringpiece.h"
17 #include "unicode/uobject.h"
18 #include "units_converter.h"
19 #include "units_router.h"
20 
21 U_NAMESPACE_BEGIN
22 
23 using ::icu::units::ComplexUnitsConverter;
24 using ::icu::units::UnitsRouter;
25 
26 namespace number::impl {
27 
28 /**
29  * A MicroPropsGenerator which uses UnitsRouter to produce output converted to a
30  * MeasureUnit appropriate for a particular localized usage: see
31  * NumberFormatterSettings::usage().
32  */
33 class U_I18N_API UsagePrefsHandler : public MicroPropsGenerator, public UMemory {
34   public:
35     UsagePrefsHandler(const Locale &locale, const MeasureUnit &inputUnit, const StringPiece usage,
36                       const MicroPropsGenerator *parent, UErrorCode &status);
37 
38     /**
39      * Obtains the appropriate output value, MeasureUnit and
40      * rounding/precision behaviour from the UnitsRouter.
41      *
42      * The output unit is passed on to the LongNameHandler via
43      * micros.outputUnit.
44      */
45     void processQuantity(DecimalQuantity &quantity, MicroProps &micros,
46                          UErrorCode &status) const override;
47 
48     /**
49      * Returns the list of possible output units, i.e. the full set of
50      * preferences, for the localized, usage-specific unit preferences.
51      *
52      * The returned pointer should be valid for the lifetime of the
53      * UsagePrefsHandler instance.
54      */
getOutputUnits()55     const MaybeStackVector<MeasureUnit> *getOutputUnits() const {
56         return fUnitsRouter.getOutputUnits();
57     }
58 
59   private:
60     UnitsRouter fUnitsRouter;
61     const MicroPropsGenerator *fParent;
62 };
63 
64 } // namespace number::impl
65 
66 // Export explicit template instantiations of LocalPointerBase and LocalPointer.
67 // This is required when building DLLs for Windows. (See datefmt.h,
68 // collationiterator.h, erarules.h and others for similar examples.)
69 //
70 // Note: These need to be outside of the number::impl namespace, or Clang will
71 // generate a compile error.
72 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
73 #if defined(_MSC_VER)
74 // Ignore warning 4661 as LocalPointerBase does not use operator== or operator!=
75 #pragma warning(push)
76 #pragma warning(disable: 4661)
77 #endif
78 template class U_I18N_API LocalPointerBase<ComplexUnitsConverter>;
79 template class U_I18N_API LocalPointer<ComplexUnitsConverter>;
80 #if defined(_MSC_VER)
81 #pragma warning(pop)
82 #endif
83 #endif
84 
85 namespace number::impl {
86 
87 /**
88  * A MicroPropsGenerator which converts a measurement from one MeasureUnit to
89  * another. In particular, the output MeasureUnit may be a mixed unit. (The
90  * input unit may not be a mixed unit.)
91  */
92 class U_I18N_API UnitConversionHandler : public MicroPropsGenerator, public UMemory {
93   public:
94     /**
95      * Constructor.
96      *
97      * @param targetUnit Specifies the output MeasureUnit. The input MeasureUnit
98      *     is derived from it: in case of a mixed unit, the biggest unit is
99      *     taken as the input unit. If not a mixed unit, the input unit will be
100      *     the same as the output unit and no unit conversion takes place.
101      * @param parent The parent MicroPropsGenerator.
102      * @param status Receives status.
103      */
104     UnitConversionHandler(const MeasureUnit &targetUnit, const MicroPropsGenerator *parent,
105                           UErrorCode &status);
106 
107     /**
108      * Obtains the appropriate output values from the Unit Converter.
109      */
110     void processQuantity(DecimalQuantity &quantity, MicroProps &micros,
111                          UErrorCode &status) const override;
112   private:
113     MeasureUnit fOutputUnit;
114     LocalPointer<ComplexUnitsConverter> fUnitConverter;
115     const MicroPropsGenerator *fParent;
116 };
117 
118 } // namespace number::impl
119 
120 U_NAMESPACE_END
121 
122 #endif // __NUMBER_USAGEPREFS_H__
123 #endif /* #if !UCONFIG_NO_FORMATTING */
124