Boost logo

Boost :

Subject: Re: [boost] [boost::endian] Request for comments/interest
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2010-06-02 08:25:21


----- Original Message -----
From: "Stewart, Robert" <Robert.Stewart_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Wednesday, June 02, 2010 1:24 PM
Subject: Re: [boost] [boost::endian] Request for comments/interest

>
> Terry Golubiewski wrote:
>> Assuming you just read a TCP stream of big-endian T's into
>> memory at a location 'char* buf', then you could endian-
>> convert the T's, if necessary, like this...
>>
>> const endian<big, T>* src = reinterpret_cast<const
>> endian<big, T>*>(buf);
>> endian<native, T>* dst = reinterpret_cast<endian<native, T>*>(buf);
>>
>> (void) copy(src, src+numTs, dst);
>
> A call to swap_in_place() avoids the ugly reinterpret_casts and looks far simpler.

You need a cast to see a void* as a T*. This was already the case with Tom code:

>void * data = ...;
>read(fh, data, size);
>MatrixHeader * mh = static_cast<MatrixHeader*>(data);

> Thus far, I'm not convinced that there is any value in the endian types, but keep trying!

If I'm not wrong, swap_in_place can be defined on top of the endian types as follows

template <typename E, typename T>
swap_in_place(T* data) {
    const endian<E, T>* src = reinterpret_cast<const endian<E T>*>(data);
    endian<native, T>* dst = reinterpret_cast<endian<native, T>*>(data);
    copy(src, src+numTs, dst);
}

used as follows

swap_in_place<big>(data);

This swap_in_place is a no-op if native is big.

Best,
Vicente


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