ICU 76.1  76.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"
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
95  UnlocalizedNumberFormatter formatter1; // = NumberFormatter::with();
96 
98  UnlocalizedNumberFormatter formatter2; // = NumberFormatter::with();
99 
101  bool singleFormatter = true;
102 
105 
108 
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>
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 
384 // Explicit instantiations in source/i18n/numrange_fluent.cpp.
385 // (MSVC treats imports/exports of explicit instantiations differently.)
386 #ifndef _MSC_VER
387 extern template class NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>;
388 extern template class NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>;
389 #endif
390 
401 
402  public:
412  LocalizedNumberRangeFormatter locale(const icu::Locale &locale) const &;
413 
423  LocalizedNumberRangeFormatter locale(const icu::Locale &locale) &&;
424 
431 
437 
444 
450 
457 
458  private:
461 
464 
466 
468 
469  // To give the fluent setters access to this class's constructor:
471 
472  // To give NumberRangeFormatter::with() access to this class's constructor:
473  friend class NumberRangeFormatter;
474 
475  // To give LNRF::withoutLocale() access to this class's constructor:
476  friend class LocalizedNumberRangeFormatter;
477 };
478 
489  public:
503  FormattedNumberRange formatFormattableRange(
504  const Formattable& first, const Formattable& second, UErrorCode& status) const;
505 
506 #ifndef U_HIDE_DRAFT_API
507 
513  UnlocalizedNumberRangeFormatter withoutLocale() const &;
514 
522  UnlocalizedNumberRangeFormatter withoutLocale() &&;
523 #endif // U_HIDE_DRAFT_API
524 
530  LocalizedNumberRangeFormatter() = default;
531 
537 
544 
550 
557 
558 #ifndef U_HIDE_INTERNAL_API
559 
570  void formatImpl(impl::UFormattedNumberRangeData& results, bool equalBeforeRounding,
571  UErrorCode& status) const;
572 
573 #endif /* U_HIDE_INTERNAL_API */
574 
580 
581  private:
582  std::atomic<impl::NumberRangeFormatterImpl*> fAtomicFormatter = {};
583 
584  const impl::NumberRangeFormatterImpl* getFormatter(UErrorCode& stauts) const;
585 
588 
591 
592  LocalizedNumberRangeFormatter(const impl::RangeMacroProps &macros, const Locale &locale);
593 
595 
596  // To give the fluent setters access to this class's constructor:
599 
600  // To give UnlocalizedNumberRangeFormatter::locale() access to this class's constructor:
601  friend class UnlocalizedNumberRangeFormatter;
602 };
603 
613  public:
614  // Copybrief: this method is older than the parent method
622  UnicodeString toString(UErrorCode& status) const override;
623 
624  // Copydoc: this method is new in ICU 64
626  UnicodeString toTempString(UErrorCode& status) const override;
627 
628  // Copybrief: this method is older than the parent method
636  Appendable &appendTo(Appendable &appendable, UErrorCode& status) const override;
637 
638  // Copydoc: this method is new in ICU 64
640  UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const override;
641 
661  template<typename StringClass>
662  inline std::pair<StringClass, StringClass> getDecimalNumbers(UErrorCode& status) const;
663 
673  UNumberRangeIdentityResult getIdentityResult(UErrorCode& status) const;
674 
680  : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
681 
686 
690  FormattedNumberRange& operator=(const FormattedNumberRange&) = delete;
691 
698 
704  FormattedNumberRange& operator=(FormattedNumberRange&& src) noexcept;
705 
711 
712  private:
713  // Can't use LocalPointer because UFormattedNumberRangeData is forward-declared
714  const impl::UFormattedNumberRangeData *fData;
715 
716  // Error code for the terminal methods
717  UErrorCode fErrorCode;
718 
722  explicit FormattedNumberRange(impl::UFormattedNumberRangeData *results)
723  : fData(results), fErrorCode(U_ZERO_ERROR) {}
724 
725  explicit FormattedNumberRange(UErrorCode errorCode)
726  : fData(nullptr), fErrorCode(errorCode) {}
727 
728  void getDecimalNumbers(ByteSink& sink1, ByteSink& sink2, UErrorCode& status) const;
729 
730  const impl::UFormattedNumberRangeData* getData(UErrorCode& status) const;
731 
732  // To allow PluralRules to access the underlying data
733  friend class ::icu::PluralRules;
734 
735  // To give LocalizedNumberRangeFormatter format methods access to this class's constructor:
736  friend class LocalizedNumberRangeFormatter;
737 
738  // To give C API access to internals
739  friend struct impl::UFormattedNumberRangeImpl;
740 };
741 
742 // inline impl of @stable ICU 68 method
743 template<typename StringClass>
744 std::pair<StringClass, StringClass> FormattedNumberRange::getDecimalNumbers(UErrorCode& status) const {
745  StringClass str1;
746  StringClass str2;
747  StringByteSink<StringClass> sink1(&str1);
748  StringByteSink<StringClass> sink2(&str2);
749  getDecimalNumbers(sink1, sink2, status);
750  return std::make_pair(str1, str2);
751 }
752 
759  public:
767  static UnlocalizedNumberRangeFormatter with();
768 
778  static LocalizedNumberRangeFormatter withLocale(const Locale &locale);
779 
783  NumberRangeFormatter() = delete;
784 };
785 
786 } // namespace number
787 U_NAMESPACE_END
788 
789 #endif /* #if !UCONFIG_NO_FORMATTING */
790 
791 #endif /* U_SHOW_CPLUSPLUS_API */
792 
793 #endif // __NUMBERRANGEFORMATTER_H__
794 
C++ API: Abstract operations for localized strings.
#define U_FAILURE(x)
Does the error code indicate a failure?
Definition: utypes.h:747
The result of a number range formatting operation.
C++ API: FieldPosition Iterator.
"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...
C++ API: Appendable class: Sink for Unicode code points and 16-bit code units (char16_ts).
No error, no warning.
Definition: utypes.h:465
An abstract base class for specifying settings related to number formatting.
An abstract formatted value: a string with associated field attributes.
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:316
C++ API: FieldPosition identifies the fields in a formatted output.
C API: Localized number range formatting.
UNumberRangeIdentityResult
Used in the result class FormattedNumberRange to indicate to the user whether the numbers formatted i...
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.
A NumberRangeFormatter that does not yet have a locale.
Represents a span of a string containing a given field.
A NumberFormatter that does not yet have a locale.
UnlocalizedNumberFormatter formatter2
Use locale data and heuristics to determine how much of the string to collapse.
UNumberRangeCollapse
Defines how to merge fields that are identical across the range sign.
FormattedNumberRange()
Default constructor; makes an empty FormattedNumberRange.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:430
UnlocalizedNumberFormatter formatter1
Basic definitions for ICU, for both C and C++ APIs.
Implementation of ByteSink that writes to a "string".
Definition: bytestream.h:267
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:295
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:63
Show the number using a locale-sensitive approximation pattern.
UNumberRangeIdentityFallback
Defines the behavior when the two numbers in the range are identical after rounding.
UMemory is the common ICU base class.
Definition: uobject.h:115
C++ API: All-in-one formatter for localized numbers, currencies, and units.
Requested operation can not be completed with ICU in its current state.
Definition: utypes.h:494
bool copyErrorTo(UErrorCode &status) const
Check all members for errors.
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:247
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:54
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195