number_usageprefs.h (3288B)
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 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 µs, 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 */ 55 const MaybeStackVector<MeasureUnit> *getOutputUnits() const { 56 return fUnitsRouter.getOutputUnits(); 57 } 58 59 private: 60 UnitsRouter fUnitsRouter; 61 const MicroPropsGenerator *fParent; 62 }; 63 64 /** 65 * A MicroPropsGenerator which converts a measurement from one MeasureUnit to 66 * another. In particular, the output MeasureUnit may be a mixed unit. (The 67 * input unit may not be a mixed unit.) 68 */ 69 class UnitConversionHandler : public MicroPropsGenerator, public UMemory { 70 public: 71 /** 72 * Constructor. 73 * 74 * @param targetUnit Specifies the output MeasureUnit. The input MeasureUnit 75 * is derived from it: in case of a mixed unit, the biggest unit is 76 * taken as the input unit. If not a mixed unit, the input unit will be 77 * the same as the output unit and no unit conversion takes place. 78 * @param parent The parent MicroPropsGenerator. 79 * @param status Receives status. 80 */ 81 UnitConversionHandler(const MeasureUnit &targetUnit, const MicroPropsGenerator *parent, 82 UErrorCode &status); 83 84 /** 85 * Obtains the appropriate output values from the Unit Converter. 86 */ 87 void processQuantity(DecimalQuantity &quantity, MicroProps µs, 88 UErrorCode &status) const override; 89 private: 90 MeasureUnit fOutputUnit; 91 LocalPointer<ComplexUnitsConverter> fUnitConverter; 92 const MicroPropsGenerator *fParent; 93 }; 94 95 } // namespace number::impl 96 97 U_NAMESPACE_END 98 99 #endif // __NUMBER_USAGEPREFS_H__ 100 #endif /* #if !UCONFIG_NO_FORMATTING */