Add generic Read, Write functions for buffer_vector.

This commit is contained in:
vng 2011-12-13 17:17:52 +03:00 committed by Alex Zolotarev
parent 56a6e7f8c7
commit a6f4179f4f

View file

@ -2,12 +2,26 @@
#include "varint.hpp"
#include "../base/buffer_vector.hpp"
#include "../std/string.hpp"
#include "../std/vector.hpp"
namespace rw
{
template <class TSink>
void Write(TSink & sink, uint32_t i)
{
WriteVarUint(sink, i);
}
template <class TSource>
void Read(TSource & src, uint32_t & i)
{
i = ReadVarUint<uint32_t>(src);
}
template <class TSink>
void Write(TSink & sink, string const & s)
{
@ -26,21 +40,48 @@ namespace rw
src.Read(&s[0], count);
}
namespace impl
{
template <class TSink, class TCont>
void WriteCont(TSink & sink, TCont const & v)
{
uint32_t const count = static_cast<uint32_t>(v.size());
WriteVarUint(sink, count);
for (uint32_t i = 0; i < count; ++i)
Write(sink, v[i]);
}
template <class TSource, class TCont>
void ReadCont(TSource & src, TCont & v)
{
uint32_t const count = ReadVarUint<uint32_t>(src);
v.resize(count);
for (size_t i = 0; i < count; ++i)
Read(src, v[i]);
}
}
template <class TSink, class T>
void Write(TSink & sink, vector<T> const & v)
{
uint32_t const count = static_cast<uint32_t>(v.size());
WriteVarUint(sink, count);
for (uint32_t i = 0; i < count; ++i)
Write(sink, v[i]);
impl::WriteCont(sink, v);
}
template <class TSource, class T>
void Read(TSource & src, vector<T> & v)
{
uint32_t const count = ReadVarUint<uint32_t>(src);
v.resize(count);
for (size_t i = 0; i < count; ++i)
Read(src, v[i]);
impl::ReadCont(src, v);
}
template <class TSink, class T, size_t N>
void Write(TSink & sink, buffer_vector<T, N> const & v)
{
impl::WriteCont(sink, v);
}
template <class TSource, class T, size_t N>
void Read(TSource & src, buffer_vector<T, N> & v)
{
impl::ReadCont(src, v);
}
}