00001
00002
00003
00004 #ifndef __NUMBERRANGEFORMATTER_H__
00005 #define __NUMBERRANGEFORMATTER_H__
00006
00007 #include "unicode/utypes.h"
00008
00009 #if U_SHOW_CPLUSPLUS_API
00010
00011 #if !UCONFIG_NO_FORMATTING
00012
00013 #include <atomic>
00014 #include "unicode/appendable.h"
00015 #include "unicode/fieldpos.h"
00016 #include "unicode/formattedvalue.h"
00017 #include "unicode/fpositer.h"
00018 #include "unicode/numberformatter.h"
00019
00052 typedef enum UNumberRangeCollapse {
00061 UNUM_RANGE_COLLAPSE_AUTO,
00062
00068 UNUM_RANGE_COLLAPSE_NONE,
00069
00076 UNUM_RANGE_COLLAPSE_UNIT,
00077
00084 UNUM_RANGE_COLLAPSE_ALL
00085 } UNumberRangeCollapse;
00086
00094 typedef enum UNumberRangeIdentityFallback {
00100 UNUM_IDENTITY_FALLBACK_SINGLE_VALUE,
00101
00108 UNUM_IDENTITY_FALLBACK_APPROXIMATELY_OR_SINGLE_VALUE,
00109
00116 UNUM_IDENTITY_FALLBACK_APPROXIMATELY,
00117
00124 UNUM_IDENTITY_FALLBACK_RANGE
00125 } UNumberRangeIdentityFallback;
00126
00134 typedef enum UNumberRangeIdentityResult {
00141 UNUM_IDENTITY_RESULT_EQUAL_BEFORE_ROUNDING,
00142
00149 UNUM_IDENTITY_RESULT_EQUAL_AFTER_ROUNDING,
00150
00157 UNUM_IDENTITY_RESULT_NOT_EQUAL,
00158
00159 #ifndef U_HIDE_INTERNAL_API
00160
00164 UNUM_IDENTITY_RESULT_COUNT
00165 #endif
00166
00167 } UNumberRangeIdentityResult;
00168
00169 U_NAMESPACE_BEGIN
00170
00171 namespace number {
00172
00173
00174 class UnlocalizedNumberRangeFormatter;
00175 class LocalizedNumberRangeFormatter;
00176 class FormattedNumberRange;
00177
00178 namespace impl {
00179
00180
00181 struct RangeMacroProps;
00182 class DecimalQuantity;
00183 class UFormattedNumberRangeData;
00184 class NumberRangeFormatterImpl;
00185
00186 }
00187
00193 #if U_PLATFORM == U_PF_WINDOWS && !defined(U_IN_DOXYGEN)
00194 }
00195 U_NAMESPACE_END
00196
00197 template struct U_I18N_API std::atomic< U_NAMESPACE_QUALIFIER number::impl::NumberRangeFormatterImpl*>;
00198
00199 U_NAMESPACE_BEGIN
00200 namespace number {
00201 #endif
00202
00204
00205
00206 namespace impl {
00207
00208
00210 struct U_I18N_API RangeMacroProps : public UMemory {
00212 UnlocalizedNumberFormatter formatter1;
00213
00215 UnlocalizedNumberFormatter formatter2;
00216
00218 bool singleFormatter = true;
00219
00221 UNumberRangeCollapse collapse = UNUM_RANGE_COLLAPSE_AUTO;
00222
00224 UNumberRangeIdentityFallback identityFallback = UNUM_IDENTITY_FALLBACK_APPROXIMATELY;
00225
00227 Locale locale;
00228
00229
00230
00235 bool copyErrorTo(UErrorCode &status) const {
00236 return formatter1.copyErrorTo(status) || formatter2.copyErrorTo(status);
00237 }
00238 };
00239
00240 }
00241
00247 template<typename Derived>
00248 class U_I18N_API NumberRangeFormatterSettings {
00249 public:
00262 Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) const &;
00263
00273 Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) &&;
00274
00284 Derived numberFormatterBoth(UnlocalizedNumberFormatter &&formatter) const &;
00285
00295 Derived numberFormatterBoth(UnlocalizedNumberFormatter &&formatter) &&;
00296
00308 Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) const &;
00309
00319 Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) &&;
00320
00330 Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) const &;
00331
00341 Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) &&;
00342
00354 Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) const &;
00355
00365 Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) &&;
00366
00376 Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) const &;
00377
00387 Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) &&;
00388
00406 Derived collapse(UNumberRangeCollapse collapse) const &;
00407
00417 Derived collapse(UNumberRangeCollapse collapse) &&;
00418
00439 Derived identityFallback(UNumberRangeIdentityFallback identityFallback) const &;
00440
00450 Derived identityFallback(UNumberRangeIdentityFallback identityFallback) &&;
00451
00463 LocalPointer<Derived> clone() const &;
00464
00472 LocalPointer<Derived> clone() &&;
00473
00480 UBool copyErrorTo(UErrorCode &outErrorCode) const {
00481 if (U_FAILURE(outErrorCode)) {
00482
00483 return TRUE;
00484 }
00485 fMacros.copyErrorTo(outErrorCode);
00486 return U_FAILURE(outErrorCode);
00487 }
00488
00489
00490
00491 private:
00492 impl::RangeMacroProps fMacros;
00493
00494
00495 NumberRangeFormatterSettings() = default;
00496
00497 friend class LocalizedNumberRangeFormatter;
00498 friend class UnlocalizedNumberRangeFormatter;
00499 };
00500
00509 class U_I18N_API UnlocalizedNumberRangeFormatter
00510 : public NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>, public UMemory {
00511
00512 public:
00522 LocalizedNumberRangeFormatter locale(const icu::Locale &locale) const &;
00523
00533 LocalizedNumberRangeFormatter locale(const icu::Locale &locale) &&;
00534
00540 UnlocalizedNumberRangeFormatter() = default;
00541
00546 UnlocalizedNumberRangeFormatter(const UnlocalizedNumberRangeFormatter &other);
00547
00553 UnlocalizedNumberRangeFormatter(UnlocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
00554
00559 UnlocalizedNumberRangeFormatter& operator=(const UnlocalizedNumberRangeFormatter& other);
00560
00566 UnlocalizedNumberRangeFormatter& operator=(UnlocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
00567
00568 private:
00569 explicit UnlocalizedNumberRangeFormatter(
00570 const NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>& other);
00571
00572 explicit UnlocalizedNumberRangeFormatter(
00573 NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>&& src) U_NOEXCEPT;
00574
00575
00576 friend class NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>;
00577
00578
00579 friend class NumberRangeFormatter;
00580 };
00581
00590 class U_I18N_API LocalizedNumberRangeFormatter
00591 : public NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>, public UMemory {
00592 public:
00606 FormattedNumberRange formatFormattableRange(
00607 const Formattable& first, const Formattable& second, UErrorCode& status) const;
00608
00614 LocalizedNumberRangeFormatter() = default;
00615
00620 LocalizedNumberRangeFormatter(const LocalizedNumberRangeFormatter &other);
00621
00627 LocalizedNumberRangeFormatter(LocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
00628
00633 LocalizedNumberRangeFormatter& operator=(const LocalizedNumberRangeFormatter& other);
00634
00640 LocalizedNumberRangeFormatter& operator=(LocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
00641
00642 #ifndef U_HIDE_INTERNAL_API
00643
00654 void formatImpl(impl::UFormattedNumberRangeData& results, bool equalBeforeRounding,
00655 UErrorCode& status) const;
00656
00657 #endif
00658
00663 ~LocalizedNumberRangeFormatter();
00664
00665 private:
00666 std::atomic<impl::NumberRangeFormatterImpl*> fAtomicFormatter = {};
00667
00668 const impl::NumberRangeFormatterImpl* getFormatter(UErrorCode& stauts) const;
00669
00670 explicit LocalizedNumberRangeFormatter(
00671 const NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>& other);
00672
00673 explicit LocalizedNumberRangeFormatter(
00674 NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>&& src) U_NOEXCEPT;
00675
00676 LocalizedNumberRangeFormatter(const impl::RangeMacroProps ¯os, const Locale &locale);
00677
00678 LocalizedNumberRangeFormatter(impl::RangeMacroProps &¯os, const Locale &locale);
00679
00680 void clear();
00681
00682
00683 friend class NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>;
00684 friend class NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>;
00685
00686
00687 friend class UnlocalizedNumberRangeFormatter;
00688 };
00689
00698 class U_I18N_API FormattedNumberRange : public UMemory, public FormattedValue {
00699 public:
00700
00708 UnicodeString toString(UErrorCode& status) const U_OVERRIDE;
00709
00710
00712 UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE;
00713
00714
00722 Appendable &appendTo(Appendable &appendable, UErrorCode& status) const U_OVERRIDE;
00723
00724
00726 UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE;
00727
00728 #ifndef U_HIDE_DRAFT_API
00729
00743 UnicodeString getFirstDecimal(UErrorCode& status) const;
00744
00759 UnicodeString getSecondDecimal(UErrorCode& status) const;
00760 #endif // U_HIDE_DRAFT_API
00761
00771 UNumberRangeIdentityResult getIdentityResult(UErrorCode& status) const;
00772
00776 FormattedNumberRange(const FormattedNumberRange&) = delete;
00777
00781 FormattedNumberRange& operator=(const FormattedNumberRange&) = delete;
00782
00788 FormattedNumberRange(FormattedNumberRange&& src) U_NOEXCEPT;
00789
00795 FormattedNumberRange& operator=(FormattedNumberRange&& src) U_NOEXCEPT;
00796
00801 ~FormattedNumberRange();
00802
00803 private:
00804
00805 const impl::UFormattedNumberRangeData *fData;
00806
00807
00808 UErrorCode fErrorCode;
00809
00813 explicit FormattedNumberRange(impl::UFormattedNumberRangeData *results)
00814 : fData(results), fErrorCode(U_ZERO_ERROR) {}
00815
00816 explicit FormattedNumberRange(UErrorCode errorCode)
00817 : fData(nullptr), fErrorCode(errorCode) {}
00818
00819 void getAllFieldPositionsImpl(FieldPositionIteratorHandler& fpih, UErrorCode& status) const;
00820
00821
00822 friend class LocalizedNumberRangeFormatter;
00823 };
00824
00830 class U_I18N_API NumberRangeFormatter final {
00831 public:
00839 static UnlocalizedNumberRangeFormatter with();
00840
00850 static LocalizedNumberRangeFormatter withLocale(const Locale &locale);
00851
00855 NumberRangeFormatter() = delete;
00856 };
00857
00858 }
00859 U_NAMESPACE_END
00860
00861 #endif
00862
00863 #endif
00864
00865 #endif // __NUMBERRANGEFORMATTER_H__
00866