7 #ifndef NUKLEI_SERIALDEFINITIONS_H
8 #define NUKLEI_SERIALDEFINITIONS_H
14 #include <boost/iostreams/filtering_stream.hpp>
15 #include <boost/iostreams/filter/gzip.hpp>
16 #include <boost/iostreams/device/file.hpp>
30 void Serial::readObject(T &
object,
const std::string& filename,
31 const std::string &typeName)
34 Type type = typeFromName<Serial>(typeName);
36 NUKLEI_UNIQUE_PTR<std::istream> ifs;
37 if (type == BOOSTXML_COMPRESSED || type == BOOSTBIN_COMPRESSED)
40 std::ifstream testIfs(filename.c_str());
42 testIfs.read((
char*)(&byte1), 1);
43 testIfs.read((
char*)(&byte2), 1);
44 if ((byte1) != 0x1f || (byte2) != 0x8b)
45 throw SerialError(
"File does note appear to be GZIP");
47 NUKLEI_UNIQUE_PTR<boost::iostreams::filtering_istream>
48 decompressingIfs(
new boost::iostreams::filtering_istream);
49 decompressingIfs->push(boost::iostreams::gzip_decompressor());
50 decompressingIfs->push(boost::iostreams::file_source(filename));
51 ifs = NUKLEI_MOVE(decompressingIfs);
53 else if (type == BOOSTXML || type == BOOSTBIN)
55 ifs.reset(
new std::ifstream(filename.c_str()));
57 else throw SerialError(std::string(
"Unknown output format `") + typeName);
64 case BOOSTXML_COMPRESSED:
66 NUKLEI_SERIALIZATION_XML_IARCHIVE ia(*ifs);
67 ia & NUKLEI_SERIALIZATION_NVP(
object);
73 case BOOSTBIN_COMPRESSED:
75 NUKLEI_SERIALIZATION_BINARY_IARCHIVE ia(*ifs);
76 ia & NUKLEI_SERIALIZATION_NVP(
object);
77 if (ifs->peek() != EOF)
throw SerialError(
"EOF not reached");
82 throw SerialError(std::string(
"Unknown output format `") + typeName);
90 void Serial::writeObject(
const T &
object,
const std::string& filename,
91 const std::string &typeName,
95 Type type = typeFromName<Serial>(typeName);
97 NUKLEI_UNIQUE_PTR<std::ostream> ofs;
98 if (type == BOOSTXML_COMPRESSED || type == BOOSTBIN_COMPRESSED)
100 NUKLEI_UNIQUE_PTR<boost::iostreams::filtering_ostream>
101 decompressingOfs(
new boost::iostreams::filtering_ostream);
102 decompressingOfs->push(boost::iostreams::gzip_compressor());
103 decompressingOfs->push(boost::iostreams::file_sink(filename));
104 ofs = NUKLEI_MOVE(decompressingOfs);
106 else if (type == BOOSTXML || type == BOOSTBIN)
108 ofs.reset(
new std::ofstream(filename.c_str()));
110 else throw SerialError(std::string(
"Unknown output format `") + typeName);
113 object.assertConsistency();
118 case BOOSTXML_COMPRESSED:
120 NUKLEI_SERIALIZATION_XML_OARCHIVE oa(*ofs);
121 oa & NUKLEI_SERIALIZATION_NVP(
object);
125 case BOOSTBIN_COMPRESSED:
127 NUKLEI_SERIALIZATION_BINARY_OARCHIVE oa(*ofs);
128 oa & NUKLEI_SERIALIZATION_NVP(
object);
133 throw SerialError(std::string(
"Unknown output format `") + typeName +
"'.");