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
00452 #ifndef U_HIDE_DRAFT_API
00453
00464 LocalPointer<Derived> clone() const &;
00465
00473 LocalPointer<Derived> clone() &&;
00474 #endif
00475
00482 UBool copyErrorTo(UErrorCode &outErrorCode) const {
00483 if (U_FAILURE(outErrorCode)) {
00484
00485 return TRUE;
00486 }
00487 fMacros.copyErrorTo(outErrorCode);
00488 return U_FAILURE(outErrorCode);
00489 }
00490
00491
00492
00493 private:
00494 impl::RangeMacroProps fMacros;
00495
00496
00497 NumberRangeFormatterSettings() = default;
00498
00499 friend class LocalizedNumberRangeFormatter;
00500 friend class UnlocalizedNumberRangeFormatter;
00501 };
00502
00511 class U_I18N_API UnlocalizedNumberRangeFormatter
00512 : public NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>, public UMemory {
00513
00514 public:
00524 LocalizedNumberRangeFormatter locale(const icu::Locale &locale) const &;
00525
00535 LocalizedNumberRangeFormatter locale(const icu::Locale &locale) &&;
00536
00542 UnlocalizedNumberRangeFormatter() = default;
00543
00548 UnlocalizedNumberRangeFormatter(const UnlocalizedNumberRangeFormatter &other);
00549
00555 UnlocalizedNumberRangeFormatter(UnlocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
00556
00561 UnlocalizedNumberRangeFormatter& operator=(const UnlocalizedNumberRangeFormatter& other);
00562
00568 UnlocalizedNumberRangeFormatter& operator=(UnlocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
00569
00570 private:
00571 explicit UnlocalizedNumberRangeFormatter(
00572 const NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>& other);
00573
00574 explicit UnlocalizedNumberRangeFormatter(
00575 NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>&& src) U_NOEXCEPT;
00576
00577
00578 friend class NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>;
00579
00580
00581 friend class NumberRangeFormatter;
00582 };
00583
00592 class U_I18N_API LocalizedNumberRangeFormatter
00593 : public NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>, public UMemory {
00594 public:
00608 FormattedNumberRange formatFormattableRange(
00609 const Formattable& first, const Formattable& second, UErrorCode& status) const;
00610
00616 LocalizedNumberRangeFormatter() = default;
00617
00622 LocalizedNumberRangeFormatter(const LocalizedNumberRangeFormatter &other);
00623
00629 LocalizedNumberRangeFormatter(LocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
00630
00635 LocalizedNumberRangeFormatter& operator=(const LocalizedNumberRangeFormatter& other);
00636
00642 LocalizedNumberRangeFormatter& operator=(LocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
00643
00644 #ifndef U_HIDE_INTERNAL_API
00645
00656 void formatImpl(impl::UFormattedNumberRangeData& results, bool equalBeforeRounding,
00657 UErrorCode& status) const;
00658
00659 #endif
00660
00665 ~LocalizedNumberRangeFormatter();
00666
00667 private:
00668 std::atomic<impl::NumberRangeFormatterImpl*> fAtomicFormatter = {};
00669
00670 const impl::NumberRangeFormatterImpl* getFormatter(UErrorCode& stauts) const;
00671
00672 explicit LocalizedNumberRangeFormatter(
00673 const NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>& other);
00674
00675 explicit LocalizedNumberRangeFormatter(
00676 NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>&& src) U_NOEXCEPT;
00677
00678 LocalizedNumberRangeFormatter(const impl::RangeMacroProps ¯os, const Locale &locale);
00679
00680 LocalizedNumberRangeFormatter(impl::RangeMacroProps &¯os, const Locale &locale);
00681
00682 void clear();
00683
00684
00685 friend class NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>;
00686 friend class NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>;
00687
00688
00689 friend class UnlocalizedNumberRangeFormatter;
00690 };
00691
00700 class U_I18N_API FormattedNumberRange : public UMemory, public FormattedValue {
00701 public:
00702
00710 UnicodeString toString(UErrorCode& status) const U_OVERRIDE;
00711
00712
00714 UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE;
00715
00716
00724 Appendable &appendTo(Appendable &appendable, UErrorCode& status) const U_OVERRIDE;
00725
00726
00728 UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE;
00729
00730 #ifndef U_HIDE_DRAFT_API
00731
00760 UBool nextFieldPosition(FieldPosition& fieldPosition, UErrorCode& status) const;
00761
00775 void getAllFieldPositions(FieldPositionIterator &iterator, UErrorCode &status) const;
00776
00791 UnicodeString getFirstDecimal(UErrorCode& status) const;
00792
00807 UnicodeString getSecondDecimal(UErrorCode& status) const;
00808 #endif // U_HIDE_DRAFT_API
00809
00819 UNumberRangeIdentityResult getIdentityResult(UErrorCode& status) const;
00820
00824 FormattedNumberRange(const FormattedNumberRange&) = delete;
00825
00829 FormattedNumberRange& operator=(const FormattedNumberRange&) = delete;
00830
00836 FormattedNumberRange(FormattedNumberRange&& src) U_NOEXCEPT;
00837
00843 FormattedNumberRange& operator=(FormattedNumberRange&& src) U_NOEXCEPT;
00844
00849 ~FormattedNumberRange();
00850
00851 private:
00852
00853 const impl::UFormattedNumberRangeData *fData;
00854
00855
00856 UErrorCode fErrorCode;
00857
00861 explicit FormattedNumberRange(impl::UFormattedNumberRangeData *results)
00862 : fData(results), fErrorCode(U_ZERO_ERROR) {}
00863
00864 explicit FormattedNumberRange(UErrorCode errorCode)
00865 : fData(nullptr), fErrorCode(errorCode) {}
00866
00867 void getAllFieldPositionsImpl(FieldPositionIteratorHandler& fpih, UErrorCode& status) const;
00868
00869
00870 friend class LocalizedNumberRangeFormatter;
00871 };
00872
00878 class U_I18N_API NumberRangeFormatter final {
00879 public:
00887 static UnlocalizedNumberRangeFormatter with();
00888
00898 static LocalizedNumberRangeFormatter withLocale(const Locale &locale);
00899
00903 NumberRangeFormatter() = delete;
00904 };
00905
00906 }
00907 U_NAMESPACE_END
00908
00909 #endif
00910
00911 #endif
00912
00913 #endif // __NUMBERRANGEFORMATTER_H__
00914