Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template and bit-field

Reply
Thread Tools

template and bit-field

 
 
mathieu
Guest
Posts: n/a
 
      02-17-2007
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 maskBitsAllocated-BitsStored);
};
// mask first:
template<int BitsAllocated, int BitsStored>
struct PixelDetail<BitsAllocated, BitsStored, -1>
{
typedef typename BitsAllocatedToType<BitsAllocated>::Type Type;
private:
Type maskBitsAllocated-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;
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments