1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
#pragma once
#include "coding/varint.hpp"
#include "base/buffer_vector.hpp"
#include "std/string.hpp"
#include "std/vector.hpp"
#include "std/type_traits.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)
{
uint32_t const count = static_cast<uint32_t>(s.size());
WriteVarUint(sink, count);
if (!s.empty())
sink.Write(&s[0], count);
}
template <class TSource>
void Read(TSource & src, string & s)
{
uint32_t const count = ReadVarUint<uint32_t>(src);
s.resize(count);
if (count > 0)
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)
{
impl::WriteCont(sink, v);
}
template <class TSource, class T>
void Read(TSource & src, vector<T> & v)
{
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);
}
template <class TSource, class TCont>
void ReadVectorOfPOD(TSource & src, TCont & v)
{
typedef typename TCont::value_type ValueT;
/// This assert fails on std::pair<int, int> and OsmID class.
/// @todo Review this logic in future with new compiler abilities.
/// https://trello.com/c/hzCc9bzN/1254-is-trivial-copy-read-write-utils-hpp
//static_assert(is_trivially_copyable<ValueT>::value, "");
uint32_t const count = ReadVarUint<uint32_t>(src);
if (count > 0)
{
v.resize(count);
src.Read(&v[0], count * sizeof(ValueT));
}
}
template <class TSink, class TCont>
void WriteVectorOfPOD(TSink & sink, TCont const & v)
{
typedef typename TCont::value_type ValueT;
/// This assert fails on std::pair<int, int> and OsmID class.
/// @todo Review this logic in future with new compiler abilities.
/// https://trello.com/c/hzCc9bzN/1254-is-trivial-copy-read-write-utils-hpp
//static_assert(is_trivially_copyable<ValueT>::value, "");
uint32_t const count = static_cast<uint32_t>(v.size());
WriteVarUint(sink, count);
if (count > 0)
sink.Write(&v[0], count * sizeof(ValueT));
}
template <class ReaderT, class WriterT>
void ReadAndWrite(ReaderT & reader, WriterT & writer, size_t bufferSize = 4*1024)
{
uint64_t size = reader.Size();
vector<char> buffer(min(bufferSize, static_cast<size_t>(size)));
while (size > 0)
{
size_t const curr = min(bufferSize, static_cast<size_t>(size));
reader.Read(&buffer[0], curr);
writer.Write(&buffer[0], curr);
size -= curr;
}
}
}
|