20 #ifndef TPIE_SERIALIZATION_STREAM_H
21 #define TPIE_SERIALIZATION_STREAM_H
41 static memory_size_type block_size() {
47 stream_size_type m_blocksWritten;
48 stream_size_type m_size;
56 void open(std::string path,
bool reverse);
57 void open(
temp_file & tempFile,
bool reverse);
60 void open_inner(std::string path,
bool reverse);
71 void write_block(
const char *
const s,
const memory_size_type n);
73 void close(
bool reverse);
76 static memory_size_type memory_usage() {
return block_size(); }
78 stream_size_type file_size();
88 memory_size_type m_index;
100 void write(
const char *
const s,
const memory_size_type n) {
102 memory_size_type written = 0;
103 while (written != n) {
104 if (wr.m_index >= wr.block_size()) wr.write_block();
106 memory_size_type remaining = n - written;
107 memory_size_type blockRemaining = wr.block_size() - wr.m_index;
109 memory_size_type writeSize = std::min(remaining, blockRemaining);
111 std::copy(i, i + writeSize, &wr.m_block[wr.m_index]);
113 written += writeSize;
114 wr.m_index += writeSize;
124 void open(std::string path);
134 template <
typename T>
148 template <
typename IT>
165 memory_size_type m_index;
166 std::vector<char> m_serializationBuffer;
178 void write(
const char *
const s,
const memory_size_type n) {
179 std::vector<char> & data = wr.m_serializationBuffer;
180 memory_size_type offs = data.size();
181 data.resize(data.size() + n);
182 std::copy(s, s + n, &data[offs]);
186 std::vector<char> & data = wr.m_serializationBuffer;
187 const memory_size_type n = data.size();
188 const char *
const s = &data[0];
189 if (wr.m_index + n <= wr.block_size()) {
190 std::copy(s, s + n, &wr.m_block[block_size() - wr.m_index - n]);
193 const char * i = s + n;
194 memory_size_type written = 0;
195 while (written != n) {
196 if (wr.m_index >= wr.block_size()) wr.write_block();
198 memory_size_type remaining = n - written;
199 memory_size_type blockRemaining = wr.block_size() - wr.m_index;
201 memory_size_type writeSize = std::min(remaining, blockRemaining);
203 std::copy(i - writeSize, i, &wr.m_block[block_size() - wr.m_index - writeSize]);
205 written += writeSize;
206 wr.m_index += writeSize;
218 void open(std::string path);
228 template <
typename T>
242 template <
typename IT>
254 static memory_size_type block_size() {
255 return serialization_writer_base::block_size();
264 stream_size_type m_size;
265 memory_size_type m_index;
266 memory_size_type m_blockSize;
270 void open(std::string path,
bool reverse);
272 void read_block(
const stream_size_type blk);
275 virtual void next_block() = 0;
286 void read(
char *
const s,
const memory_size_type n) {
289 memory_size_type written = 0;
290 while (written != n) {
291 if (m_index >= m_blockSize) {
296 memory_size_type remaining = n - written;
297 memory_size_type blockRemaining = m_blockSize - m_index;
299 memory_size_type readSize = std::min(remaining, blockRemaining);
301 i = std::copy(m_block.
get() + m_index,
302 m_block.
get() + (m_index + readSize),
319 template <
typename T>
334 template <
typename IT>
340 static memory_size_type memory_usage() {
return block_size(); }
352 stream_size_type
size();
359 stream_size_type m_blockNumber;
362 void next_block()
override;
368 void open(std::string path);
372 if (m_index < m_blockSize)
return true;
373 return m_blockNumber * (stream_size_type)block_size() + m_index < m_size;
381 stream_size_type
offset();
386 stream_size_type m_blockNumber;
389 void next_block()
override;
394 void open(std::string path);
398 if (m_index < m_blockSize)
return true;
399 return m_blockNumber > 0;
407 stream_size_type
offset();
412 #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 a reference to 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.