number_symbolswrapper.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // © 2020 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. #include "number_microprops.h"
  6. #include "unicode/numberformatter.h"
  7. using namespace icu;
  8. using namespace icu::number;
  9. using namespace icu::number::impl;
  10. SymbolsWrapper::SymbolsWrapper(const SymbolsWrapper &other) {
  11. doCopyFrom(other);
  12. }
  13. SymbolsWrapper::SymbolsWrapper(SymbolsWrapper &&src) noexcept {
  14. doMoveFrom(std::move(src));
  15. }
  16. SymbolsWrapper &SymbolsWrapper::operator=(const SymbolsWrapper &other) {
  17. if (this == &other) {
  18. return *this;
  19. }
  20. doCleanup();
  21. doCopyFrom(other);
  22. return *this;
  23. }
  24. SymbolsWrapper &SymbolsWrapper::operator=(SymbolsWrapper &&src) noexcept {
  25. if (this == &src) {
  26. return *this;
  27. }
  28. doCleanup();
  29. doMoveFrom(std::move(src));
  30. return *this;
  31. }
  32. SymbolsWrapper::~SymbolsWrapper() {
  33. doCleanup();
  34. }
  35. void SymbolsWrapper::setTo(const DecimalFormatSymbols &dfs) {
  36. doCleanup();
  37. fType = SYMPTR_DFS;
  38. fPtr.dfs = new DecimalFormatSymbols(dfs);
  39. }
  40. void SymbolsWrapper::setTo(const NumberingSystem *ns) {
  41. doCleanup();
  42. fType = SYMPTR_NS;
  43. fPtr.ns = ns;
  44. }
  45. void SymbolsWrapper::doCopyFrom(const SymbolsWrapper &other) {
  46. fType = other.fType;
  47. switch (fType) {
  48. case SYMPTR_NONE:
  49. // No action necessary
  50. break;
  51. case SYMPTR_DFS:
  52. // Memory allocation failures are exposed in copyErrorTo()
  53. if (other.fPtr.dfs != nullptr) {
  54. fPtr.dfs = new DecimalFormatSymbols(*other.fPtr.dfs);
  55. } else {
  56. fPtr.dfs = nullptr;
  57. }
  58. break;
  59. case SYMPTR_NS:
  60. // Memory allocation failures are exposed in copyErrorTo()
  61. if (other.fPtr.ns != nullptr) {
  62. fPtr.ns = new NumberingSystem(*other.fPtr.ns);
  63. } else {
  64. fPtr.ns = nullptr;
  65. }
  66. break;
  67. }
  68. }
  69. void SymbolsWrapper::doMoveFrom(SymbolsWrapper &&src) {
  70. fType = src.fType;
  71. switch (fType) {
  72. case SYMPTR_NONE:
  73. // No action necessary
  74. break;
  75. case SYMPTR_DFS:
  76. fPtr.dfs = src.fPtr.dfs;
  77. src.fPtr.dfs = nullptr;
  78. break;
  79. case SYMPTR_NS:
  80. fPtr.ns = src.fPtr.ns;
  81. src.fPtr.ns = nullptr;
  82. break;
  83. }
  84. }
  85. void SymbolsWrapper::doCleanup() {
  86. switch (fType) {
  87. case SYMPTR_NONE:
  88. // No action necessary
  89. break;
  90. case SYMPTR_DFS:
  91. delete fPtr.dfs;
  92. break;
  93. case SYMPTR_NS:
  94. delete fPtr.ns;
  95. break;
  96. }
  97. }
  98. bool SymbolsWrapper::isDecimalFormatSymbols() const {
  99. return fType == SYMPTR_DFS;
  100. }
  101. bool SymbolsWrapper::isNumberingSystem() const {
  102. return fType == SYMPTR_NS;
  103. }
  104. const DecimalFormatSymbols *SymbolsWrapper::getDecimalFormatSymbols() const {
  105. U_ASSERT(fType == SYMPTR_DFS);
  106. return fPtr.dfs;
  107. }
  108. const NumberingSystem *SymbolsWrapper::getNumberingSystem() const {
  109. U_ASSERT(fType == SYMPTR_NS);
  110. return fPtr.ns;
  111. }
  112. #endif /* #if !UCONFIG_NO_FORMATTING */