ICU 59.1  59.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 
98  public:
99  using Format::parseObject;
100  using Format::format;
101 
107  const Locale &locale, UMeasureFormatWidth width, UErrorCode &status);
108 
114  const Locale &locale,
115  UMeasureFormatWidth width,
116  NumberFormat *nfToAdopt,
117  UErrorCode &status);
118 
123  MeasureFormat(const MeasureFormat &other);
124 
130 
135  virtual ~MeasureFormat();
136 
141  virtual UBool operator==(const Format &other) const;
142 
147  virtual Format *clone() const;
148 
153  virtual UnicodeString &format(
154  const Formattable &obj,
155  UnicodeString &appendTo,
156  FieldPosition &pos,
157  UErrorCode &status) const;
158 
165  virtual void parseObject(
166  const UnicodeString &source,
167  Formattable &reslt,
168  ParsePosition &pos) const;
169 
186  UnicodeString &formatMeasures(
187  const Measure *measures,
188  int32_t measureCount,
189  UnicodeString &appendTo,
190  FieldPosition &pos,
191  UErrorCode &status) const;
192 
206  UnicodeString &formatMeasurePerUnit(
207  const Measure &measure,
208  const MeasureUnit &perUnit,
209  UnicodeString &appendTo,
210  FieldPosition &pos,
211  UErrorCode &status) const;
212 
213 #ifndef U_HIDE_DRAFT_API
214 
225  UnicodeString getUnitDisplayName(const MeasureUnit& unit, UErrorCode &status) const;
226 #endif /* U_HIDE_DRAFT_API */
227 
228 
237  static MeasureFormat* U_EXPORT2 createCurrencyFormat(const Locale& locale,
238  UErrorCode& ec);
239 
247  static MeasureFormat* U_EXPORT2 createCurrencyFormat(UErrorCode& ec);
248 
260  static UClassID U_EXPORT2 getStaticClassID(void);
261 
273  virtual UClassID getDynamicClassID(void) const;
274 
275  protected:
280  MeasureFormat();
281 
282 #ifndef U_HIDE_INTERNAL_API
283 
289  void initMeasureFormat(
290  const Locale &locale,
291  UMeasureFormatWidth width,
292  NumberFormat *nfToAdopt,
293  UErrorCode &status);
301  UBool setMeasureFormatLocale(const Locale &locale, UErrorCode &status);
302 
308  void adoptNumberFormat(NumberFormat *nfToAdopt, UErrorCode &status);
309 
314  const NumberFormat &getNumberFormat() const;
315 
320  const PluralRules &getPluralRules() const;
321 
326  Locale getLocale(UErrorCode &status) const;
327 
332  const char *getLocaleID(UErrorCode &status) const;
333 
334 #endif /* U_HIDE_INTERNAL_API */
335 
336  private:
337  const MeasureFormatCacheData *cache;
338  const SharedNumberFormat *numberFormat;
339  const SharedPluralRules *pluralRules;
340  UMeasureFormatWidth width;
341 
342  // Declared outside of MeasureFormatSharedData because ListFormatter
343  // objects are relatively cheap to copy; therefore, they don't need to be
344  // shared across instances.
345  ListFormatter *listFormatter;
346 
347  const SimpleFormatter *getFormatterOrNull(
348  const MeasureUnit &unit, UMeasureFormatWidth width, int32_t index) const;
349 
350  const SimpleFormatter *getFormatter(
351  const MeasureUnit &unit, UMeasureFormatWidth width, int32_t index,
352  UErrorCode &errorCode) const;
353 
354  const SimpleFormatter *getPluralFormatter(
355  const MeasureUnit &unit, UMeasureFormatWidth width, int32_t index,
356  UErrorCode &errorCode) const;
357 
358  const SimpleFormatter *getPerFormatter(
359  UMeasureFormatWidth width,
360  UErrorCode &status) const;
361 
362  int32_t withPerUnitAndAppend(
363  const UnicodeString &formatted,
364  const MeasureUnit &perUnit,
365  UnicodeString &appendTo,
366  UErrorCode &status) const;
367 
368  UnicodeString &formatMeasure(
369  const Measure &measure,
370  const NumberFormat &nf,
371  UnicodeString &appendTo,
372  FieldPosition &pos,
373  UErrorCode &status) const;
374 
375  UnicodeString &formatMeasuresSlowTrack(
376  const Measure *measures,
377  int32_t measureCount,
378  UnicodeString& appendTo,
379  FieldPosition& pos,
380  UErrorCode& status) const;
381 
382  UnicodeString &formatNumeric(
383  const Formattable *hms, // always length 3: [0] is hour; [1] is
384  // minute; [2] is second.
385  int32_t bitMap, // 1=hour set, 2=minute set, 4=second set
386  UnicodeString &appendTo,
387  UErrorCode &status) const;
388 
389  UnicodeString &formatNumeric(
390  UDate date,
391  const DateFormat &dateFmt,
392  UDateFormatField smallestField,
393  const Formattable &smallestAmount,
394  UnicodeString &appendTo,
395  UErrorCode &status) const;
396 };
397 
399 
400 #endif // #if !UCONFIG_NO_FORMATTING
401 #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
A formatter for measure objects.
Definition: measfmt.h:97
Use symbols for measure units when possible.
Definition: measfmt.h:55
Abstract base class for all number formats.
Definition: numfmt.h:169
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:197
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:131
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:132
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:149
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:296
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:68
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