9 #ifndef _SERIALIZER_HPP_
10 #define _SERIALIZER_HPP_
54 #ifdef SERIALIZER_INTERFACE_TEST
56 virtual void abstractTest() = 0;
87 std::istream& stream =
_stream->read(data, n);
118 if (retrieve<uint8_t>() != (uint8_t)
r_sum1 )
return false;
119 if (retrieve<uint8_t>() != (uint8_t)
r_sum2 )
return false;
157 template <
typename Type>
160 writeToStream(reinterpret_cast<const char *> (&value),
sizeof (Type));
168 template <
typename Type>
172 readFromStream (reinterpret_cast<char *> (&value),
sizeof (Type));
187 size_t tlen = len > 21 ? 21 : len;
208 size_t tlen = len > 21 ? 21 : len;
250 void SerializerBackend::add<std::string>(
const std::string &str)
252 const size_t len(str.length());
254 writeToStream(str.data(), len);
265 std::string SerializerBackend::retrieve<std::string>()
267 const size_t len(retrieve<size_t>());
268 std::string str(len,
' ');
269 readFromStream (&str[0], len);
297 explicit Serializer(
int compressionlevel = Z_BEST_COMPRESSION)
300 _stream =
new std::stringstream(std::ios_base::binary|std::ios_base::out);
313 std::ios_base::binary|std::ios_base::in);
349 memset(&zs, 0,
sizeof(zs));
352 throw(std::runtime_error(
"Serialzer::compress(): deflateInit failed"));
354 zs.next_in = (Bytef*)str.data();
356 zs.avail_in = str.size();
359 char outbuffer[32768];
360 std::string outstring;
364 zs.next_out =
reinterpret_cast<Bytef*
>(outbuffer);
365 zs.avail_out =
sizeof(outbuffer);
367 ret = deflate(&zs, Z_FINISH);
369 if (outstring.size() < zs.total_out)
372 outstring.append(outbuffer,
373 zs.total_out - outstring.size());
375 }
while (ret == Z_OK);
380 if (ret != Z_STREAM_END)
382 std::ostringstream oss;
383 oss <<
"Serialzier::compress(): Error during zlib compression. ErrorCode: ";
384 oss << ret <<
" (" << zs.msg <<
")";
385 throw(std::runtime_error(oss.str()));
413 memset(&zs, 0,
sizeof(zs));
415 if (inflateInit(&zs) != Z_OK)
416 throw(std::runtime_error(
"Serializer::decompress(): inflateInit failed."));
418 zs.next_in = (Bytef*)str.data();
419 zs.avail_in = str.size();
422 char outbuffer[32768];
423 std::string outstring;
427 zs.next_out =
reinterpret_cast<Bytef*
>(outbuffer);
428 zs.avail_out =
sizeof(outbuffer);
430 ret = inflate(&zs, 0);
432 if (outstring.size() < zs.total_out)
434 outstring.append(outbuffer, zs.total_out - outstring.size());
437 }
while (ret == Z_OK);
442 if (ret != Z_STREAM_END)
444 std::ostringstream oss;
445 oss <<
"Serializer::decompress(): Error during zlib decompression: Error Code:";
446 oss << ret <<
" (" << zs.msg <<
")";
447 throw(std::runtime_error(oss.str()));
465 #ifdef SERIALIZER_INTERFACE_TEST
467 virtual void abstractTest() {}
490 _stream =
new std::fstream(filename, std::ios_base::binary|std::ios_base::out);
507 #ifdef SERIALIZER_INTERFACE_TEST
509 virtual void abstractTest() {}
536 _stream =
new std::fstream(filename, std::ios_base::binary|std::ios_base::in);
553 #ifdef SERIALIZER_INTERFACE_TEST
555 virtual void abstractTest() {}
SerializerBackend()
constructor
bool _opened
flag to see if the file is opened
void flush()
flush the stream
A file output serializer.
void startChecksumGroupForWrite()
control checksum for writing to stream
std::string decompress(const std::string &str) const
Decompress an STL string using zlib and return the inflated data.
bool _opened
flag to tell whether the file is opened
bool _checkSumGroupStartedForWrite
flag to enable adding parts to a checksum
std::iostream * _stream
the string to serialize the objects to (buffer)
void addToInChecksum(const char *data, std::streamsize len)
fletcher16 algorithm for 8 bit input
~SerializerWriteFile()
destructor.
uint16_t w_sum2
sum 2 for writing
~SerializerReadFile()
destructor.
bool endChecksumGroupForRead()
A file input deserializer.
const std::string buffer() const
retrieve a const reference to the compressed string.
void startChecksumGroupForRead()
control checksum calculation for the next values added
std::ostream & writeToStream(const char *data, std::streamsize n)
write data to the stream
void addToOutChecksum(const char *data, std::streamsize len)
fletcher16 algorithm for 8 bit input
uint16_t w_sum1
sum 1 for writing
Serializer(const std::string &string)
constructor
auxiliary data[Processor]
void endChecksumGroupForWrite()
std::istream & readFromStream(char *data, std::streamsize n)
read data from the stream
std::string compress(const std::string &str) const
Compress a STL string using zlib with given compression level and return the binary data...
uint16_t r_sum2
sum 2 for reading
SerializerWriteFile(const char *filename)
constructor.
Type retrieve()
read arbitrary value from stream
Serializer(int compressionlevel=Z_BEST_COMPRESSION)
constructor.
uint16_t r_sum1
sum 1 for reading
check if there is some light in the chamber based upon the GMD value
void add(const Type &value)
add arbitrary value to the stream
bool _checkSumGroupStartedForRead
flag to enable getting parts to a checksum
SerializerReadFile(const char *filename)
constructor.