ICU 4.8.1.1  4.8.1.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
msgfmt.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2007-2011, International Business Machines Corporation and
3 * others. All Rights Reserved.
4 ********************************************************************************
5 *
6 * File MSGFMT.H
7 *
8 * Modification History:
9 *
10 * Date Name Description
11 * 02/19/97 aliu Converted from java.
12 * 03/20/97 helena Finished first cut of implementation.
13 * 07/22/98 stephen Removed operator!= (defined in Format)
14 * 08/19/2002 srl Removing Javaisms
15 *******************************************************************************/
16 
17 #ifndef MSGFMT_H
18 #define MSGFMT_H
19 
20 #include "unicode/utypes.h"
21 
27 #if !UCONFIG_NO_FORMATTING
28 
29 #include "unicode/format.h"
30 #include "unicode/locid.h"
31 #include "unicode/messagepattern.h"
32 #include "unicode/parseerr.h"
33 #include "unicode/plurfmt.h"
34 #include "unicode/plurrule.h"
35 
37 // Forward declaration.
38 struct UHashtable;
39 typedef struct UHashtable UHashtable;
41 
43 
44 class AppendableWrapper;
45 class DateFormat;
46 class NumberFormat;
47 
319 public:
331  kMaxFormat = 10
332  };
333 
343  MessageFormat(const UnicodeString& pattern,
344  UErrorCode &status);
345 
354  MessageFormat(const UnicodeString& pattern,
355  const Locale& newLocale,
356  UErrorCode& status);
367  MessageFormat(const UnicodeString& pattern,
368  const Locale& newLocale,
369  UParseError& parseError,
370  UErrorCode& status);
376 
381  const MessageFormat& operator=(const MessageFormat&);
382 
387  virtual ~MessageFormat();
388 
394  virtual Format* clone(void) const;
395 
403  virtual UBool operator==(const Format& other) const;
404 
410  virtual void setLocale(const Locale& theLocale);
411 
418  virtual const Locale& getLocale(void) const;
419 
428  virtual void applyPattern(const UnicodeString& pattern,
429  UErrorCode& status);
440  virtual void applyPattern(const UnicodeString& pattern,
441  UParseError& parseError,
442  UErrorCode& status);
443 
462  virtual void applyPattern(const UnicodeString& pattern,
464  UParseError* parseError,
465  UErrorCode& status);
466 
472  return msgPattern.getApostropheMode();
473  }
474 
483  virtual UnicodeString& toPattern(UnicodeString& appendTo) const;
484 
501  virtual void adoptFormats(Format** formatsToAdopt, int32_t count);
502 
517  virtual void setFormats(const Format** newFormats, int32_t cnt);
518 
519 
534  virtual void adoptFormat(int32_t formatNumber, Format* formatToAdopt);
535 
545  virtual void setFormat(int32_t formatNumber, const Format& format);
546 
555  virtual StringEnumeration* getFormatNames(UErrorCode& status);
556 
570  virtual Format* getFormat(const UnicodeString& formatName, UErrorCode& status);
571 
585  virtual void setFormat(const UnicodeString& formatName, const Format& format, UErrorCode& status);
586 
601  virtual void adoptFormat(const UnicodeString& formatName, Format* formatToAdopt, UErrorCode& status);
602 
615  virtual const Format** getFormats(int32_t& count) const;
616 
617 
618  using Format::format;
619 
637  UnicodeString& format(const Formattable* source,
638  int32_t count,
639  UnicodeString& appendTo,
640  FieldPosition& ignore,
641  UErrorCode& status) const;
642 
660  static UnicodeString& format(const UnicodeString& pattern,
661  const Formattable* arguments,
662  int32_t count,
663  UnicodeString& appendTo,
664  UErrorCode& status);
665 
686  virtual UnicodeString& format(const Formattable& obj,
687  UnicodeString& appendTo,
688  FieldPosition& pos,
689  UErrorCode& status) const;
690 
705  UnicodeString& format(const Formattable& obj,
706  UnicodeString& appendTo,
707  UErrorCode& status) const;
708 
709 
728  UnicodeString& format(const UnicodeString* argumentNames,
729  const Formattable* arguments,
730  int32_t count,
731  UnicodeString& appendTo,
732  UErrorCode& status) const;
746  virtual Formattable* parse(const UnicodeString& source,
747  ParsePosition& pos,
748  int32_t& count) const;
749 
765  virtual Formattable* parse(const UnicodeString& source,
766  int32_t& count,
767  UErrorCode& status) const;
768 
781  virtual void parseObject(const UnicodeString& source,
782  Formattable& result,
783  ParsePosition& pos) const;
784 
804  static UnicodeString autoQuoteApostrophe(const UnicodeString& pattern,
805  UErrorCode& status);
806 
807 
815  UBool usesNamedArguments() const;
816 
817 
828  int32_t getArgTypeCount() const;
829 
841  virtual UClassID getDynamicClassID(void) const;
842 
854  static UClassID U_EXPORT2 getStaticClassID(void);
855 
866  static UBool equalFormats(const void* left, const void* right);
867 
868 private:
869 
870  Locale fLocale;
871  MessagePattern msgPattern;
872  Format** formatAliases; // see getFormats
873  int32_t formatAliasesCapacity;
874 
875  MessageFormat(); // default constructor not implemented
876 
883  class U_I18N_API PluralSelectorProvider : public PluralFormat::PluralSelector {
884  public:
885  PluralSelectorProvider(const Locale* loc);
886  virtual ~PluralSelectorProvider();
887  virtual UnicodeString select(double number, UErrorCode& ec) const;
888 
889  void reset(const Locale* loc);
890  private:
891  const Locale* locale;
892  PluralRules* rules;
893  };
894 
903  Formattable::Type* argTypes;
904  int32_t argTypeCount;
905  int32_t argTypeCapacity;
906 
912  UBool hasArgTypeConflicts;
913 
914  // Variable-size array management
915  UBool allocateArgTypes(int32_t capacity, UErrorCode& status);
916 
924  NumberFormat* defaultNumberFormat;
925  DateFormat* defaultDateFormat;
926 
927  UHashtable* cachedFormatters;
928  UHashtable* customFormatArgStarts;
929 
930  PluralSelectorProvider pluralProvider;
931 
936  const NumberFormat* getDefaultNumberFormat(UErrorCode&) const;
937  const DateFormat* getDefaultDateFormat(UErrorCode&) const;
938 
945  static int32_t findKeyword( const UnicodeString& s,
946  const UChar * const *list);
947 
953  UnicodeString& format(const Formattable* arguments,
954  const UnicodeString *argumentNames,
955  int32_t cnt,
956  UnicodeString& appendTo,
957  FieldPosition* pos,
958  UErrorCode& status) const;
959 
977  void format(int32_t msgStart,
978  double pluralNumber,
979  const Formattable* arguments,
980  const UnicodeString *argumentNames,
981  int32_t cnt,
982  AppendableWrapper& appendTo,
983  FieldPosition* pos,
984  UErrorCode& success) const;
985 
986  UnicodeString getArgName(int32_t partIndex);
987 
988  void setArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status);
989 
990  void setCustomArgStartFormat(int32_t argStart, Format* formatter, UErrorCode& status);
991 
992  int32_t nextTopLevelArgStart(int32_t partIndex) const;
993 
994  UBool argNameMatches(int32_t partIndex, const UnicodeString& argName, int32_t argNumber);
995 
996  void cacheExplicitFormats(UErrorCode& status);
997 
998  Format* createAppropriateFormat(UnicodeString& type,
999  UnicodeString& style,
1000  Formattable::Type& formattableType,
1001  UParseError& parseError,
1002  UErrorCode& ec);
1003 
1004  const Formattable* getArgFromListByName(const Formattable* arguments,
1005  const UnicodeString *argumentNames,
1006  int32_t cnt, UnicodeString& name) const;
1007 
1008  Formattable* parse(int32_t msgStart,
1009  const UnicodeString& source,
1010  ParsePosition& pos,
1011  int32_t& count,
1012  UErrorCode& ec) const;
1013 
1014  FieldPosition* updateMetaData(AppendableWrapper& dest, int32_t prevLength,
1015  FieldPosition* fp, const Formattable* argId) const;
1016 
1017  Format* getCachedFormatter(int32_t argumentNumber) const;
1018 
1019  UnicodeString getLiteralStringUntilNextArgument(int32_t from) const;
1020 
1021  void copyObjects(const MessageFormat& that, UErrorCode& ec);
1022 
1023  void formatComplexSubMessage(int32_t msgStart,
1024  double pluralNumber,
1025  const Formattable* arguments,
1026  const UnicodeString *argumentNames,
1027  int32_t cnt,
1028  AppendableWrapper& appendTo,
1029  UErrorCode& success) const;
1030 
1034  NumberFormat* createIntegerFormat(const Locale& locale, UErrorCode& status) const;
1035 
1044  const Formattable::Type* getArgTypeList(int32_t& listCount) const {
1045  listCount = argTypeCount;
1046  return argTypes;
1047  }
1048 
1052  void resetPattern();
1053 
1059  class U_I18N_API DummyFormat : public Format {
1060  public:
1061  virtual UBool operator==(const Format&) const;
1062  virtual Format* clone() const;
1063  virtual UnicodeString& format(const Formattable&,
1064  UnicodeString& appendTo,
1065  FieldPosition&,
1066  UErrorCode& status) const;
1067  virtual void parseObject(const UnicodeString&,
1068  Formattable&,
1069  ParsePosition&) const;
1070  virtual UClassID getDynamicClassID() const;
1071  };
1072 
1073  friend class MessageFormatAdapter; // getFormatTypeList() access
1074 };
1075 
1076 inline UnicodeString&
1078  UnicodeString& appendTo,
1079  UErrorCode& status) const {
1080  return Format::format(obj, appendTo, status);
1081 }
1082 
1083 
1085 
1086 #endif /* #if !UCONFIG_NO_FORMATTING */
1087 
1088 #endif // _MSGFMT
1089 //eof
virtual void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const =0
Parse a string to produce an object.
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
C++ API: PluralFormat object.
C++ API: PluralRules object.
DateFormat is an abstract class for a family of classes that convert dates and times from their inter...
Definition: datefmt.h:138
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
C++ API: Base class for all formats.
#define U_CDECL_BEGIN
This is used to begin a declaration of a library private ICU C API.
Definition: umachine.h:110
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:521
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:131
Abstract base class for all number formats.
Definition: numfmt.h:165
Type
Selector for flavor of data type contained within a Formattable object.
Definition: fmtable.h:209
EFormatNumber
Enum type for kMaxFormat.
Definition: msgfmt.h:325
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:47
UMessagePatternApostropheMode getApostropheMode() const
Definition: msgfmt.h:471
virtual UBool operator==(const Format &other) const =0
Return true if the given Format objects are semantically equal.
UnicodeString & format(const Formattable *source, int32_t count, UnicodeString &appendTo, FieldPosition &ignore, UErrorCode &status) const
Formats the given array of arguments into a user-readable string.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:188
#define U_EXPORT2
Definition: platform.h:314
virtual Format * clone() const =0
Clone this object polymorphically.
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:106
uint16_t UChar
Define UChar to be wchar_t if that is 16 bits wide; always assumed to be unsigned.
Definition: umachine.h:325
#define U_CDECL_END
This is used to end a declaration of a library private ICU C API.
Definition: umachine.h:111
Parses and represents ICU MessageFormat patterns.
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:132
C API: Parse Error Information.
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:52
void * UClassID
UClassID is used to identify classes without using RTTI, since RTTI is not yet supported by all C++ c...
Definition: utypes.h:385
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:639
Base class for 'pure' C++ implementations of uenum api.
Definition: strenum.h:55
Defines rules for mapping non-negative numeric values onto a small set of keywords.
Definition: plurrule.h:126
C++ API: Locale ID object.
A UParseError struct is used to returned detailed information about parsing errors.
Definition: parseerr.h:56
Base class for all formats.
Definition: format.h:94
Basic definitions for ICU, for both C and C++ APIs.
Locale getLocale(ULocDataLocaleType type, UErrorCode &status) const
Get the locale for this format object.
C++ API: MessagePattern class: Parses and represents ICU MessageFormat patterns.
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:181
virtual UClassID getDynamicClassID() const =0
ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
UMessagePatternApostropheMode
Mode for when an apostrophe starts quoted literal text for MessageFormat output.
int8_t UBool
The ICU boolean type.
Definition: umachine.h:228
Format & operator=(const Format &)