Boost logo

Boost :

Subject: Re: [boost] Networking TS + Beast, NEW Tutorials, Read this to learn std::net !!!
From: Christof Donat (cd_at_[hidden])
Date: 2019-03-15 09:27:18


Hi,

Am 15.03.2019 01:19, schrieb Vinnie Falco via Boost:
> On Thu, Mar 14, 2019 at 5:09 PM Ivan Matek <libbooze_at_[hidden]> wrote:
>> Quite a weird pattern, where buffer does not have member begin, end.
>> Maybe a sentence or two describing the rationale and complexity would
>> be nice.
>> I know it is O(1), but it is weird that begin/end are called on
>> pointer.
>
> Yeah, well... that's a sore subject. First of all I did not invent
> these buffer concepts, they are part of Asio and Networking TS. And
> they *used* to have begin() and end(), but then the C++ Standards
> Committee decided that they didn't like `const_buffers_1` and
> `mutable_buffers_1`, so they got rid of them in the TS draft. Then
> they realized that having `const_buffer::begin()` would be confusing.
> Does it refer to the beginning of the buffer sequence, or does it
> refer to the first byte of that buffer? They decided to get rid of
> begin/end entirely for the buffer types and add buffer_sequence_begin
> and buffer_sequence_end in its place. And this is why we can't have
> nice things.

Don't be sad :-)

template <typename BufferSequenceType>
class buffer_sequence_view {
     private:
         BufferSequenceType& sequence_;
     public:
         buffer_sequence_view(BufferSequenceType& s) : sequence_{s}
               {};
         buffer_sequence_view(buffer_sequence_view& other) :
sequence_{other.sequence_} {};
         // ...

         auto begin() { return buffer_sequence_begin(sequence_); };
         auto end() { return buffer_sequence_end(sequence_); };
        // ...
};

template <typename BufferType>
class buffer_view {
     private:
         BufferType& buffer_;
     public:
         buffer_view(BufferType& b) : buffer_{b} {};
         buffer_view(buffer_view& other) : buffer_{other.buffer_} {};
         // ...

         auto begin() { return range::begin(buffer_.data()); };
         auto end() { return range::end(buffer_.data()); };
        // ...
};

for( const auto& single_buffer : buffer_sequence_view(buffer_sequence) )
{
     // iterates over all elements of the buffer sequence ...
}

for( const auto& c : buffer_view(buffer) | view::take(42) ) {
     // iterates over the first 42 characters of a buffer (sequence) ...
     // I hope my interpretation of range::begin() and range::end() is
correct;
     // this is completely untested. ;-)
}

So you can still have all the nice things with ranges based for loops,
C++20 ranges, etc. Though I agree, that these views should be part of
the standard. Otherwise everyone is going to implement them on their own
with varying quality (you probably already have spotted some issues with
my untested example code here), or even worse, works without them.

Christof


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