valueformatter.h 4.9 KB

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