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 
SymbolsWrapper(const SymbolsWrapper & other)15 SymbolsWrapper::SymbolsWrapper(const SymbolsWrapper &other) {
16     doCopyFrom(other);
17 }
18 
SymbolsWrapper(SymbolsWrapper && src)19 SymbolsWrapper::SymbolsWrapper(SymbolsWrapper &&src) U_NOEXCEPT {
20     doMoveFrom(std::move(src));
21 }
22 
operator =(const SymbolsWrapper & other)23 SymbolsWrapper &SymbolsWrapper::operator=(const SymbolsWrapper &other) {
24     if (this == &other) {
25         return *this;
26     }
27     doCleanup();
28     doCopyFrom(other);
29     return *this;
30 }
31 
operator =(SymbolsWrapper && src)32 SymbolsWrapper &SymbolsWrapper::operator=(SymbolsWrapper &&src) U_NOEXCEPT {
33     if (this == &src) {
34         return *this;
35     }
36     doCleanup();
37     doMoveFrom(std::move(src));
38     return *this;
39 }
40 
~SymbolsWrapper()41 SymbolsWrapper::~SymbolsWrapper() {
42     doCleanup();
43 }
44 
setTo(const DecimalFormatSymbols & dfs)45 void SymbolsWrapper::setTo(const DecimalFormatSymbols &dfs) {
46     doCleanup();
47     fType = SYMPTR_DFS;
48     fPtr.dfs = new DecimalFormatSymbols(dfs);
49 }
50 
setTo(const NumberingSystem * ns)51 void SymbolsWrapper::setTo(const NumberingSystem *ns) {
52     doCleanup();
53     fType = SYMPTR_NS;
54     fPtr.ns = ns;
55 }
56 
doCopyFrom(const SymbolsWrapper & other)57 void SymbolsWrapper::doCopyFrom(const SymbolsWrapper &other) {
58     fType = other.fType;
59     switch (fType) {
60     case SYMPTR_NONE:
61         // No action necessary
62         break;
63     case SYMPTR_DFS:
64         // Memory allocation failures are exposed in copyErrorTo()
65         if (other.fPtr.dfs != nullptr) {
66             fPtr.dfs = new DecimalFormatSymbols(*other.fPtr.dfs);
67         } else {
68             fPtr.dfs = nullptr;
69         }
70         break;
71     case SYMPTR_NS:
72         // Memory allocation failures are exposed in copyErrorTo()
73         if (other.fPtr.ns != nullptr) {
74             fPtr.ns = new NumberingSystem(*other.fPtr.ns);
75         } else {
76             fPtr.ns = nullptr;
77         }
78         break;
79     }
80 }
81 
doMoveFrom(SymbolsWrapper && src)82 void SymbolsWrapper::doMoveFrom(SymbolsWrapper &&src) {
83     fType = src.fType;
84     switch (fType) {
85     case SYMPTR_NONE:
86         // No action necessary
87         break;
88     case SYMPTR_DFS:
89         fPtr.dfs = src.fPtr.dfs;
90         src.fPtr.dfs = nullptr;
91         break;
92     case SYMPTR_NS:
93         fPtr.ns = src.fPtr.ns;
94         src.fPtr.ns = nullptr;
95         break;
96     }
97 }
98 
doCleanup()99 void SymbolsWrapper::doCleanup() {
100     switch (fType) {
101     case SYMPTR_NONE:
102         // No action necessary
103         break;
104     case SYMPTR_DFS:
105         delete fPtr.dfs;
106         break;
107     case SYMPTR_NS:
108         delete fPtr.ns;
109         break;
110     }
111 }
112 
isDecimalFormatSymbols() const113 bool SymbolsWrapper::isDecimalFormatSymbols() const {
114     return fType == SYMPTR_DFS;
115 }
116 
isNumberingSystem() const117 bool SymbolsWrapper::isNumberingSystem() const {
118     return fType == SYMPTR_NS;
119 }
120 
getDecimalFormatSymbols() const121 const DecimalFormatSymbols *SymbolsWrapper::getDecimalFormatSymbols() const {
122     U_ASSERT(fType == SYMPTR_DFS);
123     return fPtr.dfs;
124 }
125 
getNumberingSystem() const126 const NumberingSystem *SymbolsWrapper::getNumberingSystem() const {
127     U_ASSERT(fType == SYMPTR_NS);
128     return fPtr.ns;
129 }
130 
131 #endif /* #if !UCONFIG_NO_FORMATTING */
132