diff options
| author | Russell Yanofsky <[email protected]> | 2020-03-30 15:32:39 -0700 |
|---|---|---|
| committer | Pieter Wuille <[email protected]> | 2020-03-30 15:36:16 -0700 |
| commit | 6f9a1e5ad0a270d3b5a715f3e3ea0911193bf244 (patch) | |
| tree | 4f230a7724ea174476fa50b9d8ae704577b99d34 /src | |
| parent | Merge BigEndian functionality into CustomUintFormatter (diff) | |
| download | discoin-6f9a1e5ad0a270d3b5a715f3e3ea0911193bf244.tar.xz discoin-6f9a1e5ad0a270d3b5a715f3e3ea0911193bf244.zip | |
Extend CustomUintFormatter to support enums
Extracted by Pieter Wuille from a comment by Russ Yanofsky, see
https://github.com/bitcoin/bitcoin/pull/18317#discussion_r398821936.
Diffstat (limited to 'src')
| -rw-r--r-- | src/serialize.h | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/src/serialize.h b/src/serialize.h index 7ee9556dc..a44cc2c00 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -538,14 +538,15 @@ struct CustomUintFormatter template <typename Stream, typename I> void Unser(Stream& s, I& v) { - static_assert(std::numeric_limits<I>::max() >= MAX && std::numeric_limits<I>::min() <= 0, "CustomUintFormatter type too small"); + using U = typename std::conditional<std::is_enum<I>::value, std::underlying_type<I>, std::common_type<I>>::type::type; + static_assert(std::numeric_limits<U>::max() >= MAX && std::numeric_limits<U>::min() <= 0, "Assigned type too small"); uint64_t raw = 0; if (BigEndian) { s.read(((char*)&raw) + 8 - Bytes, Bytes); - v = be64toh(raw); + v = static_cast<I>(be64toh(raw)); } else { s.read((char*)&raw, Bytes); - v = le64toh(raw); + v = static_cast<I>(le64toh(raw)); } } }; |