ICU 73.2  73.2
simplenumberformatter.h
Go to the documentation of this file.
00001 // © 2022 and later: Unicode, Inc. and others.
00002 // License & terms of use: http://www.unicode.org/copyright.html
00003 
00004 #ifndef __SIMPLENUMBERFORMATTERH__
00005 #define __SIMPLENUMBERFORMATTERH__
00006 
00007 #include "unicode/utypes.h"
00008 
00009 #if U_SHOW_CPLUSPLUS_API
00010 
00011 #if !UCONFIG_NO_FORMATTING
00012 
00013 #include "unicode/dcfmtsym.h"
00014 #include "unicode/usimplenumberformatter.h"
00015 #include "unicode/formattednumber.h"
00016 
00031 U_NAMESPACE_BEGIN
00032 
00033 
00034 namespace number {  // icu::number
00035 
00036 
00037 namespace impl {
00038 class UFormattedNumberData;
00039 struct SimpleMicroProps;
00040 class AdoptingSignumModifierStore;
00041 }  // icu::number::impl
00042 
00043 
00044 #ifndef U_HIDE_DRAFT_API
00045 
00046 
00054 class U_I18N_API SimpleNumber : public UMemory {
00055   public:
00061     static SimpleNumber forInt64(int64_t value, UErrorCode& status);
00062 
00070     void multiplyByPowerOfTen(int32_t power, UErrorCode& status);
00071 
00079     void roundTo(int32_t power, UNumberFormatRoundingMode roundingMode, UErrorCode& status);
00080 
00088     void truncateStart(uint32_t maximumIntegerDigits, UErrorCode& status);
00089 
00097     void setMinimumIntegerDigits(uint32_t minimumIntegerDigits, UErrorCode& status);
00098 
00106     void setMinimumFractionDigits(uint32_t minimumFractionDigits, UErrorCode& status);
00107 
00117     void setSign(USimpleNumberSign sign, UErrorCode& status);
00118 
00126     SimpleNumber() = default;
00127 
00133     ~SimpleNumber() {
00134         cleanup();
00135     }
00136 
00142     SimpleNumber(SimpleNumber&& other) noexcept {
00143         fData = other.fData;
00144         fSign = other.fSign;
00145         other.fData = nullptr;
00146     }
00147 
00153     SimpleNumber& operator=(SimpleNumber&& other) noexcept {
00154         cleanup();
00155         fData = other.fData;
00156         fSign = other.fSign;
00157         other.fData = nullptr;
00158         return *this;
00159     }
00160 
00161   private:
00162     SimpleNumber(impl::UFormattedNumberData* data, UErrorCode& status);
00163     SimpleNumber(const SimpleNumber&) = delete;
00164     SimpleNumber& operator=(const SimpleNumber&) = delete;
00165 
00166     void cleanup();
00167 
00168     impl::UFormattedNumberData* fData = nullptr;
00169     USimpleNumberSign fSign = UNUM_SIMPLE_NUMBER_NO_SIGN;
00170 
00171     friend class SimpleNumberFormatter;
00172 };
00173 
00174 
00185 class U_I18N_API SimpleNumberFormatter : public UMemory {
00186   public:
00192     static SimpleNumberFormatter forLocale(
00193         const icu::Locale &locale,
00194         UErrorCode &status);
00195 
00201     static SimpleNumberFormatter forLocaleAndGroupingStrategy(
00202         const icu::Locale &locale,
00203         UNumberGroupingStrategy groupingStrategy,
00204         UErrorCode &status);
00205 
00214     static SimpleNumberFormatter forLocaleAndSymbolsAndGroupingStrategy(
00215         const icu::Locale &locale,
00216         const DecimalFormatSymbols &symbols,
00217         UNumberGroupingStrategy groupingStrategy,
00218         UErrorCode &status);
00219 
00228     FormattedNumber format(SimpleNumber value, UErrorCode &status) const;
00229 
00237     FormattedNumber formatInt64(int64_t value, UErrorCode &status) const {
00238         return format(SimpleNumber::forInt64(value, status), status);
00239     }
00240 
00241 #ifndef U_HIDE_INTERNAL_API
00242 
00246     void formatImpl(impl::UFormattedNumberData* data, USimpleNumberSign sign, UErrorCode& status) const;
00247 #endif // U_HIDE_INTERNAL_API
00248 
00254     ~SimpleNumberFormatter() {
00255         cleanup();
00256     }
00257 
00263     SimpleNumberFormatter() = default;
00264 
00270     SimpleNumberFormatter(SimpleNumberFormatter&& other) noexcept {
00271         fGroupingStrategy = other.fGroupingStrategy;
00272         fOwnedSymbols = other.fOwnedSymbols;
00273         fMicros = other.fMicros;
00274         fPatternModifier = other.fPatternModifier;
00275         other.fOwnedSymbols = nullptr;
00276         other.fMicros = nullptr;
00277         other.fPatternModifier = nullptr;
00278     }
00279 
00285     SimpleNumberFormatter& operator=(SimpleNumberFormatter&& other) noexcept {
00286         cleanup();
00287         fGroupingStrategy = other.fGroupingStrategy;
00288         fOwnedSymbols = other.fOwnedSymbols;
00289         fMicros = other.fMicros;
00290         fPatternModifier = other.fPatternModifier;
00291         other.fOwnedSymbols = nullptr;
00292         other.fMicros = nullptr;
00293         other.fPatternModifier = nullptr;
00294         return *this;
00295     }
00296 
00297   private:
00298     void initialize(
00299         const icu::Locale &locale,
00300         const DecimalFormatSymbols &symbols,
00301         UNumberGroupingStrategy groupingStrategy,
00302         UErrorCode &status);
00303 
00304     void cleanup();
00305 
00306     SimpleNumberFormatter(const SimpleNumberFormatter&) = delete;
00307 
00308     SimpleNumberFormatter& operator=(const SimpleNumberFormatter&) = delete;
00309 
00310     UNumberGroupingStrategy fGroupingStrategy = UNUM_GROUPING_AUTO;
00311 
00312     // Owned Pointers:
00313     DecimalFormatSymbols* fOwnedSymbols = nullptr; // can be empty
00314     impl::SimpleMicroProps* fMicros = nullptr;
00315     impl::AdoptingSignumModifierStore* fPatternModifier = nullptr;
00316 };
00317 
00318 
00319 #endif // U_HIDE_DRAFT_API
00320 
00321 }  // namespace number
00322 U_NAMESPACE_END
00323 
00324 #endif /* #if !UCONFIG_NO_FORMATTING */
00325 
00326 #endif /* U_SHOW_CPLUSPLUS_API */
00327 
00328 #endif // __SIMPLENUMBERFORMATTERH__
00329