aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/icu/i18n/number_symbolswrapper.cpp
blob: 4742a69c1fb651cee8b6299bc6b6b060d5e41163 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// © 2020 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html

#include "unicode/utypes.h"

#if !UCONFIG_NO_FORMATTING

#include "number_microprops.h"
#include "unicode/numberformatter.h"

using namespace icu;
using namespace icu::number;
using namespace icu::number::impl;

SymbolsWrapper::SymbolsWrapper(const SymbolsWrapper &other) {
    doCopyFrom(other);
}

SymbolsWrapper::SymbolsWrapper(SymbolsWrapper &&src) noexcept {
    doMoveFrom(std::move(src));
}

SymbolsWrapper &SymbolsWrapper::operator=(const SymbolsWrapper &other) {
    if (this == &other) {
        return *this;
    }
    doCleanup();
    doCopyFrom(other);
    return *this;
}

SymbolsWrapper &SymbolsWrapper::operator=(SymbolsWrapper &&src) noexcept {
    if (this == &src) {
        return *this;
    }
    doCleanup();
    doMoveFrom(std::move(src));
    return *this;
}

SymbolsWrapper::~SymbolsWrapper() {
    doCleanup();
}

void SymbolsWrapper::setTo(const DecimalFormatSymbols &dfs) {
    doCleanup();
    fType = SYMPTR_DFS;
    fPtr.dfs = new DecimalFormatSymbols(dfs);
}

void SymbolsWrapper::setTo(const NumberingSystem *ns) {
    doCleanup();
    fType = SYMPTR_NS;
    fPtr.ns = ns;
}

void SymbolsWrapper::doCopyFrom(const SymbolsWrapper &other) {
    fType = other.fType;
    switch (fType) {
    case SYMPTR_NONE:
        // No action necessary
        break;
    case SYMPTR_DFS:
        // Memory allocation failures are exposed in copyErrorTo()
        if (other.fPtr.dfs != nullptr) {
            fPtr.dfs = new DecimalFormatSymbols(*other.fPtr.dfs);
        } else {
            fPtr.dfs = nullptr;
        }
        break;
    case SYMPTR_NS:
        // Memory allocation failures are exposed in copyErrorTo()
        if (other.fPtr.ns != nullptr) {
            fPtr.ns = new NumberingSystem(*other.fPtr.ns);
        } else {
            fPtr.ns = nullptr;
        }
        break;
    }
}

void SymbolsWrapper::doMoveFrom(SymbolsWrapper &&src) {
    fType = src.fType;
    switch (fType) {
    case SYMPTR_NONE:
        // No action necessary
        break;
    case SYMPTR_DFS:
        fPtr.dfs = src.fPtr.dfs;
        src.fPtr.dfs = nullptr;
        break;
    case SYMPTR_NS:
        fPtr.ns = src.fPtr.ns;
        src.fPtr.ns = nullptr;
        break;
    }
}

void SymbolsWrapper::doCleanup() {
    switch (fType) {
    case SYMPTR_NONE:
        // No action necessary
        break;
    case SYMPTR_DFS:
        delete fPtr.dfs;
        break;
    case SYMPTR_NS:
        delete fPtr.ns;
        break;
    }
}

bool SymbolsWrapper::isDecimalFormatSymbols() const {
    return fType == SYMPTR_DFS;
}

bool SymbolsWrapper::isNumberingSystem() const {
    return fType == SYMPTR_NS;
}

const DecimalFormatSymbols *SymbolsWrapper::getDecimalFormatSymbols() const {
    U_ASSERT(fType == SYMPTR_DFS);
    return fPtr.dfs;
}

const NumberingSystem *SymbolsWrapper::getNumberingSystem() const {
    U_ASSERT(fType == SYMPTR_NS);
    return fPtr.ns;
}

#endif /* #if !UCONFIG_NO_FORMATTING */