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/fpositer.h"
00012 #include "unicode/numberformatter.h"
00013
00014 #ifndef U_HIDE_DRAFT_API
00015
00047 typedef enum UNumberRangeCollapse {
00056 UNUM_RANGE_COLLAPSE_AUTO,
00057
00063 UNUM_RANGE_COLLAPSE_NONE,
00064
00071 UNUM_RANGE_COLLAPSE_UNIT,
00072
00079 UNUM_RANGE_COLLAPSE_ALL
00080 } UNumberRangeCollapse;
00081
00089 typedef enum UNumberRangeIdentityFallback {
00095 UNUM_IDENTITY_FALLBACK_SINGLE_VALUE,
00096
00103 UNUM_IDENTITY_FALLBACK_APPROXIMATELY_OR_SINGLE_VALUE,
00104
00111 UNUM_IDENTITY_FALLBACK_APPROXIMATELY,
00112
00119 UNUM_IDENTITY_FALLBACK_RANGE
00120 } UNumberRangeIdentityFallback;
00121
00129 typedef enum UNumberRangeIdentityResult {
00136 UNUM_IDENTITY_RESULT_EQUAL_BEFORE_ROUNDING,
00137
00144 UNUM_IDENTITY_RESULT_EQUAL_AFTER_ROUNDING,
00145
00152 UNUM_IDENTITY_RESULT_NOT_EQUAL,
00153
00154 #ifndef U_HIDE_INTERNAL_API
00155
00159 UNUM_IDENTITY_RESULT_COUNT
00160 #endif
00161
00162 } UNumberRangeIdentityResult;
00163
00164 U_NAMESPACE_BEGIN
00165
00166 namespace number {
00167
00168
00169 class UnlocalizedNumberRangeFormatter;
00170 class LocalizedNumberRangeFormatter;
00171 class FormattedNumberRange;
00172
00173 namespace impl {
00174
00175
00176 struct RangeMacroProps;
00177 class DecimalQuantity;
00178 struct UFormattedNumberRangeData;
00179 class NumberRangeFormatterImpl;
00180
00181 }
00182
00188 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN && !defined(U_IN_DOXYGEN)
00189 template struct U_I18N_API std::atomic<impl::NumberRangeFormatterImpl*>;
00190 #endif
00191
00193
00194
00195 namespace impl {
00196
00197
00199 struct U_I18N_API RangeMacroProps : public UMemory {
00201 UnlocalizedNumberFormatter formatter1;
00202
00204 UnlocalizedNumberFormatter formatter2;
00205
00207 bool singleFormatter = true;
00208
00210 UNumberRangeCollapse collapse = UNUM_RANGE_COLLAPSE_AUTO;
00211
00213 UNumberRangeIdentityFallback identityFallback = UNUM_IDENTITY_FALLBACK_APPROXIMATELY;
00214
00216 Locale locale;
00217
00218
00219
00224 bool copyErrorTo(UErrorCode &status) const {
00225 return formatter1.copyErrorTo(status) || formatter2.copyErrorTo(status);
00226 }
00227 };
00228
00229 }
00230
00236 template<typename Derived>
00237 class U_I18N_API NumberRangeFormatterSettings {
00238 public:
00251 Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) const &;
00252
00262 Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) &&;
00263
00273 Derived numberFormatterBoth(UnlocalizedNumberFormatter &&formatter) const &;
00274
00284 Derived numberFormatterBoth(UnlocalizedNumberFormatter &&formatter) &&;
00285
00297 Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) const &;
00298
00308 Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) &&;
00309
00319 Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) const &;
00320
00330 Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) &&;
00331
00343 Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) const &;
00344
00354 Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) &&;
00355
00365 Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) const &;
00366
00376 Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) &&;
00377
00395 Derived collapse(UNumberRangeCollapse collapse) const &;
00396
00406 Derived collapse(UNumberRangeCollapse collapse) &&;
00407
00428 Derived identityFallback(UNumberRangeIdentityFallback identityFallback) const &;
00429
00439 Derived identityFallback(UNumberRangeIdentityFallback identityFallback) &&;
00440
00447 UBool copyErrorTo(UErrorCode &outErrorCode) const {
00448 if (U_FAILURE(outErrorCode)) {
00449
00450 return TRUE;
00451 }
00452 fMacros.copyErrorTo(outErrorCode);
00453 return U_FAILURE(outErrorCode);
00454 };
00455
00456
00457
00458 private:
00459 impl::RangeMacroProps fMacros;
00460
00461
00462 NumberRangeFormatterSettings() = default;
00463
00464 friend class LocalizedNumberRangeFormatter;
00465 friend class UnlocalizedNumberRangeFormatter;
00466 };
00467
00474 class U_I18N_API UnlocalizedNumberRangeFormatter
00475 : public NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>, public UMemory {
00476
00477 public:
00487 LocalizedNumberRangeFormatter locale(const icu::Locale &locale) const &;
00488
00498 LocalizedNumberRangeFormatter locale(const icu::Locale &locale) &&;
00499
00505 UnlocalizedNumberRangeFormatter() = default;
00506
00511 UnlocalizedNumberRangeFormatter(const UnlocalizedNumberRangeFormatter &other);
00512
00518 UnlocalizedNumberRangeFormatter(UnlocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
00519
00524 UnlocalizedNumberRangeFormatter& operator=(const UnlocalizedNumberRangeFormatter& other);
00525
00531 UnlocalizedNumberRangeFormatter& operator=(UnlocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
00532
00533 private:
00534 explicit UnlocalizedNumberRangeFormatter(
00535 const NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>& other);
00536
00537 explicit UnlocalizedNumberRangeFormatter(
00538 NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>&& src) U_NOEXCEPT;
00539
00540
00541 friend class NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>;
00542
00543
00544 friend class NumberRangeFormatter;
00545 };
00546
00553 class U_I18N_API LocalizedNumberRangeFormatter
00554 : public NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>, public UMemory {
00555 public:
00569 FormattedNumberRange formatFormattableRange(
00570 const Formattable& first, const Formattable& second, UErrorCode& status) const;
00571
00577 LocalizedNumberRangeFormatter() = default;
00578
00583 LocalizedNumberRangeFormatter(const LocalizedNumberRangeFormatter &other);
00584
00590 LocalizedNumberRangeFormatter(LocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
00591
00596 LocalizedNumberRangeFormatter& operator=(const LocalizedNumberRangeFormatter& other);
00597
00603 LocalizedNumberRangeFormatter& operator=(LocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
00604
00605 #ifndef U_HIDE_INTERNAL_API
00606
00617 void formatImpl(impl::UFormattedNumberRangeData& results, bool equalBeforeRounding,
00618 UErrorCode& status) const;
00619
00620 #endif
00621
00626 ~LocalizedNumberRangeFormatter();
00627
00628 private:
00629 std::atomic<impl::NumberRangeFormatterImpl*> fAtomicFormatter = {};
00630
00631 const impl::NumberRangeFormatterImpl* getFormatter(UErrorCode& stauts) const;
00632
00633 explicit LocalizedNumberRangeFormatter(
00634 const NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>& other);
00635
00636 explicit LocalizedNumberRangeFormatter(
00637 NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>&& src) U_NOEXCEPT;
00638
00639 LocalizedNumberRangeFormatter(const impl::RangeMacroProps ¯os, const Locale &locale);
00640
00641 LocalizedNumberRangeFormatter(impl::RangeMacroProps &¯os, const Locale &locale);
00642
00643 void clear();
00644
00645
00646 friend class NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>;
00647 friend class NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>;
00648
00649
00650 friend class UnlocalizedNumberRangeFormatter;
00651 };
00652
00659 class U_I18N_API FormattedNumberRange : public UMemory {
00660 public:
00669 UnicodeString toString(UErrorCode& status) const;
00670
00682 Appendable &appendTo(Appendable &appendable, UErrorCode& status) const;
00683
00713 UBool nextFieldPosition(FieldPosition& fieldPosition, UErrorCode& status) const;
00714
00728 void getAllFieldPositions(FieldPositionIterator &iterator, UErrorCode &status) const;
00729
00744 UnicodeString getFirstDecimal(UErrorCode& status) const;
00745
00760 UnicodeString getSecondDecimal(UErrorCode& status) const;
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 *fResults;
00806
00807
00808 UErrorCode fErrorCode;
00809
00814 explicit FormattedNumberRange(impl::UFormattedNumberRangeData *results)
00815 : fResults(results), fErrorCode(U_ZERO_ERROR) {};
00816
00817 explicit FormattedNumberRange(UErrorCode errorCode)
00818 : fResults(nullptr), fErrorCode(errorCode) {};
00819
00820 void getAllFieldPositionsImpl(FieldPositionIteratorHandler& fpih, UErrorCode& status) const;
00821
00822
00823 friend class LocalizedNumberRangeFormatter;
00824 };
00825
00831 class U_I18N_API NumberRangeFormatter final {
00832 public:
00840 static UnlocalizedNumberRangeFormatter with();
00841
00851 static LocalizedNumberRangeFormatter withLocale(const Locale &locale);
00852
00856 NumberRangeFormatter() = delete;
00857 };
00858
00859 }
00860 U_NAMESPACE_END
00861
00862 #endif // U_HIDE_DRAFT_API
00863
00864 #endif // __NUMBERRANGEFORMATTER_H__
00865
00866 #endif