ICU 63.1  63.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 #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_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN && !defined(U_IN_DOXYGEN)
189 template struct U_I18N_API std::atomic<impl::NumberRangeFormatterImpl*>;
190 #endif
191 
193 // Other helper classes would go here, but there are none.
194 
195 namespace impl { // icu::number::impl
196 
197 // Do not enclose entire MacroProps with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
201  UnlocalizedNumberFormatter formatter1; // = NumberFormatter::with();
202 
204  UnlocalizedNumberFormatter formatter2; // = NumberFormatter::with();
205 
207  bool singleFormatter = true;
208 
211 
214 
217 
218  // NOTE: Uses default copy and move constructors.
219 
224  bool copyErrorTo(UErrorCode &status) const {
225  return formatter1.copyErrorTo(status) || formatter2.copyErrorTo(status);
226  }
227 };
228 
229 } // namespace impl
230 
236 template<typename Derived>
238  public:
251  Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) const &;
252 
262  Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) &&;
263 
273  Derived numberFormatterBoth(UnlocalizedNumberFormatter &&formatter) const &;
274 
285 
297  Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) const &;
298 
308  Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) &&;
309 
319  Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) const &;
320 
330  Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) &&;
331 
343  Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) const &;
344 
354  Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) &&;
355 
365  Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) const &;
366 
376  Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) &&;
377 
395  Derived collapse(UNumberRangeCollapse collapse) const &;
396 
406  Derived collapse(UNumberRangeCollapse collapse) &&;
407 
428  Derived identityFallback(UNumberRangeIdentityFallback identityFallback) const &;
429 
439  Derived identityFallback(UNumberRangeIdentityFallback identityFallback) &&;
440 
447  UBool copyErrorTo(UErrorCode &outErrorCode) const {
448  if (U_FAILURE(outErrorCode)) {
449  // Do not overwrite the older error code
450  return TRUE;
451  }
452  fMacros.copyErrorTo(outErrorCode);
453  return U_FAILURE(outErrorCode);
454  };
455 
456  // NOTE: Uses default copy and move constructors.
457 
458  private:
459  impl::RangeMacroProps fMacros;
460 
461  // Don't construct me directly! Use (Un)LocalizedNumberFormatter.
462  NumberRangeFormatterSettings() = default;
463 
464  friend class LocalizedNumberRangeFormatter;
465  friend class UnlocalizedNumberRangeFormatter;
466 };
467 
476 
477  public:
488 
499 
506 
512 
519 
525 
532 
533  private:
536 
539 
540  // To give the fluent setters access to this class's constructor:
542 
543  // To give NumberRangeFormatter::with() access to this class's constructor:
544  friend class NumberRangeFormatter;
545 };
546 
555  public:
570  const Formattable& first, const Formattable& second, UErrorCode& status) const;
571 
578 
584 
591 
597 
604 
605 #ifndef U_HIDE_INTERNAL_API
606 
617  void formatImpl(impl::UFormattedNumberRangeData& results, bool equalBeforeRounding,
618  UErrorCode& status) const;
619 
620 #endif
621 
627 
628  private:
629  std::atomic<impl::NumberRangeFormatterImpl*> fAtomicFormatter = {};
630 
631  const impl::NumberRangeFormatterImpl* getFormatter(UErrorCode& stauts) const;
632 
635 
638 
639  LocalizedNumberRangeFormatter(const impl::RangeMacroProps &macros, const Locale &locale);
640 
642 
643  void clear();
644 
645  // To give the fluent setters access to this class's constructor:
648 
649  // To give UnlocalizedNumberRangeFormatter::locale() access to this class's constructor:
650  friend class UnlocalizedNumberRangeFormatter;
651 };
652 
660  public:
670 
682  Appendable &appendTo(Appendable &appendable, UErrorCode& status) const;
683 
713  UBool nextFieldPosition(FieldPosition& fieldPosition, UErrorCode& status) const;
714 
728  void getAllFieldPositions(FieldPositionIterator &iterator, UErrorCode &status) const;
729 
745 
761 
772 
777 
782 
789 
796 
802 
803  private:
804  // Can't use LocalPointer because UFormattedNumberRangeData is forward-declared
805  const impl::UFormattedNumberRangeData *fResults;
806 
807  // Error code for the terminal methods
808  UErrorCode fErrorCode;
809 
814  explicit FormattedNumberRange(impl::UFormattedNumberRangeData *results)
815  : fResults(results), fErrorCode(U_ZERO_ERROR) {};
816 
817  explicit FormattedNumberRange(UErrorCode errorCode)
818  : fResults(nullptr), fErrorCode(errorCode) {};
819 
820  void getAllFieldPositionsImpl(FieldPositionIteratorHandler& fpih, UErrorCode& status) const;
821 
822  // To give LocalizedNumberRangeFormatter format methods access to this class's constructor:
823  friend class LocalizedNumberRangeFormatter;
824 };
825 
832  public:
841 
852 
857 };
858 
859 } // namespace number
861 
862 #endif // U_HIDE_DRAFT_API
863 
864 #endif // __NUMBERRANGEFORMATTER_H__
865 
866 #endif /* #if !UCONFIG_NO_FORMATTING */
icu::number::FormattedNumberRange::getSecondDecimal
UnicodeString getSecondDecimal(UErrorCode &status) const
Export the second formatted number as a decimal number.
UNUM_IDENTITY_RESULT_NOT_EQUAL
@ 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::number::UnlocalizedNumberRangeFormatter::locale
LocalizedNumberRangeFormatter locale(const icu::Locale &locale) &&
Overload of locale() for use on an rvalue reference.
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.
icu::number::FormattedNumberRange::FormattedNumberRange
FormattedNumberRange(const FormattedNumberRange &)=delete
Copying not supported; use move constructor instead.
icu::number::LocalizedNumberRangeFormatter::formatFormattableRange
FormattedNumberRange formatFormattableRange(const Formattable &first, const Formattable &second, UErrorCode &status) const
Format the given Formattables to a string using the settings specified in the NumberRangeFormatter fl...
UNUM_IDENTITY_FALLBACK_APPROXIMATELY
@ UNUM_IDENTITY_FALLBACK_APPROXIMATELY
Show the number using a locale-sensitive approximation pattern.
Definition: numberrangeformatter.h:111
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
icu::number::NumberRangeFormatter::NumberRangeFormatter
NumberRangeFormatter()=delete
Use factory methods instead of the constructor to create a NumberFormatter.
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:225
U_NOEXCEPT
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
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:475
icu::FieldPositionIterator
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:55
icu::number::NumberRangeFormatterSettings::collapse
Derived collapse(UNumberRangeCollapse collapse) &&
Overload of collapse() for use on an rvalue reference.
icu::number::NumberRangeFormatterSettings::numberFormatterFirst
Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) const &
Overload of numberFormatterFirst() for use on an rvalue reference.
icu::number::FormattedNumberRange::~FormattedNumberRange
~FormattedNumberRange()
Destruct an instance of FormattedNumberRange, cleaning up any memory it might own.
icu::number::NumberRangeFormatter::with
static UnlocalizedNumberRangeFormatter with()
Call this method at the beginning of a NumberRangeFormatter fluent chain in which the locale is not c...
icu::number::NumberRangeFormatterSettings::numberFormatterFirst
Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) &&
Overload of numberFormatterFirst() for use on an rvalue reference.
icu::number::impl::RangeMacroProps::locale
Locale locale
Definition: numberrangeformatter.h:216
UNUM_IDENTITY_RESULT_EQUAL_AFTER_ROUNDING
@ 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
icu::number::NumberRangeFormatterSettings::identityFallback
Derived identityFallback(UNumberRangeIdentityFallback identityFallback) &&
Overload of identityFallback() for use on an rvalue reference.
icu::number::LocalizedNumberRangeFormatter::operator=
LocalizedNumberRangeFormatter & operator=(LocalizedNumberRangeFormatter &&src) U_NOEXCEPT
Move assignment operator: The source LocalizedNumberRangeFormatter will be left in a valid but undefi...
icu::number::FormattedNumberRange::appendTo
Appendable & appendTo(Appendable &appendable, UErrorCode &status) const
Appends the formatted number range to an Appendable.
icu::number::FormattedNumberRange::toString
UnicodeString toString(UErrorCode &status) const
Returns a UnicodeString representation of the formatted number range.
UNumberRangeIdentityResult
UNumberRangeIdentityResult
Used in the result class FormattedNumberRange to indicate to the user whether the numbers formatted i...
Definition: numberrangeformatter.h:129
icu::number::NumberRangeFormatterSettings::collapse
Derived collapse(UNumberRangeCollapse collapse) const &
Sets the aggressiveness of "collapsing" fields across the range separator.
icu::number::UnlocalizedNumberRangeFormatter::operator=
UnlocalizedNumberRangeFormatter & operator=(UnlocalizedNumberRangeFormatter &&src) U_NOEXCEPT
Move assignment operator: The source UnlocalizedNumberRangeFormatter will be left in a valid but unde...
icu::number::NumberRangeFormatterSettings::identityFallback
Derived identityFallback(UNumberRangeIdentityFallback identityFallback) const &
Sets the behavior when the two sides of the range are the same.
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::LocalizedNumberRangeFormatter::LocalizedNumberRangeFormatter
LocalizedNumberRangeFormatter(const LocalizedNumberRangeFormatter &other)
Returns a copy of this LocalizedNumberRangeFormatter.
icu::number::FormattedNumberRange::FormattedNumberRange
FormattedNumberRange(FormattedNumberRange &&src) U_NOEXCEPT
Move constructor: Leaves the source FormattedNumberRange in an undefined state.
icu::number::UnlocalizedNumberRangeFormatter::operator=
UnlocalizedNumberRangeFormatter & operator=(const UnlocalizedNumberRangeFormatter &other)
Copy assignment operator.
icu::number::impl::RangeMacroProps
Definition: numberrangeformatter.h:199
icu::number::NumberRangeFormatterSettings::numberFormatterSecond
Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) &&
Overload of numberFormatterSecond() for use on an rvalue reference.
icu::number::UnlocalizedNumberRangeFormatter::locale
LocalizedNumberRangeFormatter locale(const icu::Locale &locale) const &
Associate the given locale with the number range formatter.
fieldpos.h
C++ API: FieldPosition identifies the fields in a formatted output.
icu::number::NumberRangeFormatterSettings::numberFormatterBoth
Derived numberFormatterBoth(UnlocalizedNumberFormatter &&formatter) const &
Overload of numberFormatterBoth() for use on an rvalue reference.
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:290
icu::number::FormattedNumberRange::operator=
FormattedNumberRange & operator=(FormattedNumberRange &&src) U_NOEXCEPT
Move assignment: Leaves the source FormattedNumberRange in an undefined state.
UNUM_IDENTITY_FALLBACK_APPROXIMATELY_OR_SINGLE_VALUE
@ 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
@ 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
icu::number::FormattedNumberRange::nextFieldPosition
UBool nextFieldPosition(FieldPosition &fieldPosition, UErrorCode &status) const
Determines the start (inclusive) and end (exclusive) indices of the next occurrence of the given fiel...
icu::number::NumberRangeFormatterSettings::numberFormatterSecond
Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) const &
Overload of numberFormatterSecond() for use on an rvalue reference.
UNUM_IDENTITY_FALLBACK_RANGE
@ 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::NumberRangeFormatterSettings::numberFormatterBoth
Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) const &
Sets the NumberFormatter instance to use for the numbers in the range.
icu::number::UnlocalizedNumberRangeFormatter::UnlocalizedNumberRangeFormatter
UnlocalizedNumberRangeFormatter()=default
Default constructor: puts the formatter into a valid but undefined state.
icu::number::FormattedNumberRange
The result of a number range formatting operation.
Definition: numberrangeformatter.h:659
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:2181
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:237
icu::number::LocalizedNumberRangeFormatter::LocalizedNumberRangeFormatter
LocalizedNumberRangeFormatter()=default
Default constructor: puts the formatter into a valid but undefined state.
icu::number::FormattedNumberRange::getFirstDecimal
UnicodeString getFirstDecimal(UErrorCode &status) const
Export the first formatted number as a decimal number.
UNUM_IDENTITY_RESULT_COUNT
@ UNUM_IDENTITY_RESULT_COUNT
The number of entries in this enum.
Definition: numberrangeformatter.h:159
icu::number::FormattedNumberRange::getIdentityResult
UNumberRangeIdentityResult getIdentityResult(UErrorCode &status) const
Returns whether the pair of numbers was successfully formatted as a range or whether an identity fall...
icu::number::NumberRangeFormatterSettings::numberFormatterSecond
Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) &&
Overload of numberFormatterSecond() for use on an rvalue reference.
U_ZERO_ERROR
@ 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
@ UNUM_RANGE_COLLAPSE_UNIT
Collapse the unit part of the number, but not the notation, if present.
Definition: numberrangeformatter.h:71
icu::number::LocalizedNumberRangeFormatter::operator=
LocalizedNumberRangeFormatter & operator=(const LocalizedNumberRangeFormatter &other)
Copy assignment operator.
icu::number::NumberRangeFormatterSettings::numberFormatterFirst
Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) const &
Sets the NumberFormatter instance to use for the first number in the range.
icu::number::UnlocalizedNumberRangeFormatter::UnlocalizedNumberRangeFormatter
UnlocalizedNumberRangeFormatter(const UnlocalizedNumberRangeFormatter &other)
Returns a copy of this UnlocalizedNumberRangeFormatter.
icu::number::impl::RangeMacroProps::formatter2
UnlocalizedNumberFormatter formatter2
Definition: numberrangeformatter.h:204
icu::number::NumberRangeFormatterSettings::numberFormatterFirst
Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) &&
Overload of numberFormatterFirst() for use on an rvalue reference.
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:447
numberformatter.h
C++ API: Library for localized number formatting introduced in ICU 60.
icu::number::FormattedNumberRange::operator=
FormattedNumberRange & operator=(const FormattedNumberRange &)=delete
Copying not supported; use move assignment instead.
icu::number::LocalizedNumberRangeFormatter::LocalizedNumberRangeFormatter
LocalizedNumberRangeFormatter(LocalizedNumberRangeFormatter &&src) U_NOEXCEPT
Move constructor: The source LocalizedNumberRangeFormatter will be left in a valid but undefined stat...
icu::number::NumberRangeFormatter
See the main description in numberrangeformatter.h for documentation and examples.
Definition: numberrangeformatter.h:831
icu::number::UnlocalizedNumberRangeFormatter::UnlocalizedNumberRangeFormatter
UnlocalizedNumberRangeFormatter(UnlocalizedNumberRangeFormatter &&src) U_NOEXCEPT
Move constructor: The source UnlocalizedNumberRangeFormatter will be left in a valid but undefined st...
UNUM_RANGE_COLLAPSE_AUTO
@ UNUM_RANGE_COLLAPSE_AUTO
Use locale data and heuristics to determine how much of the string to collapse.
Definition: numberrangeformatter.h:56
icu::number::LocalizedNumberRangeFormatter::~LocalizedNumberRangeFormatter
~LocalizedNumberRangeFormatter()
Destruct this LocalizedNumberRangeFormatter, cleaning up any memory it might own.
UNUM_IDENTITY_FALLBACK_SINGLE_VALUE
@ 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:554
icu::number::impl::RangeMacroProps::formatter1
UnlocalizedNumberFormatter formatter1
Definition: numberrangeformatter.h:201
UNUM_RANGE_COLLAPSE_ALL
@ UNUM_RANGE_COLLAPSE_ALL
Collapse any field that is equal across the range sign.
Definition: numberrangeformatter.h:79
icu::number::NumberRangeFormatterSettings::numberFormatterBoth
Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) &&
Overload of numberFormatterBoth() for use on an rvalue reference.
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
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
U_NAMESPACE_BEGIN
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
icu::number::NumberRangeFormatterSettings::numberFormatterSecond
Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) const &
Sets the NumberFormatter instance to use for the second number in the range.
icu::number::NumberRangeFormatterSettings::numberFormatterBoth
Derived numberFormatterBoth(UnlocalizedNumberFormatter &&formatter) &&
Overload of numberFormatterBoth() for use on an rvalue reference.
icu::number::LocalizedNumberRangeFormatter::formatImpl
void formatImpl(impl::UFormattedNumberRangeData &results, bool equalBeforeRounding, UErrorCode &status) const
UNUM_RANGE_COLLAPSE_NONE
@ 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:224
UNumberRangeIdentityFallback
UNumberRangeIdentityFallback
Defines the behavior when the two numbers in the range are identical after rounding.
Definition: numberrangeformatter.h:89
icu::number::NumberRangeFormatter::withLocale
static LocalizedNumberRangeFormatter withLocale(const Locale &locale)
Call this method at the beginning of a NumberRangeFormatter fluent chain in which the locale is known...
icu::number::FormattedNumberRange::getAllFieldPositions
void getAllFieldPositions(FieldPositionIterator &iterator, UErrorCode &status) const
Export the formatted number range to a FieldPositionIterator.