Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Demonstrate non-enum internal types in example #3314

Merged
merged 3 commits into from
Sep 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions docs/classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,7 @@ you can use ``py::detail::overload_cast_impl`` with an additional set of parenth
Enumerations and internal types
===============================

Let's now suppose that the example class contains an internal enumeration type,
e.g.:
Let's now suppose that the example class contains internal types like enumerations, e.g.:

.. code-block:: cpp

Expand All @@ -457,10 +456,15 @@ e.g.:
Cat
};

struct Attributes {
float age = 0;
};

Pet(const std::string &name, Kind type) : name(name), type(type) { }

std::string name;
Kind type;
Attributes attr;
};

The binding code for this example looks as follows:
Expand All @@ -471,15 +475,21 @@ The binding code for this example looks as follows:

pet.def(py::init<const std::string &, Pet::Kind>())
.def_readwrite("name", &Pet::name)
.def_readwrite("type", &Pet::type);
.def_readwrite("type", &Pet::type)
.def_readwrite("attr", &Pet::attr);

py::enum_<Pet::Kind>(pet, "Kind")
.value("Dog", Pet::Kind::Dog)
.value("Cat", Pet::Kind::Cat)
.export_values();

To ensure that the ``Kind`` type is created within the scope of ``Pet``, the
``pet`` :class:`class_` instance must be supplied to the :class:`enum_`.
py::class_<Pet::Attributes> attributes(pet, "Attributes")
.def(py::init<>())
.def_readwrite("age", &Pet::Attributes::age);


To ensure that the nested types ``Kind`` and ``Attributes`` are created within the scope of ``Pet``, the
``pet`` :class:`class_` instance must be supplied to the :class:`enum_` and :class:`class_`
constructor. The :func:`enum_::export_values` function exports the enum entries
into the parent scope, which should be skipped for newer C++11-style strongly
typed enums.
Expand Down