Changeset View
Changeset View
Standalone View
Standalone View
src/serialize.h
Show First 20 Lines • Show All 471 Lines • ▼ Show 20 Lines | public: | ||||
template <typename Stream> void Unserialize(Stream &s) { | template <typename Stream> void Unserialize(Stream &s) { | ||||
size_t size = ReadCompactSize(s); | size_t size = ReadCompactSize(s); | ||||
if (size > Limit) { | if (size > Limit) { | ||||
throw std::ios_base::failure("String length limit exceeded"); | throw std::ios_base::failure("String length limit exceeded"); | ||||
} | } | ||||
string.resize(size); | string.resize(size); | ||||
if (size != 0) { | if (size != 0) { | ||||
s.read((char *)&string[0], size); | s.read((char *)string.data(), size); | ||||
} | } | ||||
} | } | ||||
template <typename Stream> void Serialize(Stream &s) const { | template <typename Stream> void Serialize(Stream &s) const { | ||||
WriteCompactSize(s, string.size()); | WriteCompactSize(s, string.size()); | ||||
if (!string.empty()) { | if (!string.empty()) { | ||||
s.write((char *)&string[0], string.size()); | s.write((char *)string.data(), string.size()); | ||||
} | } | ||||
} | } | ||||
}; | }; | ||||
template <typename I> CVarInt<I> WrapVarInt(I &n) { | template <typename I> CVarInt<I> WrapVarInt(I &n) { | ||||
return CVarInt<I>(n); | return CVarInt<I>(n); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 101 Lines • ▼ Show 20 Lines | |||||
/** | /** | ||||
* 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) { | ||||
WriteCompactSize(os, str.size()); | WriteCompactSize(os, str.size()); | ||||
if (!str.empty()) { | if (!str.empty()) { | ||||
os.write((char *)&str[0], str.size() * sizeof(str[0])); | os.write((char *)str.data(), str.size() * sizeof(C)); | ||||
} | } | ||||
} | } | ||||
template <typename Stream, typename C> | template <typename Stream, typename C> | ||||
void Unserialize(Stream &is, std::basic_string<C> &str) { | void Unserialize(Stream &is, std::basic_string<C> &str) { | ||||
size_t nSize = ReadCompactSize(is); | size_t nSize = ReadCompactSize(is); | ||||
str.resize(nSize); | str.resize(nSize); | ||||
if (nSize != 0) { | if (nSize != 0) { | ||||
is.read((char *)&str[0], nSize * sizeof(str[0])); | is.read((char *)str.data(), nSize * sizeof(C)); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* prevector | * prevector | ||||
*/ | */ | ||||
template <typename Stream, unsigned int N, typename T> | template <typename Stream, unsigned int N, typename T> | ||||
void Serialize_impl(Stream &os, const prevector<N, T> &v, const uint8_t &) { | void Serialize_impl(Stream &os, const prevector<N, T> &v, const uint8_t &) { | ||||
WriteCompactSize(os, v.size()); | WriteCompactSize(os, v.size()); | ||||
if (!v.empty()) { | if (!v.empty()) { | ||||
os.write((char *)&v[0], v.size() * sizeof(T)); | os.write((char *)v.data(), v.size() * sizeof(T)); | ||||
} | } | ||||
} | } | ||||
template <typename Stream, unsigned int N, typename T, typename V> | template <typename Stream, unsigned int N, typename T, typename V> | ||||
void Serialize_impl(Stream &os, const prevector<N, T> &v, const V &) { | void Serialize_impl(Stream &os, const prevector<N, T> &v, const V &) { | ||||
WriteCompactSize(os, v.size()); | WriteCompactSize(os, v.size()); | ||||
for (const T &i : v) { | for (const T &i : v) { | ||||
::Serialize(os, i); | ::Serialize(os, i); | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | |||||
/** | /** | ||||
* vector | * vector | ||||
*/ | */ | ||||
template <typename Stream, typename T, typename A> | template <typename Stream, typename T, typename A> | ||||
void Serialize_impl(Stream &os, const std::vector<T, A> &v, const uint8_t &) { | void Serialize_impl(Stream &os, const std::vector<T, A> &v, const uint8_t &) { | ||||
WriteCompactSize(os, v.size()); | WriteCompactSize(os, v.size()); | ||||
if (!v.empty()) { | if (!v.empty()) { | ||||
os.write((char *)&v[0], v.size() * sizeof(T)); | os.write((char *)v.data(), v.size() * sizeof(T)); | ||||
} | } | ||||
} | } | ||||
template <typename Stream, typename T, typename A, typename V> | template <typename Stream, typename T, typename A, typename V> | ||||
void Serialize_impl(Stream &os, const std::vector<T, A> &v, const V &) { | void Serialize_impl(Stream &os, const std::vector<T, A> &v, const V &) { | ||||
WriteCompactSize(os, v.size()); | WriteCompactSize(os, v.size()); | ||||
for (const T &i : v) { | for (const T &i : v) { | ||||
::Serialize(os, i); | ::Serialize(os, i); | ||||
▲ Show 20 Lines • Show All 228 Lines • Show Last 20 Lines |