mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
pybind11: fix and generalize example
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -25,8 +25,11 @@ __pycache__
|
|||||||
# Accidents.
|
# Accidents.
|
||||||
/core
|
/core
|
||||||
/m5out
|
/m5out
|
||||||
|
|
||||||
|
# In-tree userland builds.
|
||||||
*.o
|
*.o
|
||||||
*.out
|
*.out
|
||||||
|
*.so
|
||||||
|
|
||||||
# Kernel modules.
|
# Kernel modules.
|
||||||
*.ko
|
*.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())
|
print(my_class_test.getName())
|
||||||
my_class_test.setName("012")
|
my_class_test.setName("012")
|
||||||
print(my_class_test.getName())
|
print(my_class_test.getName())
|
||||||
|
assert(my_class_test.getName() == my_class_test.name)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -eu
|
set -eux
|
||||||
g++ -O3 -Wall -shared -std=c++11 -fPIC class_test.cpp -o class_test`python3-config --extension-suffix` -I /usr/include/python3.6m
|
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
|
./class_test_main.py
|
||||||
|
|||||||
Reference in New Issue
Block a user