I have the following types:
Now I want to define a function that takes a set of TMyType and turns it into a binary representation of that set (a.k.a Flags).
TMyType = (mtOne = 1, mtTwo = 2, mtFour = 4, mtEight = 8);
TMySet = set of TMyType;
I've done the following:
It works but there are some drawbacks:
r := 0;
for I := low(TMyType) to high(TMyType) do
if I in s then
r := r or Integer(I);
> It only works for TMyType. I have to define a load of other ones if I have many set types that need conversion.
> It doesn't perform well when TMyType contains elements representing large numbers. The loop will go through all numbers between low(TMyType) and high(TMyType). This is really wastefull because in this case, I'd only use 1,2,4,8,16 etc..
Is there any better way of converting sets to binary representation? I feel that this would be a very powerfull feature. It allows you to make better wrappers for C libraries that make use of flags. Sets are much easier to use and are more high-level.