Boost logo

Boost Users :

From: Dominick Layfield (dom.layfield_at_[hidden])
Date: 2007-08-22 16:30:56


Frank Birbacher wrote:
> Robert Ramey schrieb:
>> > The real easy way is just to use a binary_object which saves/loads a
>> > specified number of bytes.
>> > so the above would look like:
>> >
>> > void write_to_cbuffer(char* buf_p, size_t* buf_sz_p) { // note only one *
>> > std::ostringstream oss;
>> > boost::archive::text_oarchive oa(oss);
>> > oa << binary_object(bf_sz_p, mc);
>> > }
>> > void read_from_cbuffer(const char* buf, const size_t buf_sz) {
>> > std::istringstream iss(buf_str);
>> > boost::archive::text_iarchive ia(iss);
>> > ia >> binary_object(buf_sz, buf)
>> > }
>
> You got this one mixed up. The char array is not something to serailize
> but it is the storage to searialize to. The "read_from_cbuffer" function
> does *read* the cbuffer and construct a my_class instance out of it by
> using serialization. The "write_to_cbuffer" serializes a my_class
> instance an stores the result in the cbuffer.
>
> I can think of using the old strstream class to read from the buffer or
> take something from Boost.IOStreams. And writing could be faster if you
> use a std::deque wrapped with IOStreams and later on std::copy this into
> a char array (instead of memcpy). A deque has usually better performance
> on push_back than a vector or string, I guess. But then I don't know the
> internals of a stringstream.
>
> Frank

Frank is right. I don't quite know what Robert thought I was looking for, but
it's pretty clear that his suggested code does not do what I want. (Nor does
it compile!)

To clarify:

write_to_cbuffer() should serialize a my_class instance into a c-style char*
buffer. It also needs to dynamically allocate storage for the buffer, set buf_p
to point at the buffer, and set buf_sz to the size of the buffer.

This is so that the legacy C code can then call write(fd, buf_p, buf_sz).

read_from_cbuffer() does the opposite: it extracts a serialized instance of
my_class from a char* buffer.

I'll take a look at the boost::iostreams stuff to see if I can figure out how to
make that work. Thanks for the tip, Frank.

BTW, I don't think a push_back() is any faster for a deque than a vector -- it
is push_front() that is faster for a deque. From the STL docs at SGI: "The main
way in which deque differs from vector is that deque also supports constant time
insertion and removal of elements at the beginning of the sequence".

-- Dominick


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