Picon

in-memory stream, binary archive,serialization

Hello,

I need to serialize into and from a memory buffer using 
boost::archive::binary_oarchive and binary_iarchive.

Using std::stringstream does not work (it does work with text_oarchive 
and text_iarchive in memory and using files).

I found a mention in the archives about similar need here:
    http://lists.boost.org/boost-users/2007/10/31580.php

But I cannot figure it out.

I am trying to flesh out the two operations marshall() and unmarshall() 
below, such that an instance of STORAGE_TYPE
will hold the resulting bytes of an archival operation of a class of 
type T, or the inverse where I have the STORAGE_TYPE
and want to create a new T from an oarchive operation.

#include <string>
#include <vector>
#include <sstream>

#ifdef USE_TEXT
typedef std::string STORAGE_TYPE;
#else
typedef std::vector<unsigned char> STORAGE_TYPE;
#endif

template<typename T>
(Continue reading)

Matthias Troyer | 12 May 17:00
Picon

Re: in-memory stream, binary archive,serialization


On 12 May 2008, at 01:20, Victor Whiskey Yankee wrote:

> Hello,
>
> I need to serialize into and from a memory buffer using
> boost::archive::binary_oarchive and binary_iarchive.
>
> Using std::stringstream does not work (it does work with text_oarchive
> and text_iarchive in memory and using files).
>
> I found a mention in the archives about similar need here:
>    http://lists.boost.org/boost-users/2007/10/31580.php
>
> But I cannot figure it out.

Have you tried using the Boost IO Stream library to create a stream  
that buffers in memory and use that with a binary archive?

Matthias
Picon

Re: in-memory stream, binary archive,serialization

Matthias Troyer wrote:
On 12 May 2008, at 01:20, Victor Whiskey Yankee wrote:
Hello, I need to serialize into and from a memory buffer using boost::archive::binary_oarchive and binary_iarchive. Using std::stringstream does not work (it does work with text_oarchive and text_iarchive in memory and using files). I found a mention in the archives about similar need here: http://lists.boost.org/boost-users/2007/10/31580.php But I cannot figure it out.
Have you tried using the Boost IO Stream library to create a stream that buffers in memory and use that with a binary archive?
The posting I mentioned above suggests the same thing. As I said, I did try for several hours, but the
iostreams documentation is so difficult I could not figure it out. 

I would have thought that in-memory iostreams would be a standard part of that lib, but obviously not...
I must be the only one that does not want to use file i/o. Jeez.

Anyway, I finally got it to work in-memory with binary archives using the ios::binary arg to stringstream, like this...

   std::ostringstream ss(std::ios::out|std::ios::binary);
   boost::archive::binary_oarchive oa(ss);
   oa << myObj;

I don't know how efficient it is. If anyone knows of a better way I sure would like to see the code.

Thanks,
Vic

_______________________________________________
Boost-users mailing list
Boost-users <at> lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Matthias Troyer | 12 May 23:53
Picon

Re: in-memory stream, binary archive,serialization


On 12 May 2008, at 17:14, Victor Whiskey Yankee wrote:

Matthias Troyer wrote:
On 12 May 2008, at 01:20, Victor Whiskey Yankee wrote:
Hello, I need to serialize into and from a memory buffer using boost::archive::binary_oarchive and binary_iarchive. Using std::stringstream does not work (it does work with text_oarchive and text_iarchive in memory and using files). I found a mention in the archives about similar need here: http://lists.boost.org/boost-users/2007/10/31580.php But I cannot figure it out.
Have you tried using the Boost IO Stream library to create a stream that buffers in memory and use that with a binary archive?
The posting I mentioned above suggests the same thing. As I said, I did try for several hours, but the
iostreams documentation is so difficult I could not figure it out. 

I have a working version that I could send. It is just a few lines of code.

Matthias

_______________________________________________
Boost-users mailing list
Boost-users <at> lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Picon

Re: in-memory stream, binary archive,serialization

Matthias Troyer wrote:

On 12 May 2008, at 17:14, Victor Whiskey Yankee wrote:

Matthias Troyer wrote:
On 12 May 2008, at 01:20, Victor Whiskey Yankee wrote:
Hello, I need to serialize into and from a memory buffer using boost::archive::binary_oarchive and binary_iarchive. Using std::stringstream does not work (it does work with text_oarchive and text_iarchive in memory and using files). I found a mention in the archives about similar need here: http://lists.boost.org/boost-users/2007/10/31580.php But I cannot figure it out.
Have you tried using the Boost IO Stream library to create a stream that buffers in memory and use that with a binary archive?
The posting I mentioned above suggests the same thing. As I said, I did try for several hours, but the
iostreams documentation is so difficult I could not figure it out. 

I have a working version that I could send. It is just a few lines of code.

Could you just post it to the list?

Thanks,
Victor

_______________________________________________
Boost-users mailing list
Boost-users <at> lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Matthias Troyer | 15 May 19:31
Picon

Re: in-memory stream, binary archive,serialization


On 14 May 2008, at 22:17, Victor Whiskey Yankee wrote:

Could you just post it to the list?

Here is an example program:

#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/iostreams/stream_buffer.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/back_inserter.hpp>
#include <boost/serialization/vector.hpp>
#include <vector>
#include <iostream>



int main()
{    

  namespace io = boost::iostreams;
  typedef std::vector<char> buffer_type;
  buffer_type buffer;

  const std::vector<double> data(10000000);

  io::stream<io::back_insert_device<buffer_type> > output_stream(buffer);
  boost::archive::binary_oarchive oa(output_stream);

  oa << data;
  output_stream.flush();

  std::vector<double> data_in;

  

  io::basic_array_source<char> source(&buffer[0],buffer.size());
  io::stream<io::basic_array_source <char> > input_stream(source);
  boost::archive::binary_iarchive ia(input_stream);

  

  ia >> data_in;
}



_______________________________________________
Boost-users mailing list
Boost-users <at> lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Gmane