Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Enums

Relevant portion of the pybind11 manual and of the nanobind manual

litgen handles enum and enum class enums.

Classic C enums

See example below:

  • litgen automatically remove the standard prefix from enum values: Foo::Foo_a is exposed as Foo.a

  • comments about values are preserved in the stub

  • litgen automatically handles the enum numbering and outputs the values as a comment in the stub

  • if some macro values are used (like MY_VALUE), we can set their value

  • Computed values are also correctly exposed (see Foo.d)

cpp_code = """
// Doc about Foo
// On several lines
enum Foo
{
    Foo_a, // This is a

    // And this is b and c's comment
    Foo_b,
    Foo_c = MY_VALUE,

    Foo_d = Foo_a | Foo_b + Foo_c, // And a computed value

    Foo_e = 4,

    Foo_count, // And this is count: by default this member is suppressed
};
"""

import litgen
from litgen.demo import litgen_demo

options = litgen.LitgenOptions()
options.srcmlcpp_options.named_number_macros = {"MY_VALUE": 256}
# options.enum_flag_skip_count = False # Uncomment this to generate a definition for Foo::Foo_count
litgen_demo.demo(options, cpp_code)
Loading...

C++ enums: enum class

enum class is also supported, see example below:

cpp_code = """
    enum class Foo
    {
        A,
        B,
        C = MY_VALUE,
        D = A | B + C,
        E = 4,
        F
    };
"""

options = litgen.LitgenOptions()
options.srcmlcpp_options.named_number_macros = {"MY_VALUE": 256}
litgen_demo.demo(options, cpp_code)
Loading...

Arithmetic enums

Use options.enum_make_arithmetic__regex to make the enum arithmetic in python, so that it can be converted to a number in python.

cpp_code = """
    enum class Foo { A, B, C};
"""
options = litgen.LitgenOptions()
options.enum_make_arithmetic__regex = "^Foo$"
litgen_demo.demo(options, cpp_code, show_pydef=True)
Loading...