asio-tr2
asio-tr2 copied to clipboard
Revisit design of separate buffer_size/buffer_cast vs member functions const_buffer/mutable_buffer
Pre-Lenexa Summary
[buffer.const], [buffer.mutable]
To access the contents of a buffer one must currently write something like:
const_buffer b = buffer(...);
size_t s = buffer_size(b);
const unsigned char* p = buffer_cast<const unsigned char*>(b);
During pre-Boost-review discussions, the design of the buffer classes was selected as a tradeoff between the need to access the underlying memory, and wanting to discourage type safety violations. That is, deliberate code uglification as a tool of discouragement.
The proposal is that we should just have simple member functions to access the contents of the buffer. For example:
class const_buffer
{
...
const void* data() const;
size_t size() const;
...
};
class mutable_buffer
{
...
void* data() const;
size_t size() const;
...
};
Which design is preferred?
However, please note that there is a ConstBufferSequence overload of buffer_size(). This would be retained even if the above change is made.
(An additional thought: perhaps if buffer_cast goes then address_cast should too as a matter of consistency. See #151.)
I like the proposed use of member functions better. This way it will be easier to write concise adapters to map one's own buffer objects to the *BufferSequence concept requirements. This is even more the case with DynamicBufferSequence, that has even more requirements than the above.
On Tue, May 5, 2015 at 6:50 PM, chriskohlhoff [email protected] wrote:
Pre-Lenexa Summary
[buffer.const] http://open-std.org/JTC1/SC22/WG21/docs/papers/2015/n4478.html#buffer.const , [buffer.mutable] http://open-std.org/JTC1/SC22/WG21/docs/papers/2015/n4478.html#buffer.mutable
To access the contents of a buffer one must currently write something like:
const_buffer b = buffer(...); size_t s = buffer_size(b); const unsigned char* p = buffer_cast<const unsigned char*>(b);
During pre-Boost-review discussions, the design of the buffer classes was selected as a tradeoff between the need to access the underlying memory, and wanting to discourage type safety violations. That is, deliberate code uglification as a tool of discouragement.
The proposal is that we should just have simple member functions to access the contents of the buffer. For example:
class const_buffer { ... const void* data() const; size_t size() const; ... };
class mutable_buffer { ... void* data() const; size_t size() const; ... };
Which design is preferred?
— Reply to this email directly or view it on GitHub https://github.com/chriskohlhoff/asio-tr2/issues/84#issuecomment-99031577 .