Boost logo

Boost :

From: Mark Borgerding (mborgerding_at_[hidden])
Date: 2000-03-10 21:39:10


jens maurer <jmaure-_at_[hidden]> wrote:
original article:http://www.egroups.com/group/boost/?start=2526
> "Borgerding, Mark A." wrote:
> > The concept is a wrapper around an integer type that automatically
convert
> > to/from the desired byte order.
>
> Please check out <boost/bin_ubin.hpp>, which already has the
> equivalent of big_endian<> in a CPU-endianess independent
> implementation.
> A little_endian<> class complements this nicely. However,
> we should merge it all in one header.
> Furthermore, I think that "big_endian<>" and "little_endian<>"
> express the semantics a bit better than the current "ubin32".

boost::bin32 makes the user pay for the byte order conversion whether
or not the machine byte order requires it. By hiding the ugly details
behind a compiler tag, the C++ motto of "pay only for what you need"
still rings true.

I also think my template version is much easier to grok.

>
> Aside from syntactical issues (namespace boost etc.), you
> should probably use std::reverse from the STL in reverse_bytes()
> instead of doing it your own (buggy) way:
>
> > template <class T>
> > inline
> > T reverse_bytes(T d)
> > {
> > T orig(d);
> > const char * pSrc = reinterpret_cast<const char *>(&orig);
> > const char * pSrcEnd = pSrc + sizeof(T);
> > char * pDest = reinterpret_cast<char *>(&d) + sizeof(T);
>
> > for (;pSrc != pSrcEnd;++pSrc)
> > *--pDest = *pSrc;
> > return d;
> >};
>
> pDest and pSrc will overlap and half of T will be lost.

If you look through it again, you will find that pSrc and pDest point
to different buffers. By doing it the way I did, I believe the number
of assignments is reduced.

I must admit I did not check this approach against std::swap. Even if
there is a slight difference in performance, it might be worth using
std::swap for readability's sake.


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