-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsample03.cpp
101 lines (81 loc) · 2.5 KB
/
sample03.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <pybind11/eval.h>
#include <pybind11/pybind11.h>
#include <iostream>
#include <memory>
class Job {
public:
std::string GetName() const { return m_name; }
void SetName(const std::string& name) { m_name = name; }
private:
std::string m_name;
};
class Person {
public:
std::string GetName() const { return m_name; }
void SetName(const std::string& name) { m_name = name; }
std::shared_ptr<Job> GetJob() const { return m_job; }
void SetJob(const std::shared_ptr<Job>& job) { m_job = job; }
private:
std::string m_name;
std::shared_ptr<Job> m_job;
};
namespace py = pybind11;
PyMODINIT_FUNC PyInit_sample03() {
py::module m("sample03", "pybind11 module sample.");
py::class_<Job, std::shared_ptr<Job>> job(m, "Job");
job.def(py::init<>()).def_property("name", &Job::GetName, &Job::SetName);
py::class_<Person, std::shared_ptr<Person>> person(m, "Person");
person.def(py::init<>())
.def_property("name", &Person::GetName, &Person::SetName)
.def_property("job", &Person::GetJob, &Person::SetJob);
return m.ptr();
}
int main(int argc, char** argv) {
wchar_t* program = Py_DecodeLocale(argv[0], nullptr);
if (program == nullptr) {
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
exit(1);
}
PyImport_AppendInittab("sample03", PyInit_sample03);
Py_SetProgramName(program);
Py_Initialize();
py::module::import("sample03");
try {
auto global = py::dict(py::module::import("__main__").attr("__dict__"));
// Python で関数を作成 (Name=Hoge, Job=Teacher)
py::eval<py::eval_statements>(
"import sample03\n"
"def initialize_person(p):\n"
" job = sample03.Job()\n"
" job.name = 'Teacher'\n"
" p.name = 'Hoge'\n"
" p.job = job\n",
global);
{
auto person = std::make_shared<Person>();
global["initialize_person"](person);
std::cout << "Name : " << person->GetName() << "\n";
std::cout << "Job : " << person->GetJob()->GetName() << "\n";
}
// 関数を変更 (Name=Foo, Job=Programmer)
py::eval<py::eval_statements>(
"import sample03\n"
"def initialize_person(p):\n"
" job = sample03.Job()\n"
" job.name = 'Programmer'\n"
" p.name = 'Foo'\n"
" p.job = job\n",
global);
{
auto person = std::make_shared<Person>();
global["initialize_person"](person);
std::cout << "Name : " << person->GetName() << "\n";
std::cout << "Job : " << person->GetJob()->GetName() << "\n";
}
} catch (py::error_already_set& e) {
std::cout << "Python error.\n" << e.what() << "\n";
}
Py_Finalize();
PyMem_RawFree(program);
return 0;
}