mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-13 20:12:26 +00:00
pybind11: fix and generalize example
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@ -25,8 +25,11 @@ __pycache__
|
||||
# Accidents.
|
||||
/core
|
||||
/m5out
|
||||
|
||||
# In-tree userland builds.
|
||||
*.o
|
||||
*.out
|
||||
*.so
|
||||
|
||||
# Kernel modules.
|
||||
*.ko
|
||||
|
||||
@ -1,24 +0,0 @@
|
||||
#include <string>
|
||||
|
||||
#include <pybind11/pybind11.h>
|
||||
|
||||
struct Pet {
|
||||
Pet(const std::string &name) : name(name) { }
|
||||
void setName(const std::string &name_) { name = name_; }
|
||||
const std::string &getName() const { return name; }
|
||||
|
||||
std::string name;
|
||||
};
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
PYBIND11_PLUGIN(example) {
|
||||
py::module m("example", "pybind11 example plugin");
|
||||
|
||||
py::class_<Pet>(m, "Pet")
|
||||
.def(py::init<const std::string &>())
|
||||
.def("setName", &Pet::setName)
|
||||
.def("getName", &Pet::getName);
|
||||
|
||||
return m.ptr();
|
||||
}
|
||||
22
userland/libs/pybind11/class_test.cpp
Normal file
22
userland/libs/pybind11/class_test.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
#include <string>
|
||||
|
||||
#include <pybind11/pybind11.h>
|
||||
|
||||
struct ClassTest {
|
||||
ClassTest(const std::string &name) : name(name) { }
|
||||
void setName(const std::string &name_) { name = name_; }
|
||||
const std::string &getName() const { return name; }
|
||||
std::string name;
|
||||
};
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
PYBIND11_PLUGIN(class_test) {
|
||||
py::module m("my_module", "pybind11 example plugin");
|
||||
py::class_<ClassTest>(m, "ClassTest")
|
||||
.def(py::init<const std::string &>())
|
||||
.def("setName", &ClassTest::setName)
|
||||
.def("getName", &ClassTest::getName)
|
||||
.def_readwrite("name", &ClassTest::name);
|
||||
return m.ptr();
|
||||
}
|
||||
@ -6,3 +6,4 @@ my_class_test = class_test.ClassTest("abc");
|
||||
print(my_class_test.getName())
|
||||
my_class_test.setName("012")
|
||||
print(my_class_test.getName())
|
||||
assert(my_class_test.getName() == my_class_test.name)
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
set -eu
|
||||
g++ -O3 -Wall -shared -std=c++11 -fPIC class_test.cpp -o class_test`python3-config --extension-suffix` -I /usr/include/python3.6m
|
||||
set -eux
|
||||
g++ `python3-config --cflags` -shared -std=c++11 -fPIC class_test.cpp -o class_test`python3-config --extension-suffix` `python3-config --libs`
|
||||
./class_test_main.py
|
||||
|
||||
Reference in New Issue
Block a user