ICU 62.1  62.1
measfmt.h
Go to the documentation of this file.
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 **********************************************************************
5 * Copyright (c) 2004-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
8 * Author: Alan Liu
9 * Created: April 20, 2004
10 * Since: ICU 3.0
11 **********************************************************************
12 */
13 #ifndef MEASUREFORMAT_H
14 #define MEASUREFORMAT_H
15 
16 #include "unicode/utypes.h"
17 
18 #if !UCONFIG_NO_FORMATTING
19 
20 #include "unicode/format.h"
21 #include "unicode/udat.h"
22 
37 
38  // Wide, short, and narrow must be first and in this order.
44 
50 
56 
63 
64 #ifndef U_HIDE_DEPRECATED_API
65 
70 #endif // U_HIDE_DEPRECATED_API
71 };
74 
76 
77 class Measure;
78 class MeasureUnit;
79 class NumberFormat;
80 class PluralRules;
81 class MeasureFormatCacheData;
82 class SharedNumberFormat;
83 class SharedPluralRules;
84 class QuantityFormatter;
85 class SimpleFormatter;
86 class ListFormatter;
87 class DateFormat;
88 
99  public:
100  using Format::parseObject;
101  using Format::format;
102 
111  const Locale &locale, UMeasureFormatWidth width, UErrorCode &status);
112 
121  const Locale &locale,
122  UMeasureFormatWidth width,
123  NumberFormat *nfToAdopt,
124  UErrorCode &status);
125 
130  MeasureFormat(const MeasureFormat &other);
131 
137 
142  virtual ~MeasureFormat();
143 
148  virtual UBool operator==(const Format &other) const;
149 
154  virtual Format *clone() const;
155 
160  virtual UnicodeString &format(
161  const Formattable &obj,
162  UnicodeString &appendTo,
163  FieldPosition &pos,
164  UErrorCode &status) const;
165 
172  virtual void parseObject(
173  const UnicodeString &source,
174  Formattable &reslt,
175  ParsePosition &pos) const;
176 
193  UnicodeString &formatMeasures(
194  const Measure *measures,
195  int32_t measureCount,
196  UnicodeString &appendTo,
197  FieldPosition &pos,
198  UErrorCode &status) const;
199 
213  UnicodeString &formatMeasurePerUnit(
214  const Measure &measure,
215  const MeasureUnit &perUnit,
216  UnicodeString &appendTo,
217  FieldPosition &pos,
218  UErrorCode &status) const;
219 
231  UnicodeString getUnitDisplayName(const MeasureUnit& unit, UErrorCode &status) const;
232 
233 
245  static MeasureFormat* U_EXPORT2 createCurrencyFormat(const Locale& locale,
246  UErrorCode& ec);
247 
258  static MeasureFormat* U_EXPORT2 createCurrencyFormat(UErrorCode& ec);
259 
271  static UClassID U_EXPORT2 getStaticClassID(void);
272 
284  virtual UClassID getDynamicClassID(void) const;
285 
286  protected:
291  MeasureFormat();
292 
293 #ifndef U_HIDE_INTERNAL_API
294 
300  void initMeasureFormat(
301  const Locale &locale,
302  UMeasureFormatWidth width,
303  NumberFormat *nfToAdopt,
304  UErrorCode &status);
312  UBool setMeasureFormatLocale(const Locale &locale, UErrorCode &status);
313 
319  void adoptNumberFormat(NumberFormat *nfToAdopt, UErrorCode &status);
320 
325  const NumberFormat &getNumberFormat() const;
326 
331  const PluralRules &getPluralRules() const;
332 
337  Locale getLocale(UErrorCode &status) const;
338 
343  const char *getLocaleID(UErrorCode &status) const;
344 
345 #endif /* U_HIDE_INTERNAL_API */
346 
347  private:
348  const MeasureFormatCacheData *cache;
349  const SharedNumberFormat *numberFormat;
350  const SharedPluralRules *pluralRules;
351  UMeasureFormatWidth width;
352 
353  // Declared outside of MeasureFormatSharedData because ListFormatter
354  // objects are relatively cheap to copy; therefore, they don't need to be
355  // shared across instances.
356  ListFormatter *listFormatter;
357 
358  const SimpleFormatter *getFormatterOrNull(
359  const MeasureUnit &unit, UMeasureFormatWidth width, int32_t index) const;
360 
361  const SimpleFormatter *getFormatter(
362  const MeasureUnit &unit, UMeasureFormatWidth width, int32_t index,
363  UErrorCode &errorCode) const;
364 
365  const SimpleFormatter *getPluralFormatter(
366  const MeasureUnit &unit, UMeasureFormatWidth width, int32_t index,
367  UErrorCode &errorCode) const;
368 
369  const SimpleFormatter *getPerFormatter(
370  UMeasureFormatWidth width,
371  UErrorCode &status) const;
372 
373  int32_t withPerUnitAndAppend(
374  const UnicodeString &formatted,
375  const MeasureUnit &perUnit,
376  UnicodeString &appendTo,
377  UErrorCode &status) const;
378 
379  UnicodeString &formatMeasure(
380  const Measure &measure,
381  const NumberFormat &nf,
382  UnicodeString &appendTo,
383  FieldPosition &pos,
384  UErrorCode &status) const;
385 
386  UnicodeString &formatMeasuresSlowTrack(
387  const Measure *measures,
388  int32_t measureCount,
389  UnicodeString& appendTo,
390  FieldPosition& pos,
391  UErrorCode& status) const;
392 
393  UnicodeString &formatNumeric(
394  const Formattable *hms, // always length 3: [0] is hour; [1] is
395  // minute; [2] is second.
396  int32_t bitMap, // 1=hour set, 2=minute set, 4=second set
397  UnicodeString &appendTo,
398  UErrorCode &status) const;
399 
400  UnicodeString &formatNumeric(
401  UDate date,
402  const DateFormat &dateFmt,
403  UDateFormatField smallestField,
404  const Formattable &smallestAmount,
405  UnicodeString &appendTo,
406  UErrorCode &status) const;
407 };
408 
410 
411 #endif // #if !UCONFIG_NO_FORMATTING
412 #endif // #ifndef MEASUREFORMAT_H
Base class for all formats.
Definition: format.h:96
const char * getLocaleID(ULocDataLocaleType type, UErrorCode &status) const
Get the locale for this format object.
UMeasureFormatWidth
Constants for various widths.
Definition: measfmt.h:36
A unit such as length, mass, volume, currency, etc.
Definition: measunit.h:38
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
One more than the highest normal UMeasureFormatWidth value.
Definition: measfmt.h:69
An amount of a specified unit, consisting of a number and a Unit.
Definition: measure.h:43
Use symbols for measure units when possible.
Definition: measfmt.h:55
double UDate
Date and Time data type.
Definition: utypes.h:203
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:93
virtual Format * clone() const =0
Clone this object polymorphically.
Defines rules for mapping non-negative numeric values onto a small set of keywords.
Definition: plurrule.h:196
C++ API: Base class for all formats.
Formats simple patterns like "{1} was born in {0}".
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:360
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
Spell out measure units.
Definition: measfmt.h:43
Abbreviate measure units.
Definition: measfmt.h:49
An immutable class for formatting a list, using data from CLDR (or supplied separately).
Definition: listformatter.h:64
C API: DateFormat.
virtual UClassID getDynamicClassID() const
ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
Completely omit measure units when possible.
Definition: measfmt.h:62
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
UDateFormatField
FieldPosition and UFieldPosition selectors for format fields defined by DateFormat and UDateFormat...
Definition: udat.h:490
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:396
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:108
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:49
DateFormat is an abstract class for a family of classes that convert dates and times from their inter...
Definition: datefmt.h:150
virtual UBool operator==(const Format &other) const =0
Return true if the given Format objects are semantically equal.
virtual void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const =0
Parse a string to produce an object.
Basic definitions for ICU, for both C and C++ APIs.
Format & operator=(const Format &)
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:286
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:62
Locale getLocale(ULocDataLocaleType type, UErrorCode &status) const
Get the locale for this format object.
int8_t UBool
The ICU boolean type.
Definition: umachine.h:236
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:188