Skip to content

Config: enum.json

Scaless edited this page Nov 3, 2024 · 4 revisions

Enumbra uses JSON configuration files with the nlohmann parser: https://github.com/nlohmann/json

An example configuration is available in the project's examples directory.

value_enum_default_value_style = "min"

Value to use when determining the default value for Value Enum functions. 
Valid Options:
  "min": The smallest entry in the values[] table. If an enum contains duplicate minimum values, the name of the first occurrence will be used.
  "max": The largest entry in the values[] table. If an enum contains duplicate maximum values, the name of the first occurrence will be used.
  "first": The first entry in the values[] table following the user-defined order.
  "last": The last entry in the values[] table following the user-defined order.

flags_enum_default_value_style = "zero"

Value to use when determining the default value for Flags Enum functions. 
Valid Options:
  "zero": Default value is 0.
  "used_bits_set": Default value equal to all valid values OR'd together. 
  "min": The smallest entry in the values[] table. If an enum contains duplicate minimum values, the name of the first occurrence will be used.
  "max": The largest entry in the values[] table. If an enum contains duplicate maximum values, the name of the first occurrence will be used.
  "first": The first entry in the values[] table following the user-defined order.
  "last": The last entry in the values[] table following the user-defined order.

Enum Definitions

Contained in the value_enums and flags_enums arrays.
The fields available to both enum types are the same.

A simple enum

{
  "name": "Simple",
  "entries": [
    { "name": "A" },
    { "name": "B" },
    { "name": "C" }
  ]
}

Roughly equivalent to:

enum class Simple : uint64_t {
  A = 0,
  B = 1,
  C = 2,
}

A bit more involved

{
  "name": "Complex",
  "default_value": "UP",
  "size_type": "signed64",
  "entries": [
    {
      "name": "NEG",
      "value": -1
    },
    {
      "name": "UP",
      "value": "1234" // positive or negative integer strings are allowed
    },
    {
      "name": "HEX",
      "value": "0xBEEF" // Hex too!
    },
    {
      "name": "BINARY",
      "value": "0b10101010" // Binary literals too!
    },
    {
      "name": "SMALL",
      "value": -9223372036854775808 // Natively supports values as low as INT64_MIN 
    },
    {
      "name": "BIG",
      "value": 18446744073709551615 // Natively supports values as high as UINT64_MAX 
      // (but this specific entry will cause a generation error since this enum has a signed64 type!)
    },
  ]
}

Roughly equivalent to:

enum class Complex : int64_t {
  NEG = -1,
  UP = 1234,
  HEX = 0xBEEF,
  BINARY = 170,
  SMALL = (-9223372036854775807 - 1), // Why subtract 1?: https://stackoverflow.com/a/11270104
//BIG = 18446744073709551615 // This would cause a failure to generate the enum at all, so this couldn't happen
}