|
Boost Users : |
From: Baranowski, Daniel (dbaranowski_at_[hidden])
Date: 2006-10-11 14:05:39
I've been tasked with creating a set of objects that will need to be
passed off to some other function as raw data in the form of a void *
and a size. Eventually, this goes through some networking code and
through the process of magic, shows up on another computer and has to be
reassembled.
So, to avoid being limited to simple structs where I could just cast it
to a void * and get the sizeof(), I was thinking about using the
serialization library.
The examples are all very good and have helped me understand how to
serialize to a file, and I even saw an example for using serializing
with asio (which I wish I could use). Unfortunately, I don't have
control over the networking and can't use the asio library.
I guess my questions is, what's the best way to serialize to a void *
and a size? I'm pretty illiterate on streams, so I've started to read up
on it, but I haven't figured it out yet, though I feel that's probably
where the answer lies.
Here's a test program that compiles, but obviously doesn't work:
#include <sstream>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
// A very simple test class with some data in it
class A
{
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */){
ar & x;
}
public:
A::A(){ x = 0; } // default constructor
int x;
virtual A::~A(){} // default destructor
};
int main(int argc, char *argv[])
{
// Create an object on the heap
A * pA = new A;
pA->x = 1234567890;
// serialize it
std::ostringstream sTestBinary1;
boost::archive::binary_oarchive oa(sTestBinary1);
oa << *pA;
// Get the size of the buffer and a pointer to it
size_t iSize = 0;//sTestBinary1.size(); // ?? How do I get the size
??
void * pVoid = sTestBinary1.rdbuf(); // ?? Is this the right pointer
??
// .. Magic occurs here (gets sent across the network as the raw
data)
// Create a stream to read from
std::istringstream sTestBinary2;
sTestBinary2.read((char *)pVoid, iSize);
// open the archive
boost::archive::binary_iarchive ia(sTestBinary2);
// restore the object from the archive
A * pB = new A; // pB->x equals 0 now
ia >> *pB;
// Make sure the objects contain the same data
assert(pA->x == pB->x);
return 0;
}
Thanks for any help,
Daniel
**********************************************************************************************
Disclaimer - This email and any files transmitted with it are proprietary and may contain privileged or copyright information. You must not present this message to another party without
gaining permission from the sender. If you are not the intended recipient you must not copy, distribute or use this email or the information contained in it for any purpose other than to notify
us. If you have received this message in error, please notify the sender immediately, and delete this email from your system. We do not guarantee that this material is free from viruses or any
other defects although due care has been taken to minimize the risk. eSafe scanned this email for viruses, vandals and malicious contentAny views expressed in this message are those of
the individual sender, except where the sender specifically states them to be the views of LSI.
**********************************************************************************************
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net