1/* 2******************************************************************************* 3* Copyright (C) 2015, International Business Machines Corporation and * 4* others. All Rights Reserved. * 5******************************************************************************* 6*/ 7 8#ifndef VALUEFORMATTER_H 9#define VALUEFORMATTER_H 10 11#if !UCONFIG_NO_FORMATTING 12 13#include "unicode/uobject.h" 14#include "unicode/utypes.h" 15 16 17 18U_NAMESPACE_BEGIN 19 20class UnicodeString; 21class DigitList; 22class FieldPositionHandler; 23class DigitGrouping; 24class PluralRules; 25class FixedPrecision; 26class DigitFormatter; 27class DigitFormatterOptions; 28class ScientificPrecision; 29class SciFormatterOptions; 30class FixedDecimal; 31class VisibleDigitsWithExponent; 32 33 34/** 35 * A closure around rounding and formatting a value. As these instances are 36 * designed to be short lived (they only exist while formatting a value), they 37 * do not own their own attributes. Rather the caller maintains ownership of 38 * all attributes. A caller first calls a prepareXXX method on an instance 39 * to share its data before using that instance. Using an 40 * instance without first calling a prepareXXX method results in an 41 * assertion error and a program crash. 42 */ 43class U_I18N_API ValueFormatter : public UObject { 44public: 45 ValueFormatter() : fType(kFormatTypeCount) { 46 } 47 48 virtual ~ValueFormatter(); 49 50 /** 51 * This function is here only to support the protected round() method 52 * in DecimalFormat. It serves no ther purpose than that. 53 * 54 * @param value this value is rounded in place. 55 * @param status any error returned here. 56 */ 57 DigitList &round(DigitList &value, UErrorCode &status) const; 58 59 /** 60 * Returns TRUE if the absolute value of value can be fast formatted 61 * using ValueFormatter::formatInt32. 62 */ 63 UBool isFastFormattable(int32_t value) const; 64 65 /** 66 * Converts value to a VisibleDigitsWithExponent. 67 * Result may be fixed point or scientific. 68 */ 69 VisibleDigitsWithExponent &toVisibleDigitsWithExponent( 70 int64_t value, 71 VisibleDigitsWithExponent &digits, 72 UErrorCode &status) const; 73 74 /** 75 * Converts value to a VisibleDigitsWithExponent. 76 * Result may be fixed point or scientific. 77 */ 78 VisibleDigitsWithExponent &toVisibleDigitsWithExponent( 79 DigitList &value, 80 VisibleDigitsWithExponent &digits, 81 UErrorCode &status) const; 82 83 /** 84 * formats positiveValue and appends to appendTo. Returns appendTo. 85 * @param positiveValue If negative, no negative sign is formatted. 86 * @param handler stores the field positions 87 * @param appendTo formatted value appended here. 88 */ 89 UnicodeString &format( 90 const VisibleDigitsWithExponent &positiveValue, 91 FieldPositionHandler &handler, 92 UnicodeString &appendTo) const; 93 94 95 /** 96 * formats positiveValue and appends to appendTo. Returns appendTo. 97 * value must be positive. Calling formatInt32 to format a value when 98 * isFastFormattable indicates that the value cannot be fast formatted 99 * results in undefined behavior. 100 */ 101 UnicodeString &formatInt32( 102 int32_t positiveValue, 103 FieldPositionHandler &handler, 104 UnicodeString &appendTo) const; 105 106 /** 107 * Returns the number of code points needed to format. 108 * @param positiveValue if negative, the negative sign is not included 109 * in count. 110 */ 111 int32_t countChar32( 112 const VisibleDigitsWithExponent &positiveValue) const; 113 114 /** 115 * Prepares this instance for fixed decimal formatting. 116 */ 117 void prepareFixedDecimalFormatting( 118 const DigitFormatter &formatter, 119 const DigitGrouping &grouping, 120 const FixedPrecision &precision, 121 const DigitFormatterOptions &options); 122 123 /** 124 * Prepares this instance for scientific formatting. 125 */ 126 void prepareScientificFormatting( 127 const DigitFormatter &formatter, 128 const ScientificPrecision &precision, 129 const SciFormatterOptions &options); 130 131private: 132 ValueFormatter(const ValueFormatter &); 133 ValueFormatter &operator=(const ValueFormatter &); 134 enum FormatType { 135 kFixedDecimal, 136 kScientificNotation, 137 kFormatTypeCount 138 }; 139 140 FormatType fType; 141 142 // for fixed decimal and scientific formatting 143 const DigitFormatter *fDigitFormatter; 144 145 // for fixed decimal formatting 146 const FixedPrecision *fFixedPrecision; 147 const DigitFormatterOptions *fFixedOptions; 148 const DigitGrouping *fGrouping; 149 150 // for scientific formatting 151 const ScientificPrecision *fScientificPrecision; 152 const SciFormatterOptions *fScientificOptions; 153}; 154 155U_NAMESPACE_END 156 157#endif /* !UCONFIG_NO_FORMATTING */ 158 159#endif /* VALUEFORMATTER_H */ 160