• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 template class U_I18N_API LocalPointerBase<ComplexUnitsConverter>;
74 template class U_I18N_API LocalPointer<ComplexUnitsConverter>;
75 #endif
76 
77 namespace number::impl {
78 
79 /**
80  * A MicroPropsGenerator which converts a measurement from one MeasureUnit to
81  * another. In particular, the output MeasureUnit may be a mixed unit. (The
82  * input unit may not be a mixed unit.)
83  */
84 class U_I18N_API UnitConversionHandler : public MicroPropsGenerator, public UMemory {
85   public:
86     /**
87      * Constructor.
88      *
89      * @param targetUnit Specifies the output MeasureUnit. The input MeasureUnit
90      *     is derived from it: in case of a mixed unit, the biggest unit is
91      *     taken as the input unit. If not a mixed unit, the input unit will be
92      *     the same as the output unit and no unit conversion takes place.
93      * @param parent The parent MicroPropsGenerator.
94      * @param status Receives status.
95      */
96     UnitConversionHandler(const MeasureUnit &targetUnit, const MicroPropsGenerator *parent,
97                           UErrorCode &status);
98 
99     /**
100      * Obtains the appropriate output values from the Unit Converter.
101      */
102     void processQuantity(DecimalQuantity &quantity, MicroProps &micros,
103                          UErrorCode &status) const override;
104   private:
105     MeasureUnit fOutputUnit;
106     LocalPointer<ComplexUnitsConverter> fUnitConverter;
107     const MicroPropsGenerator *fParent;
108 };
109 
110 } // namespace number::impl
111 
112 U_NAMESPACE_END
113 
114 #endif // __NUMBER_USAGEPREFS_H__
115 #endif /* #if !UCONFIG_NO_FORMATTING */
116