Boost logo

Boost :

Subject: Re: [boost] [integer] Promotion of endian library code from vault
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2008-09-25 19:23:27


----- Original Message -----
From: "Neil Mayhew" <neil_mayhew_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Thursday, May 29, 2008 1:59 AM
Subject: Re: [boost] [integer] Promotion of endian library code from vault

> On 26/05/08 02:31 PM Neil Mayhew wrote:
>> On 24/05/08 05:45 AM Beman Dawes wrote:
>>> Do you have a proposed design for bit-range access?
>>
>> I was thinking of adding methods to read and write the given bits within
>> the value:
>>
>> value_type endian::bits(std::size_t offset, std::size_t length) const;
>> value_type endian::bits(std::size_t offset, std::size_t length,
>> value_type value);
>>
>
> On second thoughts, I've realised this would be easier to read, and
> safer, if the bits() methods used start and end rather than offset and
> length. Then the values in the methods would match up, and it would be
> more obvious if there were gaps or overlaps:
>
> return abc.bits(0, 6);
> ...
> return abc.bits(6, 10);
> ...
> return abc.bits(10, 14);

Yes I think this is eassier to read and write.

>> A better design might be to specialize Bitfield to accept endians...

I don't think that bitfield needs to be modified to accept endians. The
support is already a parameter.

>>
>> If the design used Bitfield instead, there would be one access method
>> for both read and write, which would return by value a Bitfield on abc.
>
> Something like this:
>
> Bitfield<0, 6, big32_t> packet::field_a()
> {
> return Bitfield<0, 6, big32_t>(abc);
> };
>
Hi,

There is a problem with a single accessor, it not works for const packet.

If we want to mimic whatever we can do with C/C++ bitfields do not forget
that the library should take care of bitfields with different interger sizes
and sign respect to its support, so the resulting type must be added as
parameter. (or should the default conversions work?)

typedef bitfield<int_least8_t, 0, 6, big32_t> field_a_type;

In addition the bitfield class can provide two types reference and value and
the user can use as follows

field_a_type::reference field_a() {
    return field_a_type::reference(abc);
};
field_a_type::value::value_type field_a() const {
    return field_a_type::value(abc);
};

We can define a macro that will make easier the use of bitfield, including
the typedef and the accessors definition.

    BOOST_BITFIELD(int_least8_t, field_a, 0, 6, big32_t);

compare this to the C/C++ bitfield

    int_least8_t field_a:6

Vicente


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