File tree Expand file tree Collapse file tree 10 files changed +133
-0
lines changed Expand file tree Collapse file tree 10 files changed +133
-0
lines changed Original file line number Diff line number Diff line change 1+ cmake_minimum_required (VERSION 3.4)
2+ project (scripting VERSION 1.0 LANGUAGES CXX)
3+
4+ set (PYBIND11_PYTHON_VERSION 3.4)
5+
6+ find_package (pybind11 CONFIG)
7+
8+ add_executable (scripting
9+ src/main.cpp
10+ src/scripter.cpp
11+ )
12+ target_link_libraries (scripting
13+ pybind11::embed
14+ )
15+
16+ pybind11_add_module(fooapi
17+ src/bar.cpp
18+ src/foo.cpp
19+ )
Original file line number Diff line number Diff line change 1+ # adding multiple classes to a python module
2+
3+ the python code can set ` foo.bar.value `
4+
5+ ~~~ .sh
6+ $ mkdir build
7+ $ cmake -Dpybind11_DIR=/home/ale/bin/pybind11/share/cmake/pybind11 ..
8+ $ make
9+ $ ./scripting
10+ ~~~
Original file line number Diff line number Diff line change 1+ import fooapi
2+
3+ print (foo .bar .value )
4+ bar = foo .bar
5+ bar .value = 5
Original file line number Diff line number Diff line change 1+ #include < pybind11/pybind11.h>
2+ #include " bar.h"
3+
4+ namespace py = pybind11;
5+
6+ PYBIND11_MODULE (barapi, m) {
7+ py::class_<Bar>(m, " Bar" )
8+ .def (py::init<>())
9+ .def_readwrite (" value" , &Bar::value)
10+ ;
11+ }
12+
Original file line number Diff line number Diff line change 1+ #ifndef BAR_H
2+ #define BAR_H
3+ #include < iostream>
4+ struct Bar {
5+ int value = 1 ;
6+
7+ Bar () {std::cout << " Making a bar!" << std::endl;}
8+ Bar (const Bar& a) {value = a.value ; std::cout << " Copying a new bar" << std::endl;}
9+ ~Bar () {std::cout << " Bye-bye bar!" << std::endl;}
10+ };
11+ #endif
Original file line number Diff line number Diff line change 1+ #include < pybind11/pybind11.h>
2+ #include " foo.h"
3+
4+ namespace py = pybind11;
5+
6+ PYBIND11_MODULE (fooapi, m) {
7+ py::class_<Bar>(m, " Bar" )
8+ .def (py::init<>())
9+ .def_readwrite (" value" , &Bar::value)
10+ ;
11+ py::class_<Foo>(m, " Foo" )
12+ .def (py::init<>())
13+ .def_readwrite (" bar" , &Foo::bar)
14+ ;
15+ }
Original file line number Diff line number Diff line change 1+ #ifndef FOO_H
2+ #define FOO_H
3+ #include < iostream>
4+ #include " bar.h"
5+ struct Foo {
6+ Bar bar{};
7+
8+ Foo () {std::cout << " Making a foo!" << std::endl;}
9+ Foo (const Foo& a) {bar = a.bar ; std::cout << " Copying a new foo" << std::endl;}
10+ ~Foo () {std::cout << " Bye-bye foo!" << std::endl;}
11+ };
12+ #endif
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < pybind11/pybind11.h>
3+ #include < pybind11/embed.h>
4+
5+ #include " scripter.h"
6+
7+ int main () {
8+ Scripter scripter;
9+ scripter.runFile (" ../python/set-bar.py" );
10+ }
11+
Original file line number Diff line number Diff line change 1+ #include " scripter.h"
2+ #include " foo.h"
3+ #include " bar.h"
4+
5+ using namespace pybind11 ::literals; // for the ""_a
6+
7+ void Scripter::runFile (std::string fileName)
8+ {
9+ Foo foo;
10+
11+ std::cout << foo.bar .value << std::endl;
12+
13+ auto module = py::module::import (" fooapi" );
14+
15+ // auto locals = py::dict("foo"_a=foo); // foo by value
16+ auto locals = py::dict (" foo" _a=py::cast (foo, py::return_value_policy::reference)); // foo by reference
17+
18+ py::eval_file (fileName, py::globals (), locals);
19+
20+ std::cout << foo.bar .value << std::endl;
21+ }
Original file line number Diff line number Diff line change 1+ #ifndef SCRIPTER_H
2+ #define SCRIPTER_H
3+ #include < string>
4+
5+ #include < pybind11/pybind11.h>
6+ #include < pybind11/embed.h>
7+
8+ namespace py = pybind11;
9+
10+ class Scripter
11+ {
12+ public:
13+ void runFile (std::string fileName);
14+ private:
15+ py::scoped_interpreter guard{};
16+ };
17+ #endif
You can’t perform that action at this time.
0 commit comments