/src/icu/icu4c/source/i18n/number_symbolswrapper.cpp
Line | Count | Source |
1 | | // © 2020 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | |
4 | | #include "unicode/utypes.h" |
5 | | |
6 | | #if !UCONFIG_NO_FORMATTING |
7 | | |
8 | | #include "number_microprops.h" |
9 | | #include "unicode/numberformatter.h" |
10 | | |
11 | | using namespace icu; |
12 | | using namespace icu::number; |
13 | | using namespace icu::number::impl; |
14 | | |
15 | 0 | SymbolsWrapper::SymbolsWrapper(const SymbolsWrapper &other) { |
16 | 0 | doCopyFrom(other); |
17 | 0 | } |
18 | | |
19 | 492k | SymbolsWrapper::SymbolsWrapper(SymbolsWrapper &&src) noexcept { |
20 | 492k | doMoveFrom(std::move(src)); |
21 | 492k | } |
22 | | |
23 | 8.33k | SymbolsWrapper &SymbolsWrapper::operator=(const SymbolsWrapper &other) { |
24 | 8.33k | if (this == &other) { |
25 | 0 | return *this; |
26 | 0 | } |
27 | 8.33k | doCleanup(); |
28 | 8.33k | doCopyFrom(other); |
29 | 8.33k | return *this; |
30 | 8.33k | } |
31 | | |
32 | 1.46M | SymbolsWrapper &SymbolsWrapper::operator=(SymbolsWrapper &&src) noexcept { |
33 | 1.46M | if (this == &src) { |
34 | 0 | return *this; |
35 | 0 | } |
36 | 1.46M | doCleanup(); |
37 | 1.46M | doMoveFrom(std::move(src)); |
38 | 1.46M | return *this; |
39 | 1.46M | } |
40 | | |
41 | 2.34M | SymbolsWrapper::~SymbolsWrapper() { |
42 | 2.34M | doCleanup(); |
43 | 2.34M | } |
44 | | |
45 | 489k | void SymbolsWrapper::setTo(const DecimalFormatSymbols &dfs) { |
46 | 489k | doCleanup(); |
47 | 489k | fType = SYMPTR_DFS; |
48 | 489k | fPtr.dfs = new DecimalFormatSymbols(dfs); |
49 | 489k | } |
50 | | |
51 | 0 | void SymbolsWrapper::setTo(const NumberingSystem *ns) { |
52 | 0 | doCleanup(); |
53 | 0 | fType = SYMPTR_NS; |
54 | 0 | fPtr.ns = ns; |
55 | 0 | } |
56 | | |
57 | 8.33k | void SymbolsWrapper::doCopyFrom(const SymbolsWrapper &other) { |
58 | 8.33k | fType = other.fType; |
59 | 8.33k | switch (fType) { |
60 | 8.33k | case SYMPTR_NONE: |
61 | | // No action necessary |
62 | 8.33k | break; |
63 | 0 | case SYMPTR_DFS: |
64 | | // Memory allocation failures are exposed in copyErrorTo() |
65 | 0 | if (other.fPtr.dfs != nullptr) { |
66 | 0 | fPtr.dfs = new DecimalFormatSymbols(*other.fPtr.dfs); |
67 | 0 | } else { |
68 | 0 | fPtr.dfs = nullptr; |
69 | 0 | } |
70 | 0 | break; |
71 | 0 | case SYMPTR_NS: |
72 | | // Memory allocation failures are exposed in copyErrorTo() |
73 | 0 | if (other.fPtr.ns != nullptr) { |
74 | 0 | fPtr.ns = new NumberingSystem(*other.fPtr.ns); |
75 | 0 | } else { |
76 | 0 | fPtr.ns = nullptr; |
77 | 0 | } |
78 | 0 | break; |
79 | 8.33k | } |
80 | 8.33k | } |
81 | | |
82 | 1.96M | void SymbolsWrapper::doMoveFrom(SymbolsWrapper &&src) { |
83 | 1.96M | fType = src.fType; |
84 | 1.96M | switch (fType) { |
85 | 492k | case SYMPTR_NONE: |
86 | | // No action necessary |
87 | 492k | break; |
88 | 1.46M | case SYMPTR_DFS: |
89 | 1.46M | fPtr.dfs = src.fPtr.dfs; |
90 | 1.46M | src.fPtr.dfs = nullptr; |
91 | 1.46M | break; |
92 | 0 | case SYMPTR_NS: |
93 | 0 | fPtr.ns = src.fPtr.ns; |
94 | 0 | src.fPtr.ns = nullptr; |
95 | 0 | break; |
96 | 1.96M | } |
97 | 1.96M | } |
98 | | |
99 | 4.30M | void SymbolsWrapper::doCleanup() { |
100 | 4.30M | switch (fType) { |
101 | 2.35M | case SYMPTR_NONE: |
102 | | // No action necessary |
103 | 2.35M | break; |
104 | 1.95M | case SYMPTR_DFS: |
105 | 1.95M | delete fPtr.dfs; |
106 | 1.95M | break; |
107 | 0 | case SYMPTR_NS: |
108 | 0 | delete fPtr.ns; |
109 | 0 | break; |
110 | 4.30M | } |
111 | 4.30M | } |
112 | | |
113 | 11.4k | bool SymbolsWrapper::isDecimalFormatSymbols() const { |
114 | 11.4k | return fType == SYMPTR_DFS; |
115 | 11.4k | } |
116 | | |
117 | 11.4k | bool SymbolsWrapper::isNumberingSystem() const { |
118 | 11.4k | return fType == SYMPTR_NS; |
119 | 11.4k | } |
120 | | |
121 | 870k | const DecimalFormatSymbols *SymbolsWrapper::getDecimalFormatSymbols() const { |
122 | 870k | U_ASSERT(fType == SYMPTR_DFS); |
123 | 870k | return fPtr.dfs; |
124 | 870k | } |
125 | | |
126 | 0 | const NumberingSystem *SymbolsWrapper::getNumberingSystem() const { |
127 | 0 | U_ASSERT(fType == SYMPTR_NS); |
128 | 0 | return fPtr.ns; |
129 | 0 | } |
130 | | |
131 | | #endif /* #if !UCONFIG_NO_FORMATTING */ |