Compare commits

...

4 Commits

Author SHA1 Message Date
BLUELOVETH
51ce483c48
Merge pull request #290 from 16bit-ykiko/fix-capsule
some fix.
2024-07-02 23:21:38 +08:00
ykiko
f803a7eca6 some fix. 2024-07-02 23:14:44 +08:00
BLUELOVETH
8d0e304ecd
Merge pull request #289 from 16bit-ykiko/fix-capsule
update capsule.
2024-07-02 22:57:10 +08:00
ykiko
901cf714a2 update. 2024-07-02 22:38:37 +08:00
2 changed files with 6 additions and 5 deletions

View File

@ -10,7 +10,7 @@ struct capsule {
void* ptr; void* ptr;
void (*destructor)(void*); void (*destructor)(void*);
template <typename T> template <typename T, typename = std::enable_if_t<!(std::is_same_v<remove_cvref_t<T>, capsule>)>>
capsule(T&& value) : capsule(T&& value) :
ptr(new auto(std::forward<T>(value))), destructor([](void* ptr) { ptr(new auto(std::forward<T>(value))), destructor([](void* ptr) {
delete static_cast<std::decay_t<T>*>(ptr); delete static_cast<std::decay_t<T>*>(ptr);
@ -26,7 +26,7 @@ struct capsule {
} }
~capsule() { ~capsule() {
if(ptr != nullptr && destructor != nullptr) destructor(ptr); if(ptr && destructor) destructor(ptr);
} }
}; };
} // namespace pybind11::impl } // namespace pybind11::impl

View File

@ -53,7 +53,7 @@ public:
static type of() { static type of() {
return type_visitor::type<T>(); return type_visitor::type<T>();
} }
static type of(const handle& obj) { return type(vm->_t(obj.ptr())); } static type of(const handle& obj) { return type(vm->_t(obj.ptr())); }
}; };
@ -365,8 +365,9 @@ class capsule : public object {
PYBIND11_TYPE_IMPLEMENT(object, impl::capsule, handle(vm->builtins->attr("capsule"))._as<pkpy::Type>()); PYBIND11_TYPE_IMPLEMENT(object, impl::capsule, handle(vm->builtins->attr("capsule"))._as<pkpy::Type>());
public: public:
template <typename T> capsule(void* ptr, void (*destructor)(void*) = nullptr) : object(create(ptr, destructor)) {}
capsule(T&& value) : object(create(std::forward<T>(value))) {}
void* data() const { return self().ptr; }
template <typename T> template <typename T>
T& cast() const { T& cast() const {