Changeset View
Changeset View
Standalone View
Standalone View
src/serialize.h
Show First 20 Lines • Show All 744 Lines • ▼ Show 20 Lines | template <typename Stream, typename V> void Unser(Stream &s, V &v) { | ||||
v.emplace_back(); | v.emplace_back(); | ||||
formatter.Unser(s, v.back()); | formatter.Unser(s, v.back()); | ||||
} | } | ||||
} | } | ||||
}; | }; | ||||
}; | }; | ||||
/** | /** | ||||
* Helper for differentially encoded Compact Size integers in lists. | |||||
* | |||||
* Instead of using raw indexes, the number encoded is the difference between | |||||
* the current index and the previous index, minus one. For example, a first | |||||
* index of 0 implies a real index of 0, a second index of 0 thereafter refers | |||||
* to a real index of 1, etc. | |||||
* | |||||
* To be used with a VectorFormatter. | |||||
*/ | |||||
class DifferenceFormatter { | |||||
uint64_t m_shift = 0; | |||||
public: | |||||
template <typename Stream, typename I> void Ser(Stream &s, I v) { | |||||
if (v < m_shift || v >= std::numeric_limits<uint64_t>::max()) { | |||||
throw std::ios_base::failure("differential value overflow"); | |||||
} | |||||
WriteCompactSize(s, v - m_shift); | |||||
m_shift = uint64_t(v) + 1; | |||||
} | |||||
template <typename Stream, typename I> void Unser(Stream &s, I &v) { | |||||
uint64_t n = ReadCompactSize(s); | |||||
m_shift += n; | |||||
if (m_shift < n || m_shift >= std::numeric_limits<uint64_t>::max() || | |||||
m_shift < std::numeric_limits<I>::min() || | |||||
m_shift > std::numeric_limits<I>::max()) { | |||||
throw std::ios_base::failure("differential value overflow"); | |||||
} | |||||
v = I(m_shift++); | |||||
} | |||||
}; | |||||
/** | |||||
* Forward declarations | * Forward declarations | ||||
*/ | */ | ||||
/** | /** | ||||
* string | * string | ||||
*/ | */ | ||||
template <typename Stream, typename C> | template <typename Stream, typename C> | ||||
void Serialize(Stream &os, const std::basic_string<C> &str); | void Serialize(Stream &os, const std::basic_string<C> &str); | ||||
▲ Show 20 Lines • Show All 455 Lines • Show Last 20 Lines |