Changeset View
Changeset View
Standalone View
Standalone View
src/serialize.h
| Show First 20 Lines • Show All 616 Lines • ▼ Show 20 Lines | template <typename Stream, typename I> void Ser(Stream &s, I v) { | ||||
| WriteVarInt<Stream, Mode, typename std::remove_cv<I>::type>(s, v); | WriteVarInt<Stream, Mode, typename std::remove_cv<I>::type>(s, v); | ||||
| } | } | ||||
| template <typename Stream, typename I> void Unser(Stream &s, I &v) { | template <typename Stream, typename I> void Unser(Stream &s, I &v) { | ||||
| v = ReadVarInt<Stream, Mode, typename std::remove_cv<I>::type>(s); | v = ReadVarInt<Stream, Mode, typename std::remove_cv<I>::type>(s); | ||||
| } | } | ||||
| }; | }; | ||||
| template <int Bytes> struct CustomUintFormatter { | |||||
| static_assert(Bytes > 0 && Bytes <= 8, | |||||
| "CustomUintFormatter Bytes out of range"); | |||||
| static constexpr uint64_t MAX = 0xffffffffffffffff >> (8 * (8 - Bytes)); | |||||
| template <typename Stream, typename I> void Ser(Stream &s, I v) { | |||||
| if (v < 0 || v > MAX) { | |||||
| throw std::ios_base::failure( | |||||
| "CustomUintFormatter value out of range"); | |||||
| } | |||||
| uint64_t raw = htole64(v); | |||||
| s.write((const char *)&raw, Bytes); | |||||
| } | |||||
| 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"); | |||||
| uint64_t raw = 0; | |||||
| s.read((char *)&raw, Bytes); | |||||
| v = le64toh(raw); | |||||
| } | |||||
| }; | |||||
| /** Serialization wrapper class for big-endian integers. | /** Serialization wrapper class for big-endian integers. | ||||
| * | * | ||||
| * Use this wrapper around integer types that are stored in memory in native | * Use this wrapper around integer types that are stored in memory in native | ||||
| * byte order, but serialized in big endian notation. This is only intended | * byte order, but serialized in big endian notation. This is only intended | ||||
| * to implement serializers that are compatible with existing formats, and | * to implement serializers that are compatible with existing formats, and | ||||
| * its use is not recommended for new data structures. | * its use is not recommended for new data structures. | ||||
| * | * | ||||
| * Only 16-bit types are supported for now. | * Only 16-bit types are supported for now. | ||||
| ▲ Show 20 Lines • Show All 548 Lines • Show Last 20 Lines | |||||