|
Boost : |
From: Darryl Green (Darryl.Green_at_[hidden])
Date: 2003-07-23 03:34:15
> -----Original Message-----
> From: Jan Gaspar [mailto:jga_at_[hidden]]
> Ok, it would be possible.
>
> Pavel Vozenilek wrote:
>
> > Would it be possible to add helper function 'flatten()' into
> > circular_buffer?
> >
> > After invocation, user would be sure of:
> >
> > &buff[0] < &buff[1] < ... < &buff[n]
> >
[snip]
> > can be handy:
> > - when legacy C function requires continuous array (like Win32 API)
> > - when buffer is being saved to file or written to socket
> > - special purpose adaptor could present result as
> (nonresizable) vector<>
> >
> > Alternatives are simple but much more CPU/memory expensive.
> >
What if you don't want to flatten it, you just want it accessible as
flat ranges? Perhaps a function taking an iterator range [first,last)
and returning last_contiguous, where last_contiguous is < last and
&*last_contiguous >= &*first. Alternatively, it could return a pointer,
or pair of pointers to provide the (pointer) half closed range [&*first,
(&*last_contiguous) + 1).
Something like the following would write the content of a
circular_buffer of char to a socket:
i = buff.begin();
while ( i != buff.end() &&
n = write(fd, &*i, buff.last_contiguous(i, buff.end()) - i + 1 )) > 0)
i += n;
I'm not sure how to do something equally efficient for reading data from
a socket and appending the data to a buffer - very similar code would
work, but only for copying in, not for inserting. Ideally, I would like
to be able to obtain a contiguous uninitialised storage range starting
from buff.end(), copy data into the buffer then update the buffer size
accordingly. It might be cleaner to do this via a callback interface eg:
// function to do the read
int read_from_fd(T *p, size_type n)
{
if ((n = read(fd, p, n)) < 0) {
throw something;
}
return n;
}
// append_from calls read_from_fd passing it &*buffer.end() and the
contiguous space available
// on return, the buffer state is updated (end advanced by the value
returned).
buffer.append_from(read_from_fd);
Regards
Darryl Green.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk