ICU 74.1 74.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"
17#include "unicode/fpositer.h"
20
48U_NAMESPACE_BEGIN
49
50// Forward declarations:
51class PluralRules;
52
53namespace number { // icu::number
54
55// Forward declarations:
56class UnlocalizedNumberRangeFormatter;
57class LocalizedNumberRangeFormatter;
58class FormattedNumberRange;
59
60namespace impl {
61
62// Forward declarations:
63struct RangeMacroProps;
64class DecimalQuantity;
65class UFormattedNumberRangeData;
66class NumberRangeFormatterImpl;
67struct 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
78U_NAMESPACE_END
79
80template struct U_I18N_API std::atomic< U_NAMESPACE_QUALIFIER number::impl::NumberRangeFormatterImpl*>;
81
82U_NAMESPACE_BEGIN
83namespace number { // icu::number
84#endif
87// Other helper classes would go here, but there are none.
88
89namespace 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
130template<typename Derived>
132 public:
145 Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) const &;
146
157
168
179
191 Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) const &;
192
202 Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) &&;
203
213 Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) const &;
214
225
237 Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) const &;
238
248 Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) &&;
249
259 Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) const &;
260
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
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.
379
382};
383
384// Explicit instantiations in source/i18n/numrange_fluent.cpp.
385// (MSVC treats imports/exports of explicit instantiations differently.)
386#ifndef _MSC_VER
387extern template class NumberRangeFormatterSettings<UnlocalizedNumberRangeFormatter>;
388extern template class NumberRangeFormatterSettings<LocalizedNumberRangeFormatter>;
389#endif
390
401
402 public:
413
424
431
437
444
450
457
458 private:
461
464
465 // To give the fluent setters access to this class's constructor:
467
468 // To give NumberRangeFormatter::with() access to this class's constructor:
469 friend class NumberRangeFormatter;
470};
471
482 public:
497 const Formattable& first, const Formattable& second, UErrorCode& status) const;
498
505
511
518
524
531
532#ifndef U_HIDE_INTERNAL_API
533
544 void formatImpl(impl::UFormattedNumberRangeData& results, bool equalBeforeRounding,
545 UErrorCode& status) const;
546
547#endif /* U_HIDE_INTERNAL_API */
548
554
555 private:
556 std::atomic<impl::NumberRangeFormatterImpl*> fAtomicFormatter = {};
557
558 const impl::NumberRangeFormatterImpl* getFormatter(UErrorCode& stauts) const;
559
562
565
566 LocalizedNumberRangeFormatter(const impl::RangeMacroProps &macros, const Locale &locale);
567
569
570 // To give the fluent setters access to this class's constructor:
573
574 // To give UnlocalizedNumberRangeFormatter::locale() access to this class's constructor:
576};
577
587 public:
588 // Copybrief: this method is older than the parent method
596 UnicodeString toString(UErrorCode& status) const override;
597
598 // Copydoc: this method is new in ICU 64
600 UnicodeString toTempString(UErrorCode& status) const override;
601
602 // Copybrief: this method is older than the parent method
610 Appendable &appendTo(Appendable &appendable, UErrorCode& status) const override;
611
612 // Copydoc: this method is new in ICU 64
614 UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const override;
615
635 template<typename StringClass>
636 inline std::pair<StringClass, StringClass> getDecimalNumbers(UErrorCode& status) const;
637
648
654 : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
655
660
665
672
679
685
686 private:
687 // Can't use LocalPointer because UFormattedNumberRangeData is forward-declared
688 const impl::UFormattedNumberRangeData *fData;
689
690 // Error code for the terminal methods
691 UErrorCode fErrorCode;
692
696 explicit FormattedNumberRange(impl::UFormattedNumberRangeData *results)
697 : fData(results), fErrorCode(U_ZERO_ERROR) {}
698
699 explicit FormattedNumberRange(UErrorCode errorCode)
700 : fData(nullptr), fErrorCode(errorCode) {}
701
702 void getDecimalNumbers(ByteSink& sink1, ByteSink& sink2, UErrorCode& status) const;
703
704 const impl::UFormattedNumberRangeData* getData(UErrorCode& status) const;
705
706 // To allow PluralRules to access the underlying data
707 friend class ::icu::PluralRules;
708
709 // To give LocalizedNumberRangeFormatter format methods access to this class's constructor:
710 friend class LocalizedNumberRangeFormatter;
711
712 // To give C API access to internals
713 friend struct impl::UFormattedNumberRangeImpl;
714};
715
716// inline impl of @stable ICU 68 method
717template<typename StringClass>
718std::pair<StringClass, StringClass> FormattedNumberRange::getDecimalNumbers(UErrorCode& status) const {
719 StringClass str1;
720 StringClass str2;
721 StringByteSink<StringClass> sink1(&str1);
722 StringByteSink<StringClass> sink2(&str2);
723 getDecimalNumbers(sink1, sink2, status);
724 return std::make_pair(str1, str2);
725}
726
733 public:
742
753
758};
759
760} // namespace number
761U_NAMESPACE_END
762
763#endif /* #if !UCONFIG_NO_FORMATTING */
764
765#endif /* U_SHOW_CPLUSPLUS_API */
766
767#endif // __NUMBERRANGEFORMATTER_H__
768
C++ API: Appendable class: Sink for Unicode code points and 16-bit code units (char16_ts).
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:54
A ByteSink can be filled with bytes.
Definition: bytestream.h:53
Represents a span of a string containing a given field.
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:64
An abstract formatted value: a string with associated field attributes.
"Smart pointer" class, deletes objects via the standard C++ delete operator.
Definition: localpointer.h:191
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
Implementation of ByteSink that writes to a "string".
Definition: bytestream.h:267
UMemory is the common ICU base class.
Definition: uobject.h:115
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
The result of a number range formatting operation.
UnicodeString toTempString(UErrorCode &status) const override
Returns the formatted string as a read-only alias to memory owned by the FormattedValue.
UnicodeString toString(UErrorCode &status) const override
Returns the formatted string as a self-contained UnicodeString.
FormattedNumberRange(const FormattedNumberRange &)=delete
Copying not supported; use move constructor instead.
UBool nextPosition(ConstrainedFieldPosition &cfpos, UErrorCode &status) const override
Iterates over field positions in the FormattedValue.
UNumberRangeIdentityResult getIdentityResult(UErrorCode &status) const
Returns whether the pair of numbers was successfully formatted as a range or whether an identity fall...
Appendable & appendTo(Appendable &appendable, UErrorCode &status) const override
Appends the formatted string to an Appendable.
FormattedNumberRange(FormattedNumberRange &&src) noexcept
Move constructor: Leaves the source FormattedNumberRange in an undefined state.
FormattedNumberRange()
Default constructor; makes an empty FormattedNumberRange.
FormattedNumberRange & operator=(FormattedNumberRange &&src) noexcept
Move assignment: Leaves the source FormattedNumberRange in an undefined state.
~FormattedNumberRange()
Destruct an instance of FormattedNumberRange, cleaning up any memory it might own.
FormattedNumberRange & operator=(const FormattedNumberRange &)=delete
Copying not supported; use move assignment instead.
A NumberRangeFormatter that has a locale associated with it; this means .formatRange() methods are av...
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...
LocalizedNumberRangeFormatter & operator=(const LocalizedNumberRangeFormatter &other)
Copy assignment operator.
LocalizedNumberRangeFormatter()=default
Default constructor: puts the formatter into a valid but undefined state.
LocalizedNumberRangeFormatter(const LocalizedNumberRangeFormatter &other)
Returns a copy of this LocalizedNumberRangeFormatter.
void formatImpl(impl::UFormattedNumberRangeData &results, bool equalBeforeRounding, UErrorCode &status) const
~LocalizedNumberRangeFormatter()
Destruct this LocalizedNumberRangeFormatter, cleaning up any memory it might own.
LocalizedNumberRangeFormatter(LocalizedNumberRangeFormatter &&src) noexcept
Move constructor: The source LocalizedNumberRangeFormatter will be left in a valid but undefined stat...
LocalizedNumberRangeFormatter & operator=(LocalizedNumberRangeFormatter &&src) noexcept
Move assignment operator: The source LocalizedNumberRangeFormatter will be left in a valid but undefi...
UBool copyErrorTo(UErrorCode &outErrorCode) const
Sets the UErrorCode if an error occurred in the fluent chain.
An abstract base class for specifying settings related to number formatting.
Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) &&
Overload of numberFormatterSecond() for use on an rvalue reference.
LocalPointer< Derived > clone() const &
Returns the current (Un)LocalizedNumberRangeFormatter as a LocalPointer wrapping a heap-allocated cop...
Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) const &
Overload of numberFormatterFirst() for use on an rvalue reference.
Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) &&
Overload of numberFormatterBoth() for use on an rvalue reference.
Derived numberFormatterFirst(UnlocalizedNumberFormatter &&formatterFirst) &&
Overload of numberFormatterFirst() for use on an rvalue reference.
Derived numberFormatterBoth(UnlocalizedNumberFormatter &&formatter) &&
Overload of numberFormatterBoth() for use on an rvalue reference.
Derived numberFormatterBoth(const UnlocalizedNumberFormatter &formatter) const &
Sets the NumberFormatter instance to use for the numbers in the range.
Derived numberFormatterBoth(UnlocalizedNumberFormatter &&formatter) const &
Overload of numberFormatterBoth() for use on an rvalue reference.
Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) &&
Overload of numberFormatterFirst() for use on an rvalue reference.
Derived identityFallback(UNumberRangeIdentityFallback identityFallback) const &
Sets the behavior when the two sides of the range are the same.
Derived numberFormatterFirst(const UnlocalizedNumberFormatter &formatterFirst) const &
Sets the NumberFormatter instance to use for the first number in the range.
Derived numberFormatterSecond(UnlocalizedNumberFormatter &&formatterSecond) const &
Overload of numberFormatterSecond() for use on an rvalue reference.
Derived collapse(UNumberRangeCollapse collapse) &&
Overload of collapse() for use on an rvalue reference.
Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) const &
Sets the NumberFormatter instance to use for the second number in the range.
Derived collapse(UNumberRangeCollapse collapse) const &
Sets the aggressiveness of "collapsing" fields across the range separator.
Derived identityFallback(UNumberRangeIdentityFallback identityFallback) &&
Overload of identityFallback() for use on an rvalue reference.
Derived numberFormatterSecond(const UnlocalizedNumberFormatter &formatterSecond) &&
Overload of numberFormatterSecond() for use on an rvalue reference.
See the main description in numberrangeformatter.h for documentation and examples.
static UnlocalizedNumberRangeFormatter with()
Call this method at the beginning of a NumberRangeFormatter fluent chain in which the locale is not c...
NumberRangeFormatter()=delete
Use factory methods instead of the constructor to create a NumberFormatter.
static LocalizedNumberRangeFormatter withLocale(const Locale &locale)
Call this method at the beginning of a NumberRangeFormatter fluent chain in which the locale is known...
A NumberFormatter that does not yet have a locale.
A NumberRangeFormatter that does not yet have a locale.
UnlocalizedNumberRangeFormatter(const UnlocalizedNumberRangeFormatter &other)
Returns a copy of this UnlocalizedNumberRangeFormatter.
LocalizedNumberRangeFormatter locale(const icu::Locale &locale) const &
Associate the given locale with the number range formatter.
UnlocalizedNumberRangeFormatter & operator=(const UnlocalizedNumberRangeFormatter &other)
Copy assignment operator.
LocalizedNumberRangeFormatter locale(const icu::Locale &locale) &&
Overload of locale() for use on an rvalue reference.
UnlocalizedNumberRangeFormatter(UnlocalizedNumberRangeFormatter &&src) noexcept
Move constructor: The source UnlocalizedNumberRangeFormatter will be left in a valid but undefined st...
UnlocalizedNumberRangeFormatter()=default
Default constructor: puts the formatter into a valid but undefined state.
UnlocalizedNumberRangeFormatter & operator=(UnlocalizedNumberRangeFormatter &&src) noexcept
Move assignment operator: The source UnlocalizedNumberRangeFormatter will be left in a valid but unde...
C++ API: FieldPosition identifies the fields in a formatted output.
C++ API: Abstract operations for localized strings.
C++ API: FieldPosition Iterator.
C++ API: All-in-one formatter for localized numbers, currencies, and units.
UnlocalizedNumberFormatter formatter2
UnlocalizedNumberFormatter formatter1
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
C API: Localized number range formatting.
UNumberRangeIdentityResult
Used in the result class FormattedNumberRange to indicate to the user whether the numbers formatted i...
UNumberRangeIdentityFallback
Defines the behavior when the two numbers in the range are identical after rounding.
@ UNUM_IDENTITY_FALLBACK_APPROXIMATELY
Show the number using a locale-sensitive approximation pattern.
UNumberRangeCollapse
Defines how to merge fields that are identical across the range sign.
@ UNUM_RANGE_COLLAPSE_AUTO
Use locale data and heuristics to determine how much of the string to collapse.
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
@ U_INVALID_STATE_ERROR
Requested operation can not be completed with ICU in its current state.
Definition: utypes.h:478
@ U_ZERO_ERROR
No error, no warning.
Definition: utypes.h:449
#define U_FAILURE(x)
Does the error code indicate a failure?
Definition: utypes.h:717
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:301