diff options
author | Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> | 2021-01-21 22:27:28 +0200 |
---|---|---|
committer | William Casarin <jb55@jb55.com> | 2022-02-22 13:50:30 -0800 |
commit | 75832fdc37ea3fe9cf515bd1946e220fe07a440b (patch) | |
tree | 8c9948dc7c4a97d97ab79df95e27b1a18baa2157 /src/qt/bitcoinunits.cpp | |
parent | 5d254a234d8c1569b0161264cc6d5d8d0ce0d864 (diff) |
qt: Use QVariant instead of int for BitcoinUnit in QSettings
This change improves type safety.
Diffstat (limited to 'src/qt/bitcoinunits.cpp')
-rw-r--r-- | src/qt/bitcoinunits.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/qt/bitcoinunits.cpp b/src/qt/bitcoinunits.cpp index 69caf64d5c..81ba096a41 100644 --- a/src/qt/bitcoinunits.cpp +++ b/src/qt/bitcoinunits.cpp @@ -250,3 +250,40 @@ CAmount BitcoinUnits::maxMoney() { return MAX_MONEY; } + +namespace { +qint8 ToQint8(BitcoinUnit unit) +{ + switch (unit) { + case BitcoinUnits::BTC: return 0; + case BitcoinUnits::mBTC: return 1; + case BitcoinUnits::uBTC: return 2; + case BitcoinUnits::SAT: return 3; + } // no default case, so the compiler can warn about missing cases + assert(false); +} + +BitcoinUnit FromQint8(qint8 num) +{ + switch (num) { + case 0: return BitcoinUnits::BTC; + case 1: return BitcoinUnits::mBTC; + case 2: return BitcoinUnits::uBTC; + case 3: return BitcoinUnits::SAT; + } + assert(false); +} +} // namespace + +QDataStream& operator<<(QDataStream& out, const BitcoinUnit& unit) +{ + return out << ToQint8(unit); +} + +QDataStream& operator>>(QDataStream& in, BitcoinUnit& unit) +{ + qint8 input; + in >> input; + unit = FromQint8(input); + return in; +} |