C++, FTL, Programming

Simplifying the Flag Set

In the last few posts, I’ve covered the development of a type safe and easy to use flag set, and as a result it now contains 3 required template parameters, two of which declare default values.

template <
          typename TEnum,
          int TMaxFlags = 8,
          typename TNames = union_names<bit_set_size<TMaxFlags>::Value>
class flag_set

The last two properties have default values, but to be honest, people don’t want to have to add a Noof option to the declaration just to define the flag names.  And they don’t want to keep adding long type declarations, even when using typedef’s to cut down on the clutter.

We have the following coding standard at work which can provide an interesting structure defining both our flags and flag names

Enums should be defined within a structure to provide proper name scoping

struct MyProperty
  enum Enum
MyProperty::Enum property = MyProperty::FlagEntry1;

In the past enums were defined with the name of the enum appended to the names of its entries. While this does reduce name conflicts it is a poor way to scope a property.

Since we can guarantee that our enums are defined like the above, we can make the assumption that people will (or will be happy to) add their flag names as part of this structure.

// Structure defining the flag enums and also
// defining the flag names which we need
struct StateFlags
  // Flags
  enum Enum


  // Names
  struct Names
    uint HasTarget:1;
    uint CarryingWeapon:1;
    uint Drugged:1;

Making that assumption allows us to define the following structure

template <typename TFlags>
struct flag_set_ex
  typedef flag_set <
                     typename TFlags::Enum,
                     typename TFlags::Names
                   > flags;

This then allows users to define their flags sets quickly using all the available properties

flag_set_ex<StateFlags>::flags myStateFlags;

If programmers don’t want to use the flag names, they can obviously use the base flag set type. And to keep things consistent, we can add the a ‘::flags’ typedef to the base flag set allowing the definitions to be the same regardless of the flag set type being used.

template < typename EnumT, uint SizeT, typename NamesT >
class flag_set
  typedef flag_set<EnumT, SizeT, NamesT>     flags;

// Define a normal flag set with the same syntax as the flag_set_ex making
// it much easier to interchange the types depending on their needs
flag_set<StateFlags::Enums>::flags myStateFlags;

1 thought on “Simplifying the Flag Set”

  1. I was experimenting the other day with customizing the build chain in VS to parse files in the project that have a particular extension in a custom mannor (it simply passes the input/output filenames to a batch file). It meant that we could have a file:
    File: StateFlags.flagset

    And the batch file would create the entire StateFlags.h file, with struct, Names, enum and strings. And because it went through the build chain of VS, it means that whenever the FlagSet file is touched, it rebuilds it in the usual way, passing the file to our batch file, and updating the header.

    Might be a way to make the whole process super easy one day.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s