ICU 71.1 71.1

numberrangeformatter.h
Go to the documentation of this file.
1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3
4 #ifndef __NUMBERRANGEFORMATTER_H__
5 #define __NUMBERRANGEFORMATTER_H__
6
7 #include "unicode/utypes.h"
8
9 #if U_SHOW_CPLUSPLUS_API
10
11 #if !UCONFIG_NO_FORMATTING
12
13 #include <atomic>
14 #include "unicode/appendable.h"
15 #include "unicode/fieldpos.h"
16 #include "unicode/formattedvalue.h"
17 #include "unicode/fpositer.h"
18 #include "unicode/numberformatter.h"
19 #include "unicode/unumberrangeformatter.h"
20
48 U_NAMESPACE_BEGIN
49
50 // Forward declarations:
51 class PluralRules;
52
53 namespace number { // icu::number
54
55 // Forward declarations:
56 class UnlocalizedNumberRangeFormatter;
57 class LocalizedNumberRangeFormatter;
58 class FormattedNumberRange;
59
60 namespace impl {
61
62 // Forward declarations:
63 struct RangeMacroProps;
64 class DecimalQuantity;
65 class UFormattedNumberRangeData;
66 class NumberRangeFormatterImpl;
67 struct UFormattedNumberRangeImpl;
68
69 } // namespace impl
70
76 #if U_PLATFORM == U_PF_WINDOWS && !defined(U_IN_DOXYGEN) && !defined(U_STATIC_IMPLEMENTATION)
77 } // namespace icu::number
78 U_NAMESPACE_END
79
80 template struct U_I18N_API std::atomic< U_NAMESPACE_QUALIFIER number::impl::NumberRangeFormatterImpl*>;
81
82 U_NAMESPACE_BEGIN
83 namespace number { // icu::number
84 #endif
85
87 // Other helper classes would go here, but there are none.
88
89 namespace impl { // icu::number::impl
90
91 // Do not enclose entire MacroProps with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
93 struct U_I18N_API RangeMacroProps : public UMemory {
95 UnlocalizedNumberFormatter formatter1; // = NumberFormatter::with();
96
98 UnlocalizedNumberFormatter formatter2; // = NumberFormatter::with();
99
101 bool singleFormatter = true;
102
104 UNumberRangeCollapse collapse = UNUM_RANGE_COLLAPSE_AUTO;
105
107 UNumberRangeIdentityFallback identityFallback = UNUM_IDENTITY_FALLBACK_APPROXIMATELY;
108
110 Locale locale;
111
112 // NOTE: Uses default copy and move constructors.
113
118 bool copyErrorTo(UErrorCode &status) const {
119 return formatter1.copyErrorTo(status) || formatter2.copyErrorTo(status);
120 }
121 };
122
123 } // namespace impl
124
130 template<typename Derived>
131 class U_I18N_API NumberRangeFormatterSettings {
132 public:
145 Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) const &;
146
156 Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) &&;
157
167 Derived numberFormatterBoth(UnlocalizedNumberFormatter &&formatter) const &;
168
178 Derived numberFormatterBoth(UnlocalizedNumberFormatter &&formatter) &&;
179
191 Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) const &;
192
202 Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) &&;
203
213 Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) const &;
214
224 Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) &&;
225
237 Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) const &;
238
248 Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) &&;
249
259 Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) const &;
260
270 Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) &&;
271
289 Derived collapse(UNumberRangeCollapse collapse) const &;
290
300 Derived collapse(UNumberRangeCollapse collapse) &&;
301
322 Derived identityFallback(UNumberRangeIdentityFallback identityFallback) const &;
323
333 Derived identityFallback(UNumberRangeIdentityFallback identityFallback) &&;
334
346 LocalPointer<Derived> clone() const &;
347
355 LocalPointer<Derived> clone() &&;
356
363 UBool copyErrorTo(UErrorCode &outErrorCode) const {
364 if (U_FAILURE(outErrorCode)) {
365 // Do not overwrite the older error code
366 return true;
367 }
368 fMacros.copyErrorTo(outErrorCode);
369 return U_FAILURE(outErrorCode);
370 }
371
372 // NOTE: Uses default copy and move constructors.
373
374 private:
375 impl::RangeMacroProps fMacros;
376
377 // Don't construct me directly! Use (Un)LocalizedNumberFormatter.
378 NumberRangeFormatterSettings() = default;
379
380 friend class LocalizedNumberRangeFormatter;
381 friend class UnlocalizedNumberRangeFormatter;
382 };
383
392 class U_I18N_API UnlocalizedNumberRangeFormatter
393 : public NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>, public UMemory {
394
395 public:
405 LocalizedNumberRangeFormatter locale(const icu::Locale &locale) const &;
406
416 LocalizedNumberRangeFormatter locale(const icu::Locale &locale) &&;
417
423 UnlocalizedNumberRangeFormatter() = default;
424
429 UnlocalizedNumberRangeFormatter(const UnlocalizedNumberRangeFormatter &other);
430
436 UnlocalizedNumberRangeFormatter(UnlocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
437
442 UnlocalizedNumberRangeFormatter& operator=(const UnlocalizedNumberRangeFormatter& other);
443
449 UnlocalizedNumberRangeFormatter& operator=(UnlocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
450
451 private:
452 explicit UnlocalizedNumberRangeFormatter(
453 const NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>& other);
454
455 explicit UnlocalizedNumberRangeFormatter(
456 NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>&& src) U_NOEXCEPT;
457
458 // To give the fluent setters access to this class's constructor:
459 friend class NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>;
460
461 // To give NumberRangeFormatter::with() access to this class's constructor:
462 friend class NumberRangeFormatter;
463 };
464
473 class U_I18N_API LocalizedNumberRangeFormatter
474 : public NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>, public UMemory {
475 public:
489 FormattedNumberRange formatFormattableRange(
490 const Formattable& first, const Formattable& second, UErrorCode& status) const;
491
497 LocalizedNumberRangeFormatter() = default;
498
503 LocalizedNumberRangeFormatter(const LocalizedNumberRangeFormatter &other);
504
510 LocalizedNumberRangeFormatter(LocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
511
516 LocalizedNumberRangeFormatter& operator=(const LocalizedNumberRangeFormatter& other);
517
523 LocalizedNumberRangeFormatter& operator=(LocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
524
525 #ifndef U_HIDE_INTERNAL_API
526
537 void formatImpl(impl::UFormattedNumberRangeData& results, bool equalBeforeRounding,
538 UErrorCode& status) const;
539
540 #endif /* U_HIDE_INTERNAL_API */
541
546 ~LocalizedNumberRangeFormatter();
547
548 private:
549 std::atomic<impl::NumberRangeFormatterImpl*> fAtomicFormatter = {};
550
551 const impl::NumberRangeFormatterImpl* getFormatter(UErrorCode& stauts) const;
552
553 explicit LocalizedNumberRangeFormatter(
554 const NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>& other);
555
556 explicit LocalizedNumberRangeFormatter(
557 NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>&& src) U_NOEXCEPT;
558
559 LocalizedNumberRangeFormatter(const impl::RangeMacroProps &macros, const Locale &locale);
560
561 LocalizedNumberRangeFormatter(impl::RangeMacroProps &&macros, const Locale &locale);
562
563 void clear();
564
565 // To give the fluent setters access to this class's constructor:
566 friend class NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>;
567 friend class NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>;
568
569 // To give UnlocalizedNumberRangeFormatter::locale() access to this class's constructor:
570 friend class UnlocalizedNumberRangeFormatter;
571 };
572
581 class U_I18N_API FormattedNumberRange : public UMemory, public FormattedValue {
582 public:
583 // Copybrief: this method is older than the parent method
591 UnicodeString toString(UErrorCode& status) const U_OVERRIDE;
592
593 // Copydoc: this method is new in ICU 64
595 UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE;
596
597 // Copybrief: this method is older than the parent method
605 Appendable &appendTo(Appendable &appendable, UErrorCode& status) const U_OVERRIDE;
606
607 // Copydoc: this method is new in ICU 64
609 UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE;
610
630 template<typename StringClass>
631 inline std::pair<StringClass, StringClass> getDecimalNumbers(UErrorCode& status) const;
632
642 UNumberRangeIdentityResult getIdentityResult(UErrorCode& status) const;
643
644 #ifndef U_HIDE_DRAFT_API
645
649 FormattedNumberRange()
650 : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
651 #endif /* U_HIDE_DRAFT_API */
652
656 FormattedNumberRange(const FormattedNumberRange&) = delete;
657
661 FormattedNumberRange& operator=(const FormattedNumberRange&) = delete;
662
668 FormattedNumberRange(FormattedNumberRange&& src) U_NOEXCEPT;
669
675 FormattedNumberRange& operator=(FormattedNumberRange&& src) U_NOEXCEPT;
676
681 ~FormattedNumberRange();
682
683 private:
684 // Can't use LocalPointer because UFormattedNumberRangeData is forward-declared
685 const impl::UFormattedNumberRangeData *fData;
686
687 // Error code for the terminal methods
688 UErrorCode fErrorCode;
689
693 explicit FormattedNumberRange(impl::UFormattedNumberRangeData *results)
694 : fData(results), fErrorCode(U_ZERO_ERROR) {}
695
696 explicit FormattedNumberRange(UErrorCode errorCode)
697 : fData(nullptr), fErrorCode(errorCode) {}
698
699 void getAllFieldPositionsImpl(FieldPositionIteratorHandler& fpih, UErrorCode& status) const;
700
701 void getDecimalNumbers(ByteSink& sink1, ByteSink& sink2, UErrorCode& status) const;
702
703 const impl::UFormattedNumberRangeData* getData(UErrorCode& status) const;
704
705 // To allow PluralRules to access the underlying data
706 friend class ::icu::PluralRules;
707
708 // To give LocalizedNumberRangeFormatter format methods access to this class's constructor:
709 friend class LocalizedNumberRangeFormatter;
710
711 // To give C API access to internals
712 friend struct impl::UFormattedNumberRangeImpl;
713 };
714
715 // inline impl of @stable ICU 68 method
716 template<typename StringClass>
717 std::pair<StringClass, StringClass> FormattedNumberRange::getDecimalNumbers(UErrorCode& status) const {
718 StringClass str1;
719 StringClass str2;
720 StringByteSink<StringClass> sink1(&str1);
721 StringByteSink<StringClass> sink2(&str2);
722 getDecimalNumbers(sink1, sink2, status);
723 return std::make_pair(str1, str2);
724 }
725
731 class U_I18N_API NumberRangeFormatter final {
732 public:
740 static UnlocalizedNumberRangeFormatter with();
741
751 static LocalizedNumberRangeFormatter withLocale(const Locale &locale);
752
756 NumberRangeFormatter() = delete;
757 };
758
759 } // namespace number
760 U_NAMESPACE_END
761
762 #endif /* #if !UCONFIG_NO_FORMATTING */
763
764 #endif /* U_SHOW_CPLUSPLUS_API */
765
766 #endif // __NUMBERRANGEFORMATTER_H__
767
fpositer.h
C++ API: FieldPosition Iterator.
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
U_I18N_API
#define U_I18N_API
Definition: utypes.h:301
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
U_NOEXCEPT
#define U_NOEXCEPT
Definition: platform.h:529
icu::Formattable
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:64
icu::number::UnlocalizedNumberRangeFormatter
A NumberRangeFormatter that does not yet have a locale.
Definition: numberrangeformatter.h:392
icu::FormattedValue
An abstract formatted value: a string with associated field attributes.
Definition: formattedvalue.h:241
icu::number::impl::RangeMacroProps::locale
Locale locale
Definition: numberrangeformatter.h:110
U_OVERRIDE
#define U_OVERRIDE
Definition: umachine.h:130
icu::LocalPointer
"Smart pointer" class, deletes objects via the standard C++ delete operator.
Definition: localpointer.h:191
icu::number::NumberFormatterSettings::copyErrorTo
UBool copyErrorTo(UErrorCode &outErrorCode) const
Sets the UErrorCode if an error occurred in the fluent chain.
Definition: numberformatter.h:2363
icu::number::impl::RangeMacroProps
Definition: numberrangeformatter.h:93
fieldpos.h
C++ API: FieldPosition identifies the fields in a formatted output.
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:295
U_FAILURE
#define U_FAILURE(x)
Does the error code indicate a failure?
Definition: utypes.h:717
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
icu::number::FormattedNumberRange
The result of a number range formatting operation.
Definition: numberrangeformatter.h:581
icu::number::UnlocalizedNumberFormatter
A NumberFormatter that does not yet have a locale.
Definition: numberformatter.h:2396
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:115
icu::number::NumberRangeFormatterSettings
An abstract base class for specifying settings related to number formatting.
Definition: numberrangeformatter.h:131
U_ZERO_ERROR
@ U_ZERO_ERROR
No error, no warning.
Definition: utypes.h:449
appendable.h
C++ API: Appendable class: Sink for Unicode code points and 16-bit code units (char16_ts).
icu::number::impl::RangeMacroProps::formatter2
UnlocalizedNumberFormatter formatter2
Definition: numberrangeformatter.h:98
formattedvalue.h
C++ API: Abstract operations for localized strings.
UNumberRangeCollapse
UNumberRangeCollapse
Defines how to merge fields that are identical across the range sign.
Definition: unumberrangeformatter.h:83
icu::Appendable
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:54
icu::number::NumberRangeFormatterSettings::copyErrorTo
UBool copyErrorTo(UErrorCode &outErrorCode) const
Sets the UErrorCode if an error occurred in the fluent chain.
Definition: numberrangeformatter.h:363
UNUM_RANGE_COLLAPSE_AUTO
@ UNUM_RANGE_COLLAPSE_AUTO
Use locale data and heuristics to determine how much of the string to collapse.
Definition: unumberrangeformatter.h:92
numberformatter.h
C++ API: All-in-one formatter for localized numbers, currencies, and units.
icu::number::NumberRangeFormatter
See the main description in numberrangeformatter.h for documentation and examples.
Definition: numberrangeformatter.h:731
icu::number::LocalizedNumberRangeFormatter
A NumberRangeFormatter that has a locale associated with it; this means .formatRange() methods are av...
Definition: numberrangeformatter.h:473
icu::StringByteSink
Implementation of ByteSink that writes to a "string".
Definition: bytestream.h:267
UNumberRangeIdentityFallback
UNumberRangeIdentityFallback
Defines the behavior when the two numbers in the range are identical after rounding.
Definition: unumberrangeformatter.h:125
icu::number::impl::RangeMacroProps::formatter1
UnlocalizedNumberFormatter formatter1
Definition: numberrangeformatter.h:95
unumberrangeformatter.h
C-compatible API for localized number range formatting.
UNumberRangeIdentityResult
UNumberRangeIdentityResult
Used in the result class FormattedNumberRange to indicate to the user whether the numbers formatted i...
Definition: unumberrangeformatter.h:165
U_INVALID_STATE_ERROR
@ U_INVALID_STATE_ERROR
Requested operation can not be completed with ICU in its current state.
Definition: utypes.h:478
UNUM_IDENTITY_FALLBACK_APPROXIMATELY
@ UNUM_IDENTITY_FALLBACK_APPROXIMATELY
Show the number using a locale-sensitive approximation pattern.
Definition: unumberrangeformatter.h:147
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
icu::number::impl::RangeMacroProps::copyErrorTo
bool copyErrorTo(UErrorCode &status) const
Check all members for errors.
Definition: numberrangeformatter.h:118
icu::number::FormattedNumberRange::FormattedNumberRange
FormattedNumberRange()
Default constructor; makes an empty FormattedNumberRange.
Definition: numberrangeformatter.h:649
icu::ConstrainedFieldPosition
Represents a span of a string containing a given field.
Definition: formattedvalue.h:41