Boost logo

Boost :

Subject: Re: [boost] [boost::endian] Request for comments/interest
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2010-05-27 10:50:12


Kim Barrett wrote:
> On May 27, 2010, at 9:10 AM, Stewart, Robert wrote:
> > Here's another take on the interface:
> >
> > template <class T, class U>
> > void
> > from_big_endian(T &, U);
> >
> > [... snip several more ...]
> > template <class T, class U>
> > T
> > little_endian_cast(U);
> >
> > Obviously, I've embedded the direction and ordering in the
> > function template names rather than in a template argument,
> > but that looks more readable to me.
>
> What about various flavors of mixed / middle endian? Some of
> them actually do appear in the wild, which leads me to prefer
> the template argument approach to a potential proliferation
> of function names. The template argument approach also might
> be easier to work with in generic code.

I've never seen those, so I wasn't accounting for them. They couldn't be addressed by the library directly because they differ based upon data size and its context. Indeed, I can't think how that could be addressed except by some structure traversing functionality which would need to know the rules to apply to the fields in a given structure for a given mixed endianness.

If these functions are to apply to adapted structs, then encoding the endianness in the names is a problem. If they only apply to simpler types, then it isn't. Still, the argument about usefulness within generic code is strong as I can imagine an algorithm that is given endianness as a template parameter.

It isn't clear that directionality should be parameterized.

That leads to this:

   enum endianness { big_endian, little_endian };

   template <endianness E, class T, class U>
   void
   from(T &, U);

   template <endianness E, class T>
   void
   from(T &);

   template <endianness E, class T, class U>
   void
   to(T &, U);

   template <endianness E, class T>
   void
   to(T &);

   template <class T, class U>
   T
   big_endian_cast(U);

   template <class T, class U>
   T
   little_endian_cast(U);

The usage would change accordingly:

{
   int32_t wire(/* from wire */);
   long l;
   to<big_endian>(l, wire);
   /// OK if sizeof(long) >= sizeof(int32_t)
}
{
   int32_t value(/* from wire */);
   to<big_endian>(value);
}
{
   int32_t wire(/* from wire */);
   int i(little_endian_cast<int>(wire));
   /// OK if sizeof(int) >= sizeof(int32_t)
}

IOW, the spelling changes from "{from,to}_{big,little}_endian" to "{from,to}<{big,little}_endian>."

_____
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