Mojom: Automatically assign enum values for enum used as bitfields |
||||
Issue descriptionIn the Chrome IPC To Mojo IPC Cheat Sheet docs, an example is given where an enum is used as a bitfield where the values are specified manually[1]. Is it possible to have another attribute much like [Extensible] that automatically sets these values properly for bitfields? ------------------------------------------------------------------------- [1] https://www.chromium.org/developers/design-documents/mojo/chrome-ipc-to-mojo-ipc-cheat-sheet#TOC-Mojo3 Excerpt: // The Extensible attribute tells Mojo to skip validation. For bitfields, // there can be many combinations of possible values. Extensible should only be // used for these sorts of situations. [Extensible] enum DragOperation { None = 0, Copy = 1, Link = 2, Generic = 4, Private = 8, Move = 16, Delete = 32, };
,
Jan 24 2017
My main concern is having large numbers if the number of enum values is particularly large. This makes it harder to confirm that the value is correct and could lead to mistakes. Using bit shifts would definitely help as you could tell at a glance whether the value sets the correct bit.
,
Jan 24 2017
That documentation page is out of date. Mojom enums don't work well for bitfields. For C++, they generate scoped enums which don't implicitly cast from or to the underlying storage type so bitwise operations require casts. Instead, use constants for the values and an int of matching size as the bitfield type. See https://cs.chromium.org/chromium/src/ui/events/mojo/event_constants.mojom.
,
Jan 24 2017
I had a discussion with mbrunson@ about this off-thread a few days ago, so I'm just going to persist what I remember from it here. My comment was roughly the same as sammc@'s: C++ enum classes are not compatible with the treatment of enums as bitfields. This is trivial to see because the disjunction of any two valid enum values produces an invalid enum value. There are two alternatives: 1. Integer constants and integer IPC fields. Pro: simple, Con: not typesafe, not automagically validated 2. Structs of bools. Pro: typesafe and obviously validated, Con: more verbose, maybe not idiomatic historically? mbrunson@ informed me that #2 was the original approach used in his case, but that he was advised to change it. I would like to advocate that we consider this option more carefully.
,
Jan 24 2017
Approach #2 seems OK to me; it's not super efficient in terms of packing, but it's not that bad either. The struct traits can map from bool fields back to bitsets too if it wants.
,
Jan 24 2017
bools are packed tightly in mojom structs, if that's what you're referring to. The extra indirection is unfortunate if we don't inline the struct, but we should be able to trivially improve the rule which dictates inline vs non-inline.
,
Jan 24 2017
In that case, even better =)
,
Jan 24 2017
RE #6: when you said "inline", do you mean (1) using InlinedStructPtr to avoid heap allocation; or (2) changing mojo wire format so that we don't have indirection in the encoded message? (1) could be easily done; (2) however will affect versioning and therefore we need to be careful about this.
,
Jan 24 2017
Oh I just meant the first one. Changing the wire format doesn't seem necessary.
,
Oct 17
,
Nov 15
Per discussion, using enums for bitfields is strongly discouraged. |
||||
►
Sign in to add a comment |
||||
Comment 1 by yzshen@chromium.org
, Jan 24 2017