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 #include "unicode/unumberrangeformatter.h"
00020
00048 U_NAMESPACE_BEGIN
00049
00050
00051 class PluralRules;
00052
00053 namespace number {
00054
00055
00056 class UnlocalizedNumberRangeFormatter;
00057 class LocalizedNumberRangeFormatter;
00058 class FormattedNumberRange;
00059
00060 namespace impl {
00061
00062
00063 struct RangeMacroProps;
00064 class DecimalQuantity;
00065 class UFormattedNumberRangeData;
00066 class NumberRangeFormatterImpl;
00067 struct UFormattedNumberRangeImpl;
00068
00069 }
00070
00076 #if U_PLATFORM == U_PF_WINDOWS && !defined(U_IN_DOXYGEN) && !defined(U_STATIC_IMPLEMENTATION)
00077 }
00078 U_NAMESPACE_END
00079
00080 template struct U_I18N_API std::atomic< U_NAMESPACE_QUALIFIER number::impl::NumberRangeFormatterImpl*>;
00081
00082 U_NAMESPACE_BEGIN
00083 namespace number {
00084 #endif
00085
00087
00088
00089 namespace impl {
00090
00091
00093 struct U_I18N_API RangeMacroProps : public UMemory {
00095 UnlocalizedNumberFormatter formatter1;
00096
00098 UnlocalizedNumberFormatter formatter2;
00099
00101 bool singleFormatter = true;
00102
00104 UNumberRangeCollapse collapse = UNUM_RANGE_COLLAPSE_AUTO;
00105
00107 UNumberRangeIdentityFallback identityFallback = UNUM_IDENTITY_FALLBACK_APPROXIMATELY;
00108
00110 Locale locale;
00111
00112
00113
00118 bool copyErrorTo(UErrorCode &status) const {
00119 return formatter1.copyErrorTo(status) || formatter2.copyErrorTo(status);
00120 }
00121 };
00122
00123 }
00124
00130 template<typename Derived>
00131 class U_I18N_API NumberRangeFormatterSettings {
00132 public:
00145 Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) const &;
00146
00156 Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) &&;
00157
00167 Derived numberFormatterBoth(UnlocalizedNumberFormatter &&formatter) const &;
00168
00178 Derived numberFormatterBoth(UnlocalizedNumberFormatter &&formatter) &&;
00179
00191 Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) const &;
00192
00202 Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) &&;
00203
00213 Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) const &;
00214
00224 Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) &&;
00225
00237 Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) const &;
00238
00248 Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) &&;
00249
00259 Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) const &;
00260
00270 Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) &&;
00271
00289 Derived collapse(UNumberRangeCollapse collapse) const &;
00290
00300 Derived collapse(UNumberRangeCollapse collapse) &&;
00301
00322 Derived identityFallback(UNumberRangeIdentityFallback identityFallback) const &;
00323
00333 Derived identityFallback(UNumberRangeIdentityFallback identityFallback) &&;
00334
00346 LocalPointer<Derived> clone() const &;
00347
00355 LocalPointer<Derived> clone() &&;
00356
00363 UBool copyErrorTo(UErrorCode &outErrorCode) const {
00364 if (U_FAILURE(outErrorCode)) {
00365
00366 return true;
00367 }
00368 fMacros.copyErrorTo(outErrorCode);
00369 return U_FAILURE(outErrorCode);
00370 }
00371
00372
00373
00374 private:
00375 impl::RangeMacroProps fMacros;
00376
00377
00378 NumberRangeFormatterSettings() = default;
00379
00380 friend class LocalizedNumberRangeFormatter;
00381 friend class UnlocalizedNumberRangeFormatter;
00382 };
00383
00392 class U_I18N_API UnlocalizedNumberRangeFormatter
00393 : public NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>, public UMemory {
00394
00395 public:
00405 LocalizedNumberRangeFormatter locale(const icu::Locale &locale) const &;
00406
00416 LocalizedNumberRangeFormatter locale(const icu::Locale &locale) &&;
00417
00423 UnlocalizedNumberRangeFormatter() = default;
00424
00429 UnlocalizedNumberRangeFormatter(const UnlocalizedNumberRangeFormatter &other);
00430
00436 UnlocalizedNumberRangeFormatter(UnlocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
00437
00442 UnlocalizedNumberRangeFormatter& operator=(const UnlocalizedNumberRangeFormatter& other);
00443
00449 UnlocalizedNumberRangeFormatter& operator=(UnlocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
00450
00451 private:
00452 explicit UnlocalizedNumberRangeFormatter(
00453 const NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>& other);
00454
00455 explicit UnlocalizedNumberRangeFormatter(
00456 NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>&& src) U_NOEXCEPT;
00457
00458
00459 friend class NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>;
00460
00461
00462 friend class NumberRangeFormatter;
00463 };
00464
00473 class U_I18N_API LocalizedNumberRangeFormatter
00474 : public NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>, public UMemory {
00475 public:
00489 FormattedNumberRange formatFormattableRange(
00490 const Formattable& first, const Formattable& second, UErrorCode& status) const;
00491
00497 LocalizedNumberRangeFormatter() = default;
00498
00503 LocalizedNumberRangeFormatter(const LocalizedNumberRangeFormatter &other);
00504
00510 LocalizedNumberRangeFormatter(LocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
00511
00516 LocalizedNumberRangeFormatter& operator=(const LocalizedNumberRangeFormatter& other);
00517
00523 LocalizedNumberRangeFormatter& operator=(LocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
00524
00525 #ifndef U_HIDE_INTERNAL_API
00526
00537 void formatImpl(impl::UFormattedNumberRangeData& results, bool equalBeforeRounding,
00538 UErrorCode& status) const;
00539
00540 #endif
00541
00546 ~LocalizedNumberRangeFormatter();
00547
00548 private:
00549 std::atomic<impl::NumberRangeFormatterImpl*> fAtomicFormatter = {};
00550
00551 const impl::NumberRangeFormatterImpl* getFormatter(UErrorCode& stauts) const;
00552
00553 explicit LocalizedNumberRangeFormatter(
00554 const NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>& other);
00555
00556 explicit LocalizedNumberRangeFormatter(
00557 NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>&& src) U_NOEXCEPT;
00558
00559 LocalizedNumberRangeFormatter(const impl::RangeMacroProps ¯os, const Locale &locale);
00560
00561 LocalizedNumberRangeFormatter(impl::RangeMacroProps &¯os, const Locale &locale);
00562
00563 void clear();
00564
00565
00566 friend class NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>;
00567 friend class NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>;
00568
00569
00570 friend class UnlocalizedNumberRangeFormatter;
00571 };
00572
00581 class U_I18N_API FormattedNumberRange : public UMemory, public FormattedValue {
00582 public:
00583
00591 UnicodeString toString(UErrorCode& status) const U_OVERRIDE;
00592
00593
00595 UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE;
00596
00597
00605 Appendable &appendTo(Appendable &appendable, UErrorCode& status) const U_OVERRIDE;
00606
00607
00609 UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE;
00610
00630 template<typename StringClass>
00631 inline std::pair<StringClass, StringClass> getDecimalNumbers(UErrorCode& status) const;
00632
00642 UNumberRangeIdentityResult getIdentityResult(UErrorCode& status) const;
00643
00644 #ifndef U_HIDE_DRAFT_API
00645
00649 FormattedNumberRange()
00650 : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
00651 #endif
00652
00656 FormattedNumberRange(const FormattedNumberRange&) = delete;
00657
00661 FormattedNumberRange& operator=(const FormattedNumberRange&) = delete;
00662
00668 FormattedNumberRange(FormattedNumberRange&& src) U_NOEXCEPT;
00669
00675 FormattedNumberRange& operator=(FormattedNumberRange&& src) U_NOEXCEPT;
00676
00681 ~FormattedNumberRange();
00682
00683 private:
00684
00685 const impl::UFormattedNumberRangeData *fData;
00686
00687
00688 UErrorCode fErrorCode;
00689
00693 explicit FormattedNumberRange(impl::UFormattedNumberRangeData *results)
00694 : fData(results), fErrorCode(U_ZERO_ERROR) {}
00695
00696 explicit FormattedNumberRange(UErrorCode errorCode)
00697 : fData(nullptr), fErrorCode(errorCode) {}
00698
00699 void getAllFieldPositionsImpl(FieldPositionIteratorHandler& fpih, UErrorCode& status) const;
00700
00701 void getDecimalNumbers(ByteSink& sink1, ByteSink& sink2, UErrorCode& status) const;
00702
00703 const impl::UFormattedNumberRangeData* getData(UErrorCode& status) const;
00704
00705
00706 friend class ::icu::PluralRules;
00707
00708
00709 friend class LocalizedNumberRangeFormatter;
00710
00711
00712 friend struct impl::UFormattedNumberRangeImpl;
00713 };
00714
00715
00716 template<typename StringClass>
00717 std::pair<StringClass, StringClass> FormattedNumberRange::getDecimalNumbers(UErrorCode& status) const {
00718 StringClass str1;
00719 StringClass str2;
00720 StringByteSink<StringClass> sink1(&str1);
00721 StringByteSink<StringClass> sink2(&str2);
00722 getDecimalNumbers(sink1, sink2, status);
00723 return std::make_pair(str1, str2);
00724 }
00725
00731 class U_I18N_API NumberRangeFormatter final {
00732 public:
00740 static UnlocalizedNumberRangeFormatter with();
00741
00751 static LocalizedNumberRangeFormatter withLocale(const Locale &locale);
00752
00756 NumberRangeFormatter() = delete;
00757 };
00758
00759 }
00760 U_NAMESPACE_END
00761
00762 #endif
00763
00764 #endif
00765
00766 #endif // __NUMBERRANGEFORMATTER_H__
00767