Boost logo

Boost :

Subject: Re: [boost] [boost::endian] Request for comments/interest
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2010-06-02 09:48:53


vicente.botet wrote:
> Rob Stewart wrote:
> > 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);

Quite right, but a static_cast suffices, it is to a simpler type, and there is only one.

> > 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.

I'll accept that the casts are zero cost in this case, and copy() can be written better than Terry showed previously, but the extra complexity may thwart optimization, so performance testing or an examination of emitted object code would be necessary to verify your assertion.

_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer, Core Software using std::disclaimer;
Susquehanna International Group, LLP http://www.sig.com

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.


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