Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/unicode/measfmt.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // © 2016 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 /*
0004 **********************************************************************
0005 * Copyright (c) 2004-2016, International Business Machines
0006 * Corporation and others.  All Rights Reserved.
0007 **********************************************************************
0008 * Author: Alan Liu
0009 * Created: April 20, 2004
0010 * Since: ICU 3.0
0011 **********************************************************************
0012 */
0013 #ifndef MEASUREFORMAT_H
0014 #define MEASUREFORMAT_H
0015 
0016 #include "unicode/utypes.h"
0017 
0018 #if U_SHOW_CPLUSPLUS_API
0019 
0020 #if !UCONFIG_NO_FORMATTING
0021 
0022 #include "unicode/format.h"
0023 #include "unicode/udat.h"
0024 
0025 /**
0026  * \file 
0027  * \brief C++ API: Compatibility APIs for measure formatting.
0028  */
0029 
0030 /**
0031  * Constants for various widths.
0032  * There are 4 widths: Wide, Short, Narrow, Numeric.
0033  * For example, for English, when formatting "3 hours"
0034  * Wide is "3 hours"; short is "3 hrs"; narrow is "3h";
0035  * formatting "3 hours 17 minutes" as numeric give "3:17"
0036  * @stable ICU 53
0037  */
0038 enum UMeasureFormatWidth {
0039 
0040     // Wide, short, and narrow must be first and in this order.
0041     /**
0042      * Spell out measure units.
0043      * @stable ICU 53 
0044      */
0045     UMEASFMT_WIDTH_WIDE,
0046  
0047     /**
0048      * Abbreviate measure units.
0049      * @stable ICU 53
0050      */
0051     UMEASFMT_WIDTH_SHORT,
0052 
0053     /**
0054      * Use symbols for measure units when possible.
0055      * @stable ICU 53
0056      */
0057     UMEASFMT_WIDTH_NARROW,
0058 
0059     /**
0060      * Completely omit measure units when possible. For example, format
0061      * '5 hours, 37 minutes' as '5:37'
0062      * @stable ICU 53
0063      */
0064     UMEASFMT_WIDTH_NUMERIC,
0065 
0066 #ifndef U_HIDE_DEPRECATED_API
0067     /**
0068      * One more than the highest normal UMeasureFormatWidth value.
0069      * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
0070      */
0071     UMEASFMT_WIDTH_COUNT = 4
0072 #endif  // U_HIDE_DEPRECATED_API
0073 };
0074 /** @stable ICU 53 */
0075 typedef enum UMeasureFormatWidth UMeasureFormatWidth; 
0076 
0077 U_NAMESPACE_BEGIN
0078 
0079 class Measure;
0080 class MeasureUnit;
0081 class NumberFormat;
0082 class PluralRules;
0083 class MeasureFormatCacheData;
0084 class SharedNumberFormat;
0085 class SharedPluralRules;
0086 class QuantityFormatter;
0087 class SimpleFormatter;
0088 class ListFormatter;
0089 class DateFormat;
0090 
0091 /**
0092  * <p><strong>IMPORTANT:</strong> New users are strongly encouraged to see if
0093  * numberformatter.h fits their use case.  Although not deprecated, this header
0094  * is provided for backwards compatibility only, and has much more limited
0095  * capabilities.
0096  *
0097  * @see Format
0098  * @author Alan Liu
0099  * @stable ICU 3.0
0100  */
0101 class U_I18N_API MeasureFormat : public Format {
0102  public:
0103     using Format::parseObject;
0104     using Format::format;
0105 
0106     /**
0107      * Constructor.
0108      * <p>
0109      * <strong>NOTE:</strong> New users are strongly encouraged to use
0110      * {@link icu::number::NumberFormatter} instead of NumberFormat.
0111      * @stable ICU 53
0112      */
0113     MeasureFormat(
0114             const Locale &locale, UMeasureFormatWidth width, UErrorCode &status);
0115 
0116     /**
0117      * Constructor.
0118      * <p>
0119      * <strong>NOTE:</strong> New users are strongly encouraged to use
0120      * {@link icu::number::NumberFormatter} instead of NumberFormat.
0121      * @stable ICU 53
0122      */
0123     MeasureFormat(
0124             const Locale &locale,
0125             UMeasureFormatWidth width,
0126             NumberFormat *nfToAdopt,
0127             UErrorCode &status);
0128 
0129     /**
0130      * Copy constructor.
0131      * @stable ICU 3.0
0132      */
0133     MeasureFormat(const MeasureFormat &other);
0134 
0135     /**
0136      * Assignment operator.
0137      * @stable ICU 3.0
0138      */
0139     MeasureFormat &operator=(const MeasureFormat &rhs);
0140 
0141     /**
0142      * Destructor.
0143      * @stable ICU 3.0
0144      */
0145     virtual ~MeasureFormat();
0146 
0147     /**
0148      * Return true if given Format objects are semantically equal.
0149      * @stable ICU 53
0150      */
0151     virtual bool operator==(const Format &other) const override;
0152 
0153     /**
0154      * Clones this object polymorphically.
0155      * @stable ICU 53
0156      */
0157     virtual MeasureFormat *clone() const override;
0158 
0159     /**
0160      * Formats object to produce a string.
0161      * @stable ICU 53
0162      */
0163     virtual UnicodeString &format(
0164             const Formattable &obj,
0165             UnicodeString &appendTo,
0166             FieldPosition &pos,
0167             UErrorCode &status) const override;
0168 
0169 #ifndef U_FORCE_HIDE_DRAFT_API
0170     /**
0171      * Parse a string to produce an object. This implementation sets
0172      * status to U_UNSUPPORTED_ERROR.
0173      *
0174      * @draft ICU 53
0175      */
0176     virtual void parseObject(
0177             const UnicodeString &source,
0178             Formattable &reslt,
0179             ParsePosition &pos) const override;
0180 #endif  // U_FORCE_HIDE_DRAFT_API
0181 
0182     /**
0183      * Formats measure objects to produce a string. An example of such a
0184      * formatted string is 3 meters, 3.5 centimeters. Measure objects appear
0185      * in the formatted string in the same order they appear in the "measures"
0186      * array. The NumberFormat of this object is used only to format the amount
0187      * of the very last measure. The other amounts are formatted with zero
0188      * decimal places while rounding toward zero.
0189      * @param measures array of measure objects.
0190      * @param measureCount the number of measure objects.
0191      * @param appendTo formatted string appended here.
0192      * @param pos the field position.
0193      * @param status the error.
0194      * @return appendTo reference
0195      *
0196      * @stable ICU 53
0197      */
0198     UnicodeString &formatMeasures(
0199             const Measure *measures,
0200             int32_t measureCount,
0201             UnicodeString &appendTo,
0202             FieldPosition &pos,
0203             UErrorCode &status) const;
0204 
0205     /**
0206      * Formats a single measure per unit. An example of such a
0207      * formatted string is 3.5 meters per second.
0208      * @param measure The measure object. In above example, 3.5 meters.
0209      * @param perUnit The per unit. In above example, it is
0210      *        `*%MeasureUnit::createSecond(status)`.
0211      * @param appendTo formatted string appended here.
0212      * @param pos the field position.
0213      * @param status the error.
0214      * @return appendTo reference
0215      *
0216      * @stable ICU 55
0217      */
0218     UnicodeString &formatMeasurePerUnit(
0219             const Measure &measure,
0220             const MeasureUnit &perUnit,
0221             UnicodeString &appendTo,
0222             FieldPosition &pos,
0223             UErrorCode &status) const;
0224 
0225     /**
0226      * Gets the display name of the specified {@link MeasureUnit} corresponding to the current
0227      * locale and format width.
0228      * @param unit  The unit for which to get a display name.
0229      * @param status the error.
0230      * @return  The display name in the locale and width specified in
0231      *          the MeasureFormat constructor, or null if there is no display name available
0232      *          for the specified unit.
0233      *
0234      * @stable ICU 58
0235      */
0236     UnicodeString getUnitDisplayName(const MeasureUnit& unit, UErrorCode &status) const;
0237 
0238 
0239     /**
0240      * Return a formatter for CurrencyAmount objects in the given
0241      * locale.
0242      * <p>
0243      * <strong>NOTE:</strong> New users are strongly encouraged to use
0244      * {@link icu::number::NumberFormatter} instead of NumberFormat.
0245      * @param locale desired locale
0246      * @param ec input-output error code
0247      * @return a formatter object, or nullptr upon error
0248      * @stable ICU 3.0
0249      */
0250     static MeasureFormat* U_EXPORT2 createCurrencyFormat(const Locale& locale,
0251                                                UErrorCode& ec);
0252 
0253     /**
0254      * Return a formatter for CurrencyAmount objects in the default
0255      * locale.
0256      * <p>
0257      * <strong>NOTE:</strong> New users are strongly encouraged to use
0258      * {@link icu::number::NumberFormatter} instead of NumberFormat.
0259      * @param ec input-output error code
0260      * @return a formatter object, or nullptr upon error
0261      * @stable ICU 3.0
0262      */
0263     static MeasureFormat* U_EXPORT2 createCurrencyFormat(UErrorCode& ec);
0264 
0265     /**
0266      * Return the class ID for this class. This is useful only for comparing to
0267      * a return value from getDynamicClassID(). For example:
0268      * <pre>
0269      * .   Base* polymorphic_pointer = createPolymorphicObject();
0270      * .   if (polymorphic_pointer->getDynamicClassID() ==
0271      * .       erived::getStaticClassID()) ...
0272      * </pre>
0273      * @return          The class ID for all objects of this class.
0274      * @stable ICU 53
0275      */
0276     static UClassID U_EXPORT2 getStaticClassID(void);
0277 
0278     /**
0279      * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
0280      * method is to implement a simple version of RTTI, since not all C++
0281      * compilers support genuine RTTI. Polymorphic operator==() and clone()
0282      * methods call this method.
0283      *
0284      * @return          The class ID for this object. All objects of a
0285      *                  given class have the same class ID.  Objects of
0286      *                  other classes have different class IDs.
0287      * @stable ICU 53
0288      */
0289     virtual UClassID getDynamicClassID(void) const override;
0290 
0291  protected:
0292     /**
0293      * Default constructor.
0294      * @stable ICU 3.0
0295      */
0296     MeasureFormat();
0297 
0298 #ifndef U_HIDE_INTERNAL_API 
0299 
0300     /**
0301      * ICU use only.
0302      * Initialize or change MeasureFormat class from subclass.
0303      * @internal.
0304      */
0305     void initMeasureFormat(
0306             const Locale &locale,
0307             UMeasureFormatWidth width,
0308             NumberFormat *nfToAdopt,
0309             UErrorCode &status);
0310     /**
0311      * ICU use only.
0312      * Allows subclass to change locale. Note that this method also changes
0313      * the NumberFormat object. Returns true if locale changed; false if no
0314      * change was made.
0315      * @internal.
0316      */
0317     UBool setMeasureFormatLocale(const Locale &locale, UErrorCode &status);
0318 
0319     /**
0320      * ICU use only.
0321      * Let subclass change NumberFormat.
0322      * @internal.
0323      */
0324     void adoptNumberFormat(NumberFormat *nfToAdopt, UErrorCode &status);
0325 
0326     /**
0327      * ICU use only.
0328      * @internal.
0329      */
0330     const NumberFormat &getNumberFormatInternal() const;
0331 
0332     /**
0333      * ICU use only.
0334      * Always returns the short form currency formatter.
0335      * @internal.
0336      */
0337     const NumberFormat& getCurrencyFormatInternal() const;
0338 
0339     /**
0340      * ICU use only.
0341      * @internal.
0342      */
0343     const PluralRules &getPluralRules() const;
0344 
0345     /**
0346      * ICU use only.
0347      * @internal.
0348      */
0349     Locale getLocale(UErrorCode &status) const;
0350 
0351     /**
0352      * ICU use only.
0353      * @internal.
0354      */
0355     const char *getLocaleID(UErrorCode &status) const;
0356 
0357 #endif /* U_HIDE_INTERNAL_API */
0358 
0359  private:
0360     const MeasureFormatCacheData *cache;
0361     const SharedNumberFormat *numberFormat;
0362     const SharedPluralRules *pluralRules;
0363     UMeasureFormatWidth fWidth;    
0364 
0365     // Declared outside of MeasureFormatSharedData because ListFormatter
0366     // objects are relatively cheap to copy; therefore, they don't need to be
0367     // shared across instances.
0368     ListFormatter *listFormatter;
0369 
0370     UnicodeString &formatMeasure(
0371         const Measure &measure,
0372         const NumberFormat &nf,
0373         UnicodeString &appendTo,
0374         FieldPosition &pos,
0375         UErrorCode &status) const;
0376 
0377     UnicodeString &formatMeasuresSlowTrack(
0378         const Measure *measures,
0379         int32_t measureCount,
0380         UnicodeString& appendTo,
0381         FieldPosition& pos,
0382         UErrorCode& status) const;
0383 
0384     UnicodeString &formatNumeric(
0385         const Formattable *hms,  // always length 3: [0] is hour; [1] is
0386                                  // minute; [2] is second.
0387         int32_t bitMap,   // 1=hour set, 2=minute set, 4=second set
0388         UnicodeString &appendTo,
0389         UErrorCode &status) const;
0390 };
0391 
0392 U_NAMESPACE_END
0393 
0394 #endif // #if !UCONFIG_NO_FORMATTING
0395 
0396 #endif /* U_SHOW_CPLUSPLUS_API */
0397 
0398 #endif // #ifndef MEASUREFORMAT_H