ICU 67.1 67.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
52 typedef enum UNumberRangeCollapse {
61 UNUM_RANGE_COLLAPSE_AUTO,
62
68 UNUM_RANGE_COLLAPSE_NONE,
69
76 UNUM_RANGE_COLLAPSE_UNIT,
77
84 UNUM_RANGE_COLLAPSE_ALL
85 } UNumberRangeCollapse;
86
94 typedef enum UNumberRangeIdentityFallback {
100 UNUM_IDENTITY_FALLBACK_SINGLE_VALUE,
101
108 UNUM_IDENTITY_FALLBACK_APPROXIMATELY_OR_SINGLE_VALUE,
109
116 UNUM_IDENTITY_FALLBACK_APPROXIMATELY,
117
124 UNUM_IDENTITY_FALLBACK_RANGE
125 } UNumberRangeIdentityFallback;
126
134 typedef enum UNumberRangeIdentityResult {
141 UNUM_IDENTITY_RESULT_EQUAL_BEFORE_ROUNDING,
142
149 UNUM_IDENTITY_RESULT_EQUAL_AFTER_ROUNDING,
150
157 UNUM_IDENTITY_RESULT_NOT_EQUAL,
158
159 #ifndef U_HIDE_INTERNAL_API
160
164 UNUM_IDENTITY_RESULT_COUNT
165 #endif
166
167 } UNumberRangeIdentityResult;
168
169 U_NAMESPACE_BEGIN
170
171 namespace number { // icu::number
172
173 // Forward declarations:
174 class UnlocalizedNumberRangeFormatter;
175 class LocalizedNumberRangeFormatter;
176 class FormattedNumberRange;
177
178 namespace impl {
179
180 // Forward declarations:
181 struct RangeMacroProps;
182 class DecimalQuantity;
183 class UFormattedNumberRangeData;
184 class NumberRangeFormatterImpl;
185
186 } // namespace impl
187
193 #if U_PLATFORM == U_PF_WINDOWS && !defined(U_IN_DOXYGEN)
194 } // namespace icu::number
195 U_NAMESPACE_END
196
197 template struct U_I18N_API std::atomic< U_NAMESPACE_QUALIFIER number::impl::NumberRangeFormatterImpl*>;
198
199 U_NAMESPACE_BEGIN
200 namespace number { // icu::number
201 #endif
202
204 // Other helper classes would go here, but there are none.
205
206 namespace impl { // icu::number::impl
207
208 // Do not enclose entire MacroProps with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
210 struct U_I18N_API RangeMacroProps : public UMemory {
212 UnlocalizedNumberFormatter formatter1; // = NumberFormatter::with();
213
215 UnlocalizedNumberFormatter formatter2; // = NumberFormatter::with();
216
218 bool singleFormatter = true;
219
221 UNumberRangeCollapse collapse = UNUM_RANGE_COLLAPSE_AUTO;
222
224 UNumberRangeIdentityFallback identityFallback = UNUM_IDENTITY_FALLBACK_APPROXIMATELY;
225
227 Locale locale;
228
229 // NOTE: Uses default copy and move constructors.
230
235 bool copyErrorTo(UErrorCode &status) const {
236 return formatter1.copyErrorTo(status) || formatter2.copyErrorTo(status);
237 }
238 };
239
240 } // namespace impl
241
247 template<typename Derived>
248 class U_I18N_API NumberRangeFormatterSettings {
249 public:
262 Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) const &;
263
273 Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) &&;
274
284 Derived numberFormatterBoth(UnlocalizedNumberFormatter &&formatter) const &;
285
295 Derived numberFormatterBoth(UnlocalizedNumberFormatter &&formatter) &&;
296
308 Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) const &;
309
319 Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) &&;
320
330 Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) const &;
331
341 Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) &&;
342
354 Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) const &;
355
365 Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) &&;
366
376 Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) const &;
377
387 Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) &&;
388
406 Derived collapse(UNumberRangeCollapse collapse) const &;
407
417 Derived collapse(UNumberRangeCollapse collapse) &&;
418
439 Derived identityFallback(UNumberRangeIdentityFallback identityFallback) const &;
440
450 Derived identityFallback(UNumberRangeIdentityFallback identityFallback) &&;
451
463 LocalPointer<Derived> clone() const &;
464
472 LocalPointer<Derived> clone() &&;
473
480 UBool copyErrorTo(UErrorCode &outErrorCode) const {
481 if (U_FAILURE(outErrorCode)) {
482 // Do not overwrite the older error code
483 return TRUE;
484 }
485 fMacros.copyErrorTo(outErrorCode);
486 return U_FAILURE(outErrorCode);
487 }
488
489 // NOTE: Uses default copy and move constructors.
490
491 private:
492 impl::RangeMacroProps fMacros;
493
494 // Don't construct me directly! Use (Un)LocalizedNumberFormatter.
495 NumberRangeFormatterSettings() = default;
496
497 friend class LocalizedNumberRangeFormatter;
498 friend class UnlocalizedNumberRangeFormatter;
499 };
500
509 class U_I18N_API UnlocalizedNumberRangeFormatter
510 : public NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>, public UMemory {
511
512 public:
522 LocalizedNumberRangeFormatter locale(const icu::Locale &locale) const &;
523
533 LocalizedNumberRangeFormatter locale(const icu::Locale &locale) &&;
534
540 UnlocalizedNumberRangeFormatter() = default;
541
546 UnlocalizedNumberRangeFormatter(const UnlocalizedNumberRangeFormatter &other);
547
553 UnlocalizedNumberRangeFormatter(UnlocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
554
559 UnlocalizedNumberRangeFormatter& operator=(const UnlocalizedNumberRangeFormatter& other);
560
566 UnlocalizedNumberRangeFormatter& operator=(UnlocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
567
568 private:
569 explicit UnlocalizedNumberRangeFormatter(
570 const NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>& other);
571
572 explicit UnlocalizedNumberRangeFormatter(
573 NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>&& src) U_NOEXCEPT;
574
575 // To give the fluent setters access to this class's constructor:
576 friend class NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>;
577
578 // To give NumberRangeFormatter::with() access to this class's constructor:
579 friend class NumberRangeFormatter;
580 };
581
590 class U_I18N_API LocalizedNumberRangeFormatter
591 : public NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>, public UMemory {
592 public:
606 FormattedNumberRange formatFormattableRange(
607 const Formattable& first, const Formattable& second, UErrorCode& status) const;
608
614 LocalizedNumberRangeFormatter() = default;
615
620 LocalizedNumberRangeFormatter(const LocalizedNumberRangeFormatter &other);
621
627 LocalizedNumberRangeFormatter(LocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
628
633 LocalizedNumberRangeFormatter& operator=(const LocalizedNumberRangeFormatter& other);
634
640 LocalizedNumberRangeFormatter& operator=(LocalizedNumberRangeFormatter&& src) U_NOEXCEPT;
641
642 #ifndef U_HIDE_INTERNAL_API
643
654 void formatImpl(impl::UFormattedNumberRangeData& results, bool equalBeforeRounding,
655 UErrorCode& status) const;
656
657 #endif /* U_HIDE_INTERNAL_API */
658
663 ~LocalizedNumberRangeFormatter();
664
665 private:
666 std::atomic<impl::NumberRangeFormatterImpl*> fAtomicFormatter = {};
667
668 const impl::NumberRangeFormatterImpl* getFormatter(UErrorCode& stauts) const;
669
670 explicit LocalizedNumberRangeFormatter(
671 const NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>& other);
672
673 explicit LocalizedNumberRangeFormatter(
674 NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>&& src) U_NOEXCEPT;
675
676 LocalizedNumberRangeFormatter(const impl::RangeMacroProps &macros, const Locale &locale);
677
678 LocalizedNumberRangeFormatter(impl::RangeMacroProps &&macros, const Locale &locale);
679
680 void clear();
681
682 // To give the fluent setters access to this class's constructor:
683 friend class NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>;
684 friend class NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>;
685
686 // To give UnlocalizedNumberRangeFormatter::locale() access to this class's constructor:
687 friend class UnlocalizedNumberRangeFormatter;
688 };
689
698 class U_I18N_API FormattedNumberRange : public UMemory, public FormattedValue {
699 public:
700 // Copybrief: this method is older than the parent method
708 UnicodeString toString(UErrorCode& status) const U_OVERRIDE;
709
710 // Copydoc: this method is new in ICU 64
712 UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE;
713
714 // Copybrief: this method is older than the parent method
722 Appendable &appendTo(Appendable &appendable, UErrorCode& status) const U_OVERRIDE;
723
724 // Copydoc: this method is new in ICU 64
726 UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE;
727
728 #ifndef U_HIDE_DRAFT_API
729
743 UnicodeString getFirstDecimal(UErrorCode& status) const;
744
759 UnicodeString getSecondDecimal(UErrorCode& status) const;
760 #endif // U_HIDE_DRAFT_API
761
771 UNumberRangeIdentityResult getIdentityResult(UErrorCode& status) const;
772
776 FormattedNumberRange(const FormattedNumberRange&) = delete;
777
781 FormattedNumberRange& operator=(const FormattedNumberRange&) = delete;
782
788 FormattedNumberRange(FormattedNumberRange&& src) U_NOEXCEPT;
789
795 FormattedNumberRange& operator=(FormattedNumberRange&& src) U_NOEXCEPT;
796
801 ~FormattedNumberRange();
802
803 private:
804 // Can't use LocalPointer because UFormattedNumberRangeData is forward-declared
805 const impl::UFormattedNumberRangeData *fData;
806
807 // Error code for the terminal methods
808 UErrorCode fErrorCode;
809
813 explicit FormattedNumberRange(impl::UFormattedNumberRangeData *results)
814 : fData(results), fErrorCode(U_ZERO_ERROR) {}
815
816 explicit FormattedNumberRange(UErrorCode errorCode)
817 : fData(nullptr), fErrorCode(errorCode) {}
818
819 void getAllFieldPositionsImpl(FieldPositionIteratorHandler& fpih, UErrorCode& status) const;
820
821 // To give LocalizedNumberRangeFormatter format methods access to this class's constructor:
822 friend class LocalizedNumberRangeFormatter;
823 };
824
830 class U_I18N_API NumberRangeFormatter final {
831 public:
839 static UnlocalizedNumberRangeFormatter with();
840
850 static LocalizedNumberRangeFormatter withLocale(const Locale &locale);
851
855 NumberRangeFormatter() = delete;
856 };
857
858 } // namespace number
859 U_NAMESPACE_END
860
861 #endif /* #if !UCONFIG_NO_FORMATTING */
862
863 #endif /* U_SHOW_CPLUSPLUS_API */
864
865 #endif // __NUMBERRANGEFORMATTER_H__
866
formattedvalue.h
C++ API: Abstract operations for localized strings.
UNUM_RANGE_COLLAPSE_UNIT
Collapse the unit part of the number, but not the notation, if present.
Definition: numberrangeformatter.h:76
U_OVERRIDE
#define U_OVERRIDE
Defined to the C++11 "override" keyword if available.
Definition: umachine.h:129
U_FAILURE
#define U_FAILURE(x)
Does the error code indicate a failure?
Definition: utypes.h:709
icu::number::FormattedNumberRange
The result of a number range formatting operation.
Definition: numberrangeformatter.h:698
fpositer.h
C++ API: FieldPosition Iterator.
UNUM_IDENTITY_RESULT_EQUAL_BEFORE_ROUNDING
Used to indicate that the two numbers in the range were equal, even before any rounding rules were ap...
Definition: numberrangeformatter.h:141
icu::LocalPointer
"Smart pointer" class, deletes objects via the standard C++ delete operator.
Definition: localpointer.h:191
icu::number::NumberRangeFormatter
See the main description in numberrangeformatter.h for documentation and examples.
Definition: numberrangeformatter.h:830
icu::number::LocalizedNumberRangeFormatter
A NumberRangeFormatter that has a locale associated with it; this means .formatRange() methods are av...
Definition: numberrangeformatter.h:590
UNUM_IDENTITY_FALLBACK_APPROXIMATELY
Show the number using a locale-sensitive approximation pattern.
Definition: numberrangeformatter.h:116
appendable.h
C++ API: Appendable class: Sink for Unicode code points and 16-bit code units (char16_ts).
icu::number::impl::RangeMacroProps::locale
Locale locale
Definition: numberrangeformatter.h:227
UNumberRangeIdentityResult
UNumberRangeIdentityResult
Used in the result class FormattedNumberRange to indicate to the user whether the numbers formatted i...
Definition: numberrangeformatter.h:134
U_ZERO_ERROR
No error, no warning.
Definition: utypes.h:449
UNUM_IDENTITY_RESULT_EQUAL_AFTER_ROUNDING
Used to indicate that the two numbers in the range were equal, but only after rounding rules were app...
Definition: numberrangeformatter.h:149
icu::number::NumberRangeFormatterSettings
An abstract base class for specifying settings related to number formatting.
Definition: numberrangeformatter.h:248
icu::FormattedValue
An abstract formatted value: a string with associated field attributes.
Definition: formattedvalue.h:241
UNUM_IDENTITY_RESULT_NOT_EQUAL
Used to indicate that the two numbers in the range were not equal, even after rounding rules were app...
Definition: numberrangeformatter.h:157
U_I18N_API
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:301
fieldpos.h
C++ API: FieldPosition identifies the fields in a formatted output.
icu::number::impl::RangeMacroProps
Definition: numberrangeformatter.h:210
UNUM_IDENTITY_FALLBACK_APPROXIMATELY_OR_SINGLE_VALUE
Show the number using a locale-sensitive approximation pattern.
Definition: numberrangeformatter.h:108
UNumberRangeCollapse
UNumberRangeCollapse
Defines how to merge fields that are identical across the range sign.
Definition: numberrangeformatter.h:52
UNUM_IDENTITY_FALLBACK_RANGE
Show the number as the range of two equal values.
Definition: numberrangeformatter.h:124
icu::number::NumberRangeFormatterSettings::copyErrorTo
UBool copyErrorTo(UErrorCode &outErrorCode) const
Sets the UErrorCode if an error occurred in the fluent chain.
Definition: numberrangeformatter.h:480
icu::number::NumberFormatterSettings::copyErrorTo
UBool copyErrorTo(UErrorCode &outErrorCode) const
Sets the UErrorCode if an error occurred in the fluent chain.
Definition: numberformatter.h:2126
UNUM_RANGE_COLLAPSE_AUTO
Use locale data and heuristics to determine how much of the string to collapse.
Definition: numberrangeformatter.h:61
icu::number::UnlocalizedNumberRangeFormatter
A NumberRangeFormatter that does not yet have a locale.
Definition: numberrangeformatter.h:509
UNUM_RANGE_COLLAPSE_NONE
Do not collapse any part of the number.
Definition: numberrangeformatter.h:68
TRUE
#define TRUE
The TRUE value of a UBool.
Definition: umachine.h:265
UNUM_IDENTITY_FALLBACK_SINGLE_VALUE
Show the number as a single value rather than a range.
Definition: numberrangeformatter.h:100
icu::ConstrainedFieldPosition
Represents a span of a string containing a given field.
Definition: formattedvalue.h:41
U_NOEXCEPT
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:529
UNUM_IDENTITY_RESULT_COUNT
The number of entries in this enum.
Definition: numberrangeformatter.h:164
icu::number::UnlocalizedNumberFormatter
A NumberFormatter that does not yet have a locale.
Definition: numberformatter.h:2159
icu::number::impl::RangeMacroProps::formatter2
UnlocalizedNumberFormatter formatter2
Definition: numberrangeformatter.h:215
UNumberRangeIdentityFallback
UNumberRangeIdentityFallback
Defines the behavior when the two numbers in the range are identical after rounding.
Definition: numberrangeformatter.h:94
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
icu::number::impl::RangeMacroProps::formatter1
UnlocalizedNumberFormatter formatter1
Definition: numberrangeformatter.h:212
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:294
icu::Formattable
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:64
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:115
numberformatter.h
C++ API: Library for localized number formatting introduced in ICU 60.
UNUM_RANGE_COLLAPSE_ALL
Collapse any field that is equal across the range sign.
Definition: numberrangeformatter.h:84
icu::number::impl::RangeMacroProps::copyErrorTo
bool copyErrorTo(UErrorCode &status) const
Check all members for errors.
Definition: numberrangeformatter.h:235
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:261
icu::Appendable
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:54
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195