added feature to pass void* pointers by popular demand

This commit is contained in:
Wenzel Jakob 2016-03-26 17:51:09 +01:00
parent 12cf543804
commit de1bca864e
4 changed files with 29 additions and 2 deletions

View File

@ -1,5 +1,5 @@
/*
example/example14.cpp -- opaque types
example/example14.cpp -- opaque types, passing void pointers
Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
@ -26,4 +26,7 @@ void init_ex14(py::module &m) {
for (auto entry : l)
std::cout << " " << entry << std::endl;
});
m.def("return_void_ptr", []() { return (void *) 1234; });
m.def("print_void_ptr", [](void *ptr) { std::cout << "Got void ptr : " << (uint64_t) ptr << std::endl; });
}

View File

@ -12,3 +12,7 @@ print_opaque_list(l)
print("Back element is %s" % l.back())
l.pop_back()
print_opaque_list(l)
from example import return_void_ptr, print_void_ptr
print_void_ptr(return_void_ptr())

View File

@ -4,3 +4,4 @@ Opaque list:
Back element is Element 2
Opaque list:
Element 1
Got void ptr : 1234

View File

@ -323,7 +323,26 @@ public:
PYBIND11_TYPE_CASTER(void_type, _("NoneType"));
};
template <> class type_caster<void> : public type_caster<void_type> { };
template <> class type_caster<void> : public type_caster<void_type> {
public:
using type_caster<void_type>::cast;
bool load(handle h, bool) {
capsule c(h, true);
if (!c.check())
return false;
value = (void *) c;
return true;
}
static handle cast(void *ptr, return_value_policy /* policy */, handle /* parent */) {
return capsule(ptr).inc_ref();
}
operator void *() { return value; }
private:
void *value;
};
template <> class type_caster<std::nullptr_t> : public type_caster<void_type> { };
template <> class type_caster<bool> {