template and bit-field

M

mathieu

Hello,

I am trying to use template programing and bit-field and I came up
with (*). I was not able to make a really clean solution but instead
separate into 3 cases:
1. mask is declare before the value
2. value is declare before the mask
3. for zero bit field mask need to be unnamed

Is there another solution that would have been cleaner (less
specific cases) ?

Thanks
-Mathieu

(*)
/*
Bits Allocated = 16
Bits Stored = 12
High Bit = 11

|<------------------ pixel -----------------
______________ ______________ ______________
______________
|XXXXXXXXXXXXXX| | |
|
|______________|______________|______________|
______________|
15 12 11 8 7 4 3
0

---------------------------

Bits Allocated = 16
Bits Stored = 12
High Bit = 15

|<------------------ pixel ----------------->|
______________ ______________ ______________
______________
| | | |
XXXXXXXXXXXXXX|
|______________|______________|______________|
______________|
15 12 11 8 7 4 3
0

---------------------------

Bits Allocated = 12
Bits Stored = 12
High Bit = 11

------ 2 ----->|<------------------ pixel 1 ---------------
______________ ______________ ______________
______________
| | | |
|
|______________|______________|______________|
______________|
15 12 11 8 7 4 3
0


-------------- 3 ------------>|<------------ 2
--------------
______________ ______________ ______________
______________
| | | |
|
|______________|______________|______________|
______________|
15 12 11 8 7 4 3
0


|<------------------ pixel 4 --------------->|<----- 3
------
______________ ______________ ______________
______________
| | | |
|
|______________|______________|______________|
______________|
15 12 11 8 7 4 3
0
*/

#include <iostream>

template <int> struct BitsAllocatedToType;
template<> struct BitsAllocatedToType<16> { typedef unsigned short
Type; };
template<> struct BitsAllocatedToType<12> { typedef unsigned short
Type; };
template<> struct BitsAllocatedToType<8> { typedef unsigned char
Type; };

template <int BitsAllocated, int BitsStored, int type> struct
PixelDetail;
// mask last:
template<int BitsAllocated, int BitsStored>
struct PixelDetail<BitsAllocated, BitsStored, 1>
{
typedef typename BitsAllocatedToType<BitsAllocated>::Type Type;
public:
Type v:BitsStored;
private:
Type mask:(BitsAllocated-BitsStored);
};
// mask first:
template<int BitsAllocated, int BitsStored>
struct PixelDetail<BitsAllocated, BitsStored, -1>
{
typedef typename BitsAllocatedToType<BitsAllocated>::Type Type;
private:
Type mask:(BitsAllocated-BitsStored);
public:
Type v:BitsStored;
};
// unamed zero bit-field:
template<int BitsAllocated, int BitsStored>
struct PixelDetail<BitsAllocated, BitsStored, 0>
{
// static_assert( BitsAllocated == BitsStored )
typedef typename BitsAllocatedToType<BitsAllocated>::Type Type;
private:
Type :(BitsAllocated-BitsStored);
public:
Type v:BitsStored;
};


// Bits Allocated
// Bits Stored
// High Bit
template <int BitsAllocated, int BitsStored, int HighBits>
struct Pixel : public PixelDetail<BitsAllocated, BitsStored,
(BitsAllocated - BitsStored) ? ((BitsStored < HighBits) ? 1 : -1) : 0>
{
typedef typename BitsAllocatedToType<BitsAllocated>::Type Type;
operator Type () const { return this->v; }
};

int main()
{
Pixel<16,12,15> pixel1;
Pixel<16,12,11> pixel2;
Pixel<12,12,11> pixel3;
Pixel<16,16,15> pixel4;

const unsigned short v = 0xcdef;
memcpy(&pixel1,&v,sizeof(unsigned short));
memcpy(&pixel2,&v,sizeof(unsigned short));
memcpy(&pixel3,&v,sizeof(unsigned short));
memcpy(&pixel4,&v,sizeof(unsigned short));

std::cout << std::hex << pixel1 << std::endl;
std::cout << std::hex << pixel2 << std::endl;
std::cout << std::hex << pixel3 << std::endl;
std::cout << std::hex << pixel4 << std::endl;

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top