ICU 70.1  70.1
plurfmt.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) 2007-2014, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
8 *
9 
10 * File PLURFMT.H
11 ********************************************************************************
12 */
13 
14 #ifndef PLURFMT
15 #define PLURFMT
16 
17 #include "unicode/utypes.h"
18 
19 #if U_SHOW_CPLUSPLUS_API
20 
26 #if !UCONFIG_NO_FORMATTING
27 
28 #include "unicode/messagepattern.h"
29 #include "unicode/numfmt.h"
30 #include "unicode/plurrule.h"
31 
32 U_NAMESPACE_BEGIN
33 
34 class Hashtable;
35 class NFRule;
36 
149 public:
150 
160 
170  PluralFormat(const Locale& locale, UErrorCode& status);
171 
181  PluralFormat(const PluralRules& rules, UErrorCode& status);
182 
199  PluralFormat(const Locale& locale, const PluralRules& rules, UErrorCode& status);
200 
211  PluralFormat(const Locale& locale, UPluralType type, UErrorCode& status);
212 
223  PluralFormat(const UnicodeString& pattern, UErrorCode& status);
224 
239  PluralFormat(const Locale& locale, const UnicodeString& pattern, UErrorCode& status);
240 
252  PluralFormat(const PluralRules& rules,
253  const UnicodeString& pattern,
254  UErrorCode& status);
255 
270  PluralFormat(const Locale& locale,
271  const PluralRules& rules,
272  const UnicodeString& pattern,
273  UErrorCode& status);
274 
288  PluralFormat(const Locale& locale,
289  UPluralType type,
290  const UnicodeString& pattern,
291  UErrorCode& status);
292 
297  PluralFormat(const PluralFormat& other);
298 
303  virtual ~PluralFormat();
304 
317  void applyPattern(const UnicodeString& pattern, UErrorCode& status);
318 
319 
320  using Format::format;
321 
334  UnicodeString format(int32_t number, UErrorCode& status) const;
335 
348  UnicodeString format(double number, UErrorCode& status) const;
349 
366  UnicodeString& format(int32_t number,
367  UnicodeString& appendTo,
368  FieldPosition& pos,
369  UErrorCode& status) const;
370 
387  UnicodeString& format(double number,
388  UnicodeString& appendTo,
389  FieldPosition& pos,
390  UErrorCode& status) const;
391 
392 #ifndef U_HIDE_DEPRECATED_API
408  void setLocale(const Locale& locale, UErrorCode& status);
409 #endif /* U_HIDE_DEPRECATED_API */
410 
420  void setNumberFormat(const NumberFormat* format, UErrorCode& status);
421 
429 
437  virtual bool operator==(const Format& other) const override;
438 
446  virtual bool operator!=(const Format& other) const;
447 
453  virtual PluralFormat* clone() const override;
454 
470  UnicodeString& appendTo,
471  FieldPosition& pos,
472  UErrorCode& status) const override;
473 
483 
506  virtual void parseObject(const UnicodeString& source,
507  Formattable& result,
508  ParsePosition& parse_pos) const override;
509 
516  static UClassID U_EXPORT2 getStaticClassID(void);
517 
523  virtual UClassID getDynamicClassID() const override;
524 
525 private:
529  class U_I18N_API PluralSelector : public UMemory {
530  public:
531  virtual ~PluralSelector();
541  virtual UnicodeString select(void *context, double number, UErrorCode& ec) const = 0;
542  };
543 
544  class U_I18N_API PluralSelectorAdapter : public PluralSelector {
545  public:
546  PluralSelectorAdapter() : pluralRules(NULL) {
547  }
548 
549  virtual ~PluralSelectorAdapter();
550 
551  virtual UnicodeString select(void *context, double number, UErrorCode& /*ec*/) const override;
552 
553  void reset();
554 
555  PluralRules* pluralRules;
556  };
557 
558  Locale locale;
559  MessagePattern msgPattern;
560  NumberFormat* numberFormat;
561  double offset;
562  PluralSelectorAdapter pluralRulesWrapper;
563 
564  PluralFormat(); // default constructor not implemented
565  void init(const PluralRules* rules, UPluralType type, UErrorCode& status);
570  void copyObjects(const PluralFormat& other);
571 
572  UnicodeString& format(const Formattable& numberObject, double number,
573  UnicodeString& appendTo,
574  FieldPosition& pos,
575  UErrorCode& status) const;
576 
588  static int32_t findSubMessage(
589  const MessagePattern& pattern, int32_t partIndex,
590  const PluralSelector& selector, void *context, double number, UErrorCode& ec);
591 
592  void parseType(const UnicodeString& source, const NFRule *rbnfLenientScanner,
593  Formattable& result, FieldPosition& pos) const;
594 
595  friend class MessageFormat;
596  friend class NFRule;
597 };
598 
599 U_NAMESPACE_END
600 
601 #endif /* #if !UCONFIG_NO_FORMATTING */
602 
603 #endif /* U_SHOW_CPLUSPLUS_API */
604 
605 #endif // _PLURFMT
606 //eof
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:110
Base class for all formats.
Definition: format.h:98
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:64
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:52
void setNumberFormat(const NumberFormat *format, UErrorCode &status)
Sets the number format used by this formatter.
PluralFormat(const Locale &locale, UPluralType type, const UnicodeString &pattern, UErrorCode &status)
Creates a new PluralFormat for a plural type, a pattern and a locale.
PluralFormat(const Locale &locale, const UnicodeString &pattern, UErrorCode &status)
Creates a new cardinal-number PluralFormat for a given pattern string and locale.
virtual UClassID getDynamicClassID() const override
ICU "poor man's RTTI", returns a UClassID for the actual class.
PluralFormat & operator=(const PluralFormat &other)
Assignment operator.
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const override
Formats a plural message for a number taken from a Formattable object.
virtual void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const override
This method is not yet supported by PluralFormat.
virtual bool operator!=(const Format &other) const
Return true if another object is semantically unequal to this one.
PluralFormat(const UnicodeString &pattern, UErrorCode &status)
Creates a new cardinal-number PluralFormat for a given pattern string.
void applyPattern(const UnicodeString &pattern, UErrorCode &status)
Sets the pattern used by this plural format.
PluralFormat(const PluralRules &rules, UErrorCode &status)
Creates a new PluralFormat for a given set of rules.
UnicodeString & format(int32_t number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Formats a plural message for a given number.
PluralFormat(const Locale &locale, UPluralType type, UErrorCode &status)
Creates a new PluralFormat for the plural type.
PluralFormat(const PluralRules &rules, const UnicodeString &pattern, UErrorCode &status)
Creates a new PluralFormat for a given set of rules, a pattern and a locale.
PluralFormat(UErrorCode &status)
Creates a new cardinal-number PluralFormat for the default locale.
UnicodeString & toPattern(UnicodeString &appendTo)
Returns the pattern from applyPattern() or constructor().
PluralFormat(const Locale &locale, const PluralRules &rules, const UnicodeString &pattern, UErrorCode &status)
Creates a new PluralFormat for a given set of rules, a pattern and a locale.
virtual PluralFormat * clone() const override
Clones this Format object polymorphically.
UnicodeString format(double number, UErrorCode &status) const
Formats a plural message for a given number.
void setLocale(const Locale &locale, UErrorCode &status)
Sets the locale used by this PluraFormat object.
UnicodeString format(int32_t number, UErrorCode &status) const
Formats a plural message for a given number.
PluralFormat(const PluralFormat &other)
copy constructor.
virtual bool operator==(const Format &other) const override
Return true if another object is semantically equal to this one.
PluralFormat(const Locale &locale, UErrorCode &status)
Creates a new cardinal-number PluralFormat for a given locale.
UnicodeString & format(double number, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Formats a plural message for a given number.
PluralFormat(const Locale &locale, const PluralRules &rules, UErrorCode &status)
Creates a new PluralFormat for a given set of rules.
static UClassID getStaticClassID(void)
ICU "poor man's RTTI", returns a UClassID for this class.
virtual ~PluralFormat()
Destructor.
Defines rules for mapping non-negative numeric values onto a small set of keywords.
Definition: plurrule.h:206
UMemory is the common ICU base class.
Definition: uobject.h:115
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
C++ API: MessagePattern class: Parses and represents ICU MessageFormat patterns.
C++ API: Compatibility APIs for number formatting.
C++ API: PluralRules object.
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:96
UPluralType
Type of plurals and PluralRules.
Definition: upluralrules.h:58
Basic definitions for ICU, for both C and C++ APIs.
#define NULL
Define NULL if necessary, to nullptr for C++ and to ((void *)0) for C.
Definition: utypes.h:188
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:301