First Off: Streams Are Not Archives.
My first reaction would be "have you tried". But, I was intrigued and couldn't find anything about this in the documentation, so I did a few tests myself:
- the answer seems to be "No", it's not supported
- it seems to work for binary archives
- it seems to break down because the xml/text archives leave trailing
0xa
characters in the input buffer. These will not pose a problem if the "next" archive to be read is text as well, but obviously break binary archives.
Here's my tester:
Live On Coliru
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
int data = 42;
template <typename Ar>
void some_output(std::ostream& os)
{
std::cout << "Writing archive at " << os.tellp() << "
";
Ar ar(os);
ar << BOOST_SERIALIZATION_NVP(data);
}
template <typename Ar>
void roundtrip(std::istream& is)
{
data = -1;
std::cout << "Reading archive at " << is.tellg() << "
";
Ar ar(is);
ar >> BOOST_SERIALIZATION_NVP(data);
assert(data == 42);
}
#include <sstream>
int main()
{
std::stringstream ss;
//some_output<boost::archive::text_oarchive>(ss); // this derails the binary archive that follows
some_output<boost::archive::binary_oarchive>(ss);
some_output<boost::archive::xml_oarchive>(ss);
some_output<boost::archive::text_oarchive>(ss);
//roundtrip<boost::archive::text_iarchive>(ss);
roundtrip<boost::archive::binary_iarchive>(ss);
roundtrip<boost::archive::xml_iarchive>(ss);
roundtrip<boost::archive::text_iarchive>(ss);
// just to prove that there's remaining whitespace
std::cout << "remaining: ";
char ch;
while (ss>>std::noskipws>>ch)
std::cout << " " << std::showbase << std::hex << ((int)(ch));
std::cout << "
";
// of course, anything else will fail:
try {
roundtrip<boost::archive::text_iarchive>(ss);
} catch(boost::archive::archive_exception const& e)
{
std::cout << "Can't deserialize from a stream a EOF: " << e.what();
}
}
Prints:
Writing archive at 0
Writing archive at 44
Writing archive at 242
Reading archive at 0
Reading archive at 44
Reading archive at 240
remaining: 0xa
Reading archive at 0xffffffffffffffff
Can't deserialize from a stream a EOF: input stream error
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…