number_decnum.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // © 2017 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. #include "unicode/utypes.h"
  4. #if !UCONFIG_NO_FORMATTING
  5. #ifndef __NUMBER_DECNUM_H__
  6. #define __NUMBER_DECNUM_H__
  7. #include "decNumber.h"
  8. #include "charstr.h"
  9. U_NAMESPACE_BEGIN
  10. #define DECNUM_INITIAL_CAPACITY 34
  11. // Export an explicit template instantiation of the MaybeStackHeaderAndArray that is used as a data member of DecNum.
  12. // When building DLLs for Windows this is required even though no direct access to the MaybeStackHeaderAndArray leaks out of the i18n library.
  13. // (See digitlst.h, pluralaffix.h, datefmt.h, and others for similar examples.)
  14. #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
  15. template class U_I18N_API MaybeStackHeaderAndArray<decNumber, char, DECNUM_INITIAL_CAPACITY>;
  16. #endif
  17. namespace number {
  18. namespace impl {
  19. /** A very thin C++ wrapper around decNumber.h */
  20. // Exported as U_I18N_API for tests
  21. class U_I18N_API DecNum : public UMemory {
  22. public:
  23. DecNum(); // leaves object in valid but undefined state
  24. // Copy-like constructor; use the default move operators.
  25. DecNum(const DecNum& other, UErrorCode& status);
  26. /** Sets the decNumber to the StringPiece. */
  27. void setTo(StringPiece str, UErrorCode& status);
  28. /** Sets the decNumber to the NUL-terminated char string. */
  29. void setTo(const char* str, UErrorCode& status);
  30. /** Uses double_conversion to set this decNumber to the given double. */
  31. void setTo(double d, UErrorCode& status);
  32. /** Sets the decNumber to the BCD representation. */
  33. void setTo(const uint8_t* bcd, int32_t length, int32_t scale, bool isNegative, UErrorCode& status);
  34. void normalize();
  35. void multiplyBy(const DecNum& rhs, UErrorCode& status);
  36. void divideBy(const DecNum& rhs, UErrorCode& status);
  37. bool isNegative() const;
  38. bool isZero() const;
  39. void toString(ByteSink& output, UErrorCode& status) const;
  40. inline const decNumber* getRawDecNumber() const {
  41. return fData.getAlias();
  42. }
  43. private:
  44. static constexpr int32_t kDefaultDigits = DECNUM_INITIAL_CAPACITY;
  45. MaybeStackHeaderAndArray<decNumber, char, kDefaultDigits> fData;
  46. decContext fContext;
  47. void _setTo(const char* str, int32_t maxDigits, UErrorCode& status);
  48. };
  49. } // namespace impl
  50. } // namespace number
  51. U_NAMESPACE_END
  52. #endif // __NUMBER_DECNUM_H__
  53. #endif /* #if !UCONFIG_NO_FORMATTING */