ICU 78.1: i18n/unicode/listformatter.h Source File

ICU 78.1 78.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 #if !UCONFIG_NO_FORMATTING
27
28 #include "unicode/unistr.h"
29 #include "unicode/locid.h"
30 #include "unicode/formattedvalue.h"
31 #include "unicode/ulistformatter.h"
32
33 U_NAMESPACE_BEGIN
34
35 class FieldPositionHandler;
36 class FormattedListData;
37 class ListFormatter;
38
40 class Hashtable;
41
43 struct ListFormatInternal;
44
45 /* The following can't be #ifndef U_HIDE_INTERNAL_API, needed for other .h file declarations */
50 struct ListFormatData : public UMemory {
51 UnicodeString twoPattern;
52 UnicodeString startPattern;
53 UnicodeString middlePattern;
54 UnicodeString endPattern;
55 Locale locale;
56
57 ListFormatData(const UnicodeString& two, const UnicodeString& start, const UnicodeString& middle, const UnicodeString& end,
58 const Locale& loc) :
59 twoPattern(two), startPattern(start), middlePattern(middle), endPattern(end), locale(loc) {}
60 };
86 class U_I18N_API FormattedList : public UMemory, public FormattedValue {
87 public:
92 FormattedList() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
93
98 FormattedList(FormattedList&& src) noexcept;
99
104 virtual ~FormattedList() override;
105
107 FormattedList(const FormattedList&) = delete;
108
110 FormattedList& operator=(const FormattedList&) = delete;
111
116 FormattedList& operator=(FormattedList&& src) noexcept;
117
119 UnicodeString toString(UErrorCode& status) const override;
120
122 UnicodeString toTempString(UErrorCode& status) const override;
123
125 Appendable &appendTo(Appendable& appendable, UErrorCode& status) const override;
126
128 UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const override;
129
130 private:
131 FormattedListData *fData;
132 UErrorCode fErrorCode;
133 explicit FormattedList(FormattedListData *results)
134 : fData(results), fErrorCode(U_ZERO_ERROR) {}
135 explicit FormattedList(UErrorCode errorCode)
136 : fData(nullptr), fErrorCode(errorCode) {}
137 friend class ListFormatter;
138 };
139
140
151 class U_I18N_API ListFormatter : public UObject{
152
153 public:
154
159 ListFormatter(const ListFormatter&);
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
198 static ListFormatter* createInstance(
199 const Locale& locale, UListFormatterType type, UListFormatterWidth width, UErrorCode& errorCode);
200
206 virtual ~ListFormatter();
207
208
219 UnicodeString& format(const UnicodeString items[], int32_t n_items,
220 UnicodeString& appendTo, UErrorCode& errorCode) const;
221
233 FormattedList formatStringsToValue(
234 const UnicodeString items[],
235 int32_t n_items,
236 UErrorCode& errorCode) const;
237
238 #ifndef U_HIDE_INTERNAL_API
242 UnicodeString& format(
243 const UnicodeString items[],
244 int32_t n_items,
245 UnicodeString& appendTo,
246 int32_t index,
247 int32_t &offset,
248 UErrorCode& errorCode) const;
252 ListFormatter(const ListFormatData &data, UErrorCode &errorCode);
256 ListFormatter(const ListFormatInternal* listFormatterInternal);
257 #endif /* U_HIDE_INTERNAL_API */
258
259 private:
260
267 static ListFormatter* createInstance(const Locale& locale, const char* style, UErrorCode& errorCode);
268
269 static void initializeHash(UErrorCode& errorCode);
270 static const ListFormatInternal* getListFormatInternal(const Locale& locale, const char *style, UErrorCode& errorCode);
271 struct U_HIDDEN ListPatternsSink;
272 static ListFormatInternal* loadListFormatInternal(const Locale& locale, const char* style, UErrorCode& errorCode);
273
274 ListFormatter() = delete;
275
276 ListFormatInternal* owned;
277 const ListFormatInternal* data;
278 };
279
280 U_NAMESPACE_END
281
282 #endif /* #if !UCONFIG_NO_FORMATTING */
283
284 #endif /* U_SHOW_CPLUSPLUS_API */
285
286 #endif // __LISTFORMATTER_H__
icu::Appendable
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:54
icu::ConstrainedFieldPosition
Represents a span of a string containing a given field.
Definition: formattedvalue.h:41
icu::FormattedList
An immutable class containing the result of a list formatting operation.
Definition: listformatter.h:86
icu::FormattedList::FormattedList
FormattedList(const FormattedList &)=delete
Copying not supported; use move constructor instead.
icu::FormattedList::FormattedList
FormattedList(FormattedList &&src) noexcept
Move constructor: Leaves the source FormattedList in an undefined state.
icu::FormattedList::toTempString
UnicodeString toTempString(UErrorCode &status) const override
Returns the formatted string as a read-only alias to memory owned by the FormattedValue.
icu::FormattedList::toString
UnicodeString toString(UErrorCode &status) const override
Returns the formatted string as a self-contained UnicodeString.
icu::FormattedList::FormattedList
FormattedList()
Default constructor; makes an empty FormattedList.
Definition: listformatter.h:92
icu::FormattedList::nextPosition
UBool nextPosition(ConstrainedFieldPosition &cfpos, UErrorCode &status) const override
Iterates over field positions in the FormattedValue.
icu::FormattedList::operator=
FormattedList & operator=(const FormattedList &)=delete
Copying not supported; use move assignment instead.
icu::FormattedList::appendTo
Appendable & appendTo(Appendable &appendable, UErrorCode &status) const override
Appends the formatted string to an Appendable.
icu::FormattedList::~FormattedList
virtual ~FormattedList() override
Destruct an instance of FormattedList.
icu::FormattedList::operator=
FormattedList & operator=(FormattedList &&src) noexcept
Move assignment: Leaves the source FormattedList in an undefined state.
icu::FormattedValue
An abstract formatted value: a string with associated field attributes.
Definition: formattedvalue.h:241
icu::ListFormatter
An immutable class for formatting a list, using data from CLDR (or supplied separately).
Definition: listformatter.h:151
icu::ListFormatter::createInstance
static ListFormatter * createInstance(UErrorCode &errorCode)
Creates a ListFormatter appropriate for the default locale.
icu::ListFormatter::createInstance
static ListFormatter * createInstance(const Locale &locale, UErrorCode &errorCode)
Creates a ListFormatter appropriate for a locale.
icu::ListFormatter::ListFormatter
ListFormatter(const ListFormatInternal *listFormatterInternal)
icu::ListFormatter::~ListFormatter
virtual ~ListFormatter()
Destructor.
icu::ListFormatter::createInstance
static ListFormatter * createInstance(const Locale &locale, UListFormatterType type, UListFormatterWidth width, UErrorCode &errorCode)
Creates a ListFormatter for the given locale, list type, and style.
icu::ListFormatter::formatStringsToValue
FormattedList formatStringsToValue(const UnicodeString items[], int32_t n_items, UErrorCode &errorCode) const
Formats a list of strings to a FormattedList, which exposes field position information.
icu::ListFormatter::ListFormatter
ListFormatter(const ListFormatter &)
Copy constructor.
icu::ListFormatter::ListFormatter
ListFormatter(const ListFormatData &data, UErrorCode &errorCode)
icu::ListFormatter::operator=
ListFormatter & operator=(const ListFormatter &other)
Assignment operator.
icu::ListFormatter::format
UnicodeString & format(const UnicodeString items[], int32_t n_items, UnicodeString &appendTo, int32_t index, int32_t &offset, UErrorCode &errorCode) const
icu::ListFormatter::format
UnicodeString & format(const UnicodeString items[], int32_t n_items, UnicodeString &appendTo, UErrorCode &errorCode) const
Formats a list of strings.
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:198
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:115
icu::UObject
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:222
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:303
formattedvalue.h
C++ API: Abstract operations for localized strings.
locid.h
C++ API: Locale ID object.
U_HIDDEN
#define U_HIDDEN
This is used to mark internal structs declared within external classes, to prevent the internal struc...
Definition: platform.h:814
ulistformatter.h
C API: Format a list in a locale-appropriate way.
UListFormatterWidth
UListFormatterWidth
Verbosity level of the list patterns.
Definition: ulistformatter.h:99
UListFormatterType
UListFormatterType
Type of meaning expressed by the list.
Definition: ulistformatter.h:70
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
unistr.h
C++ API: Unicode String.
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:509
U_INVALID_STATE_ERROR
@ U_INVALID_STATE_ERROR
Requested operation can not be completed with ICU in its current state.
Definition: utypes.h:573
U_ZERO_ERROR
@ U_ZERO_ERROR
No error, no warning.
Definition: utypes.h:544
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:316