ICU 73.2  73.2
listformatter.h
Go to the documentation of this file.
00001 // © 2016 and later: Unicode, Inc. and others.
00002 // License & terms of use: http://www.unicode.org/copyright.html
00003 /*
00004 *******************************************************************************
00005 *
00006 *   Copyright (C) 2012-2016, International Business Machines
00007 *   Corporation and others.  All Rights Reserved.
00008 *
00009 *******************************************************************************
00010 *   file name:  listformatter.h
00011 *   encoding:   UTF-8
00012 *   tab size:   8 (not used)
00013 *   indentation:4
00014 *
00015 *   created on: 20120426
00016 *   created by: Umesh P. Nair
00017 */
00018 
00019 #ifndef __LISTFORMATTER_H__
00020 #define __LISTFORMATTER_H__
00021 
00022 #include "unicode/utypes.h"
00023 
00024 #if U_SHOW_CPLUSPLUS_API
00025 
00026 #if !UCONFIG_NO_FORMATTING
00027 
00028 #include "unicode/unistr.h"
00029 #include "unicode/locid.h"
00030 #include "unicode/formattedvalue.h"
00031 #include "unicode/ulistformatter.h"
00032 
00033 U_NAMESPACE_BEGIN
00034 
00035 class FieldPositionHandler;
00036 class FormattedListData;
00037 class ListFormatter;
00038 
00040 class Hashtable;
00041 
00043 struct ListFormatInternal;
00044 
00045 /* The following can't be #ifndef U_HIDE_INTERNAL_API, needed for other .h file declarations */
00050 struct ListFormatData : public UMemory {
00051     UnicodeString twoPattern;
00052     UnicodeString startPattern;
00053     UnicodeString middlePattern;
00054     UnicodeString endPattern;
00055     Locale locale;
00056 
00057   ListFormatData(const UnicodeString& two, const UnicodeString& start, const UnicodeString& middle, const UnicodeString& end,
00058                  const Locale& loc) :
00059       twoPattern(two), startPattern(start), middlePattern(middle), endPattern(end), locale(loc) {}
00060 };
00086 class U_I18N_API FormattedList : public UMemory, public FormattedValue {
00087   public:
00092     FormattedList() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
00093 
00098     FormattedList(FormattedList&& src) noexcept;
00099 
00104     virtual ~FormattedList() override;
00105 
00107     FormattedList(const FormattedList&) = delete;
00108 
00110     FormattedList& operator=(const FormattedList&) = delete;
00111 
00116     FormattedList& operator=(FormattedList&& src) noexcept;
00117 
00119     UnicodeString toString(UErrorCode& status) const override;
00120 
00122     UnicodeString toTempString(UErrorCode& status) const override;
00123 
00125     Appendable &appendTo(Appendable& appendable, UErrorCode& status) const override;
00126 
00128     UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const override;
00129 
00130   private:
00131     FormattedListData *fData;
00132     UErrorCode fErrorCode;
00133     explicit FormattedList(FormattedListData *results)
00134         : fData(results), fErrorCode(U_ZERO_ERROR) {}
00135     explicit FormattedList(UErrorCode errorCode)
00136         : fData(nullptr), fErrorCode(errorCode) {}
00137     friend class ListFormatter;
00138 };
00139 
00140 
00151 class U_I18N_API ListFormatter : public UObject{
00152 
00153   public:
00154 
00159     ListFormatter(const ListFormatter&);
00160 
00165     ListFormatter& operator=(const ListFormatter& other);
00166 
00175     static ListFormatter* createInstance(UErrorCode& errorCode);
00176 
00186     static ListFormatter* createInstance(const Locale& locale, UErrorCode& errorCode);
00187 
00198     static ListFormatter* createInstance(
00199       const Locale& locale, UListFormatterType type, UListFormatterWidth width, UErrorCode& errorCode);
00200 
00206     virtual ~ListFormatter();
00207 
00208 
00219     UnicodeString& format(const UnicodeString items[], int32_t n_items,
00220         UnicodeString& appendTo, UErrorCode& errorCode) const;
00221 
00233     FormattedList formatStringsToValue(
00234         const UnicodeString items[],
00235         int32_t n_items,
00236         UErrorCode& errorCode) const;
00237 
00238 #ifndef U_HIDE_INTERNAL_API
00239 
00242     UnicodeString& format(
00243             const UnicodeString items[],
00244             int32_t n_items,
00245             UnicodeString& appendTo,
00246             int32_t index,
00247             int32_t &offset,
00248             UErrorCode& errorCode) const;
00252     ListFormatter(const ListFormatData &data, UErrorCode &errorCode);
00256     ListFormatter(const ListFormatInternal* listFormatterInternal);
00257 #endif  /* U_HIDE_INTERNAL_API */
00258 
00259   private:
00260   
00267     static ListFormatter* createInstance(const Locale& locale, const char* style, UErrorCode& errorCode);
00268 
00269     static void initializeHash(UErrorCode& errorCode);
00270     static const ListFormatInternal* getListFormatInternal(const Locale& locale, const char *style, UErrorCode& errorCode);
00271     struct U_HIDDEN ListPatternsSink;
00272     static ListFormatInternal* loadListFormatInternal(const Locale& locale, const char* style, UErrorCode& errorCode);
00273 
00274     ListFormatter() = delete;
00275 
00276     ListFormatInternal* owned;
00277     const ListFormatInternal* data;
00278 };
00279 
00280 U_NAMESPACE_END
00281 
00282 #endif /* #if !UCONFIG_NO_FORMATTING */
00283 
00284 #endif /* U_SHOW_CPLUSPLUS_API */
00285 
00286 #endif // __LISTFORMATTER_H__