ICU 64.2  64.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/formattedvalue.h"
12 #include "unicode/fpositer.h"
14 
15 #ifndef U_HIDE_DRAFT_API
16 
49 typedef enum UNumberRangeCollapse {
59 
66 
74 
83 
98 
106 
114 
123 
139 
147 
155 
156 #ifndef U_HIDE_INTERNAL_API
157 
162 #endif
163 
165 
167 
168 namespace number { // icu::number
169 
170 // Forward declarations:
171 class UnlocalizedNumberRangeFormatter;
172 class LocalizedNumberRangeFormatter;
173 class FormattedNumberRange;
174 
175 namespace impl {
176 
177 // Forward declarations:
178 struct RangeMacroProps;
179 class DecimalQuantity;
180 class UFormattedNumberRangeData;
181 class NumberRangeFormatterImpl;
182 
183 } // namespace impl
184 
190 #if U_PLATFORM == U_PF_WINDOWS && !defined(U_IN_DOXYGEN)
191 } // namespace icu::number
193 
194 template struct U_I18N_API std::atomic< U_NAMESPACE_QUALIFIER number::impl::NumberRangeFormatterImpl*>;
195 
197 namespace number { // icu::number
198 #endif
199 
201 // Other helper classes would go here, but there are none.
202 
203 namespace impl { // icu::number::impl
204 
205 // Do not enclose entire MacroProps with #ifndef U_HIDE_INTERNAL_API, needed for a protected field
209  UnlocalizedNumberFormatter formatter1; // = NumberFormatter::with();
210 
212  UnlocalizedNumberFormatter formatter2; // = NumberFormatter::with();
213 
215  bool singleFormatter = true;
216 
219 
222 
225 
226  // NOTE: Uses default copy and move constructors.
227 
232  bool copyErrorTo(UErrorCode &status) const {
233  return formatter1.copyErrorTo(status) || formatter2.copyErrorTo(status);
234  }
235 };
236 
237 } // namespace impl
238 
244 template<typename Derived>
246  public:
259  Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) const &;
260 
270  Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) &&;
271 
281  Derived numberFormatterBoth(UnlocalizedNumberFormatter &&formatter) const &;
282 
292  Derived numberFormatterBoth(UnlocalizedNumberFormatter &&formatter) &&;
293 
305  Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) const &;
306 
316  Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) &&;
317 
327  Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) const &;
328 
338  Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) &&;
339 
351  Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) const &;
352 
362  Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) &&;
363 
373  Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) const &;
374 
384  Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) &&;
385 
403  Derived collapse(UNumberRangeCollapse collapse) const &;
404 
414  Derived collapse(UNumberRangeCollapse collapse) &&;
415 
436  Derived identityFallback(UNumberRangeIdentityFallback identityFallback) const &;
437 
447  Derived identityFallback(UNumberRangeIdentityFallback identityFallback) &&;
448 
460  LocalPointer<Derived> clone() const &;
461 
469  LocalPointer<Derived> clone() &&;
470 
477  UBool copyErrorTo(UErrorCode &outErrorCode) const {
478  if (U_FAILURE(outErrorCode)) {
479  // Do not overwrite the older error code
480  return TRUE;
481  }
482  fMacros.copyErrorTo(outErrorCode);
483  return U_FAILURE(outErrorCode);
484  }
485 
486  // NOTE: Uses default copy and move constructors.
487 
488  private:
489  impl::RangeMacroProps fMacros;
490 
491  // Don't construct me directly! Use (Un)LocalizedNumberFormatter.
492  NumberRangeFormatterSettings() = default;
493 
494  friend class LocalizedNumberRangeFormatter;
495  friend class UnlocalizedNumberRangeFormatter;
496 };
497 
508 
509  public:
519  LocalizedNumberRangeFormatter locale(const icu::Locale &locale) const &;
520 
530  LocalizedNumberRangeFormatter locale(const icu::Locale &locale) &&;
531 
538 
544 
551 
557 
564 
565  private:
568 
571 
572  // To give the fluent setters access to this class's constructor:
574 
575  // To give NumberRangeFormatter::with() access to this class's constructor:
576  friend class NumberRangeFormatter;
577 };
578 
589  public:
603  FormattedNumberRange formatFormattableRange(
604  const Formattable& first, const Formattable& second, UErrorCode& status) const;
605 
611  LocalizedNumberRangeFormatter() = default;
612 
618 
625 
631 
638 
639 #ifndef U_HIDE_INTERNAL_API
640 
651  void formatImpl(impl::UFormattedNumberRangeData& results, bool equalBeforeRounding,
652  UErrorCode& status) const;
653 
654 #endif
655 
661 
662  private:
663  std::atomic<impl::NumberRangeFormatterImpl*> fAtomicFormatter = {};
664 
665  const impl::NumberRangeFormatterImpl* getFormatter(UErrorCode& stauts) const;
666 
669 
672 
673  LocalizedNumberRangeFormatter(const impl::RangeMacroProps &macros, const Locale &locale);
674 
676 
677  void clear();
678 
679  // To give the fluent setters access to this class's constructor:
682 
683  // To give UnlocalizedNumberRangeFormatter::locale() access to this class's constructor:
684  friend class UnlocalizedNumberRangeFormatter;
685 };
686 
696  public:
697  // Copybrief: this method is older than the parent method
705  UnicodeString toString(UErrorCode& status) const U_OVERRIDE;
706 
707  // Copydoc: this method is new in ICU 64
709  UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE;
710 
711  // Copybrief: this method is older than the parent method
719  Appendable &appendTo(Appendable &appendable, UErrorCode& status) const U_OVERRIDE;
720 
721  // Copydoc: this method is new in ICU 64
723  UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE;
724 
754  UBool nextFieldPosition(FieldPosition& fieldPosition, UErrorCode& status) const;
755 
769  void getAllFieldPositions(FieldPositionIterator &iterator, UErrorCode &status) const;
770 
785  UnicodeString getFirstDecimal(UErrorCode& status) const;
786 
801  UnicodeString getSecondDecimal(UErrorCode& status) const;
802 
812  UNumberRangeIdentityResult getIdentityResult(UErrorCode& status) const;
813 
818 
822  FormattedNumberRange& operator=(const FormattedNumberRange&) = delete;
823 
830 
837 
843 
844  private:
845  // Can't use LocalPointer because UFormattedNumberRangeData is forward-declared
846  const impl::UFormattedNumberRangeData *fData;
847 
848  // Error code for the terminal methods
849  UErrorCode fErrorCode;
850 
855  explicit FormattedNumberRange(impl::UFormattedNumberRangeData *results)
856  : fData(results), fErrorCode(U_ZERO_ERROR) {}
857 
858  explicit FormattedNumberRange(UErrorCode errorCode)
859  : fData(nullptr), fErrorCode(errorCode) {}
860 
861  void getAllFieldPositionsImpl(FieldPositionIteratorHandler& fpih, UErrorCode& status) const;
862 
863  // To give LocalizedNumberRangeFormatter format methods access to this class's constructor:
864  friend class LocalizedNumberRangeFormatter;
865 };
866 
873  public:
881  static UnlocalizedNumberRangeFormatter with();
882 
892  static LocalizedNumberRangeFormatter withLocale(const Locale &locale);
893 
897  NumberRangeFormatter() = delete;
898 };
899 
900 } // namespace number
902 
903 #endif // U_HIDE_DRAFT_API
904 
905 #endif // __NUMBERRANGEFORMATTER_H__
906 
907 #endif /* #if !UCONFIG_NO_FORMATTING */
C++ API: Abstract operations for localized strings.
Collapse the unit part of the number, but not the notation, if present.
#define U_OVERRIDE
Defined to the C++11 "override" keyword if available.
Definition: umachine.h:129
#define U_FAILURE(x)
Does the error code indicate a failure?
Definition: utypes.h:695
The result of a number range formatting operation.
C++ API: FieldPosition Iterator.
Used to indicate that the two numbers in the range were equal, even before any rounding rules were ap...
"Smart pointer" class, deletes objects via the standard C++ delete operator.
Definition: localpointer.h:191
See the main description in numberrangeformatter.h for documentation and examples.
A NumberRangeFormatter that has a locale associated with it; this means .formatRange() methods are av...
Show the number using a locale-sensitive approximation pattern.
C++ API: Appendable class: Sink for Unicode code points and 16-bit code units (char16_ts).
UNumberRangeIdentityResult
Used in the result class FormattedNumberRange to indicate to the user whether the numbers formatted i...
No error, no warning.
Definition: utypes.h:435
Used to indicate that the two numbers in the range were equal, but only after rounding rules were app...
An abstract base class for specifying settings related to number formatting.
An abstract formatted value: a string with associated field attributes.
Used to indicate that the two numbers in the range were not equal, even after rounding rules were app...
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:301
C++ API: FieldPosition identifies the fields in a formatted output.
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
Show the number using a locale-sensitive approximation pattern.
UNumberRangeCollapse
Defines how to merge fields that are identical across the range sign.
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:55
Show the number as the range of two equal values.
UBool copyErrorTo(UErrorCode &outErrorCode) const
Sets the UErrorCode if an error occurred in the fluent chain.
UBool copyErrorTo(UErrorCode &outErrorCode) const
Sets the UErrorCode if an error occurred in the fluent chain.
Use locale data and heuristics to determine how much of the string to collapse.
A NumberRangeFormatter that does not yet have a locale.
Do not collapse any part of the number.
#define TRUE
The TRUE value of a UBool.
Definition: umachine.h:229
Show the number as a single value rather than a range.
Represents a span of a string containing a given field.
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:503
The number of entries in this enum.
A NumberFormatter that does not yet have a locale.
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
UnlocalizedNumberFormatter formatter2
UNumberRangeIdentityFallback
Defines the behavior when the two numbers in the range are identical after rounding.
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:401
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:108
UnlocalizedNumberFormatter formatter1
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:289
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:62
UMemory is the common ICU base class.
Definition: uobject.h:112
C++ API: Library for localized number formatting introduced in ICU 60.
Collapse any field that is equal across the range sign.
bool copyErrorTo(UErrorCode &status) const
Check all members for errors.
int8_t UBool
The ICU boolean type.
Definition: umachine.h:225
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:51
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:192