template and bit-field

Discussion in 'C++' started by mathieu, Feb 17, 2007.

  1. mathieu

    mathieu Guest

    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;
    }
    mathieu, Feb 17, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    1,717
    Timothy Bendfelt
    Jan 19, 2007
  2. JustSomeGuy

    8 bit bit field?

    JustSomeGuy, Feb 27, 2006, in forum: C++
    Replies:
    2
    Views:
    361
    Victor Bazarov
    Feb 27, 2006
  3. Replies:
    9
    Views:
    939
    Juha Nieminen
    Aug 22, 2007
  4. Sound
    Replies:
    2
    Views:
    428
    Randy Webb
    Sep 28, 2006
  5. Jeff.M
    Replies:
    6
    Views:
    162
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...

Share This Page