user5688956
user5688956

Reputation:

Can you explain this enum declaration?

I was reading the code from a JsonParser and I am wondering how this enum works:

enum JsonTag {
    JSON_NUMBER = 0,
    JSON_STRING,
    JSON_ARRAY,
    JSON_OBJECT,
    JSON_TRUE,
    JSON_FALSE,
    JSON_NULL = 0xF
};

Upvotes: 0

Views: 189

Answers (4)

Dusteh
Dusteh

Reputation: 1536

  • Does this mean that NULL is defined as a half byte 1111?

No, in general, unless specified explicitly the underlying enum type is an int. The value in this case is simply 0xF - no half bytes there.

  • Does this mean that every value in between NUMBER and NULL will be between 0 and 0xF?

It will start from JSON_NUMBER=0 and increase by one until JSON_FALSE, then for JSON_NULL it will be 0xF

  • Does this mean NULL is some kind of memory location?

No.

  • What would the value of STRING, ARRAY... be?

As described previously.

  • Why declare an enum this way?

Typically to have a value which can be used as undefined or invalid. It can denote that something went wrong when executing a function so the enum value was not set to a valid entry.

Upvotes: 0

Thomas Matthews
Thomas Matthews

Reputation: 57688

Does this mean that JSON_NULL is defined as a half byte 1111?

The value 0x0F (hexadecimal) is 15 in decimal and 1111 in binary.

Does this mean that every value in between JSON_NUMBER and JSON_NULL will be between 0 and 0xF?

Not necessarily. Assigned enum values have nothing to do with ranges.
When an enum identifier does not have a specific value, it will use the previous value + 1.

Does this mean JSON_NULL is some kind of memory location?

The only meaning you can get is that the identifier JSON_NULL is assigned the value 15. There is nothing stating the purpose of any of the identifiers.

What would the value of JSON_STRING, JSON_ARRAY... be?

Here are the values of the identifiers:

enum JsonTag {
/* 0 */    JSON_NUMBER = 0,
/* 1 */    JSON_STRING,
/* 2 */    JSON_ARRAY,
/* 3 */    JSON_OBJECT,
/* 4 */    JSON_TRUE,
/* 5 */    JSON_FALSE,
/* 15 */    JSON_NULL = 0xF
};

Why declare an enum this way?

One reason is to have sequential symbols, and allow for a symbol that is not sequential. Perhaps the author doesn't have 15 symbols defined, but only the first 6. The JSON_NULL seems to have a special value of 15, so it is listed that way.

You may want to explore the JSON object types in the json data specification.

Upvotes: 2

SergeyA
SergeyA

Reputation: 62573

It is very simple. Enum values can be either explicitly assigned or implicitly defined. When assigned explicitly - like JSON_NULL in your example - they simply have this value. In this case it is 15. (Not sure why fancy hex notation is used here).

When assigned implicitly, they are always equal to previous enum value + 1, the very first value being set to 0.

And, just in case, the full names are defined there. Not NULL, but JSON_NULL, not NUMBER, but JSON_NUMBER, etc.

Upvotes: 4

Jesper Juhl
Jesper Juhl

Reputation: 31465

0xF is just 15 (decimal) in hexadecimal notation - nothing special about it. Enum entries either have the value explicitly assigned or the value of the previous entry plus one. There's nothing strange here, the enum just has 6 values numbered 0 through 5 and then a final value numbered 15. 7 values in total, numbered 0, 1, 2, 3, 4, 5, 15. Why this numbering? I don't know; ask whomever wrote the code.

Upvotes: 0

Related Questions