ICU 57.1  57.1
plurfmt.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 * Copyright (C) 2007-2014, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
6 *
7 
8 * File PLURFMT.H
9 ********************************************************************************
10 */
11 
12 #ifndef PLURFMT
13 #define PLURFMT
14 
15 #include "unicode/utypes.h"
16 
22 #if !UCONFIG_NO_FORMATTING
23 
24 #include "unicode/messagepattern.h"
25 #include "unicode/numfmt.h"
26 #include "unicode/plurrule.h"
27 
29 
30 class Hashtable;
31 class NFRule;
32 
145 public:
146 
155  PluralFormat(UErrorCode& status);
156 
166  PluralFormat(const Locale& locale, UErrorCode& status);
167 
177  PluralFormat(const PluralRules& rules, UErrorCode& status);
178 
195  PluralFormat(const Locale& locale, const PluralRules& rules, UErrorCode& status);
196 
207  PluralFormat(const Locale& locale, UPluralType type, UErrorCode& status);
208 
219  PluralFormat(const UnicodeString& pattern, UErrorCode& status);
220 
235  PluralFormat(const Locale& locale, const UnicodeString& pattern, UErrorCode& status);
236 
248  PluralFormat(const PluralRules& rules,
249  const UnicodeString& pattern,
250  UErrorCode& status);
251 
266  PluralFormat(const Locale& locale,
267  const PluralRules& rules,
268  const UnicodeString& pattern,
269  UErrorCode& status);
270 
284  PluralFormat(const Locale& locale,
285  UPluralType type,
286  const UnicodeString& pattern,
287  UErrorCode& status);
288 
293  PluralFormat(const PluralFormat& other);
294 
299  virtual ~PluralFormat();
300 
313  void applyPattern(const UnicodeString& pattern, UErrorCode& status);
314 
315 
316  using Format::format;
317 
330  UnicodeString format(int32_t number, UErrorCode& status) const;
331 
344  UnicodeString format(double number, UErrorCode& status) const;
345 
362  UnicodeString& format(int32_t number,
363  UnicodeString& appendTo,
364  FieldPosition& pos,
365  UErrorCode& status) const;
366 
383  UnicodeString& format(double number,
384  UnicodeString& appendTo,
385  FieldPosition& pos,
386  UErrorCode& status) const;
387 
388 #ifndef U_HIDE_DEPRECATED_API
389 
404  void setLocale(const Locale& locale, UErrorCode& status);
405 #endif /* U_HIDE_DEPRECATED_API */
406 
416  void setNumberFormat(const NumberFormat* format, UErrorCode& status);
417 
424  PluralFormat& operator=(const PluralFormat& other);
425 
433  virtual UBool operator==(const Format& other) const;
434 
442  virtual UBool operator!=(const Format& other) const;
443 
449  virtual Format* clone(void) const;
450 
465  UnicodeString& format(const Formattable& obj,
466  UnicodeString& appendTo,
467  FieldPosition& pos,
468  UErrorCode& status) const;
469 
478  UnicodeString& toPattern(UnicodeString& appendTo);
479 
502  virtual void parseObject(const UnicodeString& source,
503  Formattable& result,
504  ParsePosition& parse_pos) const;
505 
512  static UClassID U_EXPORT2 getStaticClassID(void);
513 
519  virtual UClassID getDynamicClassID() const;
520 
521 #if (defined(__xlC__) && (__xlC__ < 0x0C00)) || (U_PLATFORM == U_PF_OS390) || (U_PLATFORM ==U_PF_OS400)
522 // Work around a compiler bug on xlC 11.1 on AIX 7.1 that would
523 // prevent PluralSelectorAdapter from implementing private PluralSelector.
524 // xlC error message:
525 // 1540-0300 (S) The "private" member "class icu_49::PluralFormat::PluralSelector" cannot be accessed.
526 public:
527 #else
528 private:
529 #endif
530 
534  public:
535  virtual ~PluralSelector();
545  virtual UnicodeString select(void *context, double number, UErrorCode& ec) const = 0;
546  };
547 
552  public:
553  PluralSelectorAdapter() : pluralRules(NULL) {
554  }
555 
556  virtual ~PluralSelectorAdapter();
557 
558  virtual UnicodeString select(void *context, double number, UErrorCode& /*ec*/) const;
560  void reset();
561 
562  PluralRules* pluralRules;
563  };
564 
565 #if defined(__xlC__)
566 // End of xlC bug workaround, keep remaining definitions private.
567 private:
568 #endif
569  Locale locale;
570  MessagePattern msgPattern;
571  NumberFormat* numberFormat;
572  double offset;
573  PluralSelectorAdapter pluralRulesWrapper;
574 
575  PluralFormat(); // default constructor not implemented
576  void init(const PluralRules* rules, UPluralType type, UErrorCode& status);
581  void copyObjects(const PluralFormat& other);
582 
583  UnicodeString& format(const Formattable& numberObject, double number,
584  UnicodeString& appendTo,
585  FieldPosition& pos,
586  UErrorCode& status) const;
599  static int32_t findSubMessage(
600  const MessagePattern& pattern, int32_t partIndex,
601  const PluralSelector& selector, void *context, double number, UErrorCode& ec);
603  void parseType(const UnicodeString& source, const NFRule *rbnfLenientScanner,
604  Formattable& result, FieldPosition& pos) const;
605 
606  friend class MessageFormat;
607  friend class NFRule;
608 };
609 
611 
612 #endif /* #if !UCONFIG_NO_FORMATTING */
613 
614 #endif // _PLURFMT
615 //eof
Base class for all formats.
Definition: format.h:94
void copyObjects(const PluralFormat &other)
Copies dynamically allocated values (pointer fields).
Abstract base class for all number formats.
Definition: numfmt.h:167
virtual UClassID getDynamicClassID() const
ICU4C "poor man&#39;s RTTI", returns a UClassID for the actual ICU class.
void * UClassID
UClassID is used to identify classes without using the compiler&#39;s RTTI.
Definition: uobject.h:91
virtual Format * clone() const =0
Clone this object polymorphically.
UnicodeString format(int32_t number, UErrorCode &status) const
Formats a plural message for a given number.
C++ API: PluralRules object.
Defines rules for mapping non-negative numeric values onto a small set of keywords.
Definition: plurrule.h:192
Parses and represents ICU MessageFormat patterns.
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:358
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:129
#define NULL
Define NULL if necessary, to 0 for C++ and to ((void *)0) for C.
Definition: utypes.h:186
virtual UnicodeString select(void *context, double number, UErrorCode &ec) const =0
Given a number, returns the appropriate PluralFormat keyword.
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:130
static int32_t findSubMessage(const MessagePattern &pattern, int32_t partIndex, const PluralSelector &selector, void *context, double number, UErrorCode &ec)
Finds the PluralFormat sub-message for the given number, or the "other" sub-message.
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:476
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:106
UPluralType
Type of plurals and PluralRules.
Definition: upluralrules.h:44
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:47
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 &)
UBool operator!=(const Format &other) const
Return true if the given Format objects are not semantically equal.
Definition: format.h:119
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:293
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:66
C++ API: MessagePattern class: Parses and represents ICU MessageFormat patterns.
C++ API: Abstract base class for all number formats.
UMemory is the common ICU base class.
Definition: uobject.h:110
int8_t UBool
The ICU boolean type.
Definition: umachine.h:234
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:185