Skip to content

Commit

Permalink
Extend CustomUintFormatter to support enums
Browse files Browse the repository at this point in the history
Extracted by Pieter Wuille from a comment by Russ Yanofsky, see
bitcoin/bitcoin#18317 (comment).

(cherry picked from commit 6f9a1e5)
  • Loading branch information
ryanofsky authored and zancas committed Dec 10, 2021
1 parent c686bfe commit b9527ba
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,14 +503,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));
}
}
};
Expand Down

0 comments on commit b9527ba

Please sign in to comment.