ICU 66.0.1  66.0.1
listformatter.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 *
6 * Copyright (C) 2012-2016, International Business Machines
7 * Corporation and others. All Rights Reserved.
8 *
9 *******************************************************************************
10 * file name: listformatter.h
11 * encoding: UTF-8
12 * tab size: 8 (not used)
13 * indentation:4
14 *
15 * created on: 20120426
16 * created by: Umesh P. Nair
17 */
18 
19 #ifndef __LISTFORMATTER_H__
20 #define __LISTFORMATTER_H__
21 
22 #include "unicode/utypes.h"
23 
24 #if U_SHOW_CPLUSPLUS_API
25 
26 #include "unicode/unistr.h"
27 #include "unicode/locid.h"
28 #include "unicode/formattedvalue.h"
29 
30 U_NAMESPACE_BEGIN
31 
32 class FieldPositionIterator;
33 class FieldPositionHandler;
34 class FormattedListData;
35 class ListFormatter;
36 
38 class Hashtable;
39 
41 struct ListFormatInternal;
42 
43 /* The following can't be #ifndef U_HIDE_INTERNAL_API, needed for other .h file declarations */
48 struct ListFormatData : public UMemory {
49  UnicodeString twoPattern;
50  UnicodeString startPattern;
51  UnicodeString middlePattern;
52  UnicodeString endPattern;
53 
54  ListFormatData(const UnicodeString& two, const UnicodeString& start, const UnicodeString& middle, const UnicodeString& end) :
55  twoPattern(two), startPattern(start), middlePattern(middle), endPattern(end) {}
56 };
66 #if !UCONFIG_NO_FORMATTING
67 #ifndef U_HIDE_DRAFT_API
68 
85  public:
90  FormattedList() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
91 
97 
102  virtual ~FormattedList() U_OVERRIDE;
103 
105  FormattedList(const FormattedList&) = delete;
106 
108  FormattedList& operator=(const FormattedList&) = delete;
109 
114  FormattedList& operator=(FormattedList&& src) U_NOEXCEPT;
115 
117  UnicodeString toString(UErrorCode& status) const U_OVERRIDE;
118 
120  UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE;
121 
123  Appendable &appendTo(Appendable& appendable, UErrorCode& status) const U_OVERRIDE;
124 
126  UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE;
127 
128  private:
129  FormattedListData *fData;
130  UErrorCode fErrorCode;
131  explicit FormattedList(FormattedListData *results)
132  : fData(results), fErrorCode(U_ZERO_ERROR) {}
133  explicit FormattedList(UErrorCode errorCode)
134  : fData(nullptr), fErrorCode(errorCode) {}
135  friend class ListFormatter;
136 };
137 #endif /* U_HIDE_DRAFT_API */
138 #endif // !UCONFIG_NO_FORMATTING
139 
140 
152 
153  public:
154 
160 
165  ListFormatter& operator=(const ListFormatter& other);
166 
175  static ListFormatter* createInstance(UErrorCode& errorCode);
176 
186  static ListFormatter* createInstance(const Locale& locale, UErrorCode& errorCode);
187 
188 #ifndef U_HIDE_INTERNAL_API
189 
199  static ListFormatter* createInstance(const Locale& locale, const char* style, UErrorCode& errorCode);
200 #endif /* U_HIDE_INTERNAL_API */
201 
207  virtual ~ListFormatter();
208 
209 
220  UnicodeString& format(const UnicodeString items[], int32_t n_items,
221  UnicodeString& appendTo, UErrorCode& errorCode) const;
222 
223 #ifndef U_HIDE_DRAFT_API
224 
239  UnicodeString& format(const UnicodeString items[], int32_t n_items,
240  UnicodeString & appendTo, FieldPositionIterator* posIter,
241  UErrorCode& errorCode) const;
242 #endif // U_HIDE_DRAFT_API
243 
244 #if !UCONFIG_NO_FORMATTING
245 #ifndef U_HIDE_DRAFT_API
246 
257  FormattedList formatStringsToValue(
258  const UnicodeString items[],
259  int32_t n_items,
260  UErrorCode& errorCode) const;
261 #endif /* U_HIDE_DRAFT_API */
262 #endif // !UCONFIG_NO_FORMATTING
263 
264 #ifndef U_HIDE_INTERNAL_API
265 
268  UnicodeString& format(
269  const UnicodeString items[],
270  int32_t n_items,
271  UnicodeString& appendTo,
272  int32_t index,
273  int32_t &offset,
274  UErrorCode& errorCode) const;
278  ListFormatter(const ListFormatData &data, UErrorCode &errorCode);
282  ListFormatter(const ListFormatInternal* listFormatterInternal);
283 #endif /* U_HIDE_INTERNAL_API */
284 
285  private:
286  static void initializeHash(UErrorCode& errorCode);
287  static const ListFormatInternal* getListFormatInternal(const Locale& locale, const char *style, UErrorCode& errorCode);
288  struct ListPatternsSink;
289  static ListFormatInternal* loadListFormatInternal(const Locale& locale, const char* style, UErrorCode& errorCode);
290 
291  UnicodeString& format_(
292  const UnicodeString items[], int32_t n_items, UnicodeString& appendTo,
293  int32_t index, int32_t &offset, FieldPositionHandler* handler, UErrorCode& errorCode) const;
294 
295  ListFormatter();
296 
297  ListFormatInternal* owned;
298  const ListFormatInternal* data;
299 };
300 
301 U_NAMESPACE_END
302 
303 #endif /* U_SHOW_CPLUSPLUS_API */
304 
305 #endif // __LISTFORMATTER_H__
C++ API: Abstract operations for localized strings.
#define U_OVERRIDE
Defined to the C++11 "override" keyword if available.
Definition: umachine.h:129
FormattedList()
Default constructor; makes an empty FormattedList.
Definition: listformatter.h:90
C++ API: Unicode String.
No error, no warning.
Definition: utypes.h:449
An abstract formatted value: a string with associated field attributes.
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:301
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:58
An immutable class for formatting a list, using data from CLDR (or supplied separately).
Represents a span of a string containing a given field.
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:529
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
C++ API: Locale ID object.
Basic definitions for ICU, for both C and C++ APIs.
An immutable class containing the result of a list formatting operation.
Definition: listformatter.h:84
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:294
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
UMemory is the common ICU base class.
Definition: uobject.h:115
Requested operation can not be completed with ICU in its current state.
Definition: utypes.h:478
int8_t UBool
The ICU boolean type.
Definition: umachine.h:261
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:54
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195