Today I landed bug 1371771 [1] (on inbound at the time of writing
this), which adds a new MOZ_DEFINE_ENUM macro to MFBT.

This macro defines an enumeration, together with a constant that
stores the number of enumerators (and, in addition, another constant
that stores the highest enumerator).

For example,

  MOZ_DEFINE_ENUM(Foo, (A, B, C));

will expand to:

  enum Foo { A, B, C };
  constexpr size_t kFooCount = 3;
  constexpr Foo kHighestFoo = Foo(kFooCount - 1);

This avoids having to give your enumeration a "sentinel" value for
purposes such as IPC serialization (since you can just use the
constant generated by the macro instead), which in turn means you
don't need special handling for a sentinel value in switch statements
and such.

There are also some variants of the macro to support use cases such as
enum classes, enums with a specified underlying type, and enums at
class scope. See the documentation in mfbt/DefineEnum.h for more
details and motivation.

Consider using MOZ_DEFINE_ENUM instead of a sentinel enumerator in
newly added enumerations, and, if you're so inclined, consider
refactoring existing enumerations to use MOZ_DEFINE_ENUM instead of a
sentinel (I've already done so for enumerations in gfx/layers in bug
1377020 [2]).

Cheers,
Botond

[1] https://bugzilla.mozilla.org/show_bug.cgi?id=1371771
[2] https://bugzilla.mozilla.org/show_bug.cgi?id=1377020
_______________________________________________
dev-platform mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to