forked from tdegeus/pybind11_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cpp
44 lines (34 loc) · 859 Bytes
/
example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <string>
#include <iostream>
#include <pybind11/pybind11.h>
// ------------------
// regular C++ header
// ------------------
struct Type {
enum Value {
Cat,
Dog,
};
};
// ----------------
// regular C++ code
// ----------------
void whichAnimal(int animal)
{
if ( animal == Type::Cat ) std::cout << "Cat" << std::endl;
else if ( animal == Type::Dog ) std::cout << "Dog" << std::endl;
else std::cout << "Unknown" << std::endl;
}
// ----------------
// Python interface
// ----------------
namespace py = pybind11;
PYBIND11_MODULE(example, m)
{
py::module sm = m.def_submodule("Type", "Type enumerator");
py::enum_<Type::Value>(sm, "Type")
.value("Cat", Type::Cat)
.value("Dog", Type::Dog)
.export_values();
m.def("whichAnimal", &whichAnimal, py::arg("animal"));
}