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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
#pragma once
#include "base/assert.hpp"
#include "base/base.hpp"
#include "base/exception.hpp"
#include "std/algorithm.hpp"
#include "std/shared_ptr.hpp"
#include "std/cstring.hpp"
#include "std/string.hpp"
#include "std/vector.hpp"
// Generic Writer. Not thread-safe.
// When SubWriter is used, pos can negative, so int64_t is used to store pos.
class Writer
{
public:
DECLARE_EXCEPTION(Exception, RootException);
DECLARE_EXCEPTION(OpenException, Exception);
DECLARE_EXCEPTION(WriteException, Exception);
DECLARE_EXCEPTION(PosException, Exception);
DECLARE_EXCEPTION(SeekException, Exception);
DECLARE_EXCEPTION(CreateDirException, Exception);
virtual ~Writer() {}
virtual void Seek(int64_t pos) = 0;
virtual int64_t Pos() const = 0;
virtual void Write(void const * p, size_t size) = 0;
};
template <typename ContainerT>
class MemWriter : public Writer
{
public:
inline MemWriter(ContainerT & data) : m_Data(data), m_Pos(0)
{
static_assert(sizeof(typename ContainerT::value_type) == 1, "");
}
inline void Seek(int64_t pos)
{
ASSERT_EQUAL(pos, static_cast<intptr_t>(pos), ());
ASSERT_GREATER_OR_EQUAL(pos, 0, ());
m_Pos = static_cast<intptr_t>(pos);
}
inline int64_t Pos() const
{
return m_Pos;
}
inline void Write(void const * p, size_t size)
{
intptr_t freeSize = m_Data.size() - m_Pos;
if (freeSize < 0)
{
m_Data.resize(m_Pos + size);
freeSize = size;
}
memcpy(&m_Data[m_Pos], p, min(size, static_cast<size_t>(freeSize)));
if (size > static_cast<size_t>(freeSize))
{
uint8_t const * it = reinterpret_cast<uint8_t const *>(p);
m_Data.insert(m_Data.end(), it + freeSize, it + size);
}
m_Pos += size;
}
private:
ContainerT & m_Data;
size_t m_Pos;
};
// Original writer should not be used when SubWriter is active!
// In destructor, SubWriter calls Seek() of original writer to the end of what has been written.
template <typename WriterT>
class SubWriter
{
public:
inline explicit SubWriter(WriterT & writer)
: m_writer(writer), m_pos(0), m_maxPos(0)
#ifdef DEBUG
, m_offset(GetOffset())
#endif
{
}
~SubWriter()
{
ASSERT_EQUAL(m_offset, GetOffset(), ());
if (m_pos != m_maxPos)
Seek(m_maxPos);
}
inline void Seek(int64_t pos)
{
ASSERT_EQUAL(m_offset, GetOffset(), ());
m_writer.Seek(GetOffset() + pos);
m_pos = pos;
m_maxPos = max(m_maxPos, m_pos);
}
inline int64_t Pos() const
{
ASSERT_EQUAL(m_offset, GetOffset(), ());
return m_pos;
}
inline void Write(void const * p, size_t size)
{
ASSERT_EQUAL(m_offset, GetOffset(), ());
m_writer.Write(p, size);
m_pos += size;
m_maxPos = max(m_maxPos, m_pos);
}
inline uint64_t Size() const { return m_maxPos; }
private:
inline int64_t GetOffset() const { return m_writer.Pos() - m_pos; }
private:
WriterT & m_writer;
int64_t m_pos;
int64_t m_maxPos;
#ifdef DEBUG
int64_t const m_offset;
#endif
};
template<typename WriterT>
class WriterPtr
{
public:
WriterPtr(WriterT * p = 0) : m_p(p) {}
void Seek(int64_t pos)
{
m_p->Seek(pos);
}
int64_t Pos() const
{
return m_p->Pos();
}
void Write(void const * p, size_t size)
{
m_p->Write(p, size);
}
WriterT * GetPtr() const { return m_p.get(); }
protected:
shared_ptr<WriterT> m_p;
};
template <typename WriterT>
class WriterSink
{
public:
inline WriterSink(WriterT & writer) : m_writer(writer), m_pos(0) {}
inline void Write(void const * p, size_t size)
{
m_writer.Write(p, size);
m_pos += size;
}
private:
WriterT & m_writer;
int64_t m_pos;
};
|