FLAGS style enumeration?

6,399 views
Skip to first unread message

BeamMeUpScotty

unread,
Jul 28, 2008, 12:29:56 PM7/28/08
to Protocol Buffers

Is there a way to get a FLAGS style enumeration out of a proto
definition?

Scotty

Kenton Varda

unread,
Jul 28, 2008, 12:36:45 PM7/28/08
to BeamMeUpScotty, Protocol Buffers
What is a "FLAGS style enumeration", exactly?

BeamMeUpScotty

unread,
Jul 28, 2008, 1:45:24 PM7/28/08
to Protocol Buffers
Sorry for the confusion.

In C# an enumeration can be defined as:

[Flags]
public enum Status : byte {
TemperatureAlarm = 0x01,
AcidHighLevelAlarm = 0x02,
FloodAlarm = 0x04,
ChlorineAlarm = 0x08
}

and the value as:
Status stat = Status.ChlorineAlarm | Status.FloodAlarm;

This would be coded as a single byte rather than a list of bools
represented as bytes.

I'm not sure how to implement in a protobuf though, protobuf's
representation of an enumeration
is as a discreet constant like above, but I don't see how to combine
them to attain bit packing
in this situation.

Scotty

On Jul 28, 12:36 pm, "Kenton Varda" <ken...@google.com> wrote:
> What is a "FLAGS style enumeration", exactly?
>

Kenton Varda

unread,
Jul 28, 2008, 2:06:04 PM7/28/08
to BeamMeUpScotty, Protocol Buffers
You would have to declare the field as an integer.  For example:

message Foo {
  enum Flags {
    FLAG1 = 0x01;
    FLAG2 = 0x02;
    FLAG3 = 0x04;
  }

  // Bitwise-OR of Flags.
  optional uint32 flags = 1;

BeamMeUpScotty

unread,
Jul 28, 2008, 2:11:27 PM7/28/08
to Protocol Buffers
Ahhh I see. This will certainly work.

Thanks!

Marc Gravell

unread,
Jul 28, 2008, 2:51:23 PM7/28/08
to Protocol Buffers
Kenton : to clarify, does using the enum-type in the proto validate
the values? i.e. so it will only accept the discreet cited values?

Scotty : the reason I ask is that if it doesn't vaidate them, then it
would be trivial to support this style of usage from C#; but if the
current implementations *do* validate then it may be unwise. As an
alternative, I'm sure it would be possible to make something like
protobuf-net do this for you - so that at the client you get your
[Flags] enum, but it is treated purely as an uint32 on the wire
(rather than the enum handling).

Marc

Kenton Varda

unread,
Jul 28, 2008, 2:57:14 PM7/28/08
to Marc Gravell, Protocol Buffers
On Mon, Jul 28, 2008 at 11:51 AM, Marc Gravell <marc.g...@gmail.com> wrote:
Kenton : to clarify, does using the enum-type in the proto validate
the values? i.e. so it will only accept the discreet cited values?

Yes and yes.  An unknown enum value seen on the wire is treated like an unknown tag number -- this is important for security, since it's common to write switch statements that only consider the known enum values.  This is why I suggested using an int32 for the field rather than actually declaring it as the enum type.  Thus, the enum in my example is only declaring some numeric constants, not really a type.

Marc Gravell

unread,
Jul 28, 2008, 3:17:35 PM7/28/08
to Protocol Buffers
That makes perfect sense, thanks. And it means I don't have to add any
complex flags-unwrapping code!

So yet again, the deceptively clean design of protocol buffers saves
me some tricky work ;-p

Marc

redbaron

unread,
Jul 30, 2008, 1:02:39 AM7/30/08
to Protocol Buffers
> You would have to declare the field as an integer.  For example:
> message Foo {
>   enum Flags {
>     FLAG1 = 0x01;
>     FLAG2 = 0x02;
>     FLAG3 = 0x04;
>   }
>
>   // Bitwise-OR of Flags.
>   optional uint32 flags = 1;
>
> }

That should be added to docs. Extremely usefull tip.
Reply all
Reply to author
Forward
0 new messages