[Serialization] Serializing to a void * and a size?
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
Baranowski, Daniel wrote:
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.
Look inthe documenation Reference/Serializable Concept/Serialzation Wrappers/ Binary Objects
Here's a test program that compiles, but obviously doesn't work: #include <sstream> #include
#include // 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 << binary_object(sizeof(A), pA);
//oa << *pA;
// .. 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 >> binary_object(sizeof(A), pB) //ia >> *pB;
// Make sure the objects contain the same data assert(pA->x == pB->x); return 0; }
participants (2)
-
Baranowski, Daniel
-
Robert Ramey