Boost logo

Boost Users :

From: Baranowski, Daniel (dbaranowski_at_[hidden])
Date: 2006-10-12 12:37:35


> -----Original Message-----
> From: boost-users-bounces_at_[hidden] [mailto:boost-users-
> bounces_at_[hidden]] On Behalf Of Nat Goodspeed
> Sent: Thursday, October 12, 2006 11:40 AM
> To: boost-users_at_[hidden]
> Subject: Re: [Boost-users] [Serialization] Serializing to a void * and
> asize?
>
> [Nat] Are you familiar with the std::ostringstream::str() method?

That's what I needed! I need to read up on streams (obviously). I had
seen that function before, but I guess I was stuck in the mind-set that
there would be a problem if there was a zero in the "string". I played
around with it and it work just fine!

Thanks a bunch Nat, you've been a big help. Glad it was a simple
solution.

And while I'm at it, thanks to Robert for making the library in the
first place!

V/R,
Daniel

And for completeness, here is the code:
#include <sstream>
#include <string>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/binary_object.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;
        ar & y;
        ar & z;
    }
public:
    A::A(){ x = 0; y = 0; z = 0; } // default constructor
    bool operator == (const A & RHS)
    {
       return ((x == RHS.x) && (y == RHS.y) && (z == RHS.z));
    }

    int x;
    int y;
    int z;
};

int main(int argc, char *argv[])
{
   // Create an object on the heap
   A * pA = new A;
   pA->x = 1234567890;
   pA->y = 0;
   pA->z = 987654321;

   // serialize it
   std::ostringstream sTestBinary1;
   boost::archive::binary_oarchive oa(sTestBinary1);
   oa << *pA;

   // Get the size of the buffer and a pointer to it
   std::string sBuff = sTestBinary1.str();
   size_t iSize = sBuff.size(); // ?? How do I get the size ??
   void * pVoid = (void *)sBuff.c_str(); // ?? Is this the right pointer
??

   // .. Magic occurs here (IE, gets sent across the network as the raw
data)

   std::string InBuff((char *)pVoid, iSize);
   // Create a stream to read from
   std::istringstream sTestBinary2;
   sTestBinary2.str(InBuff);

   // 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 == *pB);
   return 0;
}
**********************************************************************************************
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