ICU 63.1 63.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
36 enum UMeasureFormatWidth {
37
38 // Wide, short, and narrow must be first and in this order.
43 UMEASFMT_WIDTH_WIDE,
44
49 UMEASFMT_WIDTH_SHORT,
50
55 UMEASFMT_WIDTH_NARROW,
56
62 UMEASFMT_WIDTH_NUMERIC,
63
64 #ifndef U_HIDE_DEPRECATED_API
65
69 UMEASFMT_WIDTH_COUNT = 4
70 #endif // U_HIDE_DEPRECATED_API
71 };
73 typedef enum UMeasureFormatWidth UMeasureFormatWidth;
74
75 U_NAMESPACE_BEGIN
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 class U_I18N_API MeasureFormat : public Format {
99 public:
100 using Format::parseObject;
101 using Format::format;
102
110 MeasureFormat(
111 const Locale &locale, UMeasureFormatWidth width, UErrorCode &status);
112
120 MeasureFormat(
121 const Locale &locale,
122 UMeasureFormatWidth width,
123 NumberFormat *nfToAdopt,
124 UErrorCode &status);
125
130 MeasureFormat(const MeasureFormat &other);
131
136 MeasureFormat &operator=(const MeasureFormat &rhs);
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 fWidth;
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
409 U_NAMESPACE_END
410
411 #endif // #if !UCONFIG_NO_FORMATTING
412 #endif // #ifndef MEASUREFORMAT_H
icu::Format
Base class for all formats.
Definition: format.h:96
icu::Format::getLocaleID
const char * getLocaleID(ULocDataLocaleType type, UErrorCode &status) const
Get the locale for this format object.
UMeasureFormatWidth
UMeasureFormatWidth
Constants for various widths.
Definition: measfmt.h:36
icu::MeasureUnit
A unit such as length, mass, volume, currency, etc.
Definition: measunit.h:38
icu::Format::format
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
UMEASFMT_WIDTH_COUNT
One more than the highest normal UMeasureFormatWidth value.
Definition: measfmt.h:69
icu::Measure
An amount of a specified unit, consisting of a number and a Unit.
Definition: measure.h:43
icu::MeasureFormat
Definition: measfmt.h:98
UMEASFMT_WIDTH_NARROW
Use symbols for measure units when possible.
Definition: measfmt.h:55
icu::NumberFormat
Definition: numfmt.h:173
UDate
double UDate
Date and Time data type.
Definition: utypes.h:203
UClassID
void * UClassID
UClassID is used to identify classes without using the compiler&#39;s RTTI.
Definition: uobject.h:90
icu::Format::clone
virtual Format * clone() const =0
Clone this object polymorphically.
icu::PluralRules
Defines rules for mapping non-negative numeric values onto a small set of keywords.
Definition: plurrule.h:194
format.h
C++ API: Base class for all formats.
icu::SimpleFormatter
Formats simple patterns like "{1} was born in {0}".
Definition: simpleformatter.h:59
U_I18N_API
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:301
U_NAMESPACE_BEGIN
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
UMEASFMT_WIDTH_WIDE
Spell out measure units.
Definition: measfmt.h:43
UMEASFMT_WIDTH_SHORT
Abbreviate measure units.
Definition: measfmt.h:49
icu::ListFormatter
An immutable class for formatting a list, using data from CLDR (or supplied separately).
Definition: listformatter.h:71
udat.h
C API: DateFormat.
icu::UObject::getDynamicClassID
virtual UClassID getDynamicClassID() const
ICU4C "poor man&#39;s RTTI", returns a UClassID for the actual ICU class.
UMEASFMT_WIDTH_NUMERIC
Completely omit measure units when possible.
Definition: measfmt.h:62
U_NAMESPACE_END
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
UDateFormatField
UDateFormatField
FieldPosition and UFieldPosition selectors for format fields defined by DateFormat and UDateFormat...
Definition: udat.h:490
UErrorCode
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:401
icu::FieldPosition
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:108
icu::ParsePosition
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:49
icu::DateFormat
DateFormat is an abstract class for a family of classes that convert dates and times from their inter...
Definition: datefmt.h:154
icu::Format::operator==
virtual UBool operator==(const Format &other) const =0
Return true if the given Format objects are semantically equal.
icu::Format::parseObject
virtual void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const =0
Parse a string to produce an object.
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
icu::Format::operator=
Format & operator=(const Format &)
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:289
icu::Formattable
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:62
icu::Format::getLocale
Locale getLocale(ULocDataLocaleType type, UErrorCode &status) const
Get the locale for this format object.
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:225
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:192