20 #ifndef TPIE_SERIALIZATION_STREAM_H
21 #define TPIE_SERIALIZATION_STREAM_H
40 static memory_size_type block_size() {
46 stream_size_type m_blocksWritten;
47 stream_size_type m_size;
55 void open(std::string path,
bool reverse);
56 void open(
temp_file & tempFile,
bool reverse);
59 void open_inner(std::string path,
bool reverse);
70 void write_block(
const char *
const s,
const memory_size_type n);
72 void close(
bool reverse);
75 static memory_size_type memory_usage() {
return block_size(); }
77 stream_size_type file_size();
87 memory_size_type m_index;
97 void write(
const char *
const s,
const memory_size_type n) {
99 memory_size_type written = 0;
100 while (written != n) {
101 if (wr.m_index >= wr.block_size()) wr.write_block();
103 memory_size_type remaining = n - written;
104 memory_size_type blockRemaining = wr.block_size() - wr.m_index;
106 memory_size_type writeSize = std::min(remaining, blockRemaining);
108 std::copy(i, i + writeSize, &wr.m_block[wr.m_index]);
110 written += writeSize;
111 wr.m_index += writeSize;
116 friend class serializer;
119 void open(std::string path);
129 template <
typename T>
143 template <
typename IT>
160 memory_size_type m_index;
161 std::vector<char> m_serializationBuffer;
171 void write(
const char *
const s,
const memory_size_type n) {
172 std::vector<char> & data = wr.m_serializationBuffer;
173 memory_size_type offs = data.size();
174 data.resize(data.size() + n);
175 std::copy(s, s + n, &data[offs]);
179 std::vector<char> & data = wr.m_serializationBuffer;
180 const memory_size_type n = data.size();
181 const char *
const s = &data[0];
182 if (wr.m_index + n <= wr.block_size()) {
183 std::copy(s, s + n, &wr.m_block[block_size() - wr.m_index - n]);
186 const char * i = s + n;
187 memory_size_type written = 0;
188 while (written != n) {
189 if (wr.m_index >= wr.block_size()) wr.write_block();
191 memory_size_type remaining = n - written;
192 memory_size_type blockRemaining = wr.block_size() - wr.m_index;
194 memory_size_type writeSize = std::min(remaining, blockRemaining);
196 std::copy(i - writeSize, i, &wr.m_block[block_size() - wr.m_index - writeSize]);
198 written += writeSize;
199 wr.m_index += writeSize;
206 friend class serializer;
209 void open(std::string path);
219 template <
typename T>
233 template <
typename IT>
245 static memory_size_type block_size() {
246 return serialization_writer_base::block_size();
255 stream_size_type m_size;
256 memory_size_type m_index;
257 memory_size_type m_blockSize;
261 void open(std::string path,
bool reverse);
263 void read_block(
const stream_size_type blk);
266 virtual void next_block() = 0;
277 void read(
char *
const s,
const memory_size_type n) {
280 memory_size_type written = 0;
281 while (written != n) {
282 if (m_index >= m_blockSize) {
287 memory_size_type remaining = n - written;
288 memory_size_type blockRemaining = m_blockSize - m_index;
290 memory_size_type readSize = std::min(remaining, blockRemaining);
292 i = std::copy(m_block.
get() + m_index,
293 m_block.
get() + (m_index + readSize),
310 template <
typename T>
325 template <
typename IT>
331 static memory_size_type memory_usage() {
return block_size(); }
343 stream_size_type
size();
350 stream_size_type m_blockNumber;
353 virtual void next_block() ;
358 void open(std::string path);
362 if (m_index < m_blockSize)
return true;
363 return m_blockNumber * (stream_size_type)block_size() + m_index < m_size;
371 stream_size_type
offset();
376 stream_size_type m_blockNumber;
379 virtual void next_block() ;
384 void open(std::string path);
388 if (m_index < m_blockSize)
return true;
389 return m_blockNumber > 0;
397 stream_size_type
offset();
402 #endif // TPIE_SERIALIZATION_STREAM_H
T * get()
Return a raw pointer to the array content.
Binary serialization and unserialization.
stream_size_type offset()
Number of bytes read, not including the header.
void serialize(const T &v)
Serialize a serializable item and write it to the stream.
Declare default file accessor.
stream_size_type size()
Size of file in bytes, not including the header.
Generic internal array with known memory requirements.
void serialize(const T &v)
Serialize a serializable item and write it to the stream.
stream_size_type offset()
Number of bytes read, not including the header.
void write_block(const char *const s, const memory_size_type n)
Write n bytes from memory area s to next block in stream.
stream_size_type file_size()
Size of file in bytes, including the header.
void unserialize(S &src, foo &v)
Sample tpie::unserialize prototype.
void unserialize(T &v)
Unserialize an unserializable item from the stream.
Class representing the existence of a temporary file.
void serialize(IT a, IT b)
Serialize a sequence of serializable items and write them to the stream.
void serialize(IT a, IT b)
Serialize a sequence of serializable items and write them to the stream.
POSIX-style file accessor.
Describes how to acces a file.
void serialize(D &dst, const foo &v)
Sample tpie::serialize prototype.
void unserialize(IT a, IT b)
Unserialize a sequence of unserializable items from the stream.
void read(char *const s, const memory_size_type n)
Read n bytes from stream into buffer starting at s.