ICU 63.2  63.2
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 #if !UCONFIG_NO_FORMATTING
5 #ifndef __NUMBERRANGEFORMATTER_H__
6 #define __NUMBERRANGEFORMATTER_H__
7 
8 #include <atomic>
9 #include "unicode/appendable.h"
10 #include "unicode/fieldpos.h"
11 #include "unicode/fpositer.h"
13 
14 #ifndef U_HIDE_DRAFT_API
15 
47 typedef enum UNumberRangeCollapse {
57 
64 
72 
81 
96 
104 
112 
121 
137 
145 
153 
154 #ifndef U_HIDE_INTERNAL_API
155 
160 #endif
161 
163 
165 
166 namespace number { // icu::number
167 
168 // Forward declarations:
169 class UnlocalizedNumberRangeFormatter;
170 class LocalizedNumberRangeFormatter;
171 class FormattedNumberRange;
172 
173 namespace impl {
174 
175 // Forward declarations:
176 struct RangeMacroProps;
177 class DecimalQuantity;
178 struct UFormattedNumberRangeData;
179 class NumberRangeFormatterImpl;
180 
181 } // namespace impl
182 
188 #if U_PLATFORM == U_PF_WINDOWS && !defined(U_IN_DOXYGEN)
189 } // namespace icu::number
191 
192 template struct U_I18N_API std::atomic< U_NAMESPACE_QUALIFIER number::impl::NumberRangeFormatterImpl*>;
193 
195 namespace number { // icu::number
196 #endif
197 
199 // Other helper classes would go here, but there are none.
200 
201 namespace impl { // icu::number::impl
202 
203 // Do not enclose entire MacroProps with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
207  UnlocalizedNumberFormatter formatter1; // = NumberFormatter::with();
208 
210  UnlocalizedNumberFormatter formatter2; // = NumberFormatter::with();
211 
213  bool singleFormatter = true;
214 
217 
220 
223 
224  // NOTE: Uses default copy and move constructors.
225 
230  bool copyErrorTo(UErrorCode &status) const {
231  return formatter1.copyErrorTo(status) || formatter2.copyErrorTo(status);
232  }
233 };
234 
235 } // namespace impl
236 
242 template<typename Derived>
244  public:
257  Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) const &;
258 
268  Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) &&;
269 
279  Derived numberFormatterBoth(UnlocalizedNumberFormatter &&formatter) const &;
280 
290  Derived numberFormatterBoth(UnlocalizedNumberFormatter &&formatter) &&;
291 
303  Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) const &;
304 
314  Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) &&;
315 
325  Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) const &;
326 
336  Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) &&;
337 
349  Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) const &;
350 
360  Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) &&;
361 
371  Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) const &;
372 
382  Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) &&;
383 
401  Derived collapse(UNumberRangeCollapse collapse) const &;
402 
412  Derived collapse(UNumberRangeCollapse collapse) &&;
413 
434  Derived identityFallback(UNumberRangeIdentityFallback identityFallback) const &;
435 
445  Derived identityFallback(UNumberRangeIdentityFallback identityFallback) &&;
446 
453  UBool copyErrorTo(UErrorCode &outErrorCode) const {
454  if (U_FAILURE(outErrorCode)) {
455  // Do not overwrite the older error code
456  return TRUE;
457  }
458  fMacros.copyErrorTo(outErrorCode);
459  return U_FAILURE(outErrorCode);
460  };
461 
462  // NOTE: Uses default copy and move constructors.
463 
464  private:
465  impl::RangeMacroProps fMacros;
466 
467  // Don't construct me directly! Use (Un)LocalizedNumberFormatter.
468  NumberRangeFormatterSettings() = default;
469 
470  friend class LocalizedNumberRangeFormatter;
471  friend class UnlocalizedNumberRangeFormatter;
472 };
473 
482 
483  public:
493  LocalizedNumberRangeFormatter locale(const icu::Locale &locale) const &;
494 
504  LocalizedNumberRangeFormatter locale(const icu::Locale &locale) &&;
505 
512 
518 
525 
531 
538 
539  private:
542 
545 
546  // To give the fluent setters access to this class's constructor:
548 
549  // To give NumberRangeFormatter::with() access to this class's constructor:
550  friend class NumberRangeFormatter;
551 };
552 
561  public:
575  FormattedNumberRange formatFormattableRange(
576  const Formattable& first, const Formattable& second, UErrorCode& status) const;
577 
583  LocalizedNumberRangeFormatter() = default;
584 
590 
597 
603 
610 
611 #ifndef U_HIDE_INTERNAL_API
612 
623  void formatImpl(impl::UFormattedNumberRangeData& results, bool equalBeforeRounding,
624  UErrorCode& status) const;
625 
626 #endif
627 
633 
634  private:
635  std::atomic<impl::NumberRangeFormatterImpl*> fAtomicFormatter = {};
636 
637  const impl::NumberRangeFormatterImpl* getFormatter(UErrorCode& stauts) const;
638 
641 
644 
645  LocalizedNumberRangeFormatter(const impl::RangeMacroProps &macros, const Locale &locale);
646 
648 
649  void clear();
650 
651  // To give the fluent setters access to this class's constructor:
654 
655  // To give UnlocalizedNumberRangeFormatter::locale() access to this class's constructor:
656  friend class UnlocalizedNumberRangeFormatter;
657 };
658 
666  public:
675  UnicodeString toString(UErrorCode& status) const;
676 
688  Appendable &appendTo(Appendable &appendable, UErrorCode& status) const;
689 
719  UBool nextFieldPosition(FieldPosition& fieldPosition, UErrorCode& status) const;
720 
734  void getAllFieldPositions(FieldPositionIterator &iterator, UErrorCode &status) const;
735 
750  UnicodeString getFirstDecimal(UErrorCode& status) const;
751 
766  UnicodeString getSecondDecimal(UErrorCode& status) const;
767 
777  UNumberRangeIdentityResult getIdentityResult(UErrorCode& status) const;
778 
783 
787  FormattedNumberRange& operator=(const FormattedNumberRange&) = delete;
788 
795 
802 
808 
809  private:
810  // Can't use LocalPointer because UFormattedNumberRangeData is forward-declared
811  const impl::UFormattedNumberRangeData *fResults;
812 
813  // Error code for the terminal methods
814  UErrorCode fErrorCode;
815 
820  explicit FormattedNumberRange(impl::UFormattedNumberRangeData *results)
821  : fResults(results), fErrorCode(U_ZERO_ERROR) {};
822 
823  explicit FormattedNumberRange(UErrorCode errorCode)
824  : fResults(nullptr), fErrorCode(errorCode) {};
825 
826  void getAllFieldPositionsImpl(FieldPositionIteratorHandler& fpih, UErrorCode& status) const;
827 
828  // To give LocalizedNumberRangeFormatter format methods access to this class's constructor:
829  friend class LocalizedNumberRangeFormatter;
830 };
831 
838  public:
846  static UnlocalizedNumberRangeFormatter with();
847 
857  static LocalizedNumberRangeFormatter withLocale(const Locale &locale);
858 
862  NumberRangeFormatter() = delete;
863 };
864 
865 } // namespace number
867 
868 #endif // U_HIDE_DRAFT_API
869 
870 #endif // __NUMBERRANGEFORMATTER_H__
871 
872 #endif /* #if !UCONFIG_NO_FORMATTING */
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:152
icu::FieldPosition
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:108
fpositer.h
C++ API: FieldPosition Iterator.
UNUM_IDENTITY_FALLBACK_APPROXIMATELY
Show the number using a locale-sensitive approximation pattern.
Definition: numberrangeformatter.h:111
U_I18N_API
#define U_I18N_API
Definition: utypes.h:301
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:225
U_NOEXCEPT
#define U_NOEXCEPT
Definition: platform.h:503
icu::Formattable
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:62
icu::number::UnlocalizedNumberRangeFormatter
A NumberRangeFormatter that does not yet have a locale.
Definition: numberrangeformatter.h:480
icu::FieldPositionIterator
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:55
icu::number::impl::RangeMacroProps::locale
Locale locale
Definition: numberrangeformatter.h:222
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:144
UNumberRangeIdentityResult
UNumberRangeIdentityResult
Used in the result class FormattedNumberRange to indicate to the user whether the numbers formatted i...
Definition: numberrangeformatter.h:129
icu::number::NumberFormatterSettings::copyErrorTo
UBool copyErrorTo(UErrorCode &outErrorCode) const
Sets the UErrorCode if an error occurred in the fluent chain.
Definition: numberformatter.h:2149
icu::number::impl::RangeMacroProps
Definition: numberrangeformatter.h:205
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:289
UNUM_IDENTITY_FALLBACK_APPROXIMATELY_OR_SINGLE_VALUE
Show the number using a locale-sensitive approximation pattern.
Definition: numberrangeformatter.h:103
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:136
U_FAILURE
#define U_FAILURE(x)
Does the error code indicate a failure?
Definition: utypes.h:699
UNUM_IDENTITY_FALLBACK_RANGE
Show the number as the range of two equal values.
Definition: numberrangeformatter.h:119
UErrorCode
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers,...
Definition: utypes.h:401
icu::number::FormattedNumberRange
The result of a number range formatting operation.
Definition: numberrangeformatter.h:665
TRUE
#define TRUE
The TRUE value of a UBool.
Definition: umachine.h:229
icu::number::UnlocalizedNumberFormatter
A NumberFormatter that does not yet have a locale.
Definition: numberformatter.h:2180
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:109
icu::number::NumberRangeFormatterSettings
An abstract base class for specifying settings related to number formatting.
Definition: numberrangeformatter.h:243
UNUM_IDENTITY_RESULT_COUNT
The number of entries in this enum.
Definition: numberrangeformatter.h:159
U_ZERO_ERROR
No error, no warning.
Definition: utypes.h:435
appendable.h
C++ API: Appendable class: Sink for Unicode code points and 16-bit code units (char16_ts).
UNUM_RANGE_COLLAPSE_UNIT
Collapse the unit part of the number, but not the notation, if present.
Definition: numberrangeformatter.h:71
icu::number::impl::RangeMacroProps::formatter2
UnlocalizedNumberFormatter formatter2
Definition: numberrangeformatter.h:210
icu::Appendable
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:51
icu::number::NumberRangeFormatterSettings::copyErrorTo
UBool copyErrorTo(UErrorCode &outErrorCode) const
Sets the UErrorCode if an error occurred in the fluent chain.
Definition: numberrangeformatter.h:453
numberformatter.h
C++ API: Library for localized number formatting introduced in ICU 60.
icu::number::NumberRangeFormatter
See the main description in numberrangeformatter.h for documentation and examples.
Definition: numberrangeformatter.h:837
UNUM_RANGE_COLLAPSE_AUTO
Use locale data and heuristics to determine how much of the string to collapse.
Definition: numberrangeformatter.h:56
UNUM_IDENTITY_FALLBACK_SINGLE_VALUE
Show the number as a single value rather than a range.
Definition: numberrangeformatter.h:95
icu::number::LocalizedNumberRangeFormatter
A NumberRangeFormatter that has a locale associated with it; this means .formatRange() methods are av...
Definition: numberrangeformatter.h:559
icu::number::impl::RangeMacroProps::formatter1
UnlocalizedNumberFormatter formatter1
Definition: numberrangeformatter.h:207
UNUM_RANGE_COLLAPSE_ALL
Collapse any field that is equal across the range sign.
Definition: numberrangeformatter.h:79
UNumberRangeCollapse
UNumberRangeCollapse
Defines how to merge fields that are identical across the range sign.
Definition: numberrangeformatter.h:47
U_NAMESPACE_END
#define U_NAMESPACE_END
Definition: uversion.h:138
U_NAMESPACE_BEGIN
#define U_NAMESPACE_BEGIN
Definition: uversion.h:137
UNUM_RANGE_COLLAPSE_NONE
Do not collapse any part of the number.
Definition: numberrangeformatter.h:63
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:192
icu::number::impl::RangeMacroProps::copyErrorTo
bool copyErrorTo(UErrorCode &status) const
Check all members for errors.
Definition: numberrangeformatter.h:230
UNumberRangeIdentityFallback
UNumberRangeIdentityFallback
Defines the behavior when the two numbers in the range are identical after rounding.
Definition: numberrangeformatter.h:89