00001
00002
00003
00004 #if !UCONFIG_NO_FORMATTING
00005 #ifndef __NUMBERRANGEFORMATTER_H__
00006 #define __NUMBERRANGEFORMATTER_H__
00007
00008 #include <atomic>
00009 #include "unicode/appendable.h"
00010 #include "unicode/fieldpos.h"
00011 #include "unicode/formattedvalue.h"
00012 #include "unicode/fpositer.h"
00013 #include "unicode/numberformatter.h"
00014
00015 #ifndef U_HIDE_DRAFT_API
00016
00049 typedef enum UNumberRangeCollapse {
00058 UNUM_RANGE_COLLAPSE_AUTO,
00059
00065 UNUM_RANGE_COLLAPSE_NONE,
00066
00073 UNUM_RANGE_COLLAPSE_UNIT,
00074
00081 UNUM_RANGE_COLLAPSE_ALL
00082 } UNumberRangeCollapse;
00083
00091 typedef enum UNumberRangeIdentityFallback {
00097 UNUM_IDENTITY_FALLBACK_SINGLE_VALUE,
00098
00105 UNUM_IDENTITY_FALLBACK_APPROXIMATELY_OR_SINGLE_VALUE,
00106
00113 UNUM_IDENTITY_FALLBACK_APPROXIMATELY,
00114
00121 UNUM_IDENTITY_FALLBACK_RANGE
00122 } UNumberRangeIdentityFallback;
00123
00131 typedef enum UNumberRangeIdentityResult {
00138 UNUM_IDENTITY_RESULT_EQUAL_BEFORE_ROUNDING,
00139
00146 UNUM_IDENTITY_RESULT_EQUAL_AFTER_ROUNDING,
00147
00154 UNUM_IDENTITY_RESULT_NOT_EQUAL,
00155
00156 #ifndef U_HIDE_INTERNAL_API
00157
00161 UNUM_IDENTITY_RESULT_COUNT
00162 #endif
00163
00164 } UNumberRangeIdentityResult;
00165
00166 U_NAMESPACE_BEGIN
00167
00168 namespace number {
00169
00170
00171 class UnlocalizedNumberRangeFormatter;
00172 class LocalizedNumberRangeFormatter;
00173 class FormattedNumberRange;
00174
00175 namespace impl {
00176
00177
00178 struct RangeMacroProps;
00179 class DecimalQuantity;
00180 class UFormattedNumberRangeData;
00181 class NumberRangeFormatterImpl;
00182
00183 }
00184
00190 #if U_PLATFORM == U_PF_WINDOWS && !defined(U_IN_DOXYGEN)
00191 }
00192 U_NAMESPACE_END
00193
00194 template struct U_I18N_API std::atomic< U_NAMESPACE_QUALIFIER number::impl::NumberRangeFormatterImpl*>;
00195
00196 U_NAMESPACE_BEGIN
00197 namespace number {
00198 #endif
00199
00201
00202
00203 namespace impl {
00204
00205
00207 struct U_I18N_API RangeMacroProps : public UMemory {
00209 UnlocalizedNumberFormatter formatter1;
00210
00212 UnlocalizedNumberFormatter formatter2;
00213
00215 bool singleFormatter = true;
00216
00218 UNumberRangeCollapse collapse = UNUM_RANGE_COLLAPSE_AUTO;
00219
00221 UNumberRangeIdentityFallback identityFallback = UNUM_IDENTITY_FALLBACK_APPROXIMATELY;
00222
00224 Locale locale;
00225
00226
00227
00232 bool copyErrorTo(UErrorCode &status) const {
00233 return formatter1.copyErrorTo(status) || formatter2.copyErrorTo(status);
00234 }
00235 };
00236
00237 }
00238
00244 template<typename Derived>
00245 class U_I18N_API NumberRangeFormatterSettings {
00246 public:
00259 Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) const &;
00260
00270 Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) &&;
00271
00281 Derived numberFormatterBoth(UnlocalizedNumberFormatter &&formatter) const &;
00282
00292 Derived numberFormatterBoth(UnlocalizedNumberFormatter &&formatter) &&;
00293
00305 Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) const &;
00306
00316 Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) &&;
00317
00327 Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) const &;
00328
00338 Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) &&;
00339
00351 Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) const &;
00352
00362 Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) &&;
00363
00373 Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) const &;
00374
00384 Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) &&;
00385
00403 Derived collapse(UNumberRangeCollapse collapse) const &;
00404
00414 Derived collapse(UNumberRangeCollapse collapse) &&;
00415
00436 Derived identityFallback(UNumberRangeIdentityFallback identityFallback) const &;
00437
00447 Derived identityFallback(UNumberRangeIdentityFallback identityFallback) &&;
00448
00460 LocalPointer<Derived> clone() const &;
00461
00469 LocalPointer<Derived> clone() &&;
00470
00477 UBool copyErrorTo(UErrorCode &outErrorCode) const {
00478 if (U_FAILURE(outErrorCode)) {
00479
00480 return TRUE;
00481 }
00482 fMacros.copyErrorTo(outErrorCode);
00483 return U_FAILURE(outErrorCode);
00484 }
00485
00486
00487
00488 private:
00489 impl::RangeMacroProps fMacros;
00490
00491
00492 NumberRangeFormatterSettings() = default;
00493
00494 friend class LocalizedNumberRangeFormatter;
00495 friend class UnlocalizedNumberRangeFormatter;
00496 };
00497
00506 class U_I18N_API UnlocalizedNumberRangeFormatter
00507 : public NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>, public UMemory {
00508
00509 public:
00519 LocalizedNumberRangeFormatter locale(const icu::Locale &locale) const &;
00520
00530 LocalizedNumberRangeFormatter locale(const icu::Locale &locale) &&;
00531
00537 UnlocalizedNumberRangeFormatter() = default;
00538
00543 UnlocalizedNumberRangeFormatter(const UnlocalizedNumberRangeFormatter &other);
00544
00550 UnlocalizedNumberRangeFormatter(UnlocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
00551
00556 UnlocalizedNumberRangeFormatter& operator=(const UnlocalizedNumberRangeFormatter& other);
00557
00563 UnlocalizedNumberRangeFormatter& operator=(UnlocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
00564
00565 private:
00566 explicit UnlocalizedNumberRangeFormatter(
00567 const NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>& other);
00568
00569 explicit UnlocalizedNumberRangeFormatter(
00570 NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>&& src) U_NOEXCEPT;
00571
00572
00573 friend class NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>;
00574
00575
00576 friend class NumberRangeFormatter;
00577 };
00578
00587 class U_I18N_API LocalizedNumberRangeFormatter
00588 : public NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>, public UMemory {
00589 public:
00603 FormattedNumberRange formatFormattableRange(
00604 const Formattable& first, const Formattable& second, UErrorCode& status) const;
00605
00611 LocalizedNumberRangeFormatter() = default;
00612
00617 LocalizedNumberRangeFormatter(const LocalizedNumberRangeFormatter &other);
00618
00624 LocalizedNumberRangeFormatter(LocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
00625
00630 LocalizedNumberRangeFormatter& operator=(const LocalizedNumberRangeFormatter& other);
00631
00637 LocalizedNumberRangeFormatter& operator=(LocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
00638
00639 #ifndef U_HIDE_INTERNAL_API
00640
00651 void formatImpl(impl::UFormattedNumberRangeData& results, bool equalBeforeRounding,
00652 UErrorCode& status) const;
00653
00654 #endif
00655
00660 ~LocalizedNumberRangeFormatter();
00661
00662 private:
00663 std::atomic<impl::NumberRangeFormatterImpl*> fAtomicFormatter = {};
00664
00665 const impl::NumberRangeFormatterImpl* getFormatter(UErrorCode& stauts) const;
00666
00667 explicit LocalizedNumberRangeFormatter(
00668 const NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>& other);
00669
00670 explicit LocalizedNumberRangeFormatter(
00671 NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>&& src) U_NOEXCEPT;
00672
00673 LocalizedNumberRangeFormatter(const impl::RangeMacroProps ¯os, const Locale &locale);
00674
00675 LocalizedNumberRangeFormatter(impl::RangeMacroProps &¯os, const Locale &locale);
00676
00677 void clear();
00678
00679
00680 friend class NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>;
00681 friend class NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>;
00682
00683
00684 friend class UnlocalizedNumberRangeFormatter;
00685 };
00686
00695 class U_I18N_API FormattedNumberRange : public UMemory, public FormattedValue {
00696 public:
00697
00705 UnicodeString toString(UErrorCode& status) const U_OVERRIDE;
00706
00707
00709 UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE;
00710
00711
00719 Appendable &appendTo(Appendable &appendable, UErrorCode& status) const U_OVERRIDE;
00720
00721
00723 UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE;
00724
00754 UBool nextFieldPosition(FieldPosition& fieldPosition, UErrorCode& status) const;
00755
00769 void getAllFieldPositions(FieldPositionIterator &iterator, UErrorCode &status) const;
00770
00785 UnicodeString getFirstDecimal(UErrorCode& status) const;
00786
00801 UnicodeString getSecondDecimal(UErrorCode& status) const;
00802
00812 UNumberRangeIdentityResult getIdentityResult(UErrorCode& status) const;
00813
00817 FormattedNumberRange(const FormattedNumberRange&) = delete;
00818
00822 FormattedNumberRange& operator=(const FormattedNumberRange&) = delete;
00823
00829 FormattedNumberRange(FormattedNumberRange&& src) U_NOEXCEPT;
00830
00836 FormattedNumberRange& operator=(FormattedNumberRange&& src) U_NOEXCEPT;
00837
00842 ~FormattedNumberRange();
00843
00844 private:
00845
00846 const impl::UFormattedNumberRangeData *fData;
00847
00848
00849 UErrorCode fErrorCode;
00850
00855 explicit FormattedNumberRange(impl::UFormattedNumberRangeData *results)
00856 : fData(results), fErrorCode(U_ZERO_ERROR) {}
00857
00858 explicit FormattedNumberRange(UErrorCode errorCode)
00859 : fData(nullptr), fErrorCode(errorCode) {}
00860
00861 void getAllFieldPositionsImpl(FieldPositionIteratorHandler& fpih, UErrorCode& status) const;
00862
00863
00864 friend class LocalizedNumberRangeFormatter;
00865 };
00866
00872 class U_I18N_API NumberRangeFormatter final {
00873 public:
00881 static UnlocalizedNumberRangeFormatter with();
00882
00892 static LocalizedNumberRangeFormatter withLocale(const Locale &locale);
00893
00897 NumberRangeFormatter() = delete;
00898 };
00899
00900 }
00901 U_NAMESPACE_END
00902
00903 #endif // U_HIDE_DRAFT_API
00904
00905 #endif // __NUMBERRANGEFORMATTER_H__
00906
00907 #endif