Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2006-10-11 16:20:30


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 <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 << 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;
> }
>


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