mirror of
https://github.com/pybind/pybind11.git
synced 2025-01-31 15:20:34 +00:00
added feature to pass void* pointers by popular demand
This commit is contained in:
parent
12cf543804
commit
de1bca864e
@ -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>
|
Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
|
||||||
|
|
||||||
@ -26,4 +26,7 @@ void init_ex14(py::module &m) {
|
|||||||
for (auto entry : l)
|
for (auto entry : l)
|
||||||
std::cout << " " << entry << std::endl;
|
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; });
|
||||||
}
|
}
|
||||||
|
@ -12,3 +12,7 @@ print_opaque_list(l)
|
|||||||
print("Back element is %s" % l.back())
|
print("Back element is %s" % l.back())
|
||||||
l.pop_back()
|
l.pop_back()
|
||||||
print_opaque_list(l)
|
print_opaque_list(l)
|
||||||
|
|
||||||
|
from example import return_void_ptr, print_void_ptr
|
||||||
|
|
||||||
|
print_void_ptr(return_void_ptr())
|
||||||
|
@ -4,3 +4,4 @@ Opaque list:
|
|||||||
Back element is Element 2
|
Back element is Element 2
|
||||||
Opaque list:
|
Opaque list:
|
||||||
Element 1
|
Element 1
|
||||||
|
Got void ptr : 1234
|
||||||
|
@ -323,7 +323,26 @@ public:
|
|||||||
PYBIND11_TYPE_CASTER(void_type, _("NoneType"));
|
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<std::nullptr_t> : public type_caster<void_type> { };
|
||||||
|
|
||||||
template <> class type_caster<bool> {
|
template <> class type_caster<bool> {
|
||||||
|
Loading…
Reference in New Issue
Block a user