Bit flags in template variable

A field in a file is a combination of enum values, like:

enum <unsigned short> Flags {
    foo = 1,
    bar = 2,
    baz = 4
};

Where the field value could be 6, meaning “bar | baz”. When it’s a single value, it displays as expected (i.e. 1 = “foo”), but combined results just show the numerical value instead of the set flags.

Is there a way to have the value column of the template show the combination of flags for a given enum type?

010 Editor does not have a built-in flags type right now although we may add one in the future. For right now you can replicate a flags type using a custom ‘read’ function. Something like this should work:

enum <unsigned short> FlagsBase {
    foo = 1,
    bar = 2,
    baz = 4
};
typedef FlagsBase Flags <read=ReadFlags>;

string ReadFlags( Flags &f )
{
    string str;
    if( f & foo )
        str += EnumToString(foo) + " | ";
    if( f & bar )    
        str += EnumToString(bar) + " | ";
    if( f & baz )    
        str += EnumToString(baz) + " | ";
    return str;
}

Let us know if you have any trouble getting this to work.

Graeme
SweetScape Software

1 Like

I have templates that have quite a lot of enums with a lot of flags.

While this is a nice solution, I didn’t find a way to apply it generically, and writing out all those enums is a lot of work generating a lot of boilerplate code.

I appreciate your commitment to add flags in the future; IMHO a checkable dropdown list would accompany it quite nicely!

Thank you for the great product.

For this I use bit field structs:

struct Flags {
    unsigned short foo : 1;
    unsigned short bar : 1;
    unsigned short baz : 1;
};

With this it shows all combinations with no problems.
It displays all struct fields, and if result is 0 it is not enabled, and if it is 1 it is enabled.

Example result with Flags = 6:
foo = 0
bar = 1
baz = 1

If you don’t want to see 0 and 1 but a custom value, you can do a simple additional enum:

enum <unsigned short> IsOn {
    OFF = 0,
    ON = 1
};

And then use it instead:

struct Flags {
    IsOn foo : 1;
    IsOn bar : 1;
    IsOn baz : 1;
};

The same example result with 6:
foo = OFF
bar = ON
baz = ON