M
Mark A. Odell
I write a lot of drivers and there is inevitably some hardware register or
buffer descriptor field called 'flags'. The flags are defined, typically,
as bit positions. I was thinking I could get some compile-time type
checking when assigning to a flag field but I don't think I can. Here's
what I was thinking:
typedef unsigned long HwFlag;
struct HwThing
{
SomeType fieldOne;
SomeType fieldTwo;
HwFlag flags;
};
static const HwFlag flagA = 1 << 0;
static const HwFlag flagB = 1 << 1;
static const HwFlag flagC = 1 << 2;
But the problem is when combining flags:
struct HwThing dev;
dev.flags = flagA | flagB;
I think I require a cast back to HwFlag since flagA and B will promote to
some integer type won't they? That is, I'd need to write:
dev.flags = (HwFlag) (flagA | flagB);
which rather defeats my intent. Any suggestions?
Thanks.
buffer descriptor field called 'flags'. The flags are defined, typically,
as bit positions. I was thinking I could get some compile-time type
checking when assigning to a flag field but I don't think I can. Here's
what I was thinking:
typedef unsigned long HwFlag;
struct HwThing
{
SomeType fieldOne;
SomeType fieldTwo;
HwFlag flags;
};
static const HwFlag flagA = 1 << 0;
static const HwFlag flagB = 1 << 1;
static const HwFlag flagC = 1 << 2;
But the problem is when combining flags:
struct HwThing dev;
dev.flags = flagA | flagB;
I think I require a cast back to HwFlag since flagA and B will promote to
some integer type won't they? That is, I'd need to write:
dev.flags = (HwFlag) (flagA | flagB);
which rather defeats my intent. Any suggestions?
Thanks.