Boost logo

Boost :

Subject: Re: [boost] Interest in a simple buffer abstraction?
From: Boris Kolpackov (boris_at_[hidden])
Date: 2011-01-27 09:10:17


Hi Mathias,

Mathias Gaunard <mathias.gaunard_at_[hidden]> writes:

> In C++0x std::vector has a data() member just like your buffer type.

True, thought, that's not the only problem with vector's interface
when used as buffer. Probably just the ugliest.

> > b.copy (data, size); | b.assign (data, data + size);
>
> How about std::vector<char> a; std::vector<char> b = a;

Not sure I follow what you are trying to say here.

> > b.append (data, size); | b.insert (b.end (), data, data + size);
>
> Appending is more of a string thing. Are you sure you want this?

I don't see why not, but I am open to change my mind if there is a
good reason not to provide it.

> > void* data;
> > ...
> > buffer b (data, size); // Ok.
> > vbuffer vb (data, data + size); // Error.
>
> Beware of the strict aliasing rules.

Seeing that we will just pass the pointer to memcpy, I think we will
be safe.

> > Also, for a binary buffer it is nice to be able to assume ownership of
> > an existing raw buffer and to detach the underlying raw buffer.
>
> And how is that allowed by your interface?

There would a special constructor for that:

buffer (char* data, size_t size, size_t capacity, bool assume_ownership);

> Also, how do you take care of how the memory is allocated when copying
> said buffer?

You probably mean "freeing said buffer" instead of "copying said buffer".
The simplest approach would be to stipulate that such a buffer should be
allocated with operator new[] and freed with operator delete[].

Boris

-- 
Boris Kolpackov, Code Synthesis        http://codesynthesis.com/~boris/blog
Compiler-based ORM system for C++      http://codesynthesis.com/products/odb
Open-source XML data binding for C++   http://codesynthesis.com/products/xsd
XML data binding for embedded systems  http://codesynthesis.com/products/xsde

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk