Boost logo

Boost :

Subject: Re: [boost] [boost::endian] Summary of discussion #1
From: Terry Golubiewski (tjgolubi_at_[hidden])
Date: 2010-06-03 16:27:54


>> Beman's approach is integer centric and combines the use of
>> non-natively-sized integers and endianness.
>> I would like to see Beman's library split into two parts: the integer
>> part
>> and the endian-part.

It's easy for me to suggest that someone else should change. But what would
such a change look like.
How about this... (I've ignored alignment)

namespace boost { namespace interface {

template<size_t Bits, endian_t Endian=native>
class PackedInteger {
public:
   typedef typename boost::int_t<Bits>::least value_type;
   static const endian_t endianness = Endian;

private:
   boost::array<char, (Bits+CHAR_BITS-1)/CHAR_BITS> m_storage;
   void store(value_type x); // depends on Endian choice
   value_type retrieve() const; // depends on Endian choice

public:
   PackedInteger(): { m_storage.fill('\0'); }
   explicit PackedInteger(value_type x) { store(x); }
   operator value_type() const { return retrieve(); }
   PackedInteger& operator=(value_type x) { store(x); }
   // All the usual integer operations go here.
}; // PackedInteger

} } // boost::interface

My point is that one must consider "how" the compact integer will be stored
in memory, so endianness is important to an implementation of
PackedInteger<Bits>.
PackedInteger<> must provide for all possible native endiannesses, so why
not make it a template parameter that defaults to "native"?
So Beman's "endian" survives almost intact with just a name change from
"endian" to "PackedInteger" and a reordering of the template parameters.

Then I suppose endian<E, T> should be specialized to support a
PackedInteger<Bits, Endian>, but I can't think of what it would be used for,
since in a message specification it would be easier to just use
"PackedInteger<24, big>" instead of "endian<big, PackedInteger<24>>".
Still, either should work, so here it is...

template<endian_t E1, size_t Bits, endian_t E2>
class endian<E1, PackedInteger<Bits, E2>> {
public:
   typedef PackedInteger<Bits, E2> value_type;
private:
   typedef PackedInteger<Bits, E1> Storage;
   Storage m_storage;
public:
  endian() : m_storage() { }
  endian(value_type x) : m_storage(typename Storage::value_type(x)) { }
  operator value_type() const { return value_type(typename
Storage::value_type(m_storage)); }
  endian& operator=(value_type x) { m_storage = typename
Storage::value_type(x); }
};

Well, thats my 5000 cents.

terry


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