UIntSplit

Files CCore/inc/UIntSplit.h CCore/src/UIntSplit.cpp

UIntSplit is a Helper Type, it can split a bigger unsigned integral type value into an array of smaller ones and inverse.


template <class UIntBig,class UIntSmall>
class UIntSplit
 {
  public:

   static const unsigned Len = .... ;

  private:

   UIntSmall buf[Len]; // big-endian order
  
  public:
   
   // access
  
   UIntSmall operator [] (unsigned index) const { return buf[index]; }
 
   UIntSmall & operator [] (unsigned index) { return buf[index]; }
   
   PtrLen<UIntSmall> take() { return Range(buf); }

   // get
  
   UIntBig get() const;
   
   // set
   
   void set(UIntBig value);
 };

There is no default constructor, so the initial value of this class is indeterminate.

The "big" integer length must be a multiple of the "small" one. Internally the value of the class is stored in the buffer of the length Len. You can access this buffer using the method take().

You can get or set the value as UIntBig type value, using the methods get() or set(). But you can get or set UIntSmall parts of the value in the big-endian order using the overloaded operator []. An example:


struct Pair
 {
  uint16 a;
  uint16 b;

  Pair() {}

  Pair(uint16 a_,uint16 b_) : a(a_),b(b_) {}
 };

uint32 Combine(Pair p)
 {
  UIntSplit<uint32,uint16> split;

  split[0]=p.a;
  split[1]=p.b;

  return split.get();
 }

Pair Split(uint32 c)
 {
  UIntSplit<uint32,uint16> split;

  split.set(c);

  return Pair(split[0],split[1]);
 }