-
Notifications
You must be signed in to change notification settings - Fork 5
/
manual.h
64 lines (51 loc) · 2.19 KB
/
manual.h
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pragma once
#include <iostream>
#include "genpybind.h"
namespace something {
class GENPYBIND(visible) Something {
public:
bool whatever() const;
GENPYBIND(hidden)
friend std::ostream &operator<<(std::ostream &os, const Something &) {
return os << "uiae";
}
int offset = 5;
GENPYBIND_MANUAL({
// FIXME: &Something::whatever does not work
parent.def("something", &::something::Something::whatever);
// ==> genpybind_class_decl__something_Something_Something.def(
// ==> "something", &::something::Something::whatever);
// We need to use the special macro GENPYBIND_PARENT_TYPE since the class itself is
// not complete at this point. (This macro uses `auto` to turn the lambda into a
// generic lambda during parsing of the GENPYBIND_MANUAL expression. It will be
// replaced by the real type in the generated bindings.)
parent.def("__getitem__", [](GENPYBIND_PARENT_TYPE &self, int key) {
return self.offset + key;
});
// ==> genpybind_class_decl__something_Something_Something.def(
// ==> "__getitem__", [](::something::Something &self, int key) {
// ==> return self.offset + key;
// ==> });
// Note the convoluted syntax necessary for writing a call to a template function:
parent.def(
"__str__",
parent->template genpybind_stringstream_helper<::something::Something>());
// ==> genpybind_class_decl__something_Something_Something.def(
// ==> "__str__", genpybind_stringstream_helper<::something::Something>());
})
};
} // namespace something
// Unscoped GENPYBIND_MANUAL macros can be used to add preamble and postamble code to the
// generated bindings, e.g. for importing required libraries or executing python code that
// dynamically patches the generated bindings.
GENPYBIND(postamble)
GENPYBIND_MANUAL({
auto env = parent->py::module::import("os").attr("environ");
// should not have any effect as this will be run after preamble code
env.attr("setdefault")("genpybind", "postamble");
env.attr("setdefault")("genpybind_post", "postamble");
})
GENPYBIND_MANUAL({
auto env = parent->py::module::import("os").attr("environ");
env.attr("setdefault")("genpybind", "preamble");
})