
----- Original Message ----- From: "Stewart, Robert" <Robert.Stewart@sig.com> To: <boost@lists.boost.org> 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