I want to set a FourCC value in C++, i.e. an unsigned 4 byte integer.
I suppose the obvious way is a #define, e.g.
#define FOURCC(a,b,c,d) ( (uint32) (((d)<<24) | ((c)<<16) | ((b)<<8) | (a)) )
and then:
uint32 id( FOURCC('b','l','a','h') );
What is the most elegant way you can think to do this?
You can make it a compile-time constant using:
template <int a, int b, int c, int d>
struct FourCC
{
static const unsigned int value = (((((d << 8) | c) << 8) | b) << 8) | a;
};
unsigned int id(FourCC<'a', 'b', 'c', 'd'>::value);
With a little extra effort, you can make it check at compile time that each number passed in is between 0 and 255.
uint32_t FourCC = *((uint32_t*)"blah");
Why not this?
EDIT: int -> uint32_t.
And no it does not cast a char** to uint32_t. It casts a (char*) to (uint32_t*) then dereferences the (uint32_t*). There is no endian-ness involved, since its assigning an uint32_tto an uint32_t. The only defects are the alignment and the I hadn't explicitly indicated a 32bit type.
By using C++11 constexpr you can write something like:
constexpr uint32_t fourcc( char const p[5] )
{
return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
}
And then use it as:
fourcc( "blah" );
pros:
cons:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With