By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,303 Members | 1,642 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,303 IT Pros & Developers. It's quick & easy.

template and bit-field

P: n/a
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 <intstruct 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 typestruct
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,15pixel1;
Pixel<16,12,11pixel2;
Pixel<12,12,11pixel3;
Pixel<16,16,15pixel4;

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;
}

Feb 17 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.