{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Enums\n", "\n", "[Relevant portion](https://pybind11.readthedocs.io/en/stable/classes.html?highlight=enum#enumerations-and-internal-types) of the pybind11 manual.\n", "\n", "litgen handles `enum` and `enum class` enums.\n", "\n", "## Classic C enums\n", "\n", "See example below: \n", "\n", "* litgen automatically remove the standard prefix from enum values: `Foo::Foo_a` is exposed as `Foo.a`\n", "* comments about values are preserved in the stub\n", "* litgen automatically handles the enum numbering and outputs the values as a comment in the stub\n", "* if some macro values are used (like `MY_VALUE`), we can set their value\n", "* Computed values are also correctly exposed (see `Foo.d`)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
// Doc about Foo\n",
"// On several lines\n",
"enum Foo\n",
"{\n",
" Foo_a, // This is a\n",
"\n",
" // And this is b and c's comment\n",
" Foo_b,\n",
" Foo_c = MY_VALUE,\n",
"\n",
" Foo_d = Foo_a | Foo_b + Foo_c, // And a computed value\n",
"\n",
" Foo_e = 4,\n",
"\n",
" Foo_count, // And this is count: by default this member is suppressed\n",
"};\n",
"
class Foo(enum.Enum):\n",
" """ Doc about Foo\n",
" On several lines\n",
" """\n",
" a = enum.auto() # (= 0) # This is a\n",
"\n",
" # And this is b and c's comment\n",
" b = enum.auto() # (= 1)\n",
" c = enum.auto() # (= 256)\n",
"\n",
" d = enum.auto() # (= Foo.a | Foo.b + Foo.c) # And a computed value\n",
"\n",
" e = enum.auto() # (= 4)\n",
"
py::enum_<Foo>(m, "Foo", py::arithmetic(), " Doc about Foo\\n On several lines")\n",
" .value("a", Foo_a, "This is a")\n",
" .value("b", Foo_b, "")\n",
" .value("c", Foo_c, "")\n",
" .value("d", Foo_d, "And a computed value")\n",
" .value("e", Foo_e, "");\n",
"
enum class Foo\n",
" {\n",
" A,\n",
" B,\n",
" C = MY_VALUE,\n",
" D = A | B + C,\n",
" E = 4,\n",
" F\n",
" };\n",
"
class Foo(enum.Enum):\n",
" a = enum.auto() # (= 0)\n",
" b = enum.auto() # (= 1)\n",
" c = enum.auto() # (= 256)\n",
" d = enum.auto() # (= A | B + C)\n",
" e = enum.auto() # (= 4)\n",
" f = enum.auto() # (= 5)\n",
"
py::enum_<Foo>(m, "Foo", py::arithmetic(), "")\n",
" .value("a", Foo::A, "")\n",
" .value("b", Foo::B, "")\n",
" .value("c", Foo::C, "")\n",
" .value("d", Foo::D, "")\n",
" .value("e", Foo::E, "")\n",
" .value("f", Foo::F, "");\n",
"
enum class Foo { A, B, C};\n",
"
class Foo(enum.Enum):\n",
" a = enum.auto() # (= 0)\n",
" b = enum.auto() # (= 1)\n",
" c = enum.auto() # (= 2)\n",
"
py::enum_<Foo>(m, "Foo", py::arithmetic(), "")\n",
" .value("a", Foo::A, "")\n",
" .value("b", Foo::B, "")\n",
" .value("c", Foo::C, "");\n",
"