Bit field Enumerations
To define enumerations that are capable of handling bit field operations, similar to other
programming languages such as C#, Java or C++, the following example of a
Flags
class can be used:
Flags
Class
enum Flag
{
A = 1,
B = 2,
C = 4
};
enum OtherFlag
{
A = 1,
B = 2,
C = 4
};
class Flags
{
public Flags(anytype sample)
{
typeName = getTypeName(sample);
}
public add(anytype f)
{
checkType(f);
value |= (uint)f;
}
public sub(anytype f)
{
checkType(f);
value &= ~(uint)f;
}
public clear() { value = 0; }
public bool has(anytype f)
{
checkType(f);
return (value & (uint)f) != false;
}
public uint val() { return value; }
void checkType(anytype given)
{
if ( getTypeName(given) != typeName )
throw(makeError("ctrl", PRIO_SEVERE, ERR_PARAM, 28, getTypeName(given), typeName));
}
uint value;
string typeName;
};
main()
{
Flags flags = Flags(Flag::A);
flags.add(Flag::A);
flags.add(Flag::C);
DebugN(flags.val());
flags.sub(Flag::C);
DebugN(flags.val());
if ( flags.has(Flag::A) )
DebugN("flag A is set");
flags.add(OtherFlag::C);
}